@goodstate/local-storage-sync 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +27 -2
- package/dist/index.d.ts +16 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/local-storage.js +29 -0
- package/dist/local-storage.js.map +1 -0
- package/package.json +48 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Petr Chalupa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @goodstate/local-storage-sync
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sync engine that persists a store to [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) and shares it between tabs.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @goodstate/local-storage-sync @goodstate/store
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The engine loads saved state into the store when it connects and saves every change after that. Other tabs on the same origin that use the same name merge each change through the [`storage` event](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event).
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { LocalStorageSync } from "@goodstate/local-storage-sync";
|
|
17
|
+
|
|
18
|
+
const sync = new LocalStorageSync(store, "todos");
|
|
19
|
+
|
|
20
|
+
sync.onError((error) => console.error(error));
|
|
21
|
+
sync.connect();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The name defaults to `goodstate-default`. Each top-level property of the schema is saved as JSON under its own key, `<name>:<property>`. With the engine above, a `language` property is saved under `todos:language`.
|
|
25
|
+
|
|
26
|
+
`localStorage` holds about 5 MiB per origin, and every read and write blocks the main thread. Use this engine for small state, such as settings or drafts. For anything that grows, use [`@goodstate/indexdb-sync`](https://github.com/pchalupa/goodstate/tree/main/packages/indexdb-sync) with [`@goodstate/broadcast-sync`](https://github.com/pchalupa/goodstate/tree/main/packages/broadcast-sync) instead. A write over the quota is reported to `onError`, and the engine stays connected.
|
|
27
|
+
|
|
28
|
+
The engine keeps syncing when another tab removes its keys or clears `localStorage`. The store keeps its state and writes each property back on its next change. To erase saved state for good, call `dispose()` in every tab before clearing it.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SyncEngine } from "@goodstate/sync-engine";
|
|
2
|
+
import { Replica } from "@goodstate/sync-engine/replica";
|
|
3
|
+
/**
|
|
4
|
+
* Persists a replica in `localStorage` and merges what other tabs write there.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* new LocalStorageSync(store, "my-app").connect();
|
|
8
|
+
*/
|
|
9
|
+
export declare class LocalStorageSync extends SyncEngine {
|
|
10
|
+
private readonly storage;
|
|
11
|
+
constructor(replica: Replica, name?: string);
|
|
12
|
+
protected open(signal: AbortSignal): Promise<void>;
|
|
13
|
+
protected close(): void;
|
|
14
|
+
protected pull(name: string): Promise<unknown>;
|
|
15
|
+
protected push(name: string, entry: unknown): Promise<void>;
|
|
16
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LocalStorage } from "./local-storage.js";
|
|
2
|
+
import { SyncEngine } from "@goodstate/sync-engine";
|
|
3
|
+
// src/local-storage-sync.ts
|
|
4
|
+
var LocalStorageSync = class extends SyncEngine {
|
|
5
|
+
storage;
|
|
6
|
+
constructor(replica, name = "goodstate-default") {
|
|
7
|
+
super(replica);
|
|
8
|
+
this.storage = new LocalStorage(name);
|
|
9
|
+
this.storage.delegate = {
|
|
10
|
+
localStorageDidChange: (property, entry) => {
|
|
11
|
+
try {
|
|
12
|
+
this.receive({ [property]: entry });
|
|
13
|
+
} catch (error) {
|
|
14
|
+
this.errors.notify(error);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
localStorageDidFail: (error) => this.errors.notify(error)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async open(signal) {
|
|
21
|
+
this.storage.listen(signal);
|
|
22
|
+
}
|
|
23
|
+
close() {}
|
|
24
|
+
async pull(name) {
|
|
25
|
+
return this.storage.get(name);
|
|
26
|
+
}
|
|
27
|
+
async push(name, entry) {
|
|
28
|
+
this.storage.set(name, entry);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export { LocalStorageSync };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/local-storage-sync.ts"],"sourcesContent":["import { SyncEngine } from \"@goodstate/sync-engine\";\nimport type { Replica } from \"@goodstate/sync-engine/replica\";\n\nimport { LocalStorage } from \"./local-storage\";\n\n/**\n * Persists a replica in `localStorage` and merges what other tabs write there.\n *\n * @example\n * new LocalStorageSync(store, \"my-app\").connect();\n */\nexport class LocalStorageSync extends SyncEngine {\n\tprivate readonly storage: LocalStorage;\n\n\tconstructor(replica: Replica, name = \"goodstate-default\") {\n\t\tsuper(replica);\n\n\t\tthis.storage = new LocalStorage(name);\n\t\tthis.storage.delegate = {\n\t\t\tlocalStorageDidChange: (property, entry) => {\n\t\t\t\ttry {\n\t\t\t\t\tthis.receive({ [property]: entry });\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.errors.notify(error);\n\t\t\t\t}\n\t\t\t},\n\t\t\tlocalStorageDidFail: (error) => this.errors.notify(error),\n\t\t};\n\t}\n\n\tprotected async open(signal: AbortSignal): Promise<void> {\n\t\tthis.storage.listen(signal);\n\t}\n\n\tprotected close(): void {}\n\n\tprotected async pull(name: string): Promise<unknown> {\n\t\treturn this.storage.get(name);\n\t}\n\n\tprotected async push(name: string, entry: unknown): Promise<void> {\n\t\tthis.storage.set(name, entry);\n\t}\n}\n"],"mappings":";;;AAWA,IAAa,mBAAb,cAAsC,WAAW;CAC/B;CAEjB,YAAY,SAAkB,OAAO,qBAAqB;EACzD,MAAM,OAAO;EAEb,KAAK,UAAU,IAAI,aAAa,IAAI;EACpC,KAAK,QAAQ,WAAW;GACvB,wBAAwB,UAAU,UAAU;IAC3C,IAAI;KACH,KAAK,QAAQ,GAAG,WAAW,MAAM,CAAC;IACnC,SAAS,OAAO;KACf,KAAK,OAAO,OAAO,KAAK;IACzB;GACD;GACA,sBAAsB,UAAU,KAAK,OAAO,OAAO,KAAK;EACzD;CACD;CAEA,MAAgB,KAAK,QAAoC;EACxD,KAAK,QAAQ,OAAO,MAAM;CAC3B;CAEU,QAAc,CAAC;CAEzB,MAAgB,KAAK,MAAgC;EACpD,OAAO,KAAK,QAAQ,IAAI,IAAI;CAC7B;CAEA,MAAgB,KAAK,MAAc,OAA+B;EACjE,KAAK,QAAQ,IAAI,MAAM,KAAK;CAC7B;AACD"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/local-storage.ts
|
|
2
|
+
var LocalStorage = class {
|
|
3
|
+
prefix;
|
|
4
|
+
delegate;
|
|
5
|
+
constructor(name = "goodstate-default") {
|
|
6
|
+
this.prefix = `${name}:`;
|
|
7
|
+
}
|
|
8
|
+
get(name) {
|
|
9
|
+
const value = localStorage.getItem(this.prefix + name);
|
|
10
|
+
if (value) return JSON.parse(value);
|
|
11
|
+
}
|
|
12
|
+
set(name, entry) {
|
|
13
|
+
localStorage.setItem(this.prefix + name, JSON.stringify(entry));
|
|
14
|
+
}
|
|
15
|
+
listen(signal) {
|
|
16
|
+
addEventListener("storage", ({ key, newValue: value, storageArea }) => {
|
|
17
|
+
if (storageArea === localStorage && key && value && key.startsWith(this.prefix)) try {
|
|
18
|
+
const entry = JSON.parse(value);
|
|
19
|
+
this.delegate?.localStorageDidChange(key.slice(this.prefix.length), entry);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
this.delegate?.localStorageDidFail(error);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
}, { signal });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export { LocalStorage };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=local-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.js","names":[],"sources":["../src/local-storage.ts"],"sourcesContent":["import type { Delegatable } from \"@goodstate/observable\";\n\nexport interface LocalStorageDelegate {\n\tlocalStorageDidChange(name: string, entry: unknown): void;\n\tlocalStorageDidFail(error: unknown): void;\n}\n\n/** JSON entries stored under one name in `localStorage`. */\nexport class LocalStorage implements Delegatable<LocalStorageDelegate> {\n\tprivate readonly prefix: string;\n\tpublic delegate?: LocalStorageDelegate;\n\n\tconstructor(name = \"goodstate-default\") {\n\t\tthis.prefix = `${name}:`;\n\t}\n\n\tpublic get(name: string): unknown {\n\t\tconst value = localStorage.getItem(this.prefix + name);\n\n\t\tif (value) return JSON.parse(value);\n\t}\n\n\tpublic set(name: string, entry: unknown): void {\n\t\tlocalStorage.setItem(this.prefix + name, JSON.stringify(entry));\n\t}\n\n\t/** Tells the delegate about entries other tabs write until `signal` aborts. */\n\tpublic listen(signal: AbortSignal): void {\n\t\taddEventListener(\n\t\t\t\"storage\",\n\t\t\t({ key, newValue: value, storageArea }) => {\n\t\t\t\tif (storageArea === localStorage && key && value && key.startsWith(this.prefix)) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst entry = JSON.parse(value);\n\n\t\t\t\t\t\tthis.delegate?.localStorageDidChange(key.slice(this.prefix.length), entry);\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tthis.delegate?.localStorageDidFail(error);\n\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t{ signal },\n\t\t);\n\t}\n}\n"],"mappings":";AAQA,IAAa,eAAb,MAAuE;CACrD;CACV;CAEP,YAAY,OAAO,qBAAqB;EACvC,KAAK,SAAS,GAAG,KAAK;CACvB;CAEO,IAAI,MAAuB;EACjC,MAAM,QAAQ,aAAa,QAAQ,KAAK,SAAS,IAAI;EAErD,IAAI,OAAO,OAAO,KAAK,MAAM,KAAK;CACnC;CAEO,IAAI,MAAc,OAAsB;EAC9C,aAAa,QAAQ,KAAK,SAAS,MAAM,KAAK,UAAU,KAAK,CAAC;CAC/D;CAGO,OAAO,QAA2B;EACxC,iBACC,YACC,EAAE,KAAK,UAAU,OAAO,kBAAkB;GAC1C,IAAI,gBAAgB,gBAAgB,OAAO,SAAS,IAAI,WAAW,KAAK,MAAM,GAC7E,IAAI;IACH,MAAM,QAAQ,KAAK,MAAM,KAAK;IAE9B,KAAK,UAAU,sBAAsB,IAAI,MAAM,KAAK,OAAO,MAAM,GAAG,KAAK;GAC1E,SAAS,OAAO;IACf,KAAK,UAAU,oBAAoB,KAAK;IAExC;GACD;EAEF,GACA,EAAE,OAAO,CACV;CACD;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodstate/local-storage-sync",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Sync engine that persists a store to localStorage and shares it between tabs",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"local-first",
|
|
7
|
+
"localstorage",
|
|
8
|
+
"persistence",
|
|
9
|
+
"sync",
|
|
10
|
+
"tabs"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/pchalupa/goodstate/tree/main/packages/local-storage-sync#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/pchalupa/goodstate/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "pchalupa <chalupa.petr93@gmail.com> (https://github.com/pchalupa)",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/pchalupa/goodstate.git",
|
|
21
|
+
"directory": "packages/local-storage-sync"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@goodstate/observable": "^7.0.2",
|
|
33
|
+
"@goodstate/sync-engine": "^7.0.2"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@goodstate/build-config": "^0.0.0",
|
|
37
|
+
"@goodstate/schema": "^7.0.2",
|
|
38
|
+
"@goodstate/store": "^7.0.2",
|
|
39
|
+
"@goodstate/typescript-config": "^1.0.1",
|
|
40
|
+
"jsdom": "^31.0.1",
|
|
41
|
+
"tsdown": "^0.23.7",
|
|
42
|
+
"typescript": "7.0.2",
|
|
43
|
+
"vitest": "^5.1.1"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"ts:check": "tsc --noEmit",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"test:watch": "vitest watch"
|
|
50
|
+
}
|
|
6
51
|
}
|