@kamod-ch/signals 1.0.2 → 1.1.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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +75 -25
- package/dist/createPersistedModel.d.ts +38 -0
- package/dist/devtools.d.ts +7 -0
- package/dist/devtools.js +31 -0
- package/dist/drivers.d.ts +11 -0
- package/dist/events.d.ts +23 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1160 -0
- package/dist/persistedSignal.d.ts +38 -0
- package/dist/shared.d.ts +19 -0
- package/dist/ssr.d.ts +18 -0
- package/dist/sync.d.ts +16 -0
- package/dist/types.d.ts +43 -0
- package/dist/usePersistedSignal.d.ts +2 -0
- package/dist/versioning.d.ts +32 -0
- package/package.json +34 -17
- package/src/drivers.ts +0 -470
- package/src/index.ts +0 -11
- package/src/persistedSignal.ts +0 -307
- package/src/shared.ts +0 -25
- package/src/types.ts +0 -41
- package/src/usePersistedSignal.ts +0 -52
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @kamod-ch/signals changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 1.1.0
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- **Peer dependency:** `preact` is now `^11.0.0` (was `^10.0.0`). Upgrade Preact in your app when updating this package.
|
|
10
|
+
- Dev toolchain: TypeScript 7, Vitest 5, tsup 9, `@preact/signals` 2.11.x, jsdom 30.
|
|
11
|
+
- Core build emits `.d.ts` via `tsc` (compatible with TypeScript 7; `tsup` bundles JS only).
|
|
12
|
+
|
|
13
|
+
## 1.0.3 and earlier
|
|
14
|
+
|
|
15
|
+
- Re-export official Preact Signals primitives, models, actions, and `useModel`.
|
|
16
|
+
- Add `createPersistedModel()`.
|
|
17
|
+
- Add versioned persistence, migrations, validation, and downgrade protection.
|
|
18
|
+
- Add SSR scopes and safe dehydrate/hydrate helpers.
|
|
19
|
+
- Add optional cross-tab sync for persisted models.
|
|
20
|
+
- Add lifecycle events and `@kamod-ch/signals/devtools` logger.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Klaus Zahiragic, Kamod GmbH
|
|
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,54 +1,85 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/kamod-ch/signals/main/assets/readme-banner.svg" alt="Kamod Signals" width="304" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@kamod-ch/signals"><img src="https://img.shields.io/npm/v/%40kamod-ch%2Fsignals" alt="npm version" /></a>
|
|
7
|
+
<a href="https://github.com/kamod-ch/kamod-signals/actions/workflows/gh-pages.yml"><img src="https://github.com/kamod-ch/kamod-signals/actions/workflows/gh-pages.yml/badge.svg" alt="Docs deploy" /></a>
|
|
8
|
+
<a href="https://github.com/kamod-ch/signals/stargazers"><img src="https://img.shields.io/github/stars/kamod-ch/signals?style=social" alt="GitHub stars" /></a>
|
|
9
|
+
<a href="https://github.com/kamod-ch/signals/blob/main/packages/core/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT license" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<strong><a href="https://kamod-ch.github.io/kamod-signals/">Docs</a></strong> ·
|
|
14
|
+
<strong><a href="https://www.npmjs.com/package/@kamod-ch/signals">npm</a></strong> ·
|
|
15
|
+
<strong><a href="https://github.com/kamod-ch/signals">GitHub</a></strong> ·
|
|
16
|
+
<strong><a href="https://github.com/kamod-ch/signals/issues">Issues</a></strong>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
1
19
|
# @kamod-ch/signals
|
|
2
20
|
|
|
3
|
-
Persisted Preact signals for `localStorage`, `sessionStorage`, IndexedDB, cookies, and memory.
|
|
21
|
+
Persisted Preact signals and models for `localStorage`, `sessionStorage`, IndexedDB, cookies, and memory.
|
|
22
|
+
|
|
23
|
+
`@kamod-ch/signals` is a lightweight helper package for Preact apps that need reactive client state with durable storage. It is SSR-safe, supports cookie request contexts, request-local hydration scopes, cross-tab persisted model sync, and re-exports the official `@preact/signals` model/action APIs.
|
|
4
24
|
|
|
5
|
-
|
|
25
|
+
## Keywords
|
|
6
26
|
|
|
7
|
-
-
|
|
8
|
-
- npm: <https://www.npmjs.com/package/@kamod-ch/signals>
|
|
9
|
-
- Repository: <https://github.com/kamod-ch/kamod-signals>
|
|
27
|
+
`preact` · `signals` · `preact-signals` · `state-management` · `persistent-state` · `localstorage` · `sessionstorage` · `reactivity` · `typescript` · `vite` · `kamod`
|
|
10
28
|
|
|
11
|
-
##
|
|
29
|
+
## Installation
|
|
12
30
|
|
|
13
31
|
```bash
|
|
14
|
-
|
|
32
|
+
npm install @kamod-ch/signals @preact/signals preact
|
|
15
33
|
```
|
|
16
34
|
|
|
35
|
+
`@preact/signals` and `preact` are peer dependencies.
|
|
36
|
+
|
|
17
37
|
## Usage
|
|
18
38
|
|
|
39
|
+
### Basic persisted signal
|
|
40
|
+
|
|
19
41
|
```ts
|
|
20
|
-
import { persistedSignal
|
|
42
|
+
import { persistedSignal } from "@kamod-ch/signals";
|
|
21
43
|
|
|
22
44
|
export const theme = persistedSignal("theme", "dark", { storage: "local" });
|
|
45
|
+
```
|
|
23
46
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
47
|
+
### Preact hook
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { usePersistedSignal } from "@kamod-ch/signals";
|
|
28
51
|
|
|
29
|
-
function Sidebar() {
|
|
52
|
+
export function Sidebar() {
|
|
30
53
|
const isOpen = usePersistedSignal("sidebar-open", true, { storage: "session" });
|
|
31
54
|
|
|
32
55
|
return <button onClick={() => (isOpen.value = !isOpen.value)}>Toggle</button>;
|
|
33
56
|
}
|
|
34
57
|
```
|
|
35
58
|
|
|
59
|
+
### Cookie-backed signal
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { persistedSignal } from "@kamod-ch/signals";
|
|
63
|
+
|
|
64
|
+
export const token = persistedSignal("token", "", {
|
|
65
|
+
storage: "cookie",
|
|
66
|
+
cookie: { expires: 7, path: "/" },
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
36
70
|
## API
|
|
37
71
|
|
|
38
72
|
```ts
|
|
39
|
-
persistedSignal<T>(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
usePersistedSignal<T>(
|
|
46
|
-
key: string,
|
|
47
|
-
initialValue: T,
|
|
48
|
-
options?: PersistedSignalOptions<T>
|
|
49
|
-
): PersistedSignal<T>;
|
|
73
|
+
persistedSignal<T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>): PersistedSignal<T>
|
|
74
|
+
usePersistedSignal<T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>): PersistedSignal<T>
|
|
75
|
+
createPersistedModel(options, factory)
|
|
76
|
+
createPersistedScope(options?)
|
|
77
|
+
dehydratePersisted(scope)
|
|
78
|
+
hydratePersisted(state)
|
|
50
79
|
```
|
|
51
80
|
|
|
81
|
+
Official Preact Signals primitives such as `signal`, `computed`, `effect`, `batch`, `untracked`, `createModel`, `action`, and `useModel` are re-exported.
|
|
82
|
+
|
|
52
83
|
```ts
|
|
53
84
|
type PersistedSignalOptions<T> = {
|
|
54
85
|
storage?: "local" | "session" | "indexeddb" | "cookie" | "memory";
|
|
@@ -68,6 +99,8 @@ type PersistedSignalOptions<T> = {
|
|
|
68
99
|
};
|
|
69
100
|
```
|
|
70
101
|
|
|
102
|
+
All persisted signals keep the normal signal shape, so values are read and written via `.value`.
|
|
103
|
+
|
|
71
104
|
## SSR cookie context
|
|
72
105
|
|
|
73
106
|
Use `createCookieContext()` when a cookie-backed value must be available during server-side rendering.
|
|
@@ -87,12 +120,22 @@ const theme = persistedSignal("theme", "dark", {
|
|
|
87
120
|
});
|
|
88
121
|
```
|
|
89
122
|
|
|
123
|
+
## Storage drivers
|
|
124
|
+
|
|
125
|
+
- `local`: persists to `localStorage` in the browser.
|
|
126
|
+
- `session`: persists to `sessionStorage` in the browser.
|
|
127
|
+
- `indexeddb`: persists asynchronously to IndexedDB.
|
|
128
|
+
- `cookie`: persists to browser cookies or an SSR `cookieContext`.
|
|
129
|
+
- `memory`: keeps values in memory only.
|
|
130
|
+
|
|
90
131
|
## Notes
|
|
91
132
|
|
|
92
133
|
- SSR-safe: browser-only storage is never touched on the server.
|
|
134
|
+
- Cookie SSR works when you pass `cookieContext`, and caching stays scoped to that request context.
|
|
93
135
|
- `local`, `session`, and `indexeddb` fall back to in-memory storage when unavailable.
|
|
94
136
|
- IndexedDB hydration is async, so signals start with `initialValue` and update after the persisted value loads.
|
|
95
|
-
-
|
|
137
|
+
- `persistedSignal()` returns the same signal for the same global identity (`storage + key`, with `cookieContext` scoping for SSR cookies).
|
|
138
|
+
- Reusing that identity requires the same effective options; conflicting options throw instead of being ignored.
|
|
96
139
|
|
|
97
140
|
## Publishing
|
|
98
141
|
|
|
@@ -101,3 +144,10 @@ This package is published from `packages/core` only:
|
|
|
101
144
|
```bash
|
|
102
145
|
pnpm publish:core
|
|
103
146
|
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
Built by Klaus Zahiragic | Kamod GmbH
|
|
151
|
+
|
|
152
|
+
[Website](https://www.kamod.ch) ·
|
|
153
|
+
[LinkedIn](https://www.linkedin.com/in/klauszahiragic/)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Model, type ModelConstructor, type ReadonlySignal } from "@preact/signals";
|
|
2
|
+
import { type PersistedEventTarget } from "./events";
|
|
3
|
+
import { type PersistedScope } from "./ssr";
|
|
4
|
+
import { type PersistedSyncTransport } from "./sync";
|
|
5
|
+
import type { CookieContext, CookieOptions, IndexedDBOptions, PersistedStorage } from "./types";
|
|
6
|
+
import { type MaybePromise, type MigrationErrorStrategy } from "./versioning";
|
|
7
|
+
export type HydrationStatus = "idle" | "loading" | "ready" | "error";
|
|
8
|
+
export type { MaybePromise } from "./versioning";
|
|
9
|
+
export interface PersistedModelOptions<TModel extends object, TSnapshot> {
|
|
10
|
+
key: string;
|
|
11
|
+
storage?: PersistedStorage;
|
|
12
|
+
serialize?: (snapshot: TSnapshot) => string;
|
|
13
|
+
deserialize?: (raw: string) => TSnapshot;
|
|
14
|
+
select: (model: Model<TModel>) => TSnapshot;
|
|
15
|
+
apply: (model: Model<TModel>, snapshot: TSnapshot) => MaybePromise<void>;
|
|
16
|
+
sync?: boolean | "tabs" | PersistedSyncTransport<TSnapshot>;
|
|
17
|
+
cookie?: CookieOptions;
|
|
18
|
+
cookieContext?: CookieContext;
|
|
19
|
+
indexedDB?: IndexedDBOptions;
|
|
20
|
+
version?: number;
|
|
21
|
+
migrate?: (snapshot: unknown, fromVersion: number) => MaybePromise<TSnapshot>;
|
|
22
|
+
validate?: (snapshot: unknown) => snapshot is TSnapshot;
|
|
23
|
+
migrationErrorStrategy?: MigrationErrorStrategy;
|
|
24
|
+
legacyVersion?: number;
|
|
25
|
+
scope?: PersistedScope;
|
|
26
|
+
events?: PersistedEventTarget<TSnapshot>;
|
|
27
|
+
includeEventSnapshots?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface PersistedModelControls {
|
|
30
|
+
readonly hydration: ReadonlySignal<HydrationStatus>;
|
|
31
|
+
readonly error: ReadonlySignal<unknown | null>;
|
|
32
|
+
hydrate(): Promise<void>;
|
|
33
|
+
flush(): Promise<void>;
|
|
34
|
+
reset(): Promise<void>;
|
|
35
|
+
dispose(): void;
|
|
36
|
+
}
|
|
37
|
+
export type PersistedModel<TModel extends object> = Model<TModel & PersistedModelControls>;
|
|
38
|
+
export declare const createPersistedModel: <TModel extends object, TSnapshot, TArgs extends unknown[]>(options: PersistedModelOptions<TModel, TSnapshot>, factory: (...args: TArgs) => TModel) => ModelConstructor<TModel & PersistedModelControls, TArgs>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PersistedEventTarget } from "./events";
|
|
2
|
+
export interface PersistedDevLoggerOptions {
|
|
3
|
+
includeSnapshots?: boolean;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
console?: Pick<Console, "debug" | "error">;
|
|
6
|
+
}
|
|
7
|
+
export declare const createPersistedDevLogger: <TSnapshot = unknown>(options?: PersistedDevLoggerOptions) => PersistedEventTarget<TSnapshot>;
|
package/dist/devtools.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/devtools.ts
|
|
2
|
+
var createPersistedDevLogger = (options = {}) => {
|
|
3
|
+
const logger = options.console ?? console;
|
|
4
|
+
const enabled = options.enabled ?? process.env.NODE_ENV !== "production";
|
|
5
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
const emit = (event) => {
|
|
7
|
+
const safeEvent = options.includeSnapshots ? event : { ...event, snapshot: event.snapshot === void 0 ? void 0 : "[redacted]" };
|
|
8
|
+
if (enabled) {
|
|
9
|
+
const log = safeEvent.type.endsWith(":error") || safeEvent.type === "sync:reject" ? logger.error : logger.debug;
|
|
10
|
+
log.call(logger, "[kamod-signals]", safeEvent.type, safeEvent);
|
|
11
|
+
}
|
|
12
|
+
for (const listener of listeners) {
|
|
13
|
+
try {
|
|
14
|
+
listener(safeEvent);
|
|
15
|
+
} catch {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
subscribe(listener) {
|
|
21
|
+
listeners.add(listener);
|
|
22
|
+
return () => listeners.delete(listener);
|
|
23
|
+
},
|
|
24
|
+
emit,
|
|
25
|
+
dispose() {
|
|
26
|
+
listeners.clear();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { createPersistedDevLogger };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type StorageDriver } from "./shared";
|
|
2
|
+
import type { CookieContext, CookieOptions, PersistedSignalOptions, PersistedStorage } from "./types";
|
|
3
|
+
export declare const serializeCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
4
|
+
type CookieContextInput = {
|
|
5
|
+
cookie?: string | Headers | null;
|
|
6
|
+
onSetCookie?: (header: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const createCookieContext: (input?: string | Headers | CookieContextInput) => CookieContext;
|
|
9
|
+
export declare const drivers: Record<PersistedStorage, StorageDriver>;
|
|
10
|
+
export declare const resolveDriver: (storage: PersistedStorage, options?: PersistedSignalOptions<unknown>) => StorageDriver;
|
|
11
|
+
export {};
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type PersistedEventType = "hydrate:start" | "hydrate:success" | "hydrate:error" | "persist:start" | "persist:success" | "persist:error" | "migrate:start" | "migrate:success" | "migrate:error" | "sync:receive" | "sync:reject" | "reset" | "dispose";
|
|
2
|
+
export interface PersistedEvent<TSnapshot = unknown> {
|
|
3
|
+
type: PersistedEventType;
|
|
4
|
+
key: string;
|
|
5
|
+
storage?: string;
|
|
6
|
+
timestamp: number;
|
|
7
|
+
snapshot?: TSnapshot | "[redacted]";
|
|
8
|
+
error?: unknown;
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export type PersistedEventListener<TSnapshot = unknown> = (event: PersistedEvent<TSnapshot>) => void;
|
|
12
|
+
export type PersistedEventUnsubscribe = () => void;
|
|
13
|
+
export interface PersistedEventTarget<TSnapshot = unknown> {
|
|
14
|
+
subscribe(listener: PersistedEventListener<TSnapshot>): PersistedEventUnsubscribe;
|
|
15
|
+
emit(event: PersistedEvent<TSnapshot>): void;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
}
|
|
18
|
+
export interface PersistedEventOptions<TSnapshot = unknown> {
|
|
19
|
+
includeSnapshots?: boolean;
|
|
20
|
+
onListenerError?: (error: unknown, event: PersistedEvent<TSnapshot>) => void;
|
|
21
|
+
}
|
|
22
|
+
export declare const createPersistedEventTarget: <TSnapshot = unknown>(options?: PersistedEventOptions<TSnapshot>) => PersistedEventTarget<TSnapshot>;
|
|
23
|
+
export declare const emitPersistedEvent: <TSnapshot>(target: PersistedEventTarget<TSnapshot> | undefined, event: Omit<PersistedEvent<TSnapshot>, "timestamp">) => void;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Signal, action, batch, computed, createModel, effect, signal, untracked, useModel, } from "@preact/signals";
|
|
2
|
+
export type { Model, ModelConstructor, ReadonlySignal } from "@preact/signals";
|
|
3
|
+
export type { CookieContext, CookieOptions, IndexedDBOptions, PersistedSignal, PersistedSignalOptions, PersistedStorage, } from "./types";
|
|
4
|
+
export { createCookieContext, serializeCookie } from "./drivers";
|
|
5
|
+
export { createPersistedEventTarget, emitPersistedEvent, type PersistedEvent, type PersistedEventListener, type PersistedEventOptions, type PersistedEventTarget, type PersistedEventType, type PersistedEventUnsubscribe, } from "./events";
|
|
6
|
+
export { createPersistedModel } from "./createPersistedModel";
|
|
7
|
+
export type { HydrationStatus, MaybePromise, PersistedModel, PersistedModelControls, PersistedModelOptions, } from "./createPersistedModel";
|
|
8
|
+
export { persistedSignal } from "./persistedSignal";
|
|
9
|
+
export { createPersistedScope, dehydratePersisted, hydratePersisted, serializePersistedStateForHtml, type PersistedDehydratedState, type PersistedScope, type PersistedScopeOptions, } from "./ssr";
|
|
10
|
+
export { comparePersistedSyncMessages, createBroadcastSyncTransport, createMemorySyncTransport, type PersistedSyncMessage, type PersistedSyncTransport, } from "./sync";
|
|
11
|
+
export { FuturePersistedVersionError, PersistedValidationError, isPersistedEnvelope, type MigrationErrorStrategy, type PersistedEnvelope, } from "./versioning";
|
|
12
|
+
export { usePersistedSignal } from "./usePersistedSignal";
|