@bpd-library/utilities 2.0.3-beta.1 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/store/generators/create-local-storage-store.d.ts +10 -0
- package/dist/store/generators/create-local-storage-store.js +49 -0
- package/dist/store/generators/create-local-storage-store.js.map +1 -0
- package/dist/store/generators/create-session-storage-store.d.ts +10 -0
- package/dist/store/generators/create-session-storage-store.js +31 -0
- package/dist/store/generators/create-session-storage-store.js.map +1 -0
- package/dist/store/generators/create-store.d.ts +2 -0
- package/dist/store/generators/create-store.js +59 -0
- package/dist/store/generators/create-store.js.map +1 -0
- package/dist/store/generators/index.d.ts +3 -0
- package/dist/store/generators/index.js +4 -0
- package/dist/store/generators/index.js.map +1 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.js +1 -1
- package/dist/store/index.js.map +1 -1
- package/dist/store/store.d.ts +4 -4
- package/dist/store/store.js +6 -6
- package/dist/store/store.js.map +1 -1
- package/dist/store/store.types.d.ts +1 -1
- package/dist/store/store.types.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BrowserStorageStoreSettings } from '../store.types';
|
|
2
|
+
export declare const createLocalStorageStore: <State extends object, Action extends string>({ key, actions, initialState, expireInDays, excludeProps, initAction, }: BrowserStorageStoreSettings<State, Action>) => {
|
|
3
|
+
getState: import("../store.types").State<State>;
|
|
4
|
+
getPrevState: import("../store.types").State<State>;
|
|
5
|
+
subscribe: import("../store.types").Subscribe<State>;
|
|
6
|
+
unsubscribe: import("../store.types").Unsubscribe<State>;
|
|
7
|
+
dispatch: import("../store.types").Dispatch<Action, State>;
|
|
8
|
+
get: () => State | undefined;
|
|
9
|
+
set: (state: State) => void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createStore } from './create-store';
|
|
2
|
+
export const createLocalStorageStore = ({ key, actions, initialState, expireInDays, excludeProps, initAction, }) => {
|
|
3
|
+
const store = createStore({
|
|
4
|
+
initialState,
|
|
5
|
+
actions,
|
|
6
|
+
});
|
|
7
|
+
function updateStore(data) {
|
|
8
|
+
return store.dispatch(initAction, data);
|
|
9
|
+
}
|
|
10
|
+
function get() {
|
|
11
|
+
const state = localStorage.getItem(key);
|
|
12
|
+
if (!state)
|
|
13
|
+
return;
|
|
14
|
+
const parsedState = JSON.parse(state);
|
|
15
|
+
if (parsedState.expire && new Date() > parsedState.expire) {
|
|
16
|
+
localStorage.removeItem(key);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
return parsedState.value;
|
|
20
|
+
}
|
|
21
|
+
function set(state) {
|
|
22
|
+
try {
|
|
23
|
+
const now = new Date();
|
|
24
|
+
const serializedState = JSON.stringify({
|
|
25
|
+
value: excludeProps ? excludePropsFromState(state) : state,
|
|
26
|
+
expire: expireInDays ? now.setDate(now.getDate() + expireInDays) : undefined,
|
|
27
|
+
});
|
|
28
|
+
localStorage.setItem(key, serializedState);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.log('Error in localStorage.setItem', error);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function excludePropsFromState(state) {
|
|
35
|
+
const keys = Object.keys(state);
|
|
36
|
+
const filteredState = {};
|
|
37
|
+
keys.filter((key) => !(excludeProps === null || excludeProps === void 0 ? void 0 : excludeProps.includes(key))).forEach((key) => {
|
|
38
|
+
const value = state[key];
|
|
39
|
+
filteredState[key] = value;
|
|
40
|
+
});
|
|
41
|
+
return filteredState;
|
|
42
|
+
}
|
|
43
|
+
store.subscribe((data) => set(data));
|
|
44
|
+
const data = get();
|
|
45
|
+
if (data)
|
|
46
|
+
updateStore(data);
|
|
47
|
+
return Object.assign({ get, set }, store);
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=create-local-storage-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-local-storage-store.js","sourceRoot":"","sources":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Ae,CAAC,CAAC;SAC9C;QAAC,OAAO,KAAK,EAAE;YACZ,OAAO,CAAC,GAAG,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAC;SACvD;IACL,CAAC;IAED,SAAS,qBAAqB,CAAC,KAAY;QACvC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAgB,CAAC;QAC/C,MAAM,aAAa,GAAU,EAAW,CAAC;QAEzC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,EAAC,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,QAAQ,CAAC,GAAG,EAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC/D,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;YACzB,aAAa,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAErC,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC;IAEnB,IAAI,IAAI;QAAE,WAAW,CAAC,IAAI,CAAC,CAAC;IAE5B,uBAAS,GAAG,EAAE,GAAG,IAAK,KAAK,EAAG;AAClC,CAAC,CAAC","sourcesContent":["import { BrowserStorageStoreItem, BrowserStorageStoreSettings, Keys } from '../store.types';\nimport { createStore } from './create-store';\n\nexport const createLocalStorageStore = <State extends object, Action extends string>({\n key,\n actions,\n initialState,\n expireInDays,\n excludeProps,\n initAction,\n}: BrowserStorageStoreSettings<State, Action>) => {\n const store = createStore<State, Action>({\n initialState,\n actions,\n });\n\n function updateStore(data: State) {\n return store.dispatch(initAction, data);\n }\n\n function get(): State | undefined {\n const state = localStorage.getItem(key);\n if (!state) return;\n\n const parsedState = JSON.parse(state) as BrowserStorageStoreItem<State>;\n if (parsedState.expire && new Date() > parsedState.expire) {\n localStorage.removeItem(key);\n return;\n }\n\n return parsedState.value;\n }\n\n function set(state: State) {\n try {\n const now = new Date();\n const serializedState = JSON.stringify({\n value: excludeProps ? excludePropsFromState(state) : state,\n expire: expireInDays ? now.setDate(now.getDate() + expireInDays) : undefined,\n });\n\n localStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in localStorage.setItem', error);\n }\n }\n\n function excludePropsFromState(state: State) {\n const keys = Object.keys(state) as Keys<State>;\n const filteredState: State = {} as State;\n\n keys.filter((key) => !excludeProps?.includes(key)).forEach((key) => {\n const value = state[key];\n filteredState[key] = value;\n });\n\n return filteredState;\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BrowserStorageStoreSettings } from '../store.types';
|
|
2
|
+
export declare const createSessionStorageStore: <State extends object, Action extends string>({ key, initialState, actions, initAction, }: BrowserStorageStoreSettings<State, Action>) => {
|
|
3
|
+
getState: import("../store.types").State<State>;
|
|
4
|
+
getPrevState: import("../store.types").State<State>;
|
|
5
|
+
subscribe: import("../store.types").Subscribe<State>;
|
|
6
|
+
unsubscribe: import("../store.types").Unsubscribe<State>;
|
|
7
|
+
dispatch: import("../store.types").Dispatch<Action, State>;
|
|
8
|
+
get: () => State | undefined;
|
|
9
|
+
set: (value: State) => void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createStore } from './create-store';
|
|
2
|
+
export const createSessionStorageStore = ({ key, initialState, actions, initAction, }) => {
|
|
3
|
+
const store = createStore({
|
|
4
|
+
initialState,
|
|
5
|
+
actions,
|
|
6
|
+
});
|
|
7
|
+
function updateStore(data) {
|
|
8
|
+
return store.dispatch(initAction, data);
|
|
9
|
+
}
|
|
10
|
+
function get() {
|
|
11
|
+
const value = sessionStorage.getItem(key);
|
|
12
|
+
if (!value)
|
|
13
|
+
return;
|
|
14
|
+
return JSON.parse(value);
|
|
15
|
+
}
|
|
16
|
+
function set(value) {
|
|
17
|
+
try {
|
|
18
|
+
const serializedState = JSON.stringify(value);
|
|
19
|
+
sessionStorage.setItem(key, serializedState);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.log('Error in sessionStorage.setItem', error);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
store.subscribe((data) => set(data));
|
|
26
|
+
const data = get();
|
|
27
|
+
if (data)
|
|
28
|
+
updateStore(data);
|
|
29
|
+
return Object.assign({ get, set }, store);
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=create-session-storage-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-session-storage-store.js","sourceRoot":"","sources":["../../../src/store/generators/create-session-storage-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAA8C,EACnF,GAAG,EACH,YAAY,EACZ,OAAO,EACP,UAAU,GAC+B,EAAE,EAAE;IAC7C,MAAM,KAAK,GAAG,WAAW,CAAgB;QACrC,YAAY;QACZ,OAAO;KACV,CAAC,CAAC;IAEH,SAAS,WAAW,CAAC,IAAW;QAC5B,OAAO,KAAK,CAAC,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED,SAAS,GAAG;QACR,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC1C,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAED,SAAS,GAAG,CAAC,KAAY;QACrB,IAAI;YACA,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAE9C,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;SAChD;QAAC,OAAO,KAAK,EAAE;YACZ,OAAO,CAAC,GAAG,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;SACzD;IACL,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAErC,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC;IACnB,IAAI,IAAI;QAAE,WAAW,CAAC,IAAI,CAAC,CAAC;IAE5B,uBAAS,GAAG,EAAE,GAAG,IAAK,KAAK,EAAG;AAClC,CAAC,CAAC","sourcesContent":["import { BrowserStorageStoreSettings } from '../store.types';\nimport { createStore } from './create-store';\n\nexport const createSessionStorageStore = <State extends object, Action extends string>({\n key,\n initialState,\n actions,\n initAction,\n}: BrowserStorageStoreSettings<State, Action>) => {\n const store = createStore<State, Action>({\n initialState,\n actions,\n });\n\n function updateStore(data: State) {\n return store.dispatch(initAction, data);\n }\n\n function get(): State | undefined {\n const value = sessionStorage.getItem(key);\n if (!value) return;\n\n return JSON.parse(value);\n }\n\n function set(value: State) {\n try {\n const serializedState = JSON.stringify(value);\n\n sessionStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in sessionStorage.setItem', error);\n }\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { proxyContainer } from '../polyfill';
|
|
2
|
+
function valueHasChanged(value, old) {
|
|
3
|
+
return old !== value && (old === old || value === value);
|
|
4
|
+
}
|
|
5
|
+
export function createStore(settings) {
|
|
6
|
+
const observers = [];
|
|
7
|
+
let prevState = settings.initialState;
|
|
8
|
+
const validator = {
|
|
9
|
+
set(state, key, value) {
|
|
10
|
+
if (valueHasChanged(state[key], value)) {
|
|
11
|
+
state[key] = value;
|
|
12
|
+
callObservers(state, key);
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
let state = proxyContainer(settings.initialState || {}, validator);
|
|
18
|
+
function subscribe(observer, keys) {
|
|
19
|
+
if (typeof observer !== 'function')
|
|
20
|
+
new Error('You can only subscribe to Store changes with a valid function!');
|
|
21
|
+
observers.push({ callback: observer, keys });
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function unsubscribe(observer) {
|
|
25
|
+
if (typeof observer !== 'function')
|
|
26
|
+
new Error('You can only subscribe to Store changes with a valid function!');
|
|
27
|
+
const match = observers.find(({ callback }) => callback === observer);
|
|
28
|
+
if (match) {
|
|
29
|
+
observers.splice(observers.indexOf(match), 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function dispatch(actionKey, payload) {
|
|
33
|
+
if (!settings.actions || typeof settings.actions[actionKey] !== 'function')
|
|
34
|
+
new Error(`Action "${actionKey}" doesn't exist.`);
|
|
35
|
+
const action = settings.actions[actionKey];
|
|
36
|
+
prevState = Object.assign({}, state);
|
|
37
|
+
const newState = await action(state, payload);
|
|
38
|
+
state = newState;
|
|
39
|
+
return state;
|
|
40
|
+
}
|
|
41
|
+
function callObservers(data, key) {
|
|
42
|
+
observers.forEach(({ keys, callback }) => {
|
|
43
|
+
if (!keys) {
|
|
44
|
+
callback(data);
|
|
45
|
+
}
|
|
46
|
+
else if (Array.isArray(keys) && keys.indexOf(key) > -1) {
|
|
47
|
+
callback(data);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
subscribe,
|
|
53
|
+
unsubscribe,
|
|
54
|
+
dispatch,
|
|
55
|
+
getState: () => state,
|
|
56
|
+
getPrevState: () => prevState,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=create-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-store.js","sourceRoot":"","sources":["../../../src/store/generators/create-store.ts"],"names":[],"mappings":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AE;YACP,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;SACjD;IACL,CAAC;IAED,KAAK,UAAU,QAAQ,CAAC,SAAkB,EAAE,OAAY;QACpD,IAAI,CAAC,QAAQ,CAAC,OAAO,IAAI,OAAO,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,UAAU;YACtE,IAAI,KAAK,CAAC,WAAW,SAAS,kBAAkB,CAAC,CAAC;QAEtD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAQ,CAAC,SAAS,CAAC,CAAC;QAC5C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACrC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC9C,KAAK,GAAG,QAAQ,CAAC;QAEjB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,SAAS,aAAa,CAAC,IAAW,EAAE,GAAQ;QACxC,SAAS,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE;YACrC,IAAI,CAAC,IAAI,EAAE;gBACP,QAAQ,CAAC,IAAI,CAAC,CAAC;aAClB;iBAAM,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE;gBACtD,QAAQ,CAAC,IAAI,CAAC,CAAC;aAClB;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IAED,OAAO;QACH,SAAS;QACT,WAAW;QACX,QAAQ;QACR,QAAQ,EAAE,GAAG,EAAE,CAAC,KAAK;QACrB,YAAY,EAAE,GAAG,EAAE,CAAC,SAAS;KAChC,CAAC;AACN,CAAC","sourcesContent":["import { proxyContainer } from '../polyfill';\nimport { Keys, ObserverCallback, Observers, Store, StoreSettings } from '../store.types';\n\nfunction valueHasChanged(value: unknown, old: unknown): boolean {\n // This ensures (old==NaN, value==NaN) always returns false\n return old !== value && (old === old || value === value);\n}\n\nexport function createStore<State extends object, Actions extends string>(\n settings: StoreSettings<State, Actions>,\n): Store<State, Actions> {\n const observers: Observers<State> = [];\n\n let prevState: State = settings.initialState;\n\n const validator = {\n set(state: State, key: keyof State, value: any) {\n if (valueHasChanged(state[key], value)) {\n state[key] = value;\n callObservers(state, key);\n }\n\n return true;\n },\n };\n\n let state = proxyContainer<State>(settings.initialState || {}, validator);\n\n function subscribe(observer: ObserverCallback<State>, keys?: Keys<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n observers.push({ callback: observer, keys });\n return true;\n }\n\n function unsubscribe(observer: ObserverCallback<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n\n const match = observers.find(({ callback }) => callback === observer);\n if (match) {\n observers.splice(observers.indexOf(match), 1);\n }\n }\n\n async function dispatch(actionKey: Actions, payload: any) {\n if (!settings.actions || typeof settings.actions[actionKey] !== 'function')\n new Error(`Action \"${actionKey}\" doesn't exist.`);\n\n const action = settings.actions![actionKey];\n prevState = Object.assign({}, state);\n const newState = await action(state, payload);\n state = newState;\n\n return state;\n }\n\n function callObservers(data: State, key: any) {\n observers.forEach(({ keys, callback }) => {\n if (!keys) {\n callback(data);\n } else if (Array.isArray(keys) && keys.indexOf(key) > -1) {\n callback(data);\n }\n });\n }\n\n return {\n subscribe,\n unsubscribe,\n dispatch,\n getState: () => state,\n getPrevState: () => prevState,\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/store/generators/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC","sourcesContent":["export * from './create-store';\nexport * from './create-local-storage-store';\nexport * from './create-session-storage-store';\n"]}
|
package/dist/store/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './store.types';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './generators';
|
package/dist/store/index.js
CHANGED
package/dist/store/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC","sourcesContent":["export * from './store.types';\nexport * from './generators';\n"]}
|
package/dist/store/store.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { BrowserStorageStoreSettings, Store, StoreSettings } from './store.types';
|
|
2
2
|
export declare function createStore<State extends object, Actions extends string>(settings: StoreSettings<State, Actions>): Store<State, Actions>;
|
|
3
|
-
export declare const createLocalStorageStore: <State extends object, Action extends string>({ key, actions, initialState, expireInDays, excludeProps,
|
|
3
|
+
export declare const createLocalStorageStore: <State extends object, Action extends string>({ key, actions, initialState, expireInDays, excludeProps, initAction, }: BrowserStorageStoreSettings<State, Action>) => {
|
|
4
4
|
getState: import("./store.types").State<State>;
|
|
5
5
|
getPrevState: import("./store.types").State<State>;
|
|
6
6
|
subscribe: import("./store.types").Subscribe<State>;
|
|
7
7
|
unsubscribe: import("./store.types").Unsubscribe<State>;
|
|
8
|
-
dispatch: import("./store.types").Dispatch<Action
|
|
8
|
+
dispatch: import("./store.types").Dispatch<Action, State>;
|
|
9
9
|
get: () => State | undefined;
|
|
10
10
|
set: (state: State) => void;
|
|
11
11
|
};
|
|
12
|
-
export declare const createSessionStorageStore: <State extends object, Action extends string>({ key, initialState, actions,
|
|
12
|
+
export declare const createSessionStorageStore: <State extends object, Action extends string>({ key, initialState, actions, initAction, }: BrowserStorageStoreSettings<State, Action>) => {
|
|
13
13
|
getState: import("./store.types").State<State>;
|
|
14
14
|
getPrevState: import("./store.types").State<State>;
|
|
15
15
|
subscribe: import("./store.types").Subscribe<State>;
|
|
16
16
|
unsubscribe: import("./store.types").Unsubscribe<State>;
|
|
17
|
-
dispatch: import("./store.types").Dispatch<
|
|
17
|
+
dispatch: import("./store.types").Dispatch<Action, State>;
|
|
18
18
|
get: () => State | undefined;
|
|
19
19
|
set: (value: State) => void;
|
|
20
20
|
};
|
package/dist/store/store.js
CHANGED
|
@@ -56,13 +56,13 @@ export function createStore(settings) {
|
|
|
56
56
|
function valueHasChanged(value, old) {
|
|
57
57
|
return old !== value && (old === old || value === value);
|
|
58
58
|
}
|
|
59
|
-
export const createLocalStorageStore = ({ key, actions, initialState, expireInDays, excludeProps,
|
|
59
|
+
export const createLocalStorageStore = ({ key, actions, initialState, expireInDays, excludeProps, initAction, }) => {
|
|
60
60
|
const store = createStore({
|
|
61
61
|
initialState,
|
|
62
|
-
actions
|
|
62
|
+
actions,
|
|
63
63
|
});
|
|
64
64
|
function updateStore(data) {
|
|
65
|
-
return store.dispatch(
|
|
65
|
+
return store.dispatch(initAction, data);
|
|
66
66
|
}
|
|
67
67
|
function get() {
|
|
68
68
|
const state = localStorage.getItem(key);
|
|
@@ -103,13 +103,13 @@ export const createLocalStorageStore = ({ key, actions, initialState, expireInDa
|
|
|
103
103
|
updateStore(data);
|
|
104
104
|
return Object.assign({ get, set }, store);
|
|
105
105
|
};
|
|
106
|
-
export const createSessionStorageStore = ({ key, initialState, actions,
|
|
106
|
+
export const createSessionStorageStore = ({ key, initialState, actions, initAction, }) => {
|
|
107
107
|
const store = createStore({
|
|
108
108
|
initialState,
|
|
109
|
-
actions
|
|
109
|
+
actions,
|
|
110
110
|
});
|
|
111
111
|
function updateStore(data) {
|
|
112
|
-
return store.dispatch(
|
|
112
|
+
return store.dispatch(initAction, data);
|
|
113
113
|
}
|
|
114
114
|
function get() {
|
|
115
115
|
const value = sessionStorage.getItem(key);
|
package/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C,GAAG,CAAC,CAAC;QAC1C,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAED,SAAS,GAAG,CAAC,KAAY;QACrB,IAAI;YACA,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAE9C,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;SAChD;QAAC,OAAO,KAAK,EAAE;YACZ,OAAO,CAAC,GAAG,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;SACzD;IACL,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAErC,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC;IACnB,IAAI,IAAI;QAAE,WAAW,CAAC,IAAI,CAAC,CAAC;IAE5B,uBAAS,GAAG,EAAE,GAAG,IAAK,KAAK,EAAG;AAClC,CAAC,CAAC","sourcesContent":["import { proxyContainer } from './polyfill';\nimport {\n Actions,\n BrowserStorageStoreItem,\n BrowserStorageStoreSettings,\n Keys,\n ObserverCallback,\n Observers,\n Store,\n StoreSettings,\n} from './store.types';\n\nexport function createStore<State extends object, Actions extends string>(\n settings: StoreSettings<State, Actions>,\n): Store<State, Actions> {\n const observers: Observers<State> = [];\n\n let prevState: State = settings.initialState;\n\n const validator = {\n set(state: State, key: keyof State, value: any) {\n if (valueHasChanged(state[key], value)) {\n state[key] = value;\n callObservers(state, key);\n }\n\n return true;\n },\n };\n\n let state = proxyContainer<State>(settings.initialState || {}, validator);\n\n function subscribe(observer: ObserverCallback<State>, keys?: Keys<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n observers.push({ callback: observer, keys });\n return true;\n }\n\n function unsubscribe(observer: ObserverCallback<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n\n const match = observers.find(({ callback }) => callback === observer);\n if (match) {\n observers.splice(observers.indexOf(match), 1);\n }\n }\n\n async function dispatch(actionKey: Actions, payload: any) {\n if (!settings.actions || typeof settings.actions[actionKey] !== 'function')\n new Error(`Action \"${actionKey}\" doesn't exist.`);\n\n const action = settings.actions![actionKey];\n prevState = Object.assign({}, state);\n const newState = await action(state, payload);\n state = newState;\n\n return state;\n }\n\n function callObservers(data: State, key: any) {\n observers.forEach(({ keys, callback }) => {\n if (!keys) {\n callback(data);\n } else if (Array.isArray(keys) && keys.indexOf(key) > -1) {\n callback(data);\n }\n });\n }\n\n return {\n subscribe,\n unsubscribe,\n dispatch,\n getState: () => state,\n getPrevState: () => prevState,\n };\n}\n\nfunction valueHasChanged(value: unknown, old: unknown): boolean {\n // This ensures (old==NaN, value==NaN) always returns false\n return old !== value && (old === old || value === value);\n}\n\nexport const createLocalStorageStore = <State extends object, Action extends string>({\n key,\n actions,\n initialState,\n expireInDays,\n excludeProps,\n init,\n}: BrowserStorageStoreSettings<State, Action>) => {\n type BrowserStoreAction = Action | 'init';\n\n const store = createStore<State, BrowserStoreAction>({\n initialState,\n actions: { ...actions, init } as Actions<BrowserStoreAction, State>,\n });\n\n function updateStore(data: State) {\n return store.dispatch('init', data);\n }\n\n function get(): State | undefined {\n const state = localStorage.getItem(key);\n if (!state) return;\n\n const parsedState = JSON.parse(state) as BrowserStorageStoreItem<State>;\n if (parsedState.expire && new Date() > parsedState.expire) {\n localStorage.removeItem(key);\n return;\n }\n\n return parsedState.value;\n }\n\n function set(state: State) {\n try {\n const now = new Date();\n const serializedState = JSON.stringify({\n value: excludeProps ? excludePropsFromState(state) : state,\n expire: expireInDays ? now.setDate(now.getDate() + expireInDays) : undefined,\n });\n\n localStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in localStorage.setItem', error);\n }\n }\n\n function excludePropsFromState(state: State) {\n const keys = Object.keys(state) as Keys<State>;\n const filteredState: State = {} as State;\n\n keys.filter((key) => !excludeProps?.includes(key)).forEach((key) => {\n const value = state[key];\n filteredState[key] = value;\n });\n\n return filteredState;\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n\nexport const createSessionStorageStore = <State extends object, Action extends string>({\n key,\n initialState,\n actions,\n init,\n}: BrowserStorageStoreSettings<State, Action>) => {\n type BrowserStoreAction = Action | 'init';\n\n const store = createStore<State, BrowserStoreAction>({\n initialState,\n actions: { ...actions, init } as Actions<BrowserStoreAction, State>,\n });\n\n function updateStore(data: State) {\n return store.dispatch('init', data);\n }\n\n function get(): State | undefined {\n const value = sessionStorage.getItem(key);\n if (!value) return;\n\n return JSON.parse(value);\n }\n\n function set(value: State) {\n try {\n const serializedState = JSON.stringify(value);\n\n sessionStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in sessionStorage.setItem', error);\n }\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAW5C,MAAM,UAAU,WAAW,CACvB,QAAuC;IAEvC,MAAM,SAAS,GAAqB,EAAE,CAAC;IAEvC,IAAI,SAAS,GAAU,QAAQ,CAAC,YAAY,CAAC;IAE7C,MAAM,SAAS,GAAG;QACd,GAAG,CAAC,KAAY,EAAE,GAAgB,EAAE,KAAU;YAC1C,IAAI,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,EAAE;gBACpC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;gBACnB,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;aAC7B;YAED,OAAO,IAAI,CAAC;QAChB,CAAC;KACJ,CAAC;IAEF,IAAI,KAAK,GAAG,cAAc,CAAQ,QAAQ,CAAC,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Item,\n BrowserStorageStoreSettings,\n Keys,\n ObserverCallback,\n Observers,\n Store,\n StoreSettings,\n} from './store.types';\n\nexport function createStore<State extends object, Actions extends string>(\n settings: StoreSettings<State, Actions>,\n): Store<State, Actions> {\n const observers: Observers<State> = [];\n\n let prevState: State = settings.initialState;\n\n const validator = {\n set(state: State, key: keyof State, value: any) {\n if (valueHasChanged(state[key], value)) {\n state[key] = value;\n callObservers(state, key);\n }\n\n return true;\n },\n };\n\n let state = proxyContainer<State>(settings.initialState || {}, validator);\n\n function subscribe(observer: ObserverCallback<State>, keys?: Keys<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n observers.push({ callback: observer, keys });\n return true;\n }\n\n function unsubscribe(observer: ObserverCallback<State>) {\n if (typeof observer !== 'function')\n new Error('You can only subscribe to Store changes with a valid function!');\n\n const match = observers.find(({ callback }) => callback === observer);\n if (match) {\n observers.splice(observers.indexOf(match), 1);\n }\n }\n\n async function dispatch(actionKey: Actions, payload: any) {\n if (!settings.actions || typeof settings.actions[actionKey] !== 'function')\n new Error(`Action \"${actionKey}\" doesn't exist.`);\n\n const action = settings.actions![actionKey];\n prevState = Object.assign({}, state);\n const newState = await action(state, payload);\n state = newState;\n\n return state;\n }\n\n function callObservers(data: State, key: any) {\n observers.forEach(({ keys, callback }) => {\n if (!keys) {\n callback(data);\n } else if (Array.isArray(keys) && keys.indexOf(key) > -1) {\n callback(data);\n }\n });\n }\n\n return {\n subscribe,\n unsubscribe,\n dispatch,\n getState: () => state,\n getPrevState: () => prevState,\n };\n}\n\nfunction valueHasChanged(value: unknown, old: unknown): boolean {\n // This ensures (old==NaN, value==NaN) always returns false\n return old !== value && (old === old || value === value);\n}\n\nexport const createLocalStorageStore = <State extends object, Action extends string>({\n key,\n actions,\n initialState,\n expireInDays,\n excludeProps,\n initAction,\n}: BrowserStorageStoreSettings<State, Action>) => {\n const store = createStore<State, Action>({\n initialState,\n actions,\n });\n\n function updateStore(data: State) {\n return store.dispatch(initAction, data);\n }\n\n function get(): State | undefined {\n const state = localStorage.getItem(key);\n if (!state) return;\n\n const parsedState = JSON.parse(state) as BrowserStorageStoreItem<State>;\n if (parsedState.expire && new Date() > parsedState.expire) {\n localStorage.removeItem(key);\n return;\n }\n\n return parsedState.value;\n }\n\n function set(state: State) {\n try {\n const now = new Date();\n const serializedState = JSON.stringify({\n value: excludeProps ? excludePropsFromState(state) : state,\n expire: expireInDays ? now.setDate(now.getDate() + expireInDays) : undefined,\n });\n\n localStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in localStorage.setItem', error);\n }\n }\n\n function excludePropsFromState(state: State) {\n const keys = Object.keys(state) as Keys<State>;\n const filteredState: State = {} as State;\n\n keys.filter((key) => !excludeProps?.includes(key)).forEach((key) => {\n const value = state[key];\n filteredState[key] = value;\n });\n\n return filteredState;\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n\nexport const createSessionStorageStore = <State extends object, Action extends string>({\n key,\n initialState,\n actions,\n initAction,\n}: BrowserStorageStoreSettings<State, Action>) => {\n const store = createStore<State, Action>({\n initialState,\n actions,\n });\n\n function updateStore(data: State) {\n return store.dispatch(initAction, data);\n }\n\n function get(): State | undefined {\n const value = sessionStorage.getItem(key);\n if (!value) return;\n\n return JSON.parse(value);\n }\n\n function set(value: State) {\n try {\n const serializedState = JSON.stringify(value);\n\n sessionStorage.setItem(key, serializedState);\n } catch (error) {\n console.log('Error in sessionStorage.setItem', error);\n }\n }\n\n store.subscribe((data) => set(data));\n\n const data = get();\n if (data) updateStore(data);\n\n return { get, set, ...store };\n};\n"]}
|
|
@@ -28,7 +28,7 @@ export interface BrowserStorageStoreItem<T> {
|
|
|
28
28
|
}
|
|
29
29
|
export interface BrowserStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {
|
|
30
30
|
key: string;
|
|
31
|
-
|
|
31
|
+
initAction: A;
|
|
32
32
|
expireInDays?: number;
|
|
33
33
|
excludeProps?: Keys<T>;
|
|
34
34
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.types.js","sourceRoot":"","sources":["../../src/store/store.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ObserverCallback<T> = (state: T) => void;\nexport type Keys<T> = (keyof T)[];\n\nexport type ActionHandler<T> = (state: T, payload: any) => T;\nexport type Actions<A extends string, T> = Record<A, ActionHandler<T>>;\n\nexport type Subscribe<T> = (observer: ObserverCallback<T>, keys?: Keys<T>) => void;\nexport type Unsubscribe<T> = (observer: ObserverCallback<T>) => void;\nexport type Dispatch<A extends string, T> = (actionKey: A, payload?: any) => T | Promise<T>;\nexport type State<T> = () => T;\n\nexport interface Store<T, A extends string> {\n getState: State<T>;\n getPrevState: State<T>;\n subscribe: Subscribe<T>;\n unsubscribe: Unsubscribe<T>;\n dispatch: Dispatch<A, T>;\n}\n\nexport interface StoreSettings<T, A extends string> {\n actions?: Actions<A, T>;\n initialState: T;\n}\n\nexport interface Observer<T> {\n callback: ObserverCallback<T>;\n keys?: Keys<T>;\n}\n\nexport type Observers<T> = Array<Observer<T>>;\n\nexport interface BrowserStorageStoreItem<T> {\n value: T;\n expire?: Date;\n}\n\nexport interface BrowserStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {\n key: string;\n
|
|
1
|
+
{"version":3,"file":"store.types.js","sourceRoot":"","sources":["../../src/store/store.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ObserverCallback<T> = (state: T) => void;\nexport type Keys<T> = (keyof T)[];\n\nexport type ActionHandler<T> = (state: T, payload: any) => T;\nexport type Actions<A extends string, T> = Record<A, ActionHandler<T>>;\n\nexport type Subscribe<T> = (observer: ObserverCallback<T>, keys?: Keys<T>) => void;\nexport type Unsubscribe<T> = (observer: ObserverCallback<T>) => void;\nexport type Dispatch<A extends string, T> = (actionKey: A, payload?: any) => T | Promise<T>;\nexport type State<T> = () => T;\n\nexport interface Store<T, A extends string> {\n getState: State<T>;\n getPrevState: State<T>;\n subscribe: Subscribe<T>;\n unsubscribe: Unsubscribe<T>;\n dispatch: Dispatch<A, T>;\n}\n\nexport interface StoreSettings<T, A extends string> {\n actions?: Actions<A, T>;\n initialState: T;\n}\n\nexport interface Observer<T> {\n callback: ObserverCallback<T>;\n keys?: Keys<T>;\n}\n\nexport type Observers<T> = Array<Observer<T>>;\n\nexport interface BrowserStorageStoreItem<T> {\n value: T;\n expire?: Date;\n}\n\nexport interface BrowserStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {\n key: string;\n initAction: A;\n expireInDays?: number;\n excludeProps?: Keys<T>;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bpd-library/utilities",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Description",
|
|
5
5
|
"url": "https://github.com/{repo name}",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,13 +45,13 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "077815a3e8b163467e87285e5a304618ad0ed0c6",
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@atomify/core": "2.4.1",
|
|
51
51
|
"@atomify/hooks": "1.1.11",
|
|
52
52
|
"@atomify/jsx": "1.7.1",
|
|
53
53
|
"@atomify/kit": "1.1.11",
|
|
54
|
-
"@bpd-library/types": "^2.0.
|
|
54
|
+
"@bpd-library/types": "^2.0.4",
|
|
55
55
|
"qs": "^6.9.4",
|
|
56
56
|
"query-string": "5"
|
|
57
57
|
}
|