Skip to content

Commit

Permalink
refactor(native store): refactor native store and add session storage…
Browse files Browse the repository at this point in the history
… to node environment
  • Loading branch information
Masquerade-Circus committed Sep 12, 2024
1 parent 0ec028e commit 13f3b76
Show file tree
Hide file tree
Showing 22 changed files with 644 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ typings/
yalc.lock

.scratchpad
www
www
.session-storage
6 changes: 5 additions & 1 deletion dist/native-store/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export declare enum StorageType {
Session = "session",
Local = "local"
}
export interface NativeStorageInterface {
state: Record<string, any>;
set(key: string, value: any): void;
Expand All @@ -6,5 +10,5 @@ export interface NativeStorageInterface {
load(): void;
clear(): void;
}
export declare function createNativeStore<T>(key: string, definition?: Record<string, any>, reuseIfExist?: boolean): NativeStorageInterface & T;
export declare function createNativeStore<T>(key: string, definition?: Record<string, any>, storageType?: StorageType, reuseIfExist?: boolean): NativeStorageInterface & T;
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion dist/native-store/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 47 additions & 14 deletions dist/native-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
// lib/native-store/index.ts
var native_store_exports = {};
__export(native_store_exports, {
StorageType: () => StorageType,
createNativeStore: () => createNativeStore
});
module.exports = __toCommonJS(native_store_exports);
var nativeStore = sessionStorage || localStorage;
var import_valyrian = require("valyrian.js");
var StorageType = /* @__PURE__ */ ((StorageType2) => {
StorageType2["Session"] = "session";
StorageType2["Local"] = "local";
return StorageType2;
})(StorageType || {});
var ids = /* @__PURE__ */ new Set();
function createNativeStore(key, definition = {}, reuseIfExist = false) {
function getStorage(storageType) {
if (import_valyrian.isNodeJs && typeof localStorage === "undefined") {
throw new Error(
`localStorage and sessionStorage are not available in Node.js, to use it in your project, you need to "import "valyrian.js/node"`
);
}
return storageType === "session" /* Session */ ? sessionStorage : localStorage;
}
function createNativeStore(key, definition = {}, storageType = "local" /* Local */, reuseIfExist = false) {
const nativeStore = getStorage(storageType);
if (ids.has(key)) {
if (reuseIfExist) {
console.warn(`Store with key ${key} already exists and will be reused`);
Expand All @@ -34,34 +49,52 @@ function createNativeStore(key, definition = {}, reuseIfExist = false) {
}
}
ids.add(key);
const id = key;
const Store = {
state: {},
set(key2, value) {
this.state[key2] = value;
nativeStore.setItem(key2, JSON.stringify(this.state));
try {
this.state[key2] = value;
nativeStore.setItem(id, JSON.stringify(this.state));
} catch (e) {
console.error("Error setting item in storage:", e);
}
},
get(key2) {
if (!this.state) {
if (Object.keys(this.state).length === 0) {
this.load();
}
return this.state[key2];
},
delete(key2) {
Reflect.deleteProperty(this.state, key2);
nativeStore.setItem(key2, JSON.stringify(this.state));
try {
Reflect.deleteProperty(this.state, key2);
nativeStore.setItem(id, JSON.stringify(this.state));
} catch (e) {
console.error("Error deleting item in storage:", e);
}
},
load() {
const state = nativeStore.getItem(key);
if (!state) {
try {
const state = nativeStore.getItem(id);
if (!state) {
this.state = {};
nativeStore.setItem(id, JSON.stringify(this.state));
return;
}
this.state = JSON.parse(state);
} catch (e) {
console.error("Error loading state from storage:", e);
this.state = {};
nativeStore.setItem(key, JSON.stringify(this.state));
return;
}
this.state = JSON.parse(state);
},
clear() {
this.state = {};
nativeStore.removeItem(key);
try {
this.state = {};
nativeStore.removeItem(id);
} catch (e) {
console.error("Error clearing storage:", e);
}
},
...definition
};
Expand Down
6 changes: 3 additions & 3 deletions dist/native-store/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 47 additions & 14 deletions dist/native-store/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// lib/native-store/index.ts
var nativeStore = sessionStorage || localStorage;
import { isNodeJs } from "valyrian.js";
var StorageType = /* @__PURE__ */ ((StorageType2) => {
StorageType2["Session"] = "session";
StorageType2["Local"] = "local";
return StorageType2;
})(StorageType || {});
var ids = /* @__PURE__ */ new Set();
function createNativeStore(key, definition = {}, reuseIfExist = false) {
function getStorage(storageType) {
if (isNodeJs && typeof localStorage === "undefined") {
throw new Error(
`localStorage and sessionStorage are not available in Node.js, to use it in your project, you need to "import "valyrian.js/node"`
);
}
return storageType === "session" /* Session */ ? sessionStorage : localStorage;
}
function createNativeStore(key, definition = {}, storageType = "local" /* Local */, reuseIfExist = false) {
const nativeStore = getStorage(storageType);
if (ids.has(key)) {
if (reuseIfExist) {
console.warn(`Store with key ${key} already exists and will be reused`);
Expand All @@ -10,40 +24,59 @@ function createNativeStore(key, definition = {}, reuseIfExist = false) {
}
}
ids.add(key);
const id = key;
const Store = {
state: {},
set(key2, value) {
this.state[key2] = value;
nativeStore.setItem(key2, JSON.stringify(this.state));
try {
this.state[key2] = value;
nativeStore.setItem(id, JSON.stringify(this.state));
} catch (e) {
console.error("Error setting item in storage:", e);
}
},
get(key2) {
if (!this.state) {
if (Object.keys(this.state).length === 0) {
this.load();
}
return this.state[key2];
},
delete(key2) {
Reflect.deleteProperty(this.state, key2);
nativeStore.setItem(key2, JSON.stringify(this.state));
try {
Reflect.deleteProperty(this.state, key2);
nativeStore.setItem(id, JSON.stringify(this.state));
} catch (e) {
console.error("Error deleting item in storage:", e);
}
},
load() {
const state = nativeStore.getItem(key);
if (!state) {
try {
const state = nativeStore.getItem(id);
if (!state) {
this.state = {};
nativeStore.setItem(id, JSON.stringify(this.state));
return;
}
this.state = JSON.parse(state);
} catch (e) {
console.error("Error loading state from storage:", e);
this.state = {};
nativeStore.setItem(key, JSON.stringify(this.state));
return;
}
this.state = JSON.parse(state);
},
clear() {
this.state = {};
nativeStore.removeItem(key);
try {
this.state = {};
nativeStore.removeItem(id);
} catch (e) {
console.error("Error clearing storage:", e);
}
},
...definition
};
Store.load();
return Store;
}
export {
StorageType,
createNativeStore
};
Loading

0 comments on commit 13f3b76

Please sign in to comment.