@stevenleep/storage
Version:
enhanced local storage
42 lines (35 loc) • 1.37 kB
text/typescript
import DoActionStorage from "../dist/index.js";
import CustomStorage from "./CustomStorage";
const storage = new DoActionStorage({
/**
* @title 通过namespace配置,可以在key前面加上一个命名空间, 来实现不同插件或应用之间使用相同key不会冲突
*
* @scenes
* 比如以下场景可能会用到:
* 1. 一个应用内有多个插件,插件之间使用相同的key,但是不希望互相影响
* 2. 微前端架构下,多个子应用使用相同的key,但是不希望互相影响
* 3. 某些NPM包,可能会使用相同的key,但是不希望互相影响
*
* 默认值为:@app/
*/
// namespace: "@testNamespace/",
storage: new CustomStorage(),
});
const TEST_KEY = "test-key";
/**
* observer 能力即使不实现Storage层,也是默认具备的
*/
storage.observer.subscribe(TEST_KEY, function onChange(newValue, oldValue) {
console.log("新的值:", newValue, "旧的值:", oldValue);
});
storage.setItem(TEST_KEY, new Date().toString());
storage.setItem(TEST_KEY, Date.now().toString());
/**
* 只基础核心存储层扩展,不包含任何Reactive的能力
*/
import MyStorage from "./AdvancedCustomStorage";
const myStorage = new MyStorage();
myStorage.save("test-key", new Date().toString()).then(() => {
console.log("保存成功");
});
console.log(myStorage);