@jarenjs/app 0.72.2 → 0.72.3

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/README.md CHANGED
@@ -179,6 +179,10 @@ Collection keys and document names such as `__proto__` are ordinary own
179
179
  members of the document store and survive persistence. Loading a name
180
180
  that has not been saved returns `undefined`, including prototype-member names.
181
181
 
182
+ Stores created with the same storage adapter share one in-memory root, read
183
+ once. Saves and removals preserve other collections, including when the
184
+ adapter cannot persist writes. A new adapter starts a new read of storage.
185
+
182
186
  ## Invariants the model can't cheat
183
187
 
184
188
  ```javascript
@@ -5,8 +5,9 @@
5
5
  * the host so the package stays free of `localStorage`.
6
6
  *
7
7
  * `createDocStore` is a keyed CRUD over an injected `storage` ({ read, write }):
8
- * the store is one JSON object `{ [key]: { name → value } }`, read once at
9
- * creation and written back on every mutation. `encodeShare` / `decodeShare`
8
+ * the store is one JSON object `{ [key]: { name → value } }`, read once per
9
+ * storage adapter, shared by its collections and written back on every
10
+ * mutation. `encodeShare` / `decodeShare`
10
11
  * turn a small snapshot into a Unicode-safe base64url token and back —
11
12
  * forgivingly: a corrupt token decodes to `null`, never a throw.
12
13
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jarenjs/app",
3
3
  "private": false,
4
- "version": "0.72.2",
4
+ "version": "0.72.3",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
7
7
  "types": "./dist/types/index.d.ts",
@@ -50,8 +50,8 @@
50
50
  "prepack": "npm run build:types"
51
51
  },
52
52
  "dependencies": {
53
- "@jarenjs/core": "^0.72.2",
54
- "@jarenjs/json": "^0.72.2",
55
- "@jarenjs/view": "^0.72.2"
53
+ "@jarenjs/core": "^0.72.3",
54
+ "@jarenjs/json": "^0.72.3",
55
+ "@jarenjs/view": "^0.72.3"
56
56
  }
57
57
  }
package/src/docstore.js CHANGED
@@ -6,13 +6,18 @@
6
6
  * the host so the package stays free of `localStorage`.
7
7
  *
8
8
  * `createDocStore` is a keyed CRUD over an injected `storage` ({ read, write }):
9
- * the store is one JSON object `{ [key]: { name → value } }`, read once at
10
- * creation and written back on every mutation. `encodeShare` / `decodeShare`
9
+ * the store is one JSON object `{ [key]: { name → value } }`, read once per
10
+ * storage adapter, shared by its collections and written back on every
11
+ * mutation. `encodeShare` / `decodeShare`
11
12
  * turn a small snapshot into a Unicode-safe base64url token and back —
12
13
  * forgivingly: a corrupt token decodes to `null`, never a throw.
13
14
  */
14
15
 
15
16
  import { setObjectMember } from '@jarenjs/core/object';
17
+ import { createWeakCache } from '@jarenjs/core/cache';
18
+
19
+ /** Collection handles share their adapter's root, including unsaved fallback data. */
20
+ const storesByStorage = createWeakCache();
16
21
 
17
22
  /**
18
23
  * @param {Object} opts
@@ -29,8 +34,8 @@ import { setObjectMember } from '@jarenjs/core/object';
29
34
  * }}
30
35
  */
31
36
  export function createDocStore({ storage, key = 'experiments' }) {
32
- // read once; keep the SAME object reference for every write-back
33
- const store = storage.read() ?? {};
37
+ // Read once per adapter; every collection writes the same current root.
38
+ const store = storesByStorage.getOrCreate(storage, () => storage.read() ?? {});
34
39
  if (!Object.hasOwn(store, key) || store[key] == null) setObjectMember(store, key, {});
35
40
  return {
36
41
  save(name, value) { setObjectMember(store[key], name, value); storage.write(store); },