@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.
@@ -0,0 +1,38 @@
1
+ import { type PersistedSignalController } from "./shared";
2
+ import type { CookieOptions, PersistedSignal, PersistedSignalOptions, PersistedStorage } from "./types";
3
+ type NormalizedCookieOptions = {
4
+ expires: string | number | undefined;
5
+ path: string;
6
+ domain: string | undefined;
7
+ secure: boolean | undefined;
8
+ sameSite: CookieOptions["sameSite"] | undefined;
9
+ };
10
+ type CompatibilitySnapshot = {
11
+ storage: PersistedStorage;
12
+ serialize: unknown;
13
+ deserialize: unknown;
14
+ sync: boolean;
15
+ removeOnUndefined: boolean;
16
+ cookie: NormalizedCookieOptions | undefined;
17
+ indexedDB: {
18
+ database: string;
19
+ store: string;
20
+ version: number;
21
+ } | undefined;
22
+ version: number | undefined;
23
+ };
24
+ type RegistryEntry = {
25
+ controller: PersistedSignalController<unknown>;
26
+ snapshot: CompatibilitySnapshot;
27
+ };
28
+ declare const readInitialValue: <T>(key: string, initialValue: T, options: PersistedSignalOptions<T>, driver?: import("./shared").StorageDriver) => T;
29
+ declare const createController: <T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>) => PersistedSignalController<T>;
30
+ export declare const persistedSignal: <T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>) => PersistedSignal<T>;
31
+ export declare const createPersistedSignal: typeof createController;
32
+ export declare const __private__: {
33
+ createController: typeof createController;
34
+ readInitialValue: typeof readInitialValue;
35
+ drivers: Record<PersistedStorage, import("./shared").StorageDriver>;
36
+ globalRegistry: Map<string, RegistryEntry>;
37
+ };
38
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { PersistedSignal, PersistedSignalOptions, PersistedStorage } from "./types";
2
+ export interface StorageDriver {
3
+ readonly type: PersistedStorage;
4
+ readonly async?: boolean;
5
+ isAvailable(options?: PersistedSignalOptions<unknown>): boolean;
6
+ get(key: string, options?: PersistedSignalOptions<unknown>): string | null | Promise<string | null>;
7
+ set(key: string, value: string, options?: PersistedSignalOptions<unknown>): void | Promise<void>;
8
+ remove(key: string, options?: PersistedSignalOptions<unknown>): void | Promise<void>;
9
+ subscribe?(key: string, callback: () => void, options?: PersistedSignalOptions<unknown>): () => void;
10
+ }
11
+ export interface PersistedSignalController<T> {
12
+ signal: PersistedSignal<T>;
13
+ dispose: () => void;
14
+ }
15
+ export declare const isBrowser: boolean;
16
+ export declare const defaultSerialize: <T>(value: T) => string;
17
+ export declare const defaultDeserialize: <T>(raw: string) => T;
18
+ export declare const resolveStorageType: (storage?: PersistedStorage) => PersistedStorage;
19
+ export declare const registryKey: (storage: PersistedStorage, key: string) => string;
package/dist/ssr.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import type { CookieContext } from "./types";
2
+ export type PersistedDehydratedState = Record<string, unknown>;
3
+ export interface PersistedScopeOptions {
4
+ cookieContext?: CookieContext;
5
+ initialState?: PersistedDehydratedState;
6
+ }
7
+ export interface PersistedScope {
8
+ readonly cookieContext: CookieContext | undefined;
9
+ get<T>(key: string): T | undefined;
10
+ set<T>(key: string, value: T): void;
11
+ dehydrate(): PersistedDehydratedState;
12
+ dispose(): void;
13
+ }
14
+ export declare const createPersistedScope: (options?: PersistedScopeOptions) => PersistedScope;
15
+ export declare const dehydratePersisted: (scope: PersistedScope) => PersistedDehydratedState;
16
+ export declare const hydratePersisted: (state: PersistedDehydratedState) => void;
17
+ export declare const consumeHydratedPersistedValue: <T>(key: string) => T | undefined;
18
+ export declare const serializePersistedStateForHtml: (state: PersistedDehydratedState) => string;
package/dist/sync.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export interface PersistedSyncMessage<TPayload = unknown> {
2
+ key: string;
3
+ source: string;
4
+ revision: number;
5
+ version?: number;
6
+ payload: TPayload;
7
+ }
8
+ export interface PersistedSyncTransport<TPayload = unknown> {
9
+ post(message: PersistedSyncMessage<TPayload>): void;
10
+ subscribe(listener: (message: PersistedSyncMessage<TPayload>) => void): () => void;
11
+ dispose(): void;
12
+ }
13
+ export declare const createPersistedSyncSource: () => string;
14
+ export declare const comparePersistedSyncMessages: (left: Pick<PersistedSyncMessage, "revision" | "source">, right: Pick<PersistedSyncMessage, "revision" | "source">) => number;
15
+ export declare const createBroadcastSyncTransport: (channel: string) => PersistedSyncTransport | null;
16
+ export declare const createMemorySyncTransport: <TPayload = unknown>(channel: string) => PersistedSyncTransport<TPayload>;
@@ -0,0 +1,43 @@
1
+ import type { Signal } from "@preact/signals";
2
+ import type { PersistedScope } from "./ssr";
3
+ import type { MaybePromise, MigrationErrorStrategy } from "./versioning";
4
+ export type PersistedStorage = "local" | "session" | "cookie" | "memory" | "indexeddb";
5
+ export interface CookieOptions {
6
+ expires?: number | Date;
7
+ path?: string;
8
+ domain?: string;
9
+ secure?: boolean;
10
+ sameSite?: "Lax" | "Strict" | "None";
11
+ }
12
+ export interface CookieContext {
13
+ get(key: string): string | null;
14
+ set(key: string, value: string, options?: CookieOptions): void;
15
+ remove(key: string, options?: CookieOptions): void;
16
+ subscribe?(key: string, callback: () => void): () => void;
17
+ toSetCookieHeaders?(): string[];
18
+ }
19
+ export interface IndexedDBOptions {
20
+ database?: string;
21
+ store?: string;
22
+ version?: number;
23
+ }
24
+ export interface PersistedSignalOptions<T> {
25
+ storage?: PersistedStorage;
26
+ serialize?: (value: T) => string;
27
+ deserialize?: (raw: string) => T;
28
+ sync?: boolean;
29
+ removeOnUndefined?: boolean;
30
+ cookie?: CookieOptions;
31
+ cookieContext?: CookieContext;
32
+ indexedDB?: IndexedDBOptions;
33
+ version?: number;
34
+ migrate?: (snapshot: unknown, fromVersion: number) => MaybePromise<T>;
35
+ validate?: (snapshot: unknown) => snapshot is T;
36
+ migrationErrorStrategy?: MigrationErrorStrategy;
37
+ legacyVersion?: number;
38
+ scope?: PersistedScope;
39
+ }
40
+ export interface PersistedSignal<T> extends Signal<T> {
41
+ clear(): void;
42
+ reset(): void;
43
+ }
@@ -0,0 +1,2 @@
1
+ import type { PersistedSignal, PersistedSignalOptions } from "./types";
2
+ export declare const usePersistedSignal: <T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>) => PersistedSignal<T>;
@@ -0,0 +1,32 @@
1
+ export type MaybePromise<T> = T | Promise<T>;
2
+ export type MigrationErrorStrategy = "preserve" | "reset" | "throw";
3
+ export interface PersistedVersioningOptions<T> {
4
+ version?: number;
5
+ migrate?: (snapshot: unknown, fromVersion: number) => MaybePromise<T>;
6
+ validate?: (snapshot: unknown) => snapshot is T;
7
+ migrationErrorStrategy?: MigrationErrorStrategy;
8
+ legacyVersion?: number;
9
+ }
10
+ export type PersistedEnvelope<T> = {
11
+ __kamod: "signals";
12
+ v: number;
13
+ data: T;
14
+ };
15
+ export declare class FuturePersistedVersionError extends Error {
16
+ readonly persistedVersion: number;
17
+ readonly supportedVersion: number;
18
+ constructor(persistedVersion: number, supportedVersion: number);
19
+ }
20
+ export declare class PersistedValidationError extends Error {
21
+ constructor();
22
+ }
23
+ export declare const isPersistedEnvelope: (value: unknown) => value is PersistedEnvelope<unknown>;
24
+ export declare const serializePersistedValue: <T>(value: T, options: PersistedVersioningOptions<T>, serialize: (value: T) => string) => string;
25
+ export declare const deserializePersistedValue: <T>(raw: string, options: PersistedVersioningOptions<T>, deserialize: (raw: string) => T) => {
26
+ value: T;
27
+ migrated: boolean;
28
+ };
29
+ export declare const deserializePersistedValueAsync: <T>(raw: string, options: PersistedVersioningOptions<T>, deserialize: (raw: string) => T) => Promise<{
30
+ value: T;
31
+ migrated: boolean;
32
+ }>;
package/package.json CHANGED
@@ -1,46 +1,63 @@
1
1
  {
2
2
  "name": "@kamod-ch/signals",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Persisted Preact signals with localStorage, sessionStorage, IndexedDB, cookie, and memory drivers.",
5
+ "keywords": [
6
+ "preact",
7
+ "signals",
8
+ "preact-signals",
9
+ "state-management",
10
+ "persistent-state",
11
+ "localstorage",
12
+ "sessionstorage",
13
+ "reactivity",
14
+ "typescript",
15
+ "vite",
16
+ "kamod"
17
+ ],
5
18
  "type": "module",
6
19
  "license": "MIT",
7
20
  "author": "Klaus Zahiragic <watzak>",
8
21
  "repository": {
9
22
  "type": "git",
10
- "url": "git+https://github.com/kamod-ch/kamod-signals.git",
23
+ "url": "https://github.com/kamod-ch/kamod-signals",
11
24
  "directory": "packages/core"
12
25
  },
13
26
  "publishConfig": {
14
27
  "access": "public"
15
28
  },
16
- "main": "./src/index.ts",
17
- "types": "./src/index.ts",
29
+ "main": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
18
31
  "exports": {
19
32
  ".": {
20
- "types": "./src/index.ts",
21
- "import": "./src/index.ts"
33
+ "types": "./dist/index.d.ts",
34
+ "import": "./dist/index.js"
35
+ },
36
+ "./devtools": {
37
+ "types": "./dist/devtools.d.ts",
38
+ "import": "./dist/devtools.js"
22
39
  }
23
40
  },
24
41
  "files": [
25
- "src",
26
- "!src/**/*.spec.ts"
42
+ "dist",
43
+ "CHANGELOG.md"
27
44
  ],
28
45
  "sideEffects": false,
29
46
  "peerDependencies": {
30
47
  "@preact/signals": "^2.0.0",
31
- "preact": "^10.0.0"
48
+ "preact": "^11.0.0"
32
49
  },
33
50
  "devDependencies": {
34
- "@preact/signals": "^2.9.2",
35
- "@types/node": "^26.1.0",
36
- "jsdom": "^29.1.1",
37
- "preact": "^10.29.4",
38
- "tsup": "^8.5.1",
39
- "typescript": "^6.0.3",
40
- "vitest": "^4.1.10"
51
+ "@preact/signals": "^2.11.4",
52
+ "@types/node": "^26.6.11",
53
+ "jsdom": "^31.0.1",
54
+ "preact": "^11.2.0",
55
+ "tsup": "^9.0.0",
56
+ "typescript": "^7.0.2",
57
+ "vitest": "^5.1.2"
41
58
  },
42
59
  "scripts": {
43
- "build": "tsup",
60
+ "build": "tsup && tsc -p tsconfig.build.json",
44
61
  "typecheck": "tsc --noEmit",
45
62
  "test": "vitest run",
46
63
  "test:watch": "vitest"