@bpd-library/utilities 2.0.3-beta.0 → 2.0.3-beta.2

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.
@@ -1,11 +1,20 @@
1
- import { LocalStorageStoreSettings, Store, StoreSettings } from './store.types';
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, init, }: LocalStorageStoreSettings<State, Action>) => {
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 | "init", State>;
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, initAction, }: BrowserStorageStoreSettings<State, Action>) => {
13
+ getState: import("./store.types").State<State>;
14
+ getPrevState: import("./store.types").State<State>;
15
+ subscribe: import("./store.types").Subscribe<State>;
16
+ unsubscribe: import("./store.types").Unsubscribe<State>;
17
+ dispatch: import("./store.types").Dispatch<Action, State>;
18
+ get: () => State | undefined;
19
+ set: (value: State) => void;
20
+ };
@@ -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, init, }) => {
59
+ export const createLocalStorageStore = ({ key, actions, initialState, expireInDays, excludeProps, initAction, }) => {
60
60
  const store = createStore({
61
61
  initialState,
62
- actions: Object.assign(Object.assign({}, actions), { init }),
62
+ actions,
63
63
  });
64
64
  function updateStore(data) {
65
- return store.dispatch('init', data);
65
+ return store.dispatch(initAction, data);
66
66
  }
67
67
  function get() {
68
68
  const state = localStorage.getItem(key);
@@ -103,4 +103,33 @@ 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, initAction, }) => {
107
+ const store = createStore({
108
+ initialState,
109
+ actions,
110
+ });
111
+ function updateStore(data) {
112
+ return store.dispatch(initAction, data);
113
+ }
114
+ function get() {
115
+ const value = sessionStorage.getItem(key);
116
+ if (!value)
117
+ return;
118
+ return JSON.parse(value);
119
+ }
120
+ function set(value) {
121
+ try {
122
+ const serializedState = JSON.stringify(value);
123
+ sessionStorage.setItem(key, serializedState);
124
+ }
125
+ catch (error) {
126
+ console.log('Error in sessionStorage.setItem', error);
127
+ }
128
+ }
129
+ store.subscribe((data) => set(data));
130
+ const data = get();
131
+ if (data)
132
+ updateStore(data);
133
+ return Object.assign({ get, set }, store);
134
+ };
106
135
  //# sourceMappingURL=store.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAY5C,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,GAA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,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 { proxyContainer } from './polyfill';\nimport {\n Actions,\n Keys,\n LocalStorageStoreItem,\n LocalStorageStoreSettings,\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}\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}: LocalStorageStoreSettings<State, Action>) => {\n type LocalStoreAction = Action | 'init';\n\n const store = createStore<State, LocalStoreAction>({\n initialState,\n actions: { ...actions, init } as Actions<LocalStoreAction, 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\n if (!state) return;\n\n const parsedState = JSON.parse(state) as LocalStorageStoreItem<State>;\n\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\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"]}
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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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 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 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"]}
@@ -22,13 +22,13 @@ export interface Observer<T> {
22
22
  keys?: Keys<T>;
23
23
  }
24
24
  export declare type Observers<T> = Array<Observer<T>>;
25
- export interface LocalStorageStoreItem<T> {
25
+ export interface BrowserStorageStoreItem<T> {
26
26
  value: T;
27
27
  expire?: Date;
28
28
  }
29
- export interface LocalStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {
29
+ export interface BrowserStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {
30
30
  key: string;
31
- init: ActionHandler<T>;
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 LocalStorageStoreItem<T> {\n value: T;\n expire?: Date;\n}\n\nexport interface LocalStorageStoreSettings<T, A extends string> extends StoreSettings<T, A> {\n key: string;\n init: ActionHandler<T>;\n expireInDays?: number;\n excludeProps?: Keys<T>;\n}\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-beta.0",
3
+ "version": "2.0.3-beta.2",
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": "29fd572d2b4816cd3d433e8ef6f382ea9655910e",
48
+ "gitHead": "911d40f6b678619ef13f6422815fb345e8cfcfc5",
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.3-beta.0",
54
+ "@bpd-library/types": "^2.0.3-beta.2",
55
55
  "qs": "^6.9.4",
56
56
  "query-string": "5"
57
57
  }