Storage
The Storage class encapsulates the access to data objects over WebStorageAPI.
import { Storage } from "@schukai/monster/source/data/datasource/storage.mjs";Storage is the datasource base for browser Web Storage. It reads and writes structured JSON values behind the same datasource API used elsewhere in Monster.
This makes it useful for cached user preferences, drafts, view state and other browser-local data that should still behave like a proper datasource instead of ad-hoc storage utility code.
The base class does not decide whether the backend is localStorage or sessionStorage. Derived classes provide the concrete storage object while the read/write contract stays identical.
Read And Write JSON Through Web Storage
import { Storage } from "@schukai/monster/source/data/datasource/storage.mjs";
class DemoStorage extends Storage {
[Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject")]() {
return window.sessionStorage;
}
}
const output = document.getElementById("storage-output");
document.getElementById("storage-run").addEventListener("click", () => {
const datasource = new DemoStorage("monster-demo");
output.textContent = JSON.stringify(
{
key: datasource.getOption("key"),
backend: "sessionStorage",
},
null,
2,
);
});Persist Draft State In Session Storage
import { Storage } from "@schukai/monster/source/data/datasource/storage.mjs";
class DraftStorage extends Storage {
[Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject")]() {
return window.sessionStorage;
}
}
const datasource = new DraftStorage("docs-draft");
const output = document.getElementById("storage-session-output");
document.getElementById("storage-session-run").addEventListener("click", () => {
datasource.set({
headline: "Draft article",
dirty: true,
});
output.textContent = JSON.stringify(
{
backend: "sessionStorage",
key: datasource.getOption("key"),
data: datasource.get(),
},
null,
2,
);
});waiting
Persist User Preferences In Local Storage
import { Storage } from "@schukai/monster/source/data/datasource/storage.mjs";
class PreferenceStorage extends Storage {
[Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject")]() {
return window.localStorage;
}
}
const datasource = new PreferenceStorage("docs-preferences");
const output = document.getElementById("storage-local-output");
document.getElementById("storage-local-run").addEventListener("click", () => {
datasource.set({
density: "compact",
locale: "en-US",
});
output.textContent = JSON.stringify(
{
backend: "localStorage",
key: datasource.getOption("key"),
data: datasource.get(),
},
null,
2,
);
});waiting
Remove The Storage Entry When The Datasource Becomes Undefined
import { Storage } from "@schukai/monster/source/data/datasource/storage.mjs";
class DemoStorage extends Storage {
[Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject")]() {
return window.sessionStorage;
}
}
const datasource = new DemoStorage("docs-remove-example");
const output = document.getElementById("storage-remove-output");
document.getElementById("storage-remove-run").addEventListener("click", async () => {
datasource.set({
cleanup: true,
});
await datasource.write();
datasource.set(undefined);
await datasource.write();
output.textContent = JSON.stringify(
{
key: datasource.getOption("key"),
storedValue: window.sessionStorage.getItem(datasource.getOption("key")),
},
null,
2,
);
});waiting
Exported
Storage, storageObjectSymbolDerived from
DatasourceOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Datasource.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Datasource.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Datasource.
Constructor
constructor(key)key{string}: LocalStorage Key
{TypeError}value is not a string
Structural methods
getClone()- {Storage}
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.Other methods
[storageObjectSymbol]()- {external:Storage}
{Error}this method must be implemented by derived classes.
read()- {Promise}
{Error}the options does not contain a valid json definition{TypeError}value is not a object{Error}the data cannot be read
write()- {Storage}
{Error}the data cannot be written
Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.