@context-action/store-core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/index.cjs +3 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +32 -8
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -23,13 +23,14 @@ function createStateStore(name, initialValue, options = {}) {
|
|
|
23
23
|
};
|
|
24
24
|
const publish = (next) => {
|
|
25
25
|
if (disposed || Object.is(value, next)) return;
|
|
26
|
+
const updatedAt = now();
|
|
26
27
|
value = next;
|
|
27
28
|
version += 1;
|
|
28
29
|
snapshot = {
|
|
29
30
|
value,
|
|
30
31
|
name,
|
|
31
32
|
version,
|
|
32
|
-
lastUpdate:
|
|
33
|
+
lastUpdate: updatedAt
|
|
33
34
|
};
|
|
34
35
|
notify();
|
|
35
36
|
};
|
|
@@ -42,6 +43,7 @@ function createStateStore(name, initialValue, options = {}) {
|
|
|
42
43
|
},
|
|
43
44
|
getSnapshot: () => snapshot,
|
|
44
45
|
subscribe(listener) {
|
|
46
|
+
if (typeof listener !== "function") throw new TypeError("Store subscriber must be a function.");
|
|
45
47
|
if (disposed) return () => {};
|
|
46
48
|
listeners.add(listener);
|
|
47
49
|
return () => listeners.delete(listener);
|
package/dist/index.js
CHANGED
|
@@ -22,13 +22,14 @@ function createStateStore(name, initialValue, options = {}) {
|
|
|
22
22
|
};
|
|
23
23
|
const publish = (next) => {
|
|
24
24
|
if (disposed || Object.is(value, next)) return;
|
|
25
|
+
const updatedAt = now();
|
|
25
26
|
value = next;
|
|
26
27
|
version += 1;
|
|
27
28
|
snapshot = {
|
|
28
29
|
value,
|
|
29
30
|
name,
|
|
30
31
|
version,
|
|
31
|
-
lastUpdate:
|
|
32
|
+
lastUpdate: updatedAt
|
|
32
33
|
};
|
|
33
34
|
notify();
|
|
34
35
|
};
|
|
@@ -41,6 +42,7 @@ function createStateStore(name, initialValue, options = {}) {
|
|
|
41
42
|
},
|
|
42
43
|
getSnapshot: () => snapshot,
|
|
43
44
|
subscribe(listener) {
|
|
45
|
+
if (typeof listener !== "function") throw new TypeError("Store subscriber must be a function.");
|
|
44
46
|
if (disposed) return () => {};
|
|
45
47
|
listeners.add(listener);
|
|
46
48
|
return () => listeners.delete(listener);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Framework-neutral state contracts and a deliberately small reference store.\n *\n * This package owns no immutable-update, draft, clone, React, or DOM runtime.\n * A backend owns its value semantics; consumers must only rely on the\n * snapshot/subscription and capability contracts below.\n */\n\nexport type StateMutationOrigin = 'user' | 'system' | 'network' | 'undo' | 'redo' | 'reset';\n\n/** Metadata is extensible so adapters can preserve their own correlation data. */\nexport interface StateMutationMeta {\n readonly transactionId?: string;\n readonly actionId?: string;\n readonly origin?: StateMutationOrigin;\n readonly label?: string;\n readonly [key: string]: unknown;\n}\n\n/** Patch paths intentionally accept non-string keys such as Map keys and Symbols. */\nexport interface StatePatch {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n readonly path: readonly unknown[];\n readonly from?: readonly unknown[];\n readonly value?: unknown;\n}\n\nexport type HistoryEntryId = string | number;\n\nexport interface ReadonlyStateSnapshot<T> {\n readonly value: T;\n readonly name: string;\n /** Monotonically increases when the backend publishes a new snapshot. */\n readonly version: number;\n readonly lastUpdate: number;\n}\n\nexport interface StateReader<T> {\n readonly name: string;\n getSnapshot(): ReadonlyStateSnapshot<T>;\n subscribe(listener: () => void): () => void;\n}\n\nexport interface StateWriter<T> {\n /** The backend owns mutation safety; values are passed through unchanged. */\n setValue(value: T, meta?: StateMutationMeta): void;\n /** Returning undefined is a no-op. In-place mutation is backend-specific. */\n update(updater: (current: T) => T | undefined, meta?: StateMutationMeta): void;\n}\n\nexport interface StateBackend<T, Patch = StatePatch> extends StateReader<T>, StateWriter<T> {\n readonly capabilities?: {\n readonly patches?: boolean;\n readonly timeline?: boolean;\n readonly immutableSnapshots?: boolean;\n };\n getLastPatches?(): readonly Patch[] | null;\n /** Optional defensive read. Its absence means reads are backend-owned references. */\n getSafeValue?(): T;\n /** Optional lifecycle hook. Backends are shared by default. */\n dispose?(): void;\n isDisposed?(): boolean;\n subscribeWithPatches?(listener: (patches: readonly Patch[] | null) => void): () => void;\n}\n\nexport interface TimelineBackend<T, Patch = StatePatch> extends StateBackend<T, Patch> {\n getHistoryEntryId(): HistoryEntryId;\n hasHistoryEntry(entryId: HistoryEntryId): boolean;\n canUndo(): boolean;\n canRedo(): boolean;\n getPosition(): number;\n getHistory(): readonly T[];\n getHistoryLength(): number;\n undo(steps?: number, meta?: StateMutationMeta): void;\n redo(steps?: number, meta?: StateMutationMeta): void;\n goTo(position: number, meta?: StateMutationMeta): void;\n goToHistoryEntry(entryId: HistoryEntryId, meta?: StateMutationMeta): void;\n reset(meta?: StateMutationMeta): void;\n}\n\nexport interface TransactionBackend<T, Patch = StatePatch> extends TimelineBackend<T, Patch> {\n beginBatch(meta?: StateMutationMeta, options?: { readonly deferNotification?: boolean }): void;\n endBatch(): void;\n cancelBatch(): void;\n resumeNotifications(): void;\n flushNotifications(): void;\n isStoreDisposed(): boolean;\n}\n\nexport interface StateTransition<T> {\n readonly previous: T;\n readonly next: T;\n readonly meta: StateMutationMeta;\n readonly position?: number;\n}\n\nexport interface StateStoreOptions {\n readonly now?: () => number;\n}\n\n/**\n * A trusted-reference backend for applications that already own immutability.\n * No clone, freeze, draft, or patch generation is performed.\n */\nexport function createStateStore<T>(\n name: string,\n initialValue: T,\n options: StateStoreOptions = {},\n): StateBackend<T> {\n const now = options.now ?? Date.now;\n let value = initialValue;\n let version = 0;\n let disposed = false;\n let snapshot: ReadonlyStateSnapshot<T> = { value, name, version, lastUpdate: now() };\n const listeners = new Set<() => void>();\n\n const notify = (): void => {\n for (const listener of [...listeners]) {\n try {\n listener();\n } catch {\n // A subscriber cannot prevent other subscribers from observing a commit.\n }\n }\n };\n\n const publish = (next: T): void => {\n if (disposed || Object.is(value, next)) return;\n value = next;\n version += 1;\n snapshot = { value, name, version, lastUpdate:
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Framework-neutral state contracts and a deliberately small reference store.\n *\n * This package owns no immutable-update, draft, clone, React, or DOM runtime.\n * A backend owns its value semantics; consumers must only rely on the\n * snapshot/subscription and capability contracts below.\n */\n\nexport type StateMutationOrigin = 'user' | 'system' | 'network' | 'undo' | 'redo' | 'reset';\n\n/** Metadata is extensible so adapters can preserve their own correlation data. */\nexport interface StateMutationMeta {\n readonly transactionId?: string;\n readonly actionId?: string;\n readonly origin?: StateMutationOrigin;\n readonly label?: string;\n readonly [key: string]: unknown;\n}\n\n/** Patch paths intentionally accept non-string keys such as Map keys and Symbols. */\nexport interface StatePatch {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n readonly path: readonly unknown[];\n readonly from?: readonly unknown[];\n readonly value?: unknown;\n}\n\nexport type HistoryEntryId = string | number;\n\nexport interface ReadonlyStateSnapshot<T> {\n readonly value: T;\n readonly name: string;\n /** Monotonically increases when the backend publishes a new snapshot. */\n readonly version: number;\n readonly lastUpdate: number;\n}\n\nexport interface StateReader<T> {\n readonly name: string;\n getSnapshot(): ReadonlyStateSnapshot<T>;\n subscribe(listener: () => void): () => void;\n}\n\nexport interface StateWriter<T> {\n /** The backend owns mutation safety; values are passed through unchanged. */\n setValue(value: T, meta?: StateMutationMeta): void;\n /** Returning undefined is a no-op. In-place mutation is backend-specific. */\n update(updater: (current: T) => T | undefined, meta?: StateMutationMeta): void;\n}\n\nexport interface StateBackend<T, Patch = StatePatch> extends StateReader<T>, StateWriter<T> {\n readonly capabilities?: {\n readonly patches?: boolean;\n readonly timeline?: boolean;\n readonly immutableSnapshots?: boolean;\n };\n getLastPatches?(): readonly Patch[] | null;\n /** Optional defensive read. Its absence means reads are backend-owned references. */\n getSafeValue?(): T;\n /** Optional lifecycle hook. Backends are shared by default. */\n dispose?(): void;\n isDisposed?(): boolean;\n subscribeWithPatches?(listener: (patches: readonly Patch[] | null) => void): () => void;\n}\n\nexport interface TimelineBackend<T, Patch = StatePatch> extends StateBackend<T, Patch> {\n getHistoryEntryId(): HistoryEntryId;\n hasHistoryEntry(entryId: HistoryEntryId): boolean;\n canUndo(): boolean;\n canRedo(): boolean;\n getPosition(): number;\n getHistory(): readonly T[];\n getHistoryLength(): number;\n undo(steps?: number, meta?: StateMutationMeta): void;\n redo(steps?: number, meta?: StateMutationMeta): void;\n goTo(position: number, meta?: StateMutationMeta): void;\n goToHistoryEntry(entryId: HistoryEntryId, meta?: StateMutationMeta): void;\n reset(meta?: StateMutationMeta): void;\n}\n\nexport interface TransactionBackend<T, Patch = StatePatch> extends TimelineBackend<T, Patch> {\n beginBatch(meta?: StateMutationMeta, options?: { readonly deferNotification?: boolean }): void;\n endBatch(): void;\n cancelBatch(): void;\n resumeNotifications(): void;\n flushNotifications(): void;\n isStoreDisposed(): boolean;\n}\n\nexport interface StateTransition<T> {\n readonly previous: T;\n readonly next: T;\n readonly meta: StateMutationMeta;\n readonly position?: number;\n}\n\nexport interface StateStoreOptions {\n readonly now?: () => number;\n}\n\n/**\n * A trusted-reference backend for applications that already own immutability.\n * No clone, freeze, draft, or patch generation is performed.\n */\nexport function createStateStore<T>(\n name: string,\n initialValue: T,\n options: StateStoreOptions = {},\n): StateBackend<T> {\n const now = options.now ?? Date.now;\n let value = initialValue;\n let version = 0;\n let disposed = false;\n let snapshot: ReadonlyStateSnapshot<T> = { value, name, version, lastUpdate: now() };\n const listeners = new Set<() => void>();\n\n const notify = (): void => {\n for (const listener of [...listeners]) {\n try {\n listener();\n } catch {\n // A subscriber cannot prevent other subscribers from observing a commit.\n }\n }\n };\n\n const publish = (next: T): void => {\n if (disposed || Object.is(value, next)) return;\n // Resolve the timestamp before mutating the backend. A caller-provided\n // clock is part of the store boundary and may throw (for example, when a\n // deterministic test clock is exhausted); a failed timestamp must not\n // leave the value and snapshot out of sync.\n const updatedAt = now();\n value = next;\n version += 1;\n snapshot = { value, name, version, lastUpdate: updatedAt };\n notify();\n };\n\n const backend: StateBackend<T> = {\n name,\n capabilities: { patches: false, timeline: false, immutableSnapshots: false },\n getSnapshot: () => snapshot,\n subscribe(listener) {\n if (typeof listener !== 'function') {\n throw new TypeError('Store subscriber must be a function.');\n }\n if (disposed) return () => {};\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n setValue(next) {\n publish(next);\n },\n update(updater) {\n if (disposed) return;\n const next = updater(value);\n if (next !== undefined) publish(next);\n },\n isDisposed: () => disposed,\n dispose() {\n if (disposed) return;\n disposed = true;\n listeners.clear();\n },\n };\n return backend;\n}\n"],"mappings":";;;;;AAwGA,SAAgB,iBACd,MACA,cACA,UAA6B,CAAC,GACb;CACjB,MAAM,MAAM,QAAQ,OAAO,KAAK;CAChC,IAAI,QAAQ;CACZ,IAAI,UAAU;CACd,IAAI,WAAW;CACf,IAAI,WAAqC;EAAE;EAAO;EAAM;EAAS,YAAY,IAAI;CAAE;CACnF,MAAM,4BAAY,IAAI,IAAgB;CAEtC,MAAM,eAAqB;EACzB,KAAK,MAAM,YAAY,CAAC,GAAG,SAAS,GAClC,IAAI;GACF,SAAS;EACX,QAAQ,CAER;CAEJ;CAEA,MAAM,WAAW,SAAkB;EACjC,IAAI,YAAY,OAAO,GAAG,OAAO,IAAI,GAAG;EAKxC,MAAM,YAAY,IAAI;EACtB,QAAQ;EACR,WAAW;EACX,WAAW;GAAE;GAAO;GAAM;GAAS,YAAY;EAAU;EACzD,OAAO;CACT;CA6BA,OAAO;EA1BL;EACA,cAAc;GAAE,SAAS;GAAO,UAAU;GAAO,oBAAoB;EAAM;EAC3E,mBAAmB;EACnB,UAAU,UAAU;GAClB,IAAI,OAAO,aAAa,YACtB,MAAM,IAAI,UAAU,sCAAsC;GAE5D,IAAI,UAAU,aAAa,CAAC;GAC5B,UAAU,IAAI,QAAQ;GACtB,aAAa,UAAU,OAAO,QAAQ;EACxC;EACA,SAAS,MAAM;GACb,QAAQ,IAAI;EACd;EACA,OAAO,SAAS;GACd,IAAI,UAAU;GACd,MAAM,OAAO,QAAQ,KAAK;GAC1B,IAAI,SAAS,KAAA,GAAW,QAAQ,IAAI;EACtC;EACA,kBAAkB;EAClB,UAAU;GACR,IAAI,UAAU;GACd,WAAW;GACX,UAAU,MAAM;EAClB;CAEW;AACf"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-action/store-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Framework-neutral store and timeline contracts for Context-Action",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -8,12 +8,23 @@
|
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"import": {
|
|
12
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
},
|
|
15
21
|
"sideEffects": false,
|
|
16
|
-
"files": [
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"CHANGELOG.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
17
28
|
"scripts": {
|
|
18
29
|
"prepublishOnly": "pnpm run build",
|
|
19
30
|
"build": "tsdown",
|
|
@@ -22,11 +33,22 @@
|
|
|
22
33
|
"lint": "pnpm --workspace-root exec biome lint packages/store-core/src",
|
|
23
34
|
"clean": "rimraf dist"
|
|
24
35
|
},
|
|
25
|
-
"keywords": [
|
|
36
|
+
"keywords": [
|
|
37
|
+
"state-management",
|
|
38
|
+
"store",
|
|
39
|
+
"timeline",
|
|
40
|
+
"adapter"
|
|
41
|
+
],
|
|
26
42
|
"author": "mineclover",
|
|
27
43
|
"license": "Apache-2.0",
|
|
28
|
-
"repository": {
|
|
29
|
-
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/mineclover/context-action.git",
|
|
47
|
+
"directory": "packages/store-core"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
30
52
|
"devDependencies": {
|
|
31
53
|
"@types/node": "24.13.3",
|
|
32
54
|
"rimraf": "^6.1.3",
|
|
@@ -34,5 +56,7 @@
|
|
|
34
56
|
"tslib": "^2.8.1",
|
|
35
57
|
"typescript": "6.0.3"
|
|
36
58
|
},
|
|
37
|
-
"engines": {
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=24.11.0"
|
|
61
|
+
}
|
|
38
62
|
}
|