@kamod-ch/signals 1.0.3 → 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/README.md +8 -2
- 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 +21 -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/src/persistedSignal.ts
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import { effect, signal } from "@preact/signals";
|
|
2
|
-
import { drivers, resolveDriver } from "./drivers";
|
|
3
|
-
import {
|
|
4
|
-
defaultDeserialize,
|
|
5
|
-
defaultSerialize,
|
|
6
|
-
registryKey,
|
|
7
|
-
resolveStorageType,
|
|
8
|
-
type PersistedSignalController,
|
|
9
|
-
} from "./shared";
|
|
10
|
-
import type { CookieOptions, PersistedSignal, PersistedSignalOptions, PersistedStorage } from "./types";
|
|
11
|
-
|
|
12
|
-
type NormalizedCookieOptions = {
|
|
13
|
-
expires: string | number | undefined;
|
|
14
|
-
path: string;
|
|
15
|
-
domain: string | undefined;
|
|
16
|
-
secure: boolean | undefined;
|
|
17
|
-
sameSite: CookieOptions["sameSite"] | undefined;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type CompatibilitySnapshot = {
|
|
21
|
-
storage: PersistedStorage;
|
|
22
|
-
serialize: unknown;
|
|
23
|
-
deserialize: unknown;
|
|
24
|
-
sync: boolean;
|
|
25
|
-
removeOnUndefined: boolean;
|
|
26
|
-
cookie: NormalizedCookieOptions | undefined;
|
|
27
|
-
indexedDB:
|
|
28
|
-
| {
|
|
29
|
-
database: string;
|
|
30
|
-
store: string;
|
|
31
|
-
version: number;
|
|
32
|
-
}
|
|
33
|
-
| undefined;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
type RegistryEntry = {
|
|
37
|
-
controller: PersistedSignalController<unknown>;
|
|
38
|
-
snapshot: CompatibilitySnapshot;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const globalRegistry = new Map<string, RegistryEntry>();
|
|
42
|
-
const cookieContextRegistry = new WeakMap<object, Map<string, RegistryEntry>>();
|
|
43
|
-
const UNDEFINED_TOKEN = "__KAMOD_SIGNALS_UNDEFINED__";
|
|
44
|
-
|
|
45
|
-
const isPromiseLike = <T>(value: unknown): value is Promise<T> =>
|
|
46
|
-
typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
47
|
-
|
|
48
|
-
const parseStoredValue = <T>(raw: string | null, initialValue: T, deserialize: (raw: string) => T): T => {
|
|
49
|
-
if (raw === null) {
|
|
50
|
-
return initialValue;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (raw === UNDEFINED_TOKEN) {
|
|
54
|
-
return undefined as T;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
return deserialize(raw);
|
|
59
|
-
} catch {
|
|
60
|
-
return initialValue;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const readInitialValue = <T>(
|
|
65
|
-
key: string,
|
|
66
|
-
initialValue: T,
|
|
67
|
-
options: PersistedSignalOptions<T>,
|
|
68
|
-
driver = resolveDriver(resolveStorageType(options.storage), options as PersistedSignalOptions<unknown>),
|
|
69
|
-
): T => {
|
|
70
|
-
const deserialize = options.deserialize ?? defaultDeserialize<T>;
|
|
71
|
-
const raw = driver.get(key, options as PersistedSignalOptions<unknown>);
|
|
72
|
-
|
|
73
|
-
if (isPromiseLike<string | null>(raw)) {
|
|
74
|
-
return initialValue;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return parseStoredValue(raw, initialValue, deserialize);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const normalizeCookieOptions = (cookie?: CookieOptions): NormalizedCookieOptions => ({
|
|
81
|
-
expires: cookie?.expires instanceof Date ? cookie.expires.toUTCString() : cookie?.expires,
|
|
82
|
-
path: cookie?.path ?? "/",
|
|
83
|
-
domain: cookie?.domain,
|
|
84
|
-
secure: cookie?.secure,
|
|
85
|
-
sameSite: cookie?.sameSite,
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const createCompatibilitySnapshot = <T>(options: PersistedSignalOptions<T>): CompatibilitySnapshot => {
|
|
89
|
-
const storage = resolveStorageType(options.storage);
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
storage,
|
|
93
|
-
serialize: options.serialize ?? defaultSerialize<T>,
|
|
94
|
-
deserialize: options.deserialize ?? defaultDeserialize<T>,
|
|
95
|
-
sync: options.sync ?? true,
|
|
96
|
-
removeOnUndefined: options.removeOnUndefined ?? true,
|
|
97
|
-
cookie: storage === "cookie" ? normalizeCookieOptions(options.cookie) : undefined,
|
|
98
|
-
indexedDB:
|
|
99
|
-
storage === "indexeddb"
|
|
100
|
-
? {
|
|
101
|
-
database: options.indexedDB?.database ?? "@kamod-ch/signals",
|
|
102
|
-
store: options.indexedDB?.store ?? "signals",
|
|
103
|
-
version: options.indexedDB?.version ?? 1,
|
|
104
|
-
}
|
|
105
|
-
: undefined,
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const snapshotsMatch = (left: CompatibilitySnapshot, right: CompatibilitySnapshot) =>
|
|
110
|
-
left.storage === right.storage &&
|
|
111
|
-
left.serialize === right.serialize &&
|
|
112
|
-
left.deserialize === right.deserialize &&
|
|
113
|
-
left.sync === right.sync &&
|
|
114
|
-
left.removeOnUndefined === right.removeOnUndefined &&
|
|
115
|
-
JSON.stringify(left.cookie) === JSON.stringify(right.cookie) &&
|
|
116
|
-
JSON.stringify(left.indexedDB) === JSON.stringify(right.indexedDB);
|
|
117
|
-
|
|
118
|
-
const createOptionConflictError = (id: string) =>
|
|
119
|
-
new Error(`persistedSignal(${id}) received conflicting options for an existing global signal`);
|
|
120
|
-
|
|
121
|
-
const getCompatibleEntry = <T>(entry: RegistryEntry | undefined, snapshot: CompatibilitySnapshot, id: string) => {
|
|
122
|
-
if (!entry) {
|
|
123
|
-
return undefined;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (!snapshotsMatch(entry.snapshot, snapshot)) {
|
|
127
|
-
throw createOptionConflictError(id);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return entry.controller as PersistedSignalController<T>;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const createController = <T>(
|
|
134
|
-
key: string,
|
|
135
|
-
initialValue: T,
|
|
136
|
-
options: PersistedSignalOptions<T> = {},
|
|
137
|
-
): PersistedSignalController<T> => {
|
|
138
|
-
const storage = resolveStorageType(options.storage);
|
|
139
|
-
const driver = resolveDriver(storage, options as PersistedSignalOptions<unknown>);
|
|
140
|
-
const serialize = options.serialize ?? defaultSerialize<T>;
|
|
141
|
-
const deserialize = options.deserialize ?? defaultDeserialize<T>;
|
|
142
|
-
const removeOnUndefined = options.removeOnUndefined ?? true;
|
|
143
|
-
const state = signal<T>(readInitialValue(key, initialValue, options, driver)) as PersistedSignal<T>;
|
|
144
|
-
let isApplyingExternalValue = false;
|
|
145
|
-
let isHydrating = Boolean(driver.async);
|
|
146
|
-
let observedValue = state.value;
|
|
147
|
-
let hasPendingHydrationChange = false;
|
|
148
|
-
|
|
149
|
-
const persistValue = (value: T) => {
|
|
150
|
-
try {
|
|
151
|
-
if (value === undefined && removeOnUndefined) {
|
|
152
|
-
return driver.remove(key, options as PersistedSignalOptions<unknown>);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return driver.set(
|
|
156
|
-
key,
|
|
157
|
-
value === undefined ? UNDEFINED_TOKEN : serialize(value),
|
|
158
|
-
options as PersistedSignalOptions<unknown>,
|
|
159
|
-
);
|
|
160
|
-
} catch {
|
|
161
|
-
return undefined;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
state.clear = () => {
|
|
166
|
-
isApplyingExternalValue = true;
|
|
167
|
-
driver.remove(key, options as PersistedSignalOptions<unknown>);
|
|
168
|
-
state.value = initialValue;
|
|
169
|
-
observedValue = state.value;
|
|
170
|
-
isApplyingExternalValue = false;
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
state.reset = () => {
|
|
174
|
-
state.value = initialValue;
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
const stopEffect = effect(() => {
|
|
178
|
-
const value = state.value;
|
|
179
|
-
|
|
180
|
-
if (isApplyingExternalValue) {
|
|
181
|
-
observedValue = value;
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (isHydrating) {
|
|
186
|
-
if (!Object.is(value, observedValue)) {
|
|
187
|
-
hasPendingHydrationChange = true;
|
|
188
|
-
}
|
|
189
|
-
observedValue = value;
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
observedValue = value;
|
|
194
|
-
persistValue(value);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
if (driver.async) {
|
|
198
|
-
Promise.resolve(driver.get(key, options as PersistedSignalOptions<unknown>))
|
|
199
|
-
.then((raw) => {
|
|
200
|
-
if (!hasPendingHydrationChange) {
|
|
201
|
-
const nextValue = parseStoredValue(raw, initialValue, deserialize);
|
|
202
|
-
if (!Object.is(nextValue, state.value)) {
|
|
203
|
-
isApplyingExternalValue = true;
|
|
204
|
-
state.value = nextValue;
|
|
205
|
-
observedValue = nextValue;
|
|
206
|
-
isApplyingExternalValue = false;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
isHydrating = false;
|
|
211
|
-
|
|
212
|
-
if (hasPendingHydrationChange) {
|
|
213
|
-
persistValue(state.value);
|
|
214
|
-
}
|
|
215
|
-
})
|
|
216
|
-
.catch(() => {
|
|
217
|
-
isHydrating = false;
|
|
218
|
-
if (hasPendingHydrationChange) {
|
|
219
|
-
persistValue(state.value);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const applyExternalRaw = (raw: string | null) => {
|
|
225
|
-
const nextValue = parseStoredValue(raw, initialValue, deserialize);
|
|
226
|
-
|
|
227
|
-
if (Object.is(nextValue, state.value)) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
isApplyingExternalValue = true;
|
|
232
|
-
state.value = nextValue;
|
|
233
|
-
observedValue = nextValue;
|
|
234
|
-
isApplyingExternalValue = false;
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
const stopSync = options.sync === false || !driver.subscribe
|
|
238
|
-
? () => {}
|
|
239
|
-
: driver.subscribe(key, () => {
|
|
240
|
-
try {
|
|
241
|
-
const raw = driver.get(key, options as PersistedSignalOptions<unknown>);
|
|
242
|
-
|
|
243
|
-
if (isPromiseLike<string | null>(raw)) {
|
|
244
|
-
raw
|
|
245
|
-
.then((value) => {
|
|
246
|
-
applyExternalRaw(value);
|
|
247
|
-
})
|
|
248
|
-
.catch(() => {
|
|
249
|
-
// ignore malformed external updates
|
|
250
|
-
});
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
applyExternalRaw(raw);
|
|
255
|
-
} catch {
|
|
256
|
-
// ignore malformed external updates
|
|
257
|
-
}
|
|
258
|
-
}, options as PersistedSignalOptions<unknown>);
|
|
259
|
-
|
|
260
|
-
return {
|
|
261
|
-
signal: state,
|
|
262
|
-
dispose() {
|
|
263
|
-
stopSync();
|
|
264
|
-
stopEffect();
|
|
265
|
-
},
|
|
266
|
-
};
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
export const persistedSignal = <T>(
|
|
270
|
-
key: string,
|
|
271
|
-
initialValue: T,
|
|
272
|
-
options: PersistedSignalOptions<T> = {},
|
|
273
|
-
): PersistedSignal<T> => {
|
|
274
|
-
const storage = resolveStorageType(options.storage);
|
|
275
|
-
const id = registryKey(storage, key);
|
|
276
|
-
const snapshot = createCompatibilitySnapshot(options);
|
|
277
|
-
const context = storage === "cookie" ? options.cookieContext : undefined;
|
|
278
|
-
|
|
279
|
-
if (context) {
|
|
280
|
-
let scopedRegistry = cookieContextRegistry.get(context as object);
|
|
281
|
-
if (!scopedRegistry) {
|
|
282
|
-
scopedRegistry = new Map<string, RegistryEntry>();
|
|
283
|
-
cookieContextRegistry.set(context as object, scopedRegistry);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const existing = getCompatibleEntry<T>(scopedRegistry.get(id), snapshot, id);
|
|
287
|
-
if (existing) {
|
|
288
|
-
return existing.signal;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const controller = createController(key, initialValue, options);
|
|
292
|
-
scopedRegistry.set(id, { controller: controller as PersistedSignalController<unknown>, snapshot });
|
|
293
|
-
return controller.signal;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const existing = getCompatibleEntry<T>(globalRegistry.get(id), snapshot, id);
|
|
297
|
-
if (existing) {
|
|
298
|
-
return existing.signal;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const controller = createController(key, initialValue, options);
|
|
302
|
-
globalRegistry.set(id, { controller: controller as PersistedSignalController<unknown>, snapshot });
|
|
303
|
-
return controller.signal;
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
export const createPersistedSignal = createController;
|
|
307
|
-
export const __private__ = { createController, readInitialValue, drivers, globalRegistry };
|
package/src/shared.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { PersistedSignal, PersistedSignalOptions, PersistedStorage } from "./types";
|
|
2
|
-
|
|
3
|
-
export interface StorageDriver {
|
|
4
|
-
readonly type: PersistedStorage;
|
|
5
|
-
readonly async?: boolean;
|
|
6
|
-
isAvailable(options?: PersistedSignalOptions<unknown>): boolean;
|
|
7
|
-
get(key: string, options?: PersistedSignalOptions<unknown>): string | null | Promise<string | null>;
|
|
8
|
-
set(key: string, value: string, options?: PersistedSignalOptions<unknown>): void | Promise<void>;
|
|
9
|
-
remove(key: string, options?: PersistedSignalOptions<unknown>): void | Promise<void>;
|
|
10
|
-
subscribe?(key: string, callback: () => void, options?: PersistedSignalOptions<unknown>): () => void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface PersistedSignalController<T> {
|
|
14
|
-
signal: PersistedSignal<T>;
|
|
15
|
-
dispose: () => void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
19
|
-
|
|
20
|
-
export const defaultSerialize = <T>(value: T) => JSON.stringify(value);
|
|
21
|
-
export const defaultDeserialize = <T>(raw: string) => JSON.parse(raw) as T;
|
|
22
|
-
|
|
23
|
-
export const resolveStorageType = (storage?: PersistedStorage): PersistedStorage => storage ?? "local";
|
|
24
|
-
|
|
25
|
-
export const registryKey = (storage: PersistedStorage, key: string) => `${storage}:${key}`;
|
package/src/types.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { Signal } from "@preact/signals";
|
|
2
|
-
|
|
3
|
-
export type PersistedStorage = "local" | "session" | "cookie" | "memory" | "indexeddb";
|
|
4
|
-
|
|
5
|
-
export interface CookieOptions {
|
|
6
|
-
expires?: number | Date;
|
|
7
|
-
path?: string;
|
|
8
|
-
domain?: string;
|
|
9
|
-
secure?: boolean;
|
|
10
|
-
sameSite?: "Lax" | "Strict" | "None";
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface CookieContext {
|
|
14
|
-
get(key: string): string | null;
|
|
15
|
-
set(key: string, value: string, options?: CookieOptions): void;
|
|
16
|
-
remove(key: string, options?: CookieOptions): void;
|
|
17
|
-
subscribe?(key: string, callback: () => void): () => void;
|
|
18
|
-
toSetCookieHeaders?(): string[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface IndexedDBOptions {
|
|
22
|
-
database?: string;
|
|
23
|
-
store?: string;
|
|
24
|
-
version?: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface PersistedSignalOptions<T> {
|
|
28
|
-
storage?: PersistedStorage;
|
|
29
|
-
serialize?: (value: T) => string;
|
|
30
|
-
deserialize?: (raw: string) => T;
|
|
31
|
-
sync?: boolean;
|
|
32
|
-
removeOnUndefined?: boolean;
|
|
33
|
-
cookie?: CookieOptions;
|
|
34
|
-
cookieContext?: CookieContext;
|
|
35
|
-
indexedDB?: IndexedDBOptions;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface PersistedSignal<T> extends Signal<T> {
|
|
39
|
-
clear(): void;
|
|
40
|
-
reset(): void;
|
|
41
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { useEffect, useMemo } from "preact/hooks";
|
|
2
|
-
import { createPersistedSignal } from "./persistedSignal";
|
|
3
|
-
import { resolveStorageType } from "./shared";
|
|
4
|
-
import type { PersistedSignal, PersistedSignalOptions } from "./types";
|
|
5
|
-
|
|
6
|
-
const cookieDependencyKey = (options: PersistedSignalOptions<unknown>) => {
|
|
7
|
-
const cookie = options.cookie;
|
|
8
|
-
if (!cookie) {
|
|
9
|
-
return "";
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return JSON.stringify([
|
|
13
|
-
cookie.expires instanceof Date ? cookie.expires.toUTCString() : cookie.expires,
|
|
14
|
-
cookie.path,
|
|
15
|
-
cookie.domain,
|
|
16
|
-
cookie.secure,
|
|
17
|
-
cookie.sameSite,
|
|
18
|
-
]);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const indexedDbDependencyKey = (options: PersistedSignalOptions<unknown>) =>
|
|
22
|
-
JSON.stringify([
|
|
23
|
-
options.indexedDB?.database ?? "@kamod-ch/signals",
|
|
24
|
-
options.indexedDB?.store ?? "signals",
|
|
25
|
-
options.indexedDB?.version ?? 1,
|
|
26
|
-
]);
|
|
27
|
-
|
|
28
|
-
export const usePersistedSignal = <T>(
|
|
29
|
-
key: string,
|
|
30
|
-
initialValue: T,
|
|
31
|
-
options: PersistedSignalOptions<T> = {},
|
|
32
|
-
): PersistedSignal<T> => {
|
|
33
|
-
const controller = useMemo(
|
|
34
|
-
() => createPersistedSignal(key, initialValue, options),
|
|
35
|
-
[
|
|
36
|
-
key,
|
|
37
|
-
initialValue,
|
|
38
|
-
resolveStorageType(options.storage),
|
|
39
|
-
options.sync,
|
|
40
|
-
options.serialize,
|
|
41
|
-
options.deserialize,
|
|
42
|
-
options.removeOnUndefined,
|
|
43
|
-
options.cookieContext,
|
|
44
|
-
cookieDependencyKey(options as PersistedSignalOptions<unknown>),
|
|
45
|
-
indexedDbDependencyKey(options as PersistedSignalOptions<unknown>),
|
|
46
|
-
],
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
useEffect(() => () => controller.dispose(), [controller]);
|
|
50
|
-
|
|
51
|
-
return controller.signal;
|
|
52
|
-
};
|