global-store 1.0.0-beta.22 → 1.0.0-beta.23
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/global-store.js +1 -1
- package/dist/global-store.js.map +1 -1
- package/lib/compareVersion.d.ts.map +1 -1
- package/lib/compareVersion.js +1 -2
- package/lib/compareVersion.js.map +1 -1
- package/lib/createAsyncStore.d.ts.map +1 -1
- package/lib/createAsyncStore.js +9 -7
- package/lib/createAsyncStore.js.map +1 -1
- package/lib/createStore.d.ts.map +1 -1
- package/lib/createStore.js +9 -3
- package/lib/createStore.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/shouldInvokeInitializer.js +4 -3
- package/lib/shouldInvokeInitializer.js.map +1 -1
- package/lib/toVersionArray.d.ts.map +1 -1
- package/lib/toVersionArray.js +1 -2
- package/lib/toVersionArray.js.map +1 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/typesInternal.d.ts +1 -1
- package/lib/typesInternal.d.ts.map +1 -1
- package/lib/typesInternal.js.map +1 -1
- package/lib/util.d.ts +1 -1
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +5 -7
- package/lib/util.js.map +1 -1
- package/package.json +12 -15
- package/readme.md +245 -0
- package/README.md +0 -5
package/dist/global-store.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var GlobalStore=function(e){"use strict";function n(e){return"number"==typeof e?[0,0,e]:e.split(".").map((e=>parseInt(e,10)))}function r(e,r,i,o){const
|
|
1
|
+
var GlobalStore=function(e){"use strict";function n(e){return"number"==typeof e?[0,0,e]:e.split(".").map((e=>Number.parseInt(e,10)))}function r(e,r,i,o){const u=t(e,r);(function(e,r){const t=e.map(n),i=n(r);return function(e,n){return!e.some((e=>e[0]===n[0]))}(t,i)||function(e,n){return e.filter((e=>e[0]===n[0])).some((e=>n[1]>e[1]||n[1]===e[1]&&n[2]>e[2]))}(t,i)})(u.versions,i)&&(u.initializers.push(o),u.value=o(u.value,u.versions),u.versions.push(i))}function t(e,n){const r=e[n.moduleName]=e[n.moduleName]||Object.create(null),t=n.key??"default";return r[t]=r[t]||{versions:[],value:{},initializers:[]}}function i(e,r,t,i){(function(e){return e.sort(((e,r)=>function(e,r){const t=n(e),i=n(r);return t[0]!==i[0]?t[0]-i[0]:t[1]!==i[1]?t[1]-i[1]:t[2]-i[2]}(e.version,r.version)))})(t).forEach((({version:n,resolve:t,initializer:o})=>t(i({moduleName:e,key:r,version:n,initializer:o}))))}function o(e,n,r){const i=t(e,n);i.value=r?Object.isFrozen(r)?r:Object.freeze(r):function(e){if(Object.isFrozen(e))throw TypeError("Frozen value cannot be freezed again");Object.keys(e).forEach((n=>u(e,n))),Object.getOwnPropertySymbols&&Object.getOwnPropertySymbols(e).forEach((n=>u(e,n)));return Object.freeze(e)}(i.value)}function u(e,n){const r=e[n];Array.isArray(r)&&(e[n]=Object.freeze(r))}const s=Object.create(null);function c({moduleName:e,key:n,version:i,initializer:u}){const c={moduleName:e,key:n};return r(s,c,i,u),{get value(){return function(e,n){return t(e,n).value}(s,c)},freeze:e=>o(s,c,e),reset:()=>function(e,n){const r=t(e,n),i=r.versions;r.versions=[],r.value=r.initializers.reduce(((e,n,t)=>(e=n(e,r.versions),r.versions.push(i[t]),e)),{})}(s,c)}}const a=Object.create(null);return e.createAsyncStore=async function({moduleName:e,key:n,version:r,initializer:t}){return new Promise((i=>{const o=a[e]=a[e]||Object.create(null),u=n??"default";(o[u]=o[u]||[]).push({version:r,resolve:i,initializer:t})}))},e.createStore=c,e.default=c,e.initializeAsyncStore=function(e,n){const r=a[e];if(!r)return;if(n&&!r[n])return;(n?[n]:Object.keys(r)).forEach((n=>i(e,n,r[n],c)))},Object.defineProperty(e,"__esModule",{value:!0}),e}({});
|
|
2
2
|
//# sourceMappingURL=global-store.js.map
|
package/dist/global-store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-store.js","sources":["../ts/toVersionArray.ts","../ts/util.ts","../ts/shouldInvokeInitializer.ts","../ts/compareVersion.ts","../ts/createStore.ts","../ts/createAsyncStore.ts"],"sourcesContent":["import type { StoreVersion } from './types.js'\n\nexport type ParsedVersion = [number, number, number]\n\nexport function toVersionArray(v: StoreVersion): ParsedVersion {\n return typeof v === 'number' ? [0, 0, v] :\n v.split('.').map(v => parseInt(v, 10)) as ParsedVersion\n}\n","import { compareVersion } from './compareVersion.js'\nimport { createStore } from './createStore.js'\nimport { shouldInvokeInitializer } from './shouldInvokeInitializer.js'\nimport type { StoreInitializer, StoreValue, StoreVersion } from './types.js'\nimport type { StoreCreator, StoreId, Stores } from './typesInternal.js'\n\nexport function getStoreValue<T extends StoreValue>(stores: Stores, id: StoreId): T {\n return getStore(stores, id).value as T\n}\n\nexport function initStoreValue<T extends StoreValue>(stores: Stores, id: StoreId, version: StoreVersion, initializer: StoreInitializer<T>) {\n const store = getStore(stores, id)\n if (shouldInvokeInitializer(store.versions, version)) {\n store.initializers.push(initializer as StoreInitializer<StoreValue>)\n store.value = initializer(store.value as T, store.versions)\n store.versions.push(version)\n }\n}\n\nexport function resetStoreValue(stores: Stores, id: StoreId) {\n const store = getStore(stores, id)\n const versions = store.versions\n store.versions = []\n store.value = store.initializers.reduce<StoreValue>((value, initializer, i) => {\n value = initializer(value, store.versions)\n store.versions.push(versions[i])\n return value\n }, {})\n}\n\nexport function getStore(stores: Stores, id: StoreId) {\n const moduleStore = stores[id.moduleName] = stores[id.moduleName] || Object.create(null)\n const key = id.key ?? 'default'\n return moduleStore[key] = moduleStore[key] || { versions: [], value: {}, initializers: [] }\n}\n\nexport function resolveCreators(moduleName: string, key: string, storeCreators: Array<StoreCreator<any>>, cs: typeof createStore) {\n sortByVersion(storeCreators).forEach(({ version, resolve, initializer }) => resolve(cs({ moduleName, key, version, initializer })))\n}\n\nexport function sortByVersion<S>(storeCreators: Array<StoreCreator<S>>) {\n return storeCreators.sort((a, b) => compareVersion(a.version, b.version))\n}\n\nexport function freezeStoreValue(stores: Stores, id: StoreId, value?: StoreValue) {\n const store = getStore(stores, id)\n store.value = value ?\n Object.isFrozen(value) ? value : Object.freeze(value) :\n freezeValue(store.value)\n}\n\nfunction freezeValue(storeValue: StoreValue) {\n if (Object.isFrozen(storeValue)) throw TypeError('Frozen value cannot be freezed again')\n\n Object.keys(storeValue).forEach(k => freezeIfIsArray(storeValue, k))\n // istanbul ignore next\n if (Object.getOwnPropertySymbols) {\n Object.getOwnPropertySymbols(storeValue).forEach(k => freezeIfIsArray(storeValue, k))\n }\n\n return Object.freeze(storeValue)\n}\n\nfunction freezeIfIsArray(storeValue: StoreValue, k: string | number | symbol) {\n const value = storeValue[k]\n if (Array.isArray(value)) {\n storeValue[k] = Object.freeze(value)\n }\n}\n","import { toVersionArray, type ParsedVersion } from './toVersionArray.js'\nimport type { StoreVersion } from './types.js'\n\nexport function shouldInvokeInitializer(versions: StoreVersion[], version: StoreVersion) {\n const vs = versions.map(toVersionArray)\n const v = toVersionArray(version)\n return noMatchMajor(vs, v) || hasNewVersion(vs, v)\n}\n\nfunction noMatchMajor(versions: ParsedVersion[], version: ParsedVersion) {\n return !versions.some(v => v[0] === version[0])\n}\n\nfunction hasNewVersion(versions: ParsedVersion[], version: ParsedVersion) {\n return versions.filter(v => v[0] === version[0])\n .some(v => version[1] > v[1] || (version[1] === v[1] && version[2] > v[2]))\n}\n","import { toVersionArray } from './toVersionArray.js'\nimport type { StoreVersion } from './types.js'\n\n/**\n * Compare version.\n * Positive when processed > current\n * Negative when processed < current\n */\nexport function compareVersion(processed: StoreVersion, current: StoreVersion) {\n const v1 = toVersionArray(processed)\n const v2 = toVersionArray(current)\n return v1[0] !== v2[0] ? v1[0] - v2[0] :\n v1[1] !== v2[1] ? v1[1] - v2[1] : v1[2] - v2[2]\n}\n","import type { StoreOptions, StoreValue } from './types.js'\nimport type { Stores } from './typesInternal.js'\nimport { freezeStoreValue, getStoreValue, initStoreValue, resetStoreValue } from './util.js'\n\nexport type Store<T extends StoreValue> = {\n /**\n * The store value.\n */\n readonly value: T,\n /**\n * Freezes the store value.\n * @param value Optional new store value.\n * If supplied, this value will be freezed and used as the store value.\n * You can use this update the store value and freeze part of it.\n * If not supplied,\n * the original value will be freezed and its array property will also be freezed.\n */\n freeze(value?: { [k in keyof T]: Readonly<T[k]> }): void,\n /**\n * Resets the store to its initial value.\n * You should only use this during testing.\n */\n reset(): void,\n}\n\nconst stores = Object.create(null) as Stores\n\n/**\n * Creates a store of type T.\n * @see https://www.npmjs.com/package/global-store\n */\nexport function createStore<\n T extends StoreValue\n>({ moduleName, key, version, initializer }: StoreOptions<T>): Store<T> {\n const id = { moduleName, key }\n initStoreValue(stores, id, version, initializer)\n\n return {\n get value() { return getStoreValue<T>(stores, id) },\n freeze(value) { return freezeStoreValue(stores, id, value) },\n reset() { return resetStoreValue(stores, id) }\n }\n}\n","import { createStore, type Store } from './createStore.js'\nimport type { StoreOptions, StoreValue } from './types.js'\nimport type { StoreCreators } from './typesInternal.js'\nimport { resolveCreators } from './util.js'\n\nconst asyncStoreCreators = Object.create(null) as StoreCreators<Store<any>>\n\n/**\n * Creates a store of type T asynchronously.\n * @see https://github.com/unional/global-store#createAsyncStore\n */\nexport async function createAsyncStore<T extends StoreValue>({ moduleName, key, version, initializer }: StoreOptions<T>): Promise<Store<T>> {\n return new Promise(resolve => {\n const creatorsOfModules = asyncStoreCreators[moduleName] = asyncStoreCreators[moduleName] || Object.create(null)\n const k = key ?? 'default'\n const storeCreators = creatorsOfModules[k] = creatorsOfModules[k] || []\n storeCreators.push({ version, resolve, initializer })\n })\n}\n\n/**\n * Initializes the stores for `createAsyncStore()`.\n * @see https://github.com/unional/global-store#initializeAsyncStore\n */\nexport function initializeAsyncStore(moduleName: string, key?: string) {\n const creatorsOfModules = asyncStoreCreators[moduleName]\n if (!creatorsOfModules) return\n\n const keys = key ? [key] : Object.keys(creatorsOfModules)\n keys.forEach(key => resolveCreators(moduleName, key, creatorsOfModules[key], createStore))\n}\n"],"names":["toVersionArray","v","split","map","parseInt","initStoreValue","stores","id","version","initializer","store","getStore","versions","vs","some","noMatchMajor","filter","hasNewVersion","shouldInvokeInitializer","initializers","push","value","moduleStore","moduleName","Object","create","key","resolveCreators","storeCreators","cs","sort","a","b","processed","current","v1","v2","compareVersion","sortByVersion","forEach","resolve","freezeStoreValue","isFrozen","freeze","storeValue","TypeError","keys","k","freezeIfIsArray","getOwnPropertySymbols","freezeValue","Array","isArray","createStore","getStoreValue","reset","reduce","i","resetStoreValue","asyncStoreCreators","async","Promise","creatorsOfModules"],"mappings":"yCAIM,SAAUA,EAAeC,GAC7B,MAAoB,iBAANA,EAAiB,CAAC,EAAG,EAAGA,GACpCA,EAAEC,MAAM,KAAKC,KAAIF,GAAKG,SAASH,EAAG,KACtC,CCGM,SAAUI,EAAqCC,EAAgBC,EAAaC,EAAuBC,GACvG,MAAMC,EAAQC,EAASL,EAAQC,ICRjB,SAAwBK,EAA0BJ,GAChE,MAAMK,EAAKD,EAAST,IAAIH,GAClBC,EAAID,EAAeQ,GACzB,OAGF,SAAsBI,EAA2BJ,GAC/C,OAAQI,EAASE,MAAKb,GAAKA,EAAE,KAAOO,EAAQ,IAC9C,CALSO,CAAaF,EAAIZ,IAO1B,SAAuBW,EAA2BJ,GAChD,OAAOI,EAASI,QAAOf,GAAKA,EAAE,KAAOO,EAAQ,KAC1CM,MAAKb,GAAKO,EAAQ,GAAKP,EAAE,IAAOO,EAAQ,KAAOP,EAAE,IAAMO,EAAQ,GAAKP,EAAE,IAC3E,CAVgCgB,CAAcJ,EAAIZ,EAClD,EDKMiB,CAAwBR,EAAME,SAAUJ,KAC1CE,EAAMS,aAAaC,KAAKX,GACxBC,EAAMW,MAAQZ,EAAYC,EAAMW,MAAYX,EAAME,UAClDF,EAAME,SAASQ,KAAKZ,GAExB,CAagB,SAAAG,EAASL,EAAgBC,GACvC,MAAMe,EAAchB,EAAOC,EAAGgB,YAAcjB,EAAOC,EAAGgB,aAAeC,OAAOC,OAAO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AAEA,EAAUG,IAAEA,EAAGlB,QAAEA,EAAOC,YAAEA,IAC5B,MAAMF,EAAK,CAAEgB,aAAYG,OAGzB,OAFArB,EAAeC,EAAQC,EAAIC,EAASC,GAE7B,CACDY,YAAU,OHhCF,SAAoCf,EAAgBC,GAClE,OAAOI,EAASL,EAAQC,GAAIc,KAC9B,CG8ByBiC,CAAiBhD,EAAQC,EAAK,EACnDoC,OAAOtB,GAAgBoB,EAAiBnC,EAAQC,EAAIc,GACpDkC,MAAK,IHrBO,SAAgBjD,EAAgBC,GAC9C,MAAMG,EAAQC,EAASL,EAAQC,GACzBK,EAAWF,EAAME,SACvBF,EAAME,SAAW,GACjBF,EAAMW,MAAQX,EAAMS,aAAaqC,QAAmB,CAACnC,EAAOZ,EAAagD,KACvEpC,EAAQZ,EAAYY,EAAOX,EAAME,UACjCF,EAAME,SAASQ,KAAKR,EAAS6C,IACtBpC,IACN,CAAE,EACP,CGYqBqC,CAAgBpD,EAAQC,GAE7C,CCrCA,MAAMoD,EAAqBnC,OAAOC,OAAO,gCAMlCmC,gBAAsDrC,WAAEA,EAAUG,IAAEA,EAAGlB,QAAEA,EAAOC,YAAEA,IACvF,OAAO,IAAIoD,SAAQrB,IACjB,MAAMsB,EAAoBH,EAAmBpC,GAAcoC,EAAmBpC,IAAeC,OAAOC,OAAO,MACrGsB,EAAIrB,GAAO,WACKoC,EAAkBf,GAAKe,EAAkBf,IAAM,IACvD3B,KAAK,CAAEZ,UAASgC,UAAS/B,eAAc,GAEzD,qDAMgB,SAAqBc,EAAoBG,GACvD,MAAMoC,EAAoBH,EAAmBpC,GAC7C,IAAKuC,EAAmB,QAEXpC,EAAM,CAACA,GAAOF,OAAOsB,KAAKgB,IAClCvB,SAAQb,GAAOC,EAAgBJ,EAAYG,EAAKoC,EAAkBpC,GAAM2B,IAC/E"}
|
|
1
|
+
{"version":3,"file":"global-store.js","sources":["../ts/toVersionArray.ts","../ts/util.ts","../ts/shouldInvokeInitializer.ts","../ts/compareVersion.ts","../ts/createStore.ts","../ts/createAsyncStore.ts"],"sourcesContent":[null,null,null,null,null,null],"names":["toVersionArray","v","split","map","Number","parseInt","initStoreValue","stores","id","version","initializer","store","getStore","versions","vs","some","noMatchMajor","filter","hasNewVersion","shouldInvokeInitializer","initializers","push","value","moduleStore","moduleName","Object","create","key","resolveCreators","storeCreators","cs","sort","a","b","processed","current","v1","v2","compareVersion","sortByVersion","forEach","resolve","freezeStoreValue","isFrozen","freeze","storeValue","TypeError","keys","k","freezeIfIsArray","getOwnPropertySymbols","freezeValue","Array","isArray","createStore","getStoreValue","reset","reduce","i","resetStoreValue","asyncStoreCreators","async","Promise","creatorsOfModules","s"],"mappings":"yCAIM,SAAUA,EAAeC,GAC9B,MAAoB,iBAANA,EAAiB,CAAC,EAAG,EAAGA,GAAMA,EAAEC,MAAM,KAAKC,KAAKF,GAAMG,OAAOC,SAASJ,EAAG,KACxF,CCIM,SAAUK,EACfC,EACAC,EACAC,EACAC,GAEA,MAAMC,EAAQC,EAASL,EAAQC,ICbhB,SAAwBK,EAA0BJ,GACjE,MAAMK,EAAKD,EAASV,IAAIH,GAClBC,EAAID,EAAeS,GACzB,OAGD,SAAsBI,EAA2BJ,GAChD,OAAQI,EAASE,MAAMd,GAAMA,EAAE,KAAOQ,EAAQ,IAC/C,CALQO,CAAaF,EAAIb,IAOzB,SAAuBY,EAA2BJ,GACjD,OAAOI,EACLI,QAAQhB,GAAMA,EAAE,KAAOQ,EAAQ,KAC/BM,MAAMd,GAAMQ,EAAQ,GAAKR,EAAE,IAAOQ,EAAQ,KAAOR,EAAE,IAAMQ,EAAQ,GAAKR,EAAE,IAC3E,CAX+BiB,CAAcJ,EAAIb,EACjD,EDUKkB,CAAwBR,EAAME,SAAUJ,KAC3CE,EAAMS,aAAaC,KAAKX,GACxBC,EAAMW,MAAQZ,EAAYC,EAAMW,MAAYX,EAAME,UAClDF,EAAME,SAASQ,KAAKZ,GAEtB,CAagB,SAAAG,EAASL,EAAgBC,GACxC,MA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|
|
1
|
-
{"version":3,"file":"compareVersion.d.ts","sourceRoot":"","sources":["../ts/compareVersion.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"compareVersion.d.ts","sourceRoot":"","sources":["../ts/compareVersion.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,UAI5E"}
|
package/lib/compareVersion.js
CHANGED
|
@@ -7,7 +7,6 @@ import { toVersionArray } from './toVersionArray.js';
|
|
|
7
7
|
export function compareVersion(processed, current) {
|
|
8
8
|
const v1 = toVersionArray(processed);
|
|
9
9
|
const v2 = toVersionArray(current);
|
|
10
|
-
return v1[0] !== v2[0] ? v1[0] - v2[0] :
|
|
11
|
-
v1[1] !== v2[1] ? v1[1] - v2[1] : v1[2] - v2[2];
|
|
10
|
+
return v1[0] !== v2[0] ? v1[0] - v2[0] : v1[1] !== v2[1] ? v1[1] - v2[1] : v1[2] - v2[2];
|
|
12
11
|
}
|
|
13
12
|
//# sourceMappingURL=compareVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compareVersion.js","sourceRoot":"","sources":["../ts/compareVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,SAAuB,EAAE,OAAqB;
|
|
1
|
+
{"version":3,"file":"compareVersion.js","sourceRoot":"","sources":["../ts/compareVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,SAAuB,EAAE,OAAqB;IAC5E,MAAM,EAAE,GAAG,cAAc,CAAC,SAAS,CAAC,CAAA;IACpC,MAAM,EAAE,GAAG,cAAc,CAAC,OAAO,CAAC,CAAA;IAClC,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAA;AACzF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncStore.d.ts","sourceRoot":"","sources":["../ts/createAsyncStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAM1D;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"createAsyncStore.d.ts","sourceRoot":"","sources":["../ts/createAsyncStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAM1D;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,EAC5D,UAAU,EACV,GAAG,EACH,OAAO,EACP,WAAW,EACX,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAOrC;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,QAOpE"}
|
package/lib/createAsyncStore.js
CHANGED
|
@@ -6,10 +6,10 @@ const asyncStoreCreators = Object.create(null);
|
|
|
6
6
|
* @see https://github.com/unional/global-store#createAsyncStore
|
|
7
7
|
*/
|
|
8
8
|
export async function createAsyncStore({ moduleName, key, version, initializer }) {
|
|
9
|
-
return new Promise(resolve => {
|
|
10
|
-
const creatorsOfModules = asyncStoreCreators[moduleName] = asyncStoreCreators[moduleName] || Object.create(null);
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const creatorsOfModules = (asyncStoreCreators[moduleName] = asyncStoreCreators[moduleName] || Object.create(null));
|
|
11
11
|
const k = key ?? 'default';
|
|
12
|
-
const storeCreators = creatorsOfModules[k] = creatorsOfModules[k] || [];
|
|
12
|
+
const storeCreators = (creatorsOfModules[k] = creatorsOfModules[k] || []);
|
|
13
13
|
storeCreators.push({ version, resolve, initializer });
|
|
14
14
|
});
|
|
15
15
|
}
|
|
@@ -18,10 +18,12 @@ export async function createAsyncStore({ moduleName, key, version, initializer }
|
|
|
18
18
|
* @see https://github.com/unional/global-store#initializeAsyncStore
|
|
19
19
|
*/
|
|
20
20
|
export function initializeAsyncStore(moduleName, key) {
|
|
21
|
-
const
|
|
22
|
-
if (!
|
|
21
|
+
const s = asyncStoreCreators[moduleName];
|
|
22
|
+
if (!s)
|
|
23
23
|
return;
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
if (key && !s[key])
|
|
25
|
+
return;
|
|
26
|
+
const keys = key ? [key] : Object.keys(s);
|
|
27
|
+
keys.forEach((key) => resolveCreators(moduleName, key, s[key], createStore));
|
|
26
28
|
}
|
|
27
29
|
//# sourceMappingURL=createAsyncStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncStore.js","sourceRoot":"","sources":["../ts/createAs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AAI,CAAC,CAAC;QAAE,OAAM;IACd,IAAI,GAAG,IAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAAE,OAAM;IAEzB,MAAM,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACzC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,CAAE,EAAE,WAAW,CAAC,CAAC,CAAA;AAC9E,CAAC"}
|
package/lib/createStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.d.ts","sourceRoot":"","sources":["../ts/createStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAI1D,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,UAAU,IAAI;
|
|
1
|
+
{"version":3,"file":"createStore.d.ts","sourceRoot":"","sources":["../ts/createStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAI1D,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,UAAU,IAAI;IACzC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;IACjB;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,CAAC,EAAE;SAAG,CAAC,IAAI,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAAE,GAAG,IAAI,CAAA;IACxD;;;OAGG;IACH,KAAK,IAAI,IAAI,CAAA;CACb,CAAA;AAID;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,UAAU,EAAE,EACjD,UAAU,EACV,GAAG,EACH,OAAO,EACP,WAAW,EACX,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAe5B"}
|
package/lib/createStore.js
CHANGED
|
@@ -8,9 +8,15 @@ export function createStore({ moduleName, key, version, initializer }) {
|
|
|
8
8
|
const id = { moduleName, key };
|
|
9
9
|
initStoreValue(stores, id, version, initializer);
|
|
10
10
|
return {
|
|
11
|
-
get value() {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
get value() {
|
|
12
|
+
return getStoreValue(stores, id);
|
|
13
|
+
},
|
|
14
|
+
freeze(value) {
|
|
15
|
+
return freezeStoreValue(stores, id, value);
|
|
16
|
+
},
|
|
17
|
+
reset() {
|
|
18
|
+
return resetStoreValue(stores, id);
|
|
19
|
+
}
|
|
14
20
|
};
|
|
15
21
|
}
|
|
16
22
|
//# sourceMappingURL=createStore.js.map
|
package/lib/createStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.js","sourceRoot":"","sources":["../ts/createStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAuB5F,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAW,CAAA;AAE5C;;;GAGG;AACH,MAAM,UAAU,WAAW,
|
|
1
|
+
{"version":3,"file":"createStore.js","sourceRoot":"","sources":["../ts/createStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAuB5F,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAW,CAAA;AAE5C;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAuB,EACjD,UAAU,EACV,GAAG,EACH,OAAO,EACP,WAAW,EACM;IACjB,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAA;IAC9B,cAAc,CAAC,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,WAAW,CAAC,CAAA;IAEhD,OAAO;QACN,IAAI,KAAK;YACR,OAAO,aAAa,CAAI,MAAM,EAAE,EAAE,CAAC,CAAA;QACpC,CAAC;QACD,MAAM,CAAC,KAAK;YACX,OAAO,gBAAgB,CAAC,MAAM,EAAE,EAAE,EAAE,KAAK,CAAC,CAAA;QAC3C,CAAC;QACD,KAAK;YACJ,OAAO,eAAe,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;QACnC,CAAC;KACD,CAAA;AACF,CAAC"}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,OAAO,EAAE,WAAW,IAAI,OAAO,EAAE,MAAM,kBAAkB,CAAA;AACzD,cAAc,YAAY,CAAA"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,OAAO,EAAE,WAAW,IAAI,OAAO,EAAE,MAAM,kBAAkB,CAAA;AACzD,cAAc,YAAY,CAAA"}
|
|
@@ -5,10 +5,11 @@ export function shouldInvokeInitializer(versions, version) {
|
|
|
5
5
|
return noMatchMajor(vs, v) || hasNewVersion(vs, v);
|
|
6
6
|
}
|
|
7
7
|
function noMatchMajor(versions, version) {
|
|
8
|
-
return !versions.some(v => v[0] === version[0]);
|
|
8
|
+
return !versions.some((v) => v[0] === version[0]);
|
|
9
9
|
}
|
|
10
10
|
function hasNewVersion(versions, version) {
|
|
11
|
-
return versions
|
|
12
|
-
.
|
|
11
|
+
return versions
|
|
12
|
+
.filter((v) => v[0] === version[0])
|
|
13
|
+
.some((v) => version[1] > v[1] || (version[1] === v[1] && version[2] > v[2]));
|
|
13
14
|
}
|
|
14
15
|
//# sourceMappingURL=shouldInvokeInitializer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shouldInvokeInitializer.js","sourceRoot":"","sources":["../ts/shouldInvokeInitializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAsB,MAAM,qBAAqB,CAAA;AAGxE,MAAM,UAAU,uBAAuB,CAAC,QAAwB,EAAE,OAAqB;
|
|
1
|
+
{"version":3,"file":"shouldInvokeInitializer.js","sourceRoot":"","sources":["../ts/shouldInvokeInitializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAsB,MAAM,qBAAqB,CAAA;AAGxE,MAAM,UAAU,uBAAuB,CAAC,QAAwB,EAAE,OAAqB;IACtF,MAAM,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;IACvC,MAAM,CAAC,GAAG,cAAc,CAAC,OAAO,CAAC,CAAA;IACjC,OAAO,YAAY,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,aAAa,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AACnD,CAAC;AAED,SAAS,YAAY,CAAC,QAAyB,EAAE,OAAsB;IACtE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAClD,CAAC;AAED,SAAS,aAAa,CAAC,QAAyB,EAAE,OAAsB;IACvE,OAAO,QAAQ;SACb,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;SAClC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAC/E,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toVersionArray.d.ts","sourceRoot":"","sources":["../ts/toVersionArray.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAEpD,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,GAAG,aAAa,
|
|
1
|
+
{"version":3,"file":"toVersionArray.d.ts","sourceRoot":"","sources":["../ts/toVersionArray.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAEpD,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,GAAG,aAAa,CAE7D"}
|
package/lib/toVersionArray.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toVersionArray.js","sourceRoot":"","sources":["../ts/toVersionArray.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,cAAc,CAAC,CAAe;
|
|
1
|
+
{"version":3,"file":"toVersionArray.js","sourceRoot":"","sources":["../ts/toVersionArray.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,cAAc,CAAC,CAAe;IAC7C,OAAO,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAmB,CAAA;AAC9G,CAAC"}
|
package/lib/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../ts/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAA;AAEzD,MAAM,MAAM,YAAY,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,GAAG,MAAM,CAAA;AAEnE,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,UAAU,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,KAAK,CAAC,CAAA;AAElH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,UAAU;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../ts/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAA;AAEzD,MAAM,MAAM,YAAY,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,GAAG,MAAM,CAAA;AAEnE,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,UAAU,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,KAAK,CAAC,CAAA;AAElH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,UAAU;IACjD,UAAU,EAAE,MAAM,CAAA;IAClB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,OAAO,EAAE,YAAY,CAAA;IACrB,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;CAChC"}
|
package/lib/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../ts/types.ts"],"names":[],"mappings":""
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../ts/types.ts"],"names":[],"mappings":""}
|
package/lib/typesInternal.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { StoreInitializer, StoreValue, StoreVersion } from './types.js';
|
|
2
2
|
export interface StoreId {
|
|
3
3
|
moduleName: string;
|
|
4
|
-
key?: string;
|
|
4
|
+
key?: string | undefined;
|
|
5
5
|
}
|
|
6
6
|
export type Stores = Record<StoreId['moduleName'], Record<string, {
|
|
7
7
|
versions: StoreVersion[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typesInternal.d.ts","sourceRoot":"","sources":["../ts/typesInternal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE5E,MAAM,WAAW,OAAO;
|
|
1
|
+
{"version":3,"file":"typesInternal.d.ts","sourceRoot":"","sources":["../ts/typesInternal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE5E,MAAM,WAAW,OAAO;IACvB,UAAU,EAAE,MAAM,CAAA;IAClB,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACxB;AAED,MAAM,MAAM,MAAM,GAAG,MAAM,CAC1B,OAAO,CAAC,YAAY,CAAC,EACrB,MAAM,CAAC,MAAM,EAAE;IAAE,QAAQ,EAAE,YAAY,EAAE,CAAC;IAAC,YAAY,EAAE,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAC,CAC7G,CAAA;AAED,MAAM,WAAW,YAAY,CAAC,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAA;IACrB,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAA;IAC3B,WAAW,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAA;CACzC;AAED,MAAM,MAAM,aAAa,CAAC,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA"}
|
package/lib/typesInternal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typesInternal.js","sourceRoot":"","sources":["../ts/typesInternal.ts"],"names":[],"mappings":""
|
|
1
|
+
{"version":3,"file":"typesInternal.js","sourceRoot":"","sources":["../ts/typesInternal.ts"],"names":[],"mappings":""}
|
package/lib/util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createStore } from './createStore.js';
|
|
1
|
+
import type { createStore } from './createStore.js';
|
|
2
2
|
import type { StoreInitializer, StoreValue, StoreVersion } from './types.js';
|
|
3
3
|
import type { StoreCreator, StoreId, Stores } from './typesInternal.js';
|
|
4
4
|
export declare function getStoreValue<T extends StoreValue>(stores: Stores, id: StoreId): T;
|
package/lib/util.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../ts/util.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../ts/util.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC5E,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAEvE,wBAAgB,aAAa,CAAC,CAAC,SAAS,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,GAAG,CAAC,CAElF;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,UAAU,EAClD,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,OAAO,EACX,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,QAQhC;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,QAS1D;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO;;;;EAInD;AAED,wBAAgB,eAAe,CAC9B,UAAU,EAAE,MAAM,EAClB,GAAG,EAAE,MAAM,EACX,aAAa,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,EACvC,EAAE,EAAE,OAAO,WAAW,QAKtB;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,qBAErE;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,UAAU,QAG/E"}
|
package/lib/util.js
CHANGED
|
@@ -22,9 +22,9 @@ export function resetStoreValue(stores, id) {
|
|
|
22
22
|
}, {});
|
|
23
23
|
}
|
|
24
24
|
export function getStore(stores, id) {
|
|
25
|
-
const moduleStore = stores[id.moduleName] = stores[id.moduleName] || Object.create(null);
|
|
25
|
+
const moduleStore = (stores[id.moduleName] = stores[id.moduleName] || Object.create(null));
|
|
26
26
|
const key = id.key ?? 'default';
|
|
27
|
-
return moduleStore[key] = moduleStore[key] || { versions: [], value: {}, initializers: [] };
|
|
27
|
+
return (moduleStore[key] = moduleStore[key] || { versions: [], value: {}, initializers: [] });
|
|
28
28
|
}
|
|
29
29
|
export function resolveCreators(moduleName, key, storeCreators, cs) {
|
|
30
30
|
sortByVersion(storeCreators).forEach(({ version, resolve, initializer }) => resolve(cs({ moduleName, key, version, initializer })));
|
|
@@ -34,17 +34,15 @@ export function sortByVersion(storeCreators) {
|
|
|
34
34
|
}
|
|
35
35
|
export function freezeStoreValue(stores, id, value) {
|
|
36
36
|
const store = getStore(stores, id);
|
|
37
|
-
store.value = value ?
|
|
38
|
-
Object.isFrozen(value) ? value : Object.freeze(value) :
|
|
39
|
-
freezeValue(store.value);
|
|
37
|
+
store.value = value ? (Object.isFrozen(value) ? value : Object.freeze(value)) : freezeValue(store.value);
|
|
40
38
|
}
|
|
41
39
|
function freezeValue(storeValue) {
|
|
42
40
|
if (Object.isFrozen(storeValue))
|
|
43
41
|
throw TypeError('Frozen value cannot be freezed again');
|
|
44
|
-
Object.keys(storeValue).forEach(k => freezeIfIsArray(storeValue, k));
|
|
42
|
+
Object.keys(storeValue).forEach((k) => freezeIfIsArray(storeValue, k));
|
|
45
43
|
// istanbul ignore next
|
|
46
44
|
if (Object.getOwnPropertySymbols) {
|
|
47
|
-
Object.getOwnPropertySymbols(storeValue).forEach(k => freezeIfIsArray(storeValue, k));
|
|
45
|
+
Object.getOwnPropertySymbols(storeValue).forEach((k) => freezeIfIsArray(storeValue, k));
|
|
48
46
|
}
|
|
49
47
|
return Object.freeze(storeValue);
|
|
50
48
|
}
|
package/lib/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../ts/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAA;AAItE,MAAM,UAAU,aAAa,CAAuB,MAAc,EAAE,EAAW;
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../ts/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAA;AAItE,MAAM,UAAU,aAAa,CAAuB,MAAc,EAAE,EAAW;IAC9E,OAAO,QAAQ,CAAC,MAAM,EAAE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,CAAC,UAAU,CAAC;QAAE,MAAM,SAAS,CAAC,sCAAsC,CAAC,CAAA;IAExF,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;IACtE,uBAAuB;IACvB,IAAI,MAAM,CAAC,qBAAqB,EAAE,CAAC;QAClC,MAAM,CAAC,qBAAqB,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;IACxF,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AACjC,CAAC;AAED,SAAS,eAAe,CAAC,UAAsB,EAAE,CAA2B;IAC3E,MAAM,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;IAC3B,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACrC,CAAC;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "global-store",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.23",
|
|
4
4
|
"description": "A library provides version stable stores for libraries.",
|
|
5
|
-
"homepage": "https://github.com/
|
|
5
|
+
"homepage": "https://github.com/stable-packages/store/tree/main/packages/global-store",
|
|
6
6
|
"bugs": {
|
|
7
|
-
"url": "https://github.com/
|
|
7
|
+
"url": "https://github.com/stable-packages/store/issues"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git://github.com/
|
|
11
|
+
"url": "git+https://github.com/stable-packages/store.git",
|
|
12
|
+
"directory": "packages/global-store"
|
|
12
13
|
},
|
|
13
14
|
"license": "MIT",
|
|
14
15
|
"author": {
|
|
@@ -27,16 +28,14 @@
|
|
|
27
28
|
],
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@babel/core": "^7.18.10",
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"@repobuddy/jest": "^3.3.0",
|
|
31
|
+
"@repobuddy/jest": "^4.0.0",
|
|
32
|
+
"@repobuddy/typescript": "^2.0.0",
|
|
33
33
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
34
34
|
"@size-limit/preset-small-lib": "^8.0.1",
|
|
35
|
-
"@types/jest": "^29.5.
|
|
36
|
-
"assertron": "^11.
|
|
37
|
-
"cross-env": "^7.0.3",
|
|
35
|
+
"@types/jest": "^29.5.12",
|
|
36
|
+
"assertron": "^11.6.0",
|
|
38
37
|
"depcheck": "^1.4.3",
|
|
39
|
-
"jest": "^29.
|
|
38
|
+
"jest": "^29.7.0",
|
|
40
39
|
"jest-junit": "^16.0.0",
|
|
41
40
|
"jest-watch-suspend": "^1.1.2",
|
|
42
41
|
"jest-watch-toggle-config-2": "^2.1.0",
|
|
@@ -46,10 +45,9 @@
|
|
|
46
45
|
"rollup": "^2.78.0",
|
|
47
46
|
"rollup-plugin-terser": "^7.0.2",
|
|
48
47
|
"size-limit": "^8.0.1",
|
|
49
|
-
"ts-jest": "^29.1.
|
|
48
|
+
"ts-jest": "^29.1.2",
|
|
50
49
|
"tslib": "^2.4.0",
|
|
51
|
-
"type-plus": "
|
|
52
|
-
"typescript": "5.0.4"
|
|
50
|
+
"type-plus": "8.0.0-beta.10"
|
|
53
51
|
},
|
|
54
52
|
"size-limit": [
|
|
55
53
|
{
|
|
@@ -64,7 +62,6 @@
|
|
|
64
62
|
"clean": "rimraf cjs coverage dist esm tslib lib libm --glob *.tsbuildinfo",
|
|
65
63
|
"coverage": "jest --coverage",
|
|
66
64
|
"depcheck": "depcheck",
|
|
67
|
-
"lint": "cross-env TIMING=1 eslint --ext=js,cjs,mjs,ts,tsx,ctx,mtx,yaml,yml .",
|
|
68
65
|
"size": "size-limit",
|
|
69
66
|
"test": "jest",
|
|
70
67
|
"test:watch": "jest --watch",
|
package/readme.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# global-store
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[![NPM downloads][downloads-image]][downloads-url]
|
|
5
|
+
![badge-size-es6-url]
|
|
6
|
+
|
|
7
|
+
[![GitHub NodeJS][github-nodejs]][github-action-url]
|
|
8
|
+
[![Codecov][codecov-image]][codecov-url]
|
|
9
|
+
|
|
10
|
+
[![Visual Studio Code][vscode-image]][vscode-url]
|
|
11
|
+
|
|
12
|
+
[global-store] provides version stable stores for libraries.
|
|
13
|
+
|
|
14
|
+
Once this library reaches `1.0`, it will forever be backward compatible.
|
|
15
|
+
Meaning there will never be a breaking change or `2.0` version of this library.
|
|
16
|
+
|
|
17
|
+
PLEASE NOTE: This document describes the new `1.0.0-beta` version (finally!! 🎉).
|
|
18
|
+
|
|
19
|
+
For the current stable version, please check out [here](https://github.com/unional/global-store/blob/v0.8.2/README.md).
|
|
20
|
+
|
|
21
|
+
## Who needs this
|
|
22
|
+
|
|
23
|
+
- A library used by other libraries, and
|
|
24
|
+
- A library with state, and/or
|
|
25
|
+
- A library wants to protect its state from modification.
|
|
26
|
+
|
|
27
|
+
## Why do you need this
|
|
28
|
+
|
|
29
|
+
If you use a file-level variable to store some states,
|
|
30
|
+
and your library can be used by other libraries,
|
|
31
|
+
the state it stores could scatter around the memory,
|
|
32
|
+
and you will get inconsistent results.
|
|
33
|
+
|
|
34
|
+
For example, you have this:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const registry = {}
|
|
38
|
+
|
|
39
|
+
export function addComponent(name: string, component: any) {
|
|
40
|
+
registry[name] = component
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
When your library is used by other libraries,
|
|
45
|
+
they may use different versions.
|
|
46
|
+
For example:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
app
|
|
50
|
+
- some-library@1.5
|
|
51
|
+
- your-library@1.0
|
|
52
|
+
- your-library@2.0
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Since the versions are not compatible,
|
|
56
|
+
both versions of your library are loaded thus two instances of the `registry` variable exist and contain different values.
|
|
57
|
+
|
|
58
|
+
The solution to this problem is to use some form of global storage such as `global` in NodeJS,
|
|
59
|
+
or global variable in the browser.
|
|
60
|
+
|
|
61
|
+
The problem is that these mechanisms are shared by everything else in the application,
|
|
62
|
+
completely exposed to everyone,
|
|
63
|
+
and there is no mechanism to consolidate or protect your state when they are populated by each version of your library being loaded.
|
|
64
|
+
|
|
65
|
+
These are the problems addressed by [global-store].
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
npm install global-store
|
|
71
|
+
yarn add global-store
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
### createStore()
|
|
77
|
+
|
|
78
|
+
Type: `<T>(options: StoreOptions<T>) => Store<T>`
|
|
79
|
+
|
|
80
|
+
Creates a store of type `T`.
|
|
81
|
+
|
|
82
|
+
`T` is inferred by [`initializer`](#storeoptionsinitializer).
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { createStore } from 'global-store'
|
|
86
|
+
|
|
87
|
+
const store = createStore({
|
|
88
|
+
moduleName: 'your-module',
|
|
89
|
+
key: 'some-unique-string',
|
|
90
|
+
version: '1.0.0',
|
|
91
|
+
initializer: (current, versions) => ({
|
|
92
|
+
prop1: false,
|
|
93
|
+
prop2: [] as string[],
|
|
94
|
+
...current
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
console.log(store.value.prop1) // false
|
|
99
|
+
|
|
100
|
+
store.value.prop1 = true
|
|
101
|
+
store.value.prop2.push('a')
|
|
102
|
+
console.log(store.value) // { prop1: true, prop2: ['a'] }
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### StoreOptions#moduleName
|
|
106
|
+
|
|
107
|
+
Type: `string`
|
|
108
|
+
|
|
109
|
+
Name of your module.
|
|
110
|
+
|
|
111
|
+
This is typically your NPM package name.
|
|
112
|
+
This will be shown if something goes wrong.
|
|
113
|
+
|
|
114
|
+
#### StoreOptions#key
|
|
115
|
+
|
|
116
|
+
Type: optional `string`
|
|
117
|
+
|
|
118
|
+
A specific key for each store in your module.
|
|
119
|
+
|
|
120
|
+
You can create multiple stores in your module for different purposes.
|
|
121
|
+
The key for each store needs to be unique.
|
|
122
|
+
And the key has to remain the same across versions.
|
|
123
|
+
|
|
124
|
+
For example, you can use a format like this to make it unique: `<some-purpose>:<UUID>`
|
|
125
|
+
|
|
126
|
+
e.g. `config:d15427a4-75cf-4999-9065-1dc325839a59`
|
|
127
|
+
|
|
128
|
+
`key` + `moduleName` forms a unique ID to the store.
|
|
129
|
+
|
|
130
|
+
`key` will not be shown if something goes wrong.
|
|
131
|
+
|
|
132
|
+
If `key` is not specified, it defaults to the string `default`.
|
|
133
|
+
|
|
134
|
+
#### StoreOptions#version
|
|
135
|
+
|
|
136
|
+
Type: `StoreVersion = string | number`
|
|
137
|
+
|
|
138
|
+
The version of the store.
|
|
139
|
+
|
|
140
|
+
This is used during initialization to determine should the [`StoreOptions#initializer`](#storeoptionsinitializer) be called (and in what order for [`createAsyncStore()`](#createasyncstore)).
|
|
141
|
+
|
|
142
|
+
It will be added to the `processedVersions` argument of the [`StoreOptions#initializer()`](#storeoptionsinitializer) after it is being called.
|
|
143
|
+
|
|
144
|
+
When specified as a string (recommended),
|
|
145
|
+
it must be in this format: `major.minor.patch`.
|
|
146
|
+
|
|
147
|
+
When there is a mix of string and numeric versions across different versions of your library,
|
|
148
|
+
the numeric value is compared to the patch number of the string version.
|
|
149
|
+
|
|
150
|
+
#### StoreOptions#initializer()
|
|
151
|
+
|
|
152
|
+
Type: `<T extends StoreValue>(current: StoreValue, processedVersions: StoreVersion[]) => T`
|
|
153
|
+
|
|
154
|
+
The function to initialize the store.
|
|
155
|
+
|
|
156
|
+
Since there may be multiple copies of your library loaded,
|
|
157
|
+
multiple calls to the store creation function (e.g. [`createStore()`](#createstore)) may occur.
|
|
158
|
+
For the first call, the `current` argument will be an empty object.
|
|
159
|
+
For subsequence calls, it will be the value returned by the last call.
|
|
160
|
+
|
|
161
|
+
The `processedVersions` contains all the versions the have been processed so far.
|
|
162
|
+
You can use it to help determine what do you need to do.
|
|
163
|
+
|
|
164
|
+
For [`createAsyncStore`](#createasyncstore),
|
|
165
|
+
the `initializer` function will be called in the order of `version`.
|
|
166
|
+
|
|
167
|
+
For [`createStore`](#createstore),
|
|
168
|
+
since there is no way to control the load order,
|
|
169
|
+
they can be called by a newer version of your library before an older version.
|
|
170
|
+
This means your `initializer` needs to be future-proof.
|
|
171
|
+
|
|
172
|
+
To do that, you should fill in the pieces your specific version needs,
|
|
173
|
+
and carry over whatever is currently available.
|
|
174
|
+
|
|
175
|
+
This is only a general guideline,
|
|
176
|
+
the actual implementation will depend on how you use your store.
|
|
177
|
+
|
|
178
|
+
#### StoreValue
|
|
179
|
+
|
|
180
|
+
Type: `Record<string | symbol, any>`
|
|
181
|
+
|
|
182
|
+
The type of value stored in the stores.
|
|
183
|
+
|
|
184
|
+
Note that the key must be a string or `Symbol.for()` because `Symbol()` cannot be shared across versions.
|
|
185
|
+
|
|
186
|
+
#### Store#value
|
|
187
|
+
|
|
188
|
+
Access to the value in the store.
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
import { createStore } from 'global-store'
|
|
192
|
+
|
|
193
|
+
const store = createStore({ ..., initializer: () => ({ x: 1 })}
|
|
194
|
+
|
|
195
|
+
console.info(store.value.x) // 1
|
|
196
|
+
store.value.x = 2
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### Store#reset()
|
|
200
|
+
|
|
201
|
+
Reset the store to its initial value.
|
|
202
|
+
|
|
203
|
+
This is mostly used in tests so that the tests would not interfere with each other.
|
|
204
|
+
|
|
205
|
+
### createAsyncStore()
|
|
206
|
+
|
|
207
|
+
A `async` variant of [`createStore()`](#createstore).
|
|
208
|
+
It will return a promise,
|
|
209
|
+
which will resolve when the [`initializeAsyncStore()`](#initializeasyncstore) is called.
|
|
210
|
+
|
|
211
|
+
One benefit of using this over [`createStore()`](#createstore) is that the initializers will be called in the order of `version`.
|
|
212
|
+
|
|
213
|
+
This makes initialization much easier to handle.
|
|
214
|
+
|
|
215
|
+
### initializeAsyncStore()
|
|
216
|
+
|
|
217
|
+
Calling [`initializeAsyncStore()`](#initializeasyncstore) will start the initialization process of [`createAsyncStore()`](#createasyncstore).
|
|
218
|
+
|
|
219
|
+
It takes two arguments:
|
|
220
|
+
|
|
221
|
+
- `moduleName: string`: Name of your module.
|
|
222
|
+
- `key: string`: Optional. Key of the specific store to initialize.
|
|
223
|
+
If omitted, all stores in the module will be initialized.
|
|
224
|
+
|
|
225
|
+
## Bundling
|
|
226
|
+
|
|
227
|
+
If your library will be a standalone bundle, make sure to exclude [global-store].
|
|
228
|
+
|
|
229
|
+
If not, there will be multiple copies of [global-store] loaded and will completely defeat the purpose.
|
|
230
|
+
|
|
231
|
+
Except the consuming application, which will declare [global-store] as a regular dependency,
|
|
232
|
+
all libraries should declare [global-store] as a peer dependency.
|
|
233
|
+
|
|
234
|
+
[badge-size-es6-url]: https://img.shields.io/bundlephobia/min/global-store
|
|
235
|
+
[codecov-image]: https://codecov.io/gh/stable-packages/store/branch/main/graph/badge.svg
|
|
236
|
+
[codecov-url]: https://app.codecov.io/gh/stable-packages/store/tree/main/packages%2Fglobal-store
|
|
237
|
+
[downloads-image]: https://img.shields.io/npm/dm/global-store.svg?style=flat
|
|
238
|
+
[downloads-url]: https://npmjs.org/package/global-store
|
|
239
|
+
[github-nodejs]: https://github.com/stable-packages/store/workflows/release/badge.svg
|
|
240
|
+
[github-action-url]: https://github.com/stable-packages/store/actions
|
|
241
|
+
[global-store]: https://github.com/stable-packages/store/tree/main/packages/global-store
|
|
242
|
+
[npm-image]: https://img.shields.io/npm/v/global-store.svg?style=flat
|
|
243
|
+
[npm-url]: https://npmjs.org/package/global-store
|
|
244
|
+
[vscode-image]: https://img.shields.io/badge/vscode-ready-green.svg
|
|
245
|
+
[vscode-url]: https://code.visualstudio.com/
|