global-store 1.0.0-beta.21 → 1.0.0-beta.22

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.
Files changed (97) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +2 -244
  3. package/dist/global-store.js +2 -0
  4. package/dist/global-store.js.map +1 -0
  5. package/index.js.flow +2 -2
  6. package/lib/compareVersion.d.ts +2 -1
  7. package/lib/compareVersion.d.ts.map +1 -0
  8. package/lib/compareVersion.js +4 -8
  9. package/lib/compareVersion.js.map +1 -1
  10. package/lib/createAsyncStore.d.ts +3 -2
  11. package/lib/createAsyncStore.d.ts.map +1 -0
  12. package/lib/createAsyncStore.js +13 -59
  13. package/lib/createAsyncStore.js.map +1 -1
  14. package/lib/createStore.d.ts +3 -2
  15. package/lib/createStore.d.ts.map +1 -0
  16. package/lib/createStore.js +8 -13
  17. package/lib/createStore.js.map +1 -1
  18. package/lib/index.d.ts +5 -4
  19. package/lib/index.d.ts.map +1 -0
  20. package/lib/index.js +4 -22
  21. package/lib/index.js.map +1 -1
  22. package/lib/shouldInvokeInitializer.d.ts +2 -1
  23. package/lib/shouldInvokeInitializer.d.ts.map +1 -0
  24. package/lib/shouldInvokeInitializer.js +7 -11
  25. package/lib/shouldInvokeInitializer.js.map +1 -1
  26. package/lib/toVersionArray.d.ts +3 -2
  27. package/lib/toVersionArray.d.ts.map +1 -0
  28. package/lib/toVersionArray.js +2 -6
  29. package/lib/toVersionArray.js.map +1 -1
  30. package/lib/types.d.ts +8 -3
  31. package/lib/types.d.ts.map +1 -0
  32. package/lib/types.js +1 -2
  33. package/lib/types.js.map +1 -1
  34. package/lib/typesInternal.d.ts +4 -3
  35. package/lib/typesInternal.d.ts.map +1 -0
  36. package/lib/typesInternal.js +1 -2
  37. package/lib/typesInternal.js.map +1 -1
  38. package/lib/util.d.ts +4 -3
  39. package/lib/util.d.ts.map +1 -0
  40. package/lib/util.js +22 -36
  41. package/lib/util.js.map +1 -1
  42. package/package.json +44 -62
  43. package/dist/compareVersion.d.ts +0 -7
  44. package/dist/createAsyncStore.d.ts +0 -12
  45. package/dist/createStore.d.ts +0 -28
  46. package/dist/global-store.es.js +0 -127
  47. package/dist/global-store.es.js.map +0 -1
  48. package/dist/global-store.es5.js +0 -2
  49. package/dist/global-store.es5.js.map +0 -1
  50. package/dist/global-store.systemjs.js +0 -141
  51. package/dist/global-store.systemjs.js.map +0 -1
  52. package/dist/index.d.ts +0 -4
  53. package/dist/shouldInvokeInitializer.d.ts +0 -2
  54. package/dist/toVersionArray.d.ts +0 -3
  55. package/dist/types.d.ts +0 -9
  56. package/dist/util.d.ts +0 -14
  57. package/libm/compareVersion.d.ts +0 -7
  58. package/libm/compareVersion.js +0 -13
  59. package/libm/compareVersion.js.map +0 -1
  60. package/libm/createAsyncStore.d.ts +0 -12
  61. package/libm/createAsyncStore.js +0 -27
  62. package/libm/createAsyncStore.js.map +0 -1
  63. package/libm/createStore.d.ts +0 -28
  64. package/libm/createStore.js +0 -16
  65. package/libm/createStore.js.map +0 -1
  66. package/libm/index.d.ts +0 -4
  67. package/libm/index.js +0 -5
  68. package/libm/index.js.map +0 -1
  69. package/libm/shouldInvokeInitializer.d.ts +0 -2
  70. package/libm/shouldInvokeInitializer.js +0 -14
  71. package/libm/shouldInvokeInitializer.js.map +0 -1
  72. package/libm/toVersionArray.d.ts +0 -3
  73. package/libm/toVersionArray.js +0 -5
  74. package/libm/toVersionArray.js.map +0 -1
  75. package/libm/types.d.ts +0 -9
  76. package/libm/types.js +0 -2
  77. package/libm/types.js.map +0 -1
  78. package/libm/typesInternal.d.ts +0 -16
  79. package/libm/typesInternal.js +0 -2
  80. package/libm/typesInternal.js.map +0 -1
  81. package/libm/util.d.ts +0 -14
  82. package/libm/util.js +0 -57
  83. package/libm/util.js.map +0 -1
  84. package/ts/compareVersion.spec.ts +0 -9
  85. package/ts/compareVersion.ts +0 -14
  86. package/ts/createAsyncStore.spec.ts +0 -142
  87. package/ts/createAsyncStore.ts +0 -31
  88. package/ts/createStore.spec.ts +0 -217
  89. package/ts/createStore.ts +0 -43
  90. package/ts/index.spec.ts +0 -7
  91. package/ts/index.ts +0 -5
  92. package/ts/shouldInvokeInitializer.spec.ts +0 -41
  93. package/ts/shouldInvokeInitializer.ts +0 -17
  94. package/ts/toVersionArray.ts +0 -8
  95. package/ts/types.ts +0 -12
  96. package/ts/typesInternal.ts +0 -21
  97. package/ts/util.ts +0 -69
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 unional, Homa Wong (homawong@gmail.com)
3
+ Copyright © 2017 unional, Homa Wong (homawong@gmail.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,247 +1,5 @@
1
1
  # global-store
2
2
 
3
- [![NPM version][npm-image]][npm-url]
4
- [![NPM downloads][downloads-image]][downloads-url]
5
- ![badge-size-es5-url]
3
+ The `global-store` package.
6
4
 
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!! :tada:).
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-es5-url]: http://img.badgesize.io/unional/global-store/master/dist/global-store.es5.js.svg?label=es5_size&compression=gzip
235
- [codecov-image]: https://codecov.io/gh/unional/global-store/branch/master/graph/badge.svg
236
- [codecov-url]: https://codecov.io/gh/unional/global-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/unional/global-store/workflows/release/badge.svg
240
- [github-action-url]: https://github.com/unional/global-store/actions
241
- [global-store]: https://github.com/unional/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
- [downloads-image]: https://img.shields.io/npm/dm/global-store.svg?style=flat
245
- [downloads-url]: https://npmjs.org/package/global-store
246
- [vscode-image]: https://img.shields.io/badge/vscode-ready-green.svg
247
- [vscode-url]: https://code.visualstudio.com/
5
+ Please check out the [main read me](../../README.md) for more information.
@@ -0,0 +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 s=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)})(s.versions,i)&&(s.initializers.push(o),s.value=o(s.value,s.versions),s.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=>s(e,n))),Object.getOwnPropertySymbols&&Object.getOwnPropertySymbols(e).forEach((n=>s(e,n)));return Object.freeze(e)}(i.value)}function s(e,n){const r=e[n];Array.isArray(r)&&(e[n]=Object.freeze(r))}const u=Object.create(null);function c({moduleName:e,key:n,version:i,initializer:s}){const c={moduleName:e,key:n};return r(u,c,i,s),{get value(){return function(e,n){return t(e,n).value}(u,c)},freeze:e=>o(u,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)),{})}(u,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),s=n??"default";(o[s]=o[s]||[]).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;(n?[n]:Object.keys(r)).forEach((n=>i(e,n,r[n],c)))},Object.defineProperty(e,"__esModule",{value:!0}),e}({});
2
+ //# sourceMappingURL=global-store.js.map
@@ -0,0 +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,E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EAAoBH,EAAmBpC,GAC7C,IAAKuC,EAAmB,QAEXpC,EAAM,CAACA,GAAOF,OAAOsB,KAAKgB,IAClCvB,SAAQb,GAAOC,EAAgBJ,EAAYG,EAAKoC,EAAkBpC,GAAM2B,IAC/E"}
package/index.js.flow CHANGED
@@ -3,8 +3,8 @@ declare module 'global-store' {
3
3
  declare export type StoreVersion = string | Number
4
4
  declare export type StoreInitializer<T: StoreValue> = (current: T, processedVersions: StoreVersion[]) => T
5
5
 
6
- declare export type Store<T: StoreValue> = { get(): T, reset(): void }
7
- declare export type StoreOptions<T: StoreValue> = { moduleName: string, key: string, version: StoreVersion, initializer: StoreInitializer<T> }
6
+ declare export type Store<T: StoreValue> = { value: $ReadOnly<T>, freeze: (value: ?$ReadOnly<T>) => void, reset(): void }
7
+ declare export type StoreOptions<T: StoreValue> = { moduleName: string, key: ?string, version: StoreVersion, initializer: StoreInitializer<T> }
8
8
 
9
9
 
10
10
  declare export function createAsyncStore<T: StoreValue>(options: StoreOptions<T>): Promise<Store<T>>
@@ -1,7 +1,8 @@
1
- import { StoreVersion } from './types';
1
+ import type { StoreVersion } from './types.js';
2
2
  /**
3
3
  * Compare version.
4
4
  * Positive when processed > current
5
5
  * Negative when processed < current
6
6
  */
7
7
  export declare function compareVersion(processed: StoreVersion, current: StoreVersion): number;
8
+ //# sourceMappingURL=compareVersion.d.ts.map
@@ -0,0 +1 @@
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,UAK5E"}
@@ -1,17 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compareVersion = void 0;
4
- var toVersionArray_1 = require("./toVersionArray");
1
+ import { toVersionArray } from './toVersionArray.js';
5
2
  /**
6
3
  * Compare version.
7
4
  * Positive when processed > current
8
5
  * Negative when processed < current
9
6
  */
10
- function compareVersion(processed, current) {
11
- var v1 = (0, toVersionArray_1.toVersionArray)(processed);
12
- var v2 = (0, toVersionArray_1.toVersionArray)(current);
7
+ export function compareVersion(processed, current) {
8
+ const v1 = toVersionArray(processed);
9
+ const v2 = toVersionArray(current);
13
10
  return v1[0] !== v2[0] ? v1[0] - v2[0] :
14
11
  v1[1] !== v2[1] ? v1[1] - v2[1] : v1[2] - v2[2];
15
12
  }
16
- exports.compareVersion = compareVersion;
17
13
  //# sourceMappingURL=compareVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"compareVersion.js","sourceRoot":"","sources":["../ts/compareVersion.ts"],"names":[],"mappings":";;;AAAA,mDAAiD;AAGjD;;;;GAIG;AACH,SAAgB,cAAc,CAAC,SAAuB,EAAE,OAAqB;IAC3E,IAAM,EAAE,GAAG,IAAA,+BAAc,EAAC,SAAS,CAAC,CAAA;IACpC,IAAM,EAAE,GAAG,IAAA,+BAAc,EAAC,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;QACtC,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;AACnD,CAAC;AALD,wCAKC"}
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;IAC3E,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;QACtC,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;AACnD,CAAC","sourcesContent":["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"]}
@@ -1,5 +1,5 @@
1
- import { Store } from './createStore';
2
- import { StoreOptions, StoreValue } from './types';
1
+ import { type Store } from './createStore.js';
2
+ import type { StoreOptions, StoreValue } from './types.js';
3
3
  /**
4
4
  * Creates a store of type T asynchronously.
5
5
  * @see https://github.com/unional/global-store#createAsyncStore
@@ -10,3 +10,4 @@ export declare function createAsyncStore<T extends StoreValue>({ moduleName, key
10
10
  * @see https://github.com/unional/global-store#initializeAsyncStore
11
11
  */
12
12
  export declare function initializeAsyncStore(moduleName: string, key?: string): void;
13
+ //# sourceMappingURL=createAsyncStore.d.ts.map
@@ -0,0 +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,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAO1I;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,QAMpE"}
@@ -1,73 +1,27 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.initializeAsyncStore = exports.createAsyncStore = void 0;
40
- var createStore_1 = require("./createStore");
41
- var util_1 = require("./util");
42
- var asyncStoreCreators = Object.create(null);
1
+ import { createStore } from './createStore.js';
2
+ import { resolveCreators } from './util.js';
3
+ const asyncStoreCreators = Object.create(null);
43
4
  /**
44
5
  * Creates a store of type T asynchronously.
45
6
  * @see https://github.com/unional/global-store#createAsyncStore
46
7
  */
47
- function createAsyncStore(_a) {
48
- var moduleName = _a.moduleName, key = _a.key, version = _a.version, initializer = _a.initializer;
49
- return __awaiter(this, void 0, void 0, function () {
50
- return __generator(this, function (_b) {
51
- return [2 /*return*/, new Promise(function (resolve) {
52
- var creatorsOfModules = asyncStoreCreators[moduleName] = asyncStoreCreators[moduleName] || Object.create(null);
53
- var k = key !== null && key !== void 0 ? key : 'default';
54
- var storeCreators = creatorsOfModules[k] = creatorsOfModules[k] || [];
55
- storeCreators.push({ version: version, resolve: resolve, initializer: initializer });
56
- })];
57
- });
8
+ export async function createAsyncStore({ moduleName, key, version, initializer }) {
9
+ return new Promise(resolve => {
10
+ const creatorsOfModules = asyncStoreCreators[moduleName] = asyncStoreCreators[moduleName] || Object.create(null);
11
+ const k = key ?? 'default';
12
+ const storeCreators = creatorsOfModules[k] = creatorsOfModules[k] || [];
13
+ storeCreators.push({ version, resolve, initializer });
58
14
  });
59
15
  }
60
- exports.createAsyncStore = createAsyncStore;
61
16
  /**
62
17
  * Initializes the stores for `createAsyncStore()`.
63
18
  * @see https://github.com/unional/global-store#initializeAsyncStore
64
19
  */
65
- function initializeAsyncStore(moduleName, key) {
66
- var creatorsOfModules = asyncStoreCreators[moduleName];
20
+ export function initializeAsyncStore(moduleName, key) {
21
+ const creatorsOfModules = asyncStoreCreators[moduleName];
67
22
  if (!creatorsOfModules)
68
23
  return;
69
- var keys = key ? [key] : Object.keys(creatorsOfModules);
70
- keys.forEach(function (key) { return (0, util_1.resolveCreators)(moduleName, key, creatorsOfModules[key], createStore_1.createStore); });
24
+ const keys = key ? [key] : Object.keys(creatorsOfModules);
25
+ keys.forEach(key => resolveCreators(moduleName, key, creatorsOfModules[key], createStore));
71
26
  }
72
- exports.initializeAsyncStore = initializeAsyncStore;
73
27
  //# sourceMappingURL=createAsyncStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createAsyncStore.js","sourceRoot":"","sources":["../ts/createAsyncStore.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAkD;AAGlD,+BAAwC;AAExC,IAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAA8B,CAAA;AAE3E;;;GAGG;AACH,SAAsB,gBAAgB,CAAuB,EAA0D;QAAxD,UAAU,gBAAA,EAAE,GAAG,SAAA,EAAE,OAAO,aAAA,EAAE,WAAW,iBAAA;;;YAClG,sBAAO,IAAI,OAAO,CAAC,UAAA,OAAO;oBACxB,IAAM,iBAAiB,GAAG,kBAAkB,CAAC,UAAU,CAAC,GAAG,kBAAkB,CAAC,UA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rH,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;QAC3B,MAAM,iBAAiB,GAAG,kBAAkB,CAAC,UAAU,CAAC,GAAG,kBAAkB,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAChH,MAAM,CAAC,GAAG,GAAG,IAAI,SAAS,CAAA;QAC1B,MAAM,aAAa,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;QACvE,aAAa,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAA;IACvD,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,UAAkB,EAAE,GAAY;IACnE,MAAM,iBAAiB,GAAG,kBAAkB,CAAC,UAAU,CAAC,CAAA;IACxD,IAAI,CAAC,iBAAiB;QAAE,OAAM;IAE9B,MAAM,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;IACzD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,GAAG,EAAE,iBAAiB,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC,CAAA;AAC5F,CAAC","sourcesContent":["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"]}
@@ -1,5 +1,5 @@
1
- import { StoreOptions, StoreValue } from './types';
2
- export declare type Store<T extends StoreValue> = {
1
+ import type { StoreOptions, StoreValue } from './types.js';
2
+ export type Store<T extends StoreValue> = {
3
3
  /**
4
4
  * The store value.
5
5
  */
@@ -26,3 +26,4 @@ export declare type Store<T extends StoreValue> = {
26
26
  * @see https://www.npmjs.com/package/global-store
27
27
  */
28
28
  export declare function createStore<T extends StoreValue>({ moduleName, key, version, initializer }: StoreOptions<T>): Store<T>;
29
+ //# sourceMappingURL=createStore.d.ts.map
@@ -0,0 +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;IACxC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,CAAC,EAAE;SAAG,CAAC,IAAI,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAAE,GAAG,IAAI,CAAC;IACzD;;;OAGG;IACH,KAAK,IAAI,IAAI,CAAC;CACf,CAAA;AAID;;;GAGG;AACH,wBAAgB,WAAW,CACzB,CAAC,SAAS,UAAU,EACpB,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAStE"}
@@ -1,21 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createStore = void 0;
4
- var util_1 = require("./util");
5
- var stores = Object.create(null);
1
+ import { freezeStoreValue, getStoreValue, initStoreValue, resetStoreValue } from './util.js';
2
+ const stores = Object.create(null);
6
3
  /**
7
4
  * Creates a store of type T.
8
5
  * @see https://www.npmjs.com/package/global-store
9
6
  */
10
- function createStore(_a) {
11
- var moduleName = _a.moduleName, key = _a.key, version = _a.version, initializer = _a.initializer;
12
- var id = { moduleName: moduleName, key: key };
13
- (0, util_1.initStoreValue)(stores, id, version, initializer);
7
+ export function createStore({ moduleName, key, version, initializer }) {
8
+ const id = { moduleName, key };
9
+ initStoreValue(stores, id, version, initializer);
14
10
  return {
15
- get value() { return (0, util_1.getStoreValue)(stores, id); },
16
- freeze: function (value) { return (0, util_1.freezeStoreValue)(stores, id, value); },
17
- reset: function () { return (0, util_1.resetStoreValue)(stores, id); }
11
+ get value() { return getStoreValue(stores, id); },
12
+ freeze(value) { return freezeStoreValue(stores, id, value); },
13
+ reset() { return resetStoreValue(stores, id); }
18
14
  };
19
15
  }
20
- exports.createStore = createStore;
21
16
  //# sourceMappingURL=createStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createStore.js","sourceRoot":"","sources":["../ts/createStore.ts"],"names":[],"mappings":";;;AAEA,+BAAyF;AAuBzF,IAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAW,CAAA;AAE5C;;;GAGG;AACH,SAAgB,WAAW,CAEzB,EAA0D;QAAxD,UAAU,gBAAA,EAAE,GAAG,SAAA,EAAE,OAAO,aAAA,EAAE,WAAW,iBAAA;IACvC,IAAM,EAAE,GAAG,EAAE,UAAU,YAAA,E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ptions, 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"]}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from './createAsyncStore';
2
- export * from './createStore';
3
- export { createStore as default } from './createStore';
4
- export * from './types';
1
+ export * from './createAsyncStore.js';
2
+ export * from './createStore.js';
3
+ export { createStore as default } from './createStore.js';
4
+ export * from './types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","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"}
package/lib/index.js CHANGED
@@ -1,23 +1,5 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.default = void 0;
18
- __exportStar(require("./createAsyncStore"), exports);
19
- __exportStar(require("./createStore"), exports);
20
- var createStore_1 = require("./createStore");
21
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return createStore_1.createStore; } });
22
- __exportStar(require("./types"), exports);
1
+ export * from './createAsyncStore.js';
2
+ export * from './createStore.js';
3
+ export { createStore as default } from './createStore.js';
4
+ export * from './types.js';
23
5
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,gDAA6B;AAC7B,6CAAsD;AAA7C,sGAAA,WAAW,OAAW;AAC/B,0CAAuB"}
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","sourcesContent":["export * from './createAsyncStore.js'\nexport * from './createStore.js'\nexport { createStore as default } from './createStore.js'\nexport * from './types.js'\n\n"]}
@@ -1,2 +1,3 @@
1
- import { StoreVersion } from './types';
1
+ import type { StoreVersion } from './types.js';
2
2
  export declare function shouldInvokeInitializer(versions: StoreVersion[], version: StoreVersion): boolean;
3
+ //# sourceMappingURL=shouldInvokeInitializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shouldInvokeInitializer.d.ts","sourceRoot":"","sources":["../ts/shouldInvokeInitializer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,YAAY,WAItF"}
@@ -1,18 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.shouldInvokeInitializer = void 0;
4
- var toVersionArray_1 = require("./toVersionArray");
5
- function shouldInvokeInitializer(versions, version) {
6
- var vs = versions.map(toVersionArray_1.toVersionArray);
7
- var v = (0, toVersionArray_1.toVersionArray)(version);
1
+ import { toVersionArray } from './toVersionArray.js';
2
+ export function shouldInvokeInitializer(versions, version) {
3
+ const vs = versions.map(toVersionArray);
4
+ const v = toVersionArray(version);
8
5
  return noMatchMajor(vs, v) || hasNewVersion(vs, v);
9
6
  }
10
- exports.shouldInvokeInitializer = shouldInvokeInitializer;
11
7
  function noMatchMajor(versions, version) {
12
- return !versions.some(function (v) { return v[0] === version[0]; });
8
+ return !versions.some(v => v[0] === version[0]);
13
9
  }
14
10
  function hasNewVersion(versions, version) {
15
- return versions.filter(function (v) { return v[0] === version[0]; })
16
- .some(function (v) { return version[1] > v[1] || (version[1] === v[1] && version[2] > v[2]); });
11
+ return versions.filter(v => v[0] === version[0])
12
+ .some(v => version[1] > v[1] || (version[1] === v[1] && version[2] > v[2]));
17
13
  }
18
14
  //# sourceMappingURL=shouldInvokeInitializer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shouldInvokeInitializer.js","sourceRoot":"","sources":["../ts/shouldInvokeInitializer.ts"],"names":[],"mappings":";;;AAAA,mDAAgE;AAGhE,SAAgB,uBAAuB,CAAC,QAAwB,EAAE,OAAqB;IACrF,IAAM,EAAE,GAAG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,CAAC,CAAC,IAAI,aAAa,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AACpD,CAAC;AAED,SAAS,YAAY,CAAC,QAAyB,EAAE,OAAsB;IACrE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AACjD,CAAC;AAED,SAAS,aAAa,CAAC,QAAyB,EAAE,OAAsB;IACtE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;SAC7C,IAAI,CAAC,CAAC,CAAC,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","sourcesContent":["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"]}