@bpd-library/utilities 2.0.1-beta.0 → 2.0.1-beta.1

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.
@@ -5,4 +5,5 @@ export * from './use-scroll-to';
5
5
  export * from './use-url-params';
6
6
  export * from './use-map-storage';
7
7
  export * from './use-lazy';
8
+ export * from './use-store';
8
9
  export * from './use-keyboard-focus';
@@ -5,5 +5,6 @@ export * from './use-scroll-to';
5
5
  export * from './use-url-params';
6
6
  export * from './use-map-storage';
7
7
  export * from './use-lazy';
8
+ export * from './use-store';
8
9
  export * from './use-keyboard-focus';
9
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,sBAAsB,CAAC","sourcesContent":["export * from './use-scroll-position';\nexport * from './use-click-outside';\nexport * from './use-history';\nexport * from './use-scroll-to';\nexport * from './use-url-params';\nexport * from './use-map-storage';\nexport * from './use-lazy';\nexport * from './use-keyboard-focus';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC","sourcesContent":["export * from './use-scroll-position';\nexport * from './use-click-outside';\nexport * from './use-history';\nexport * from './use-scroll-to';\nexport * from './use-url-params';\nexport * from './use-map-storage';\nexport * from './use-lazy';\nexport * from './use-store';\nexport * from './use-keyboard-focus';\n"]}
@@ -0,0 +1,2 @@
1
+ import { Store, Subscribe } from '../store/store.types';
2
+ export declare const useStore: <T, S extends string = string>(store: Store<T, S>) => [T, Subscribe<T>, T];
@@ -0,0 +1,10 @@
1
+ import { createHook } from '@atomify/hooks';
2
+ export const useStore = (store) => createHook({
3
+ onDidLoad: () => {
4
+ const state = store.getState();
5
+ const subscribe = store.subscribe;
6
+ const prevState = store.getPrevState();
7
+ return [state, subscribe, prevState];
8
+ },
9
+ });
10
+ //# sourceMappingURL=use-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-store.js","sourceRoot":"","sources":["../../src/hooks/use-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAQ5C,MAAM,CAAC,MAAM,QAAQ,GAAG,CAA+B,KAAkB,EAAE,EAAE,CACzE,UAAU,CAAuB;IAC7B,SAAS,EAAE,GAAG,EAAE;QACZ,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;QAC/B,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC;QAClC,MAAM,SAAS,GAAG,KAAK,CAAC,YAAY,EAAE,CAAC;QAEvC,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;IACzC,CAAC;CACJ,CAAC,CAAC","sourcesContent":["import { createHook } from '@atomify/hooks';\n\nimport { Store, Subscribe } from '../store/store.types';\n/**\n * Store wrapper for functional components\n * @template T\n * @param {Store<T>} store\n */\nexport const useStore = <T, S extends string = string>(store: Store<T, S>) =>\n createHook<[T, Subscribe<T>, T]>({\n onDidLoad: () => {\n const state = store.getState();\n const subscribe = store.subscribe;\n const prevState = store.getPrevState();\n\n return [state, subscribe, prevState];\n },\n });\n"]}
package/dist/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from './to-currency';
16
16
  export * from './extended-api-models';
17
17
  export * from './throttle';
18
18
  export * from './partition';
19
+ export * from './store';
package/dist/index.js CHANGED
@@ -16,4 +16,5 @@ export * from './to-currency';
16
16
  export * from './extended-api-models';
17
17
  export * from './throttle';
18
18
  export * from './partition';
19
+ export * from './store';
19
20
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC","sourcesContent":["// Export all utilties\nexport * from './api';\nexport * from './generated';\nexport * from './dom';\nexport * from './hooks';\nexport * from './constants';\nexport * from './debounce';\nexport * from './is-touch';\nexport * from './environment';\nexport * from './raf-throttle';\nexport * from './media-query';\nexport * from './function-is-true';\nexport * from './api-utilities';\nexport * from './geometry';\nexport * from './date';\nexport * from './to-currency';\nexport * from './extended-api-models';\nexport * from './throttle';\nexport * from './partition';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC","sourcesContent":["// Export all utilties\nexport * from './api';\nexport * from './generated';\nexport * from './dom';\nexport * from './hooks';\nexport * from './constants';\nexport * from './debounce';\nexport * from './is-touch';\nexport * from './environment';\nexport * from './raf-throttle';\nexport * from './media-query';\nexport * from './function-is-true';\nexport * from './api-utilities';\nexport * from './geometry';\nexport * from './date';\nexport * from './to-currency';\nexport * from './extended-api-models';\nexport * from './throttle';\nexport * from './partition';\nexport * from './store';\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './store.types';
2
+ export * from './store';
@@ -0,0 +1,3 @@
1
+ export * from './store.types';
2
+ export * from './store';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,SAAS,CAAC","sourcesContent":["export * from './store.types';\nexport * from './store';\n"]}
@@ -0,0 +1,3 @@
1
+ declare type ProxyContainer = <T extends object = object, K extends object = object>(services: T, handler: K) => T;
2
+ export declare const proxyContainer: ProxyContainer;
3
+ export {};
@@ -0,0 +1,9 @@
1
+ import proxyPolyfill from './proxy';
2
+ export const proxyContainer = (services, handler) => {
3
+ if (!window.Proxy) {
4
+ const ProxyPolyfill = proxyPolyfill();
5
+ return new ProxyPolyfill(services, handler);
6
+ }
7
+ return new Proxy(services, handler);
8
+ };
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/store/polyfill/index.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,SAAS,CAAC;AAOpC,MAAM,CAAC,MAAM,cAAc,GAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE;IAChE,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE;QACf,MAAM,aAAa,GAAQ,aAAa,EAAE,CAAC;QAC3C,OAAO,IAAI,aAAa,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;KAC/C;IAED,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC,CAAC","sourcesContent":["import proxyPolyfill from './proxy';\n\ntype ProxyContainer = <T extends object = object, K extends object = object>(\n services: T,\n handler: K,\n) => T;\n\nexport const proxyContainer: ProxyContainer = (services, handler) => {\n if (!window.Proxy) {\n const ProxyPolyfill: any = proxyPolyfill();\n return new ProxyPolyfill(services, handler);\n }\n\n return new Proxy(services, handler);\n};\n"]}
@@ -0,0 +1,7 @@
1
+ export default function proxyPolyfill(): {
2
+ (target: any, handler: any): any;
3
+ revocable(target: any, handler: any): {
4
+ proxy: any;
5
+ revoke: any;
6
+ };
7
+ };
@@ -0,0 +1,118 @@
1
+ export default function proxyPolyfill() {
2
+ let lastRevokeFn = null;
3
+ function isObject(o) {
4
+ return o ? typeof o === 'object' || typeof o === 'function' : false;
5
+ }
6
+ const ProxyPolyfill = function (target, handler) {
7
+ if (!isObject(target) || !isObject(handler)) {
8
+ throw new TypeError('Cannot create proxy with a non-object as target or handler');
9
+ }
10
+ let throwRevoked = function () {
11
+ return;
12
+ };
13
+ lastRevokeFn = function () {
14
+ throwRevoked = function (trap) {
15
+ throw new TypeError(`Cannot perform '${trap}' on a proxy that has been revoked`);
16
+ };
17
+ };
18
+ const unsafeHandler = handler;
19
+ handler = { get: null, set: null, apply: null, construct: null };
20
+ for (const k in unsafeHandler) {
21
+ if (!(k in handler)) {
22
+ }
23
+ else {
24
+ handler[k] = unsafeHandler[k];
25
+ }
26
+ }
27
+ if (typeof unsafeHandler === 'function') {
28
+ handler.apply = unsafeHandler.apply.bind(unsafeHandler);
29
+ }
30
+ let proxy = this;
31
+ let isMethod = false;
32
+ let isArray = false;
33
+ if (typeof target === 'function') {
34
+ proxy = function ProxyPolyfill() {
35
+ const usingNew = this && this.constructor === proxy;
36
+ const args = Array.prototype.slice.call(arguments);
37
+ throwRevoked(usingNew ? 'construct' : 'apply');
38
+ if (usingNew && handler['construct']) {
39
+ return handler['construct'].call(this, target, args);
40
+ }
41
+ else if (!usingNew && handler.apply) {
42
+ return handler.apply(target, this, args);
43
+ }
44
+ if (usingNew) {
45
+ args.unshift(target);
46
+ const f = target.bind.apply(target, args);
47
+ return new f();
48
+ }
49
+ return target.apply(this, args);
50
+ };
51
+ isMethod = true;
52
+ }
53
+ else if (target instanceof Array) {
54
+ proxy = [];
55
+ isArray = true;
56
+ }
57
+ const getter = handler.get
58
+ ? function (prop) {
59
+ throwRevoked('get');
60
+ return handler.get(this, prop, proxy);
61
+ }
62
+ : function (prop) {
63
+ throwRevoked('get');
64
+ return this[prop];
65
+ };
66
+ const setter = handler.set
67
+ ? function (prop, value) {
68
+ throwRevoked('set');
69
+ handler.set(this, prop, value, proxy);
70
+ }
71
+ : function (prop, value) {
72
+ throwRevoked('set');
73
+ this[prop] = value;
74
+ };
75
+ const propertyNames = Object.getOwnPropertyNames(target);
76
+ const propertyMap = {};
77
+ propertyNames.forEach(function (prop) {
78
+ if ((isMethod || isArray) && prop in proxy) {
79
+ return;
80
+ }
81
+ const real = Object.getOwnPropertyDescriptor(target, prop);
82
+ const desc = {
83
+ enumerable: !!real.enumerable,
84
+ get: getter.bind(target, prop),
85
+ set: setter.bind(target, prop),
86
+ };
87
+ Object.defineProperty(proxy, prop, desc);
88
+ propertyMap[prop] = true;
89
+ });
90
+ let prototypeOk = true;
91
+ if (Object.setPrototypeOf) {
92
+ Object.setPrototypeOf(proxy, Object.getPrototypeOf(target));
93
+ }
94
+ else if (proxy.__proto__) {
95
+ proxy.__proto__ = target.__proto__;
96
+ }
97
+ else {
98
+ prototypeOk = false;
99
+ }
100
+ if (handler.get || !prototypeOk) {
101
+ for (const k in target) {
102
+ if (propertyMap[k]) {
103
+ continue;
104
+ }
105
+ Object.defineProperty(proxy, k, { get: getter.bind(target, k) });
106
+ }
107
+ }
108
+ Object.seal(target);
109
+ Object.seal(proxy);
110
+ return proxy;
111
+ };
112
+ ProxyPolyfill.revocable = function (target, handler) {
113
+ const p = new ProxyPolyfill(target, handler);
114
+ return { proxy: p, revoke: lastRevokeFn };
115
+ };
116
+ return ProxyPolyfill;
117
+ }
118
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/store/polyfill/proxy.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,aAAa;IACjC,IAAI,YAAY,GAAQ,IAAI,CAAC;IAM7B,SAAS,QAAQ,CAAC,CAAM;QACpB,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC;IACxE,CAAC;IAQD,MAAM,aAAa,GAAG,UAAS,MAAW,EAAE,OAAY;QACpD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YACzC,MAAM,IAAI,SAAS,CAAC,4DAA4D,CAAC,CAAC;SACrF;QAKD,IAAI,YAAY,GAAQ;YACpB,OAAO;QACX,CAAC,CAAC;QACF,YAAY,GAAG;YACX,YAAY,GAAG,UAAS,IAAY;gBAChC,MAAM,IAAI,SAAS,CAAC,mBAAmB,IAAI,oCAAoC,CAAC,CAAC;YACrF,CAAC,CAAC;QACN,CAAC,CAAC;QAIF,MAAM,aAAa,GAAG,OAAO,CAAC;QAC9B,OAAO,GAAG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AAK,CAAC,CAAC;QAEnB,OAAO,KAAK,CAAC;IACjB,CAAC,CAAC;IAEF,aAAa,CAAC,SAAS,GAAG,UAAS,MAAM,EAAE,OAAO;QAC9C,MAAM,CAAC,GAAG,IAAI,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE7C,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,aAAa,CAAC;AACzB,CAAC","sourcesContent":["// @ts-nocheck\n/* eslint-disable */\nexport default function proxyPolyfill() {\n let lastRevokeFn: any = null;\n\n /**\n * @param {*} o\n * @return {boolean} whether this is probably a (non-null) Object\n */\n function isObject(o: any) {\n return o ? typeof o === 'object' || typeof o === 'function' : false;\n }\n\n /**\n * @constructor\n * @param {!Object} target\n * @param {{apply, construct, get, set}} handler\n */\n\n const ProxyPolyfill = function(target: any, handler: any) {\n if (!isObject(target) || !isObject(handler)) {\n throw new TypeError('Cannot create proxy with a non-object as target or handler');\n }\n\n // Construct revoke function, and set lastRevokeFn so that Proxy.revocable can steal it.\n // The caller might get the wrong revoke function if a user replaces or wraps scope.Proxy\n // to call itself, but that seems unlikely especially when using the polyfill.\n let throwRevoked: any = function() {\n return;\n };\n lastRevokeFn = function() {\n throwRevoked = function(trap: string) {\n throw new TypeError(`Cannot perform '${trap}' on a proxy that has been revoked`);\n };\n };\n\n // Fail on unsupported traps: Chrome doesn't do this, but ensure that users of the polyfill\n // are a bit more careful. Copy the internal parts of handler to prevent user changes.\n const unsafeHandler = handler;\n handler = { get: null, set: null, apply: null, construct: null };\n for (const k in unsafeHandler) {\n if (!(k in handler)) {\n // throw new TypeError(`Proxy polyfill does not support trap '${k}'`);\n } else {\n handler[k] = unsafeHandler[k];\n }\n }\n if (typeof unsafeHandler === 'function') {\n // Allow handler to be a function (which has an 'apply' method). This matches what is\n // probably a bug in native versions. It treats the apply call as a trap to be configured.\n handler.apply = unsafeHandler.apply.bind(unsafeHandler);\n }\n\n // Define proxy as this, or a Function (if either it's callable, or apply is set).\n // TODO(samthor): Closure compiler doesn't know about 'construct', attempts to rename it.\n\n let proxy = this;\n let isMethod = false;\n let isArray = false;\n if (typeof target === 'function') {\n proxy = function ProxyPolyfill() {\n const usingNew = this && this.constructor === proxy;\n const args = Array.prototype.slice.call(arguments);\n\n throwRevoked(usingNew ? 'construct' : 'apply');\n\n if (usingNew && handler['construct']) {\n return handler['construct'].call(this, target, args);\n } else if (!usingNew && handler.apply) {\n return handler.apply(target, this, args);\n }\n\n // since the target was a function, fallback to calling it directly.\n if (usingNew) {\n // inspired by answers to https://stackoverflow.com/q/1606797\n args.unshift(target); // pass class as first arg to constructor, although irrelevant\n // nb. cast to convince Closure compiler that this is a constructor\n const f = /** @type {!Function} */ target.bind.apply(target, args);\n /* eslint new-cap: \"off\" */\n return new f();\n }\n\n return target.apply(this, args);\n };\n isMethod = true;\n } else if (target instanceof Array) {\n proxy = [];\n isArray = true;\n }\n\n // Create default getters/setters. Create different code paths as handler.get/handler.set can't\n // change after creation.\n\n const getter = handler.get\n ? function(prop: any) {\n throwRevoked('get');\n\n return handler.get(this, prop, proxy);\n }\n : function(prop: any) {\n throwRevoked('get');\n\n return this[prop];\n };\n const setter = handler.set\n ? function(prop: any, value: any) {\n throwRevoked('set');\n\n handler.set(this, prop, value, proxy);\n }\n : function(prop: any, value: any) {\n throwRevoked('set');\n\n this[prop] = value;\n };\n\n // Clone direct properties (i.e., not part of a prototype).\n const propertyNames = Object.getOwnPropertyNames(target);\n const propertyMap = {};\n propertyNames.forEach(function(prop) {\n if ((isMethod || isArray) && prop in proxy) {\n return; // ignore properties already here, e.g. 'bind', 'prototype' etc\n }\n const real = Object.getOwnPropertyDescriptor(target, prop);\n const desc = {\n enumerable: !!real.enumerable,\n get: getter.bind(target, prop),\n set: setter.bind(target, prop),\n };\n Object.defineProperty(proxy, prop, desc);\n\n propertyMap[prop] = true;\n });\n\n // Set the prototype, or clone all prototype methods (always required if a getter is provided).\n // TODO(samthor): We don't allow prototype methods to be set. It's (even more) awkward.\n // An alternative here would be to _just_ clone methods to keep behavior consistent.\n let prototypeOk = true;\n if (Object.setPrototypeOf) {\n Object.setPrototypeOf(proxy, Object.getPrototypeOf(target));\n /* eslint no-proto: \"off\" */\n } else if (proxy.__proto__) {\n proxy.__proto__ = target.__proto__;\n } else {\n prototypeOk = false;\n }\n if (handler.get || !prototypeOk) {\n for (const k in target) {\n if (propertyMap[k]) {\n continue;\n }\n Object.defineProperty(proxy, k, { get: getter.bind(target, k) });\n }\n }\n\n // The Proxy polyfill cannot handle adding new properties. Seal the target and proxy.\n Object.seal(target);\n Object.seal(proxy);\n\n return proxy; // nb. if isMethod is true, proxy != this\n };\n\n ProxyPolyfill.revocable = function(target, handler) {\n const p = new ProxyPolyfill(target, handler);\n\n return { proxy: p, revoke: lastRevokeFn };\n };\n\n return ProxyPolyfill;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ import { Store, StoreSettings } from './store.types';
2
+ export declare function createStore<State extends object, Actions extends string>(settings: StoreSettings<State, Actions>): Store<State, Actions>;
@@ -0,0 +1,59 @@
1
+ import { proxyContainer } from './polyfill';
2
+ export function createStore(settings) {
3
+ const observers = [];
4
+ let prevState = settings.initialState;
5
+ const validator = {
6
+ set(state, key, value) {
7
+ if (valueHasChanged(state[key], value)) {
8
+ state[key] = value;
9
+ callObservers(state, key);
10
+ }
11
+ return true;
12
+ },
13
+ };
14
+ let state = proxyContainer(settings.initialState || {}, validator);
15
+ function subscribe(observer, keys) {
16
+ if (typeof observer !== 'function')
17
+ new Error('You can only subscribe to Store changes with a valid function!');
18
+ observers.push({ callback: observer, keys });
19
+ return true;
20
+ }
21
+ function unsubscribe(observer) {
22
+ if (typeof observer !== 'function')
23
+ new Error('You can only subscribe to Store changes with a valid function!');
24
+ const match = observers.find(({ callback }) => callback === observer);
25
+ if (match) {
26
+ observers.splice(observers.indexOf(match), 1);
27
+ }
28
+ }
29
+ async function dispatch(actionKey, payload) {
30
+ if (!settings.actions || typeof settings.actions[actionKey] !== 'function')
31
+ new Error(`Action "${actionKey}" doesn't exist.`);
32
+ const action = settings.actions[actionKey];
33
+ prevState = Object.assign({}, state);
34
+ const newState = await action(state, payload);
35
+ state = newState;
36
+ return state;
37
+ }
38
+ function callObservers(data, key) {
39
+ observers.forEach(({ keys, callback }) => {
40
+ if (!keys) {
41
+ callback(data);
42
+ }
43
+ else if (Array.isArray(keys) && keys.indexOf(key) > -1) {
44
+ callback(data);
45
+ }
46
+ });
47
+ }
48
+ return {
49
+ subscribe,
50
+ unsubscribe,
51
+ dispatch,
52
+ getState: () => state,
53
+ getPrevState: () => prevState,
54
+ };
55
+ }
56
+ function valueHasChanged(value, old) {
57
+ return old !== value && (old === old || value === value);
58
+ }
59
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C,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,YAAY,IAAI,EAAE,EAAE,SAAS,CAAC,CAAC;IAE1E,SAAS,SAAS,CAAC,QAAiC,EAAE,IAAkB;QACpE,IAAI,OAAO,QAAQ,KA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;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;AACD,SAAS,eAAe,CAAC,KAAc,EAAE,GAAY;IAEjD,OAAO,GAAG,KAAK,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,IAAI,KAAK,KAAK,KAAK,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import { proxyContainer } from './polyfill';\nimport { Keys, ObserverCallback, Observers, Store, StoreSettings } 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"]}
@@ -0,0 +1,24 @@
1
+ export declare type ObserverCallback<T> = (state: T) => void;
2
+ export declare type Keys<T> = (keyof T)[];
3
+ export declare type ActionHandler<T> = (state: T, payload: any) => T;
4
+ export declare type Actions<A extends string, T> = Record<A, ActionHandler<T>>;
5
+ export declare type Subscribe<T> = (observer: ObserverCallback<T>, keys?: Keys<T>) => void;
6
+ export declare type Unsubscribe<T> = (observer: ObserverCallback<T>) => void;
7
+ export declare type Dispatch<A extends string, T> = (actionKey: A, payload?: any) => T | Promise<T>;
8
+ export declare type State<T> = () => T;
9
+ export interface Store<T, A extends string> {
10
+ getState: State<T>;
11
+ getPrevState: State<T>;
12
+ subscribe: Subscribe<T>;
13
+ unsubscribe: Unsubscribe<T>;
14
+ dispatch: Dispatch<A, T>;
15
+ }
16
+ export interface StoreSettings<T, A extends string> {
17
+ actions?: Actions<A, T>;
18
+ initialState: T;
19
+ }
20
+ export interface Observer<T> {
21
+ callback: ObserverCallback<T>;
22
+ keys?: Keys<T>;
23
+ }
24
+ export declare type Observers<T> = Array<Observer<T>>;
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=store.types.js.map
@@ -0,0 +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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bpd-library/utilities",
3
- "version": "2.0.1-beta.0",
3
+ "version": "2.0.1-beta.1",
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": "34a4fe7ebc5835d041ca16edc0aa84ce2bb38032",
48
+ "gitHead": "c783aa4e97552f8a34dbacb671ad884005dcf01d",
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.1-beta.0",
54
+ "@bpd-library/types": "^2.0.1-beta.1",
55
55
  "qs": "^6.9.4",
56
56
  "query-string": "5"
57
57
  }