@warp-drive-mirror/experiments 0.2.7-alpha.15 → 0.2.7-alpha.16
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/README.md +1 -0
- package/declarations/storage/-private/reactive-map.d.ts +13 -0
- package/declarations/storage/-private/storage-infra.d.ts +104 -0
- package/declarations/storage/cache.d.ts +43 -0
- package/declarations/storage/storage-resource.d.ts +137 -0
- package/declarations/storage/storage.d.ts +88 -0
- package/declarations/storage.d.ts +7 -0
- package/dist/storage.js +1144 -0
- package/dist/unpkg/dev/storage.js +1169 -0
- package/dist/unpkg/dev-deprecated/storage.js +1169 -0
- package/dist/unpkg/prod/storage.js +1073 -0
- package/dist/unpkg/prod-deprecated/storage.js +1073 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reactive wrapper around the browser's Map API that provides
|
|
3
|
+
* granular per-key reactivity via WarpDrive's signal system.
|
|
4
|
+
*/
|
|
5
|
+
export declare class SignalMap<K extends string> {
|
|
6
|
+
private _map;
|
|
7
|
+
private _size;
|
|
8
|
+
private _signals;
|
|
9
|
+
subscribe(key: K): boolean;
|
|
10
|
+
notify(key: K): void;
|
|
11
|
+
clear(): void;
|
|
12
|
+
get size(): number;
|
|
13
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for fields that are also query parameters
|
|
3
|
+
*/
|
|
4
|
+
export interface ParamConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Convert a value into a string for storage in the URL.
|
|
7
|
+
* `null` indicates the value should be omitted from the URL.
|
|
8
|
+
*/
|
|
9
|
+
serialize: (value: unknown, instance: any) => string | null;
|
|
10
|
+
/**
|
|
11
|
+
* Convert a string value from the URL back into
|
|
12
|
+
* its original type
|
|
13
|
+
*/
|
|
14
|
+
deserialize: (urlValue: string, instance: any) => unknown;
|
|
15
|
+
/**
|
|
16
|
+
* Get the default value for this param from the given instance.
|
|
17
|
+
*
|
|
18
|
+
* If not present, the value passed to the field initializer
|
|
19
|
+
* will be used as the default.
|
|
20
|
+
*
|
|
21
|
+
* This should return the value in the field's native type,
|
|
22
|
+
* not the serialized URL form.
|
|
23
|
+
*/
|
|
24
|
+
getDefault?: (instance: any) => unknown;
|
|
25
|
+
}
|
|
26
|
+
interface InternalParamConfig extends ParamConfig {
|
|
27
|
+
initialized?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface ValueTransition<T = unknown> {
|
|
30
|
+
key: string;
|
|
31
|
+
from: T;
|
|
32
|
+
to: T;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Metadata attached to persisted resource classes
|
|
36
|
+
*/
|
|
37
|
+
export interface StorageResourceMeta {
|
|
38
|
+
id: string;
|
|
39
|
+
namespace: string | null;
|
|
40
|
+
pkFn: KeyFn | null;
|
|
41
|
+
type: "local-resource" | "session-resource" | "cache-resource";
|
|
42
|
+
typeOverrides: Map<string, "local-storage" | "session-storage" | "cache-storage"> | null;
|
|
43
|
+
fields: Map<string, null | ((update: ValueTransition<string>) => void)>;
|
|
44
|
+
initializers: Map<string, (() => unknown) | null>;
|
|
45
|
+
paramConfigs: Map<string, InternalParamConfig> | null;
|
|
46
|
+
paramCompanion: object | null;
|
|
47
|
+
instances: WeakMap<object, StorageResourceMeta> | null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A function which generates a unique primary-key
|
|
51
|
+
* string for a given LocalResource or SessionResource
|
|
52
|
+
* instance.
|
|
53
|
+
*
|
|
54
|
+
* Use functions when you want to create more than
|
|
55
|
+
* one instance of a resource type, each with its own
|
|
56
|
+
* persisted data.
|
|
57
|
+
*/
|
|
58
|
+
export type KeyFn = (obj: any) => string;
|
|
59
|
+
/**
|
|
60
|
+
* Setup persisted resource metadata on target
|
|
61
|
+
* if not already present
|
|
62
|
+
*
|
|
63
|
+
* @private
|
|
64
|
+
*/
|
|
65
|
+
export declare function initMeta(target: object): StorageResourceMeta;
|
|
66
|
+
export declare function useMeta(meta: StorageResourceMeta, instance: object): StorageResourceMeta;
|
|
67
|
+
/**
|
|
68
|
+
* Load storage field
|
|
69
|
+
*/
|
|
70
|
+
export declare function getField(instance: object, meta: StorageResourceMeta, key: string, overrideType: "local-storage" | "session-storage" | "cache-storage" | null): Record<string, unknown> | null;
|
|
71
|
+
export declare function peekField(meta: StorageResourceMeta, key: string, overrideType: "local-storage" | "session-storage" | "cache-storage" | null): unknown;
|
|
72
|
+
export declare function initializeFields(instance: object, source: object): void;
|
|
73
|
+
/**
|
|
74
|
+
* Update storage field
|
|
75
|
+
*/
|
|
76
|
+
export declare function setField(meta: StorageResourceMeta, key: string, value: string | boolean | null | number | Record<string, unknown> | unknown[], overrideType: "local-storage" | "session-storage" | "cache-storage" | null): void;
|
|
77
|
+
export declare function _createStorageResource(id: string | KeyFn, type: "local-resource" | "session-resource" | "cache-resource", namespace: string | null): ClassDecorator;
|
|
78
|
+
/**
|
|
79
|
+
* Returns the descriptor but is cast
|
|
80
|
+
* to void to satisfy Typescript's incorrect
|
|
81
|
+
* typing for legacy decorators.
|
|
82
|
+
*/
|
|
83
|
+
export declare function setupField(target: object, key: string, orgDesc?: PropertyDescriptor, type?: "local" | "session" | "cache"): void;
|
|
84
|
+
export declare function installEffect(meta: StorageResourceMeta, key: string): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Get or create the param companion object for a StorageResource instance.
|
|
87
|
+
*
|
|
88
|
+
* The companion object contains URL-serialized versions of all @param decorated fields.
|
|
89
|
+
* Each param field gets a corresponding property on the companion that:
|
|
90
|
+
* - Reads: Serializes the storage value to a URL string (returns null if not active)
|
|
91
|
+
* - Writes: Deserializes the URL string and updates the storage value
|
|
92
|
+
*
|
|
93
|
+
* The companion object is reactive using trackedObject, ensuring that changes
|
|
94
|
+
* to the underlying storage fields trigger updates in the query param system.
|
|
95
|
+
*
|
|
96
|
+
* This companion object is what QPRoute will bind to for URL query params.
|
|
97
|
+
*
|
|
98
|
+
* @private
|
|
99
|
+
* @param instance - The StorageResource instance
|
|
100
|
+
* @param groupControlMap - Optional map of fieldName -> controlFieldName for grouped params
|
|
101
|
+
* @returns The companion object with serialized param properties
|
|
102
|
+
*/
|
|
103
|
+
export declare function getParamCompanion(instance: object, groupControlMap?: Record<string, string>): object;
|
|
104
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const DEFAULT_CACHE_ID = "reactive-cache";
|
|
2
|
+
export interface InternalCacheStorageEvent {
|
|
3
|
+
storageArea: string;
|
|
4
|
+
key: string | null;
|
|
5
|
+
oldValue: string | null;
|
|
6
|
+
newValue: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface CacheStorageEvent {
|
|
9
|
+
storageArea: CacheStorage;
|
|
10
|
+
key: string | null;
|
|
11
|
+
oldValue: string | null;
|
|
12
|
+
newValue: string | null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A reactive interface for json stored in the browser [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) API.
|
|
16
|
+
*
|
|
17
|
+
* This is a good option for larger data sets than can be efficiently stored in localStorage
|
|
18
|
+
* but should not be used as a permanent DB or storage solution.
|
|
19
|
+
*/
|
|
20
|
+
export declare class CacheStorage implements Storage {
|
|
21
|
+
#private;
|
|
22
|
+
_data: Map<string, string | null>;
|
|
23
|
+
_nextUpdate: number | null;
|
|
24
|
+
_bufferedEvents: InternalCacheStorageEvent[];
|
|
25
|
+
constructor(cacheId: string);
|
|
26
|
+
get length(): number;
|
|
27
|
+
clear(): void;
|
|
28
|
+
getItem(key: string): string | null;
|
|
29
|
+
key(index: number): string | null;
|
|
30
|
+
removeItem(key: string): void;
|
|
31
|
+
setItem(key: string, value: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Get the singleton CacheStorage instance.
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
static get(cacheId?: string): Promise<CacheStorage>;
|
|
37
|
+
static expectCache(cacheId?: string): CacheStorage;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the IDs of all {@link CacheStorage} instances that have been
|
|
40
|
+
* opened in this context via {@link CacheStorage.get}.
|
|
41
|
+
*/
|
|
42
|
+
static getAllCacheIds(): string[];
|
|
43
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { type KeyFn, type ValueTransition } from "./-private/storage-infra.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator which transforms a class into a StorageResource
|
|
4
|
+
* persisted in localStorage.
|
|
5
|
+
*
|
|
6
|
+
* LocalResources must either be singletons or expect all instances
|
|
7
|
+
* to share state unless a primary key function is provided.
|
|
8
|
+
*
|
|
9
|
+
* When a primary key function is provided, each instance
|
|
10
|
+
* will have its own persisted data based on the key generated
|
|
11
|
+
* by the function.
|
|
12
|
+
*
|
|
13
|
+
* The function will be called once per instance during
|
|
14
|
+
* initialization to determine the unique ID for that instance.
|
|
15
|
+
*/
|
|
16
|
+
export declare function LocalResource(id: string | KeyFn): ClassDecorator;
|
|
17
|
+
/**
|
|
18
|
+
* Decorator which transforms a class into a StorageResource
|
|
19
|
+
* persisted in sessionStorage.
|
|
20
|
+
*
|
|
21
|
+
* SessionResources must either be singletons or expect all instances
|
|
22
|
+
* to share state unless a primary key function is provided.
|
|
23
|
+
*
|
|
24
|
+
* When a primary key function is provided, each instance
|
|
25
|
+
* will have its own persisted data based on the key generated
|
|
26
|
+
* by the function.
|
|
27
|
+
*
|
|
28
|
+
* The function will be called once per instance during
|
|
29
|
+
* initialization to determine the unique ID for that instance.
|
|
30
|
+
*/
|
|
31
|
+
export declare function SessionResource(id: string | KeyFn): ClassDecorator;
|
|
32
|
+
/**
|
|
33
|
+
* Decorator which transforms a class into a StorageResource
|
|
34
|
+
* persisted via the [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
|
35
|
+
* api and shared across all tabs/windows under the same origin.
|
|
36
|
+
*
|
|
37
|
+
* CacheResources must either be singletons or expect all instances
|
|
38
|
+
* to share state unless a primary key function is provided.
|
|
39
|
+
*
|
|
40
|
+
* When a primary key function is provided, each instance
|
|
41
|
+
* will have its own persisted data based on the key generated
|
|
42
|
+
* by the function.
|
|
43
|
+
*
|
|
44
|
+
* The function will be called once per instance during
|
|
45
|
+
* initialization to determine the unique ID for that instance.
|
|
46
|
+
*
|
|
47
|
+
* All object cached in the same `namespace` share the namespace's storage context,
|
|
48
|
+
* so partitioning can be achieved by using different namespaces for different groups
|
|
49
|
+
* of data.
|
|
50
|
+
*/
|
|
51
|
+
export declare function CacheResource(id: string | KeyFn, namespace?: string | null): ClassDecorator;
|
|
52
|
+
/**
|
|
53
|
+
* Decorator which marks a property as a field on
|
|
54
|
+
* a LocalResource or SessionResource
|
|
55
|
+
*
|
|
56
|
+
* The field's value will be initialized from the persisted resource data
|
|
57
|
+
* if available, falling back to the property's default value otherwise.
|
|
58
|
+
*
|
|
59
|
+
* Fields can be of any type that is serializable to and restorable from JSON,
|
|
60
|
+
* but complex types (like objects or arrays) should be handled with care to avoid
|
|
61
|
+
* unintended mutations or reactivity issues.
|
|
62
|
+
*
|
|
63
|
+
* By default, fields are persisted in the storage type defined by the resource decorator
|
|
64
|
+
* (@LocalResource or @SessionResource). However, you can override this behavior
|
|
65
|
+
* by passing 'local' or 'session' as an argument to the decorator.
|
|
66
|
+
*
|
|
67
|
+
* ---
|
|
68
|
+
*
|
|
69
|
+
* **Example:**
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* @LocalResource('user-settings')
|
|
73
|
+
* class UserSettings {
|
|
74
|
+
* @field
|
|
75
|
+
* theme: 'light' | 'dark' = 'light';
|
|
76
|
+
*
|
|
77
|
+
* @field('session')
|
|
78
|
+
* sessionToken: string | null = null;
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
export declare function field(type: "local" | "session" | "cache"): PropertyDecorator;
|
|
84
|
+
export declare function field(target: object, key: string, descriptor?: PropertyDescriptor): void;
|
|
85
|
+
export declare function input(type: "number" | "boolean" | "float"): PropertyDecorator;
|
|
86
|
+
/**
|
|
87
|
+
* Effects are fields that run a side-effecting function.
|
|
88
|
+
*
|
|
89
|
+
* Effects are intended to enable synchronizing get states between
|
|
90
|
+
* tabs or windows that result in needing to synchronize other
|
|
91
|
+
* non-reactive state.
|
|
92
|
+
*
|
|
93
|
+
* For example, when a user selects a light/dark mode theme preference
|
|
94
|
+
* that differs from the system preference, effects can be used to trigger
|
|
95
|
+
* DOM updates on the documentElement necessary to ensure its state is
|
|
96
|
+
* consistent with the persisted resource state and reactive application state.
|
|
97
|
+
*
|
|
98
|
+
* To do this without an effect would require either setting up your own
|
|
99
|
+
* storage event listeners or consuming the reactive state of the property
|
|
100
|
+
* in another effect-like API such as an Ember modifier or React useEffect,
|
|
101
|
+
*
|
|
102
|
+
* Effects *only* run when the stored value changes due to storage events
|
|
103
|
+
* emitted from other tabs or windows. They do not run when the property
|
|
104
|
+
* is updated in the same context.
|
|
105
|
+
*
|
|
106
|
+
* ---
|
|
107
|
+
*
|
|
108
|
+
* **Example:**
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* @LocalResource('user-preferences')
|
|
112
|
+
* class UserPreferences {
|
|
113
|
+
* @effect(syncThemeToDOM)
|
|
114
|
+
* explicitThemePreference: 'light' | 'dark' | null = null;
|
|
115
|
+
*
|
|
116
|
+
* @matchMedia('(prefers-color-scheme: dark)')
|
|
117
|
+
* systemPrefersDarkMode: boolean = false;
|
|
118
|
+
* }
|
|
119
|
+
*
|
|
120
|
+
* function syncThemeToDOM(update: ValueTransition<'light' | 'dark' | null>): void {
|
|
121
|
+
* const newTheme = update.to;
|
|
122
|
+
* document.documentElement.style.colorScheme = newTheme ?? 'light dark';
|
|
123
|
+
*
|
|
124
|
+
* if (newTheme === 'dark') {
|
|
125
|
+
* document.documentElement.classList.add('dark-theme');
|
|
126
|
+
* document.documentElement.classList.remove('light-theme');
|
|
127
|
+
* } else if (newTheme === 'light') {
|
|
128
|
+
* document.documentElement.classList.add('light-theme');
|
|
129
|
+
* document.documentElement.classList.remove('dark-theme');
|
|
130
|
+
* } else {
|
|
131
|
+
* document.documentElement.classList.remove('light-theme');
|
|
132
|
+
* document.documentElement.classList.remove('dark-theme');
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export declare function effect(fn: <K>(update: ValueTransition<K>) => void, type?: "local" | "session"): PropertyDecorator;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type CacheStorageEvent } from "./cache.js";
|
|
2
|
+
export interface ReactiveStorageOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If true, falls back to in-memory storage when the underlying
|
|
5
|
+
* storage is unavailable (e.g., private browsing mode).
|
|
6
|
+
*/
|
|
7
|
+
fallbackToMemory?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* If true, updates signal state even when writes fail due to quota.
|
|
10
|
+
* The onQuotaExceeded callback will be invoked before retrying.
|
|
11
|
+
*/
|
|
12
|
+
updateOnQuotaExceeded?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Called when a write fails due to quota exceeded.
|
|
15
|
+
* Return true to retry the write after freeing space.
|
|
16
|
+
*/
|
|
17
|
+
onQuotaExceeded?: (key: string, value: string) => boolean | Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves the singleton instance of the LocalStorage service.
|
|
21
|
+
*
|
|
22
|
+
* If the instance does not already exist, it is created.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getLocalStorage(): ReactiveStorage;
|
|
25
|
+
export declare function getSessionStorage(): ReactiveStorage;
|
|
26
|
+
export declare function getCacheStorage(namespace?: string | null): ReactiveStorage;
|
|
27
|
+
/**
|
|
28
|
+
* Configure options for the localStorage singleton.
|
|
29
|
+
* Must be called before getLocalStorage() is first invoked.
|
|
30
|
+
*/
|
|
31
|
+
export declare function configureLocalStorage(options: ReactiveStorageOptions): void;
|
|
32
|
+
/**
|
|
33
|
+
* Configure options for the sessionStorage singleton.
|
|
34
|
+
* Must be called before getSessionStorage() is first invoked.
|
|
35
|
+
*/
|
|
36
|
+
export declare function configureSessionStorage(options: ReactiveStorageOptions): void;
|
|
37
|
+
export type EffectStorageEvent = CacheStorageEvent | StorageEvent;
|
|
38
|
+
declare global {
|
|
39
|
+
interface WindowEventMap {
|
|
40
|
+
storage: CustomEvent<CacheStorageEvent> | StorageEvent;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A reactive wrapper around the Web Storage API (localStorage/sessionStorage)
|
|
45
|
+
* that provides signal-based access to storage items and length.
|
|
46
|
+
*
|
|
47
|
+
* Will automatically update when storage events occur in other tabs/windows.
|
|
48
|
+
*/
|
|
49
|
+
declare class ReactiveStorage implements Storage {
|
|
50
|
+
private _storage;
|
|
51
|
+
private _options;
|
|
52
|
+
private _memoryOnly;
|
|
53
|
+
private _values;
|
|
54
|
+
private _signals;
|
|
55
|
+
private _length;
|
|
56
|
+
private _effects;
|
|
57
|
+
setEffect(key: string, fn: (value: EffectStorageEvent) => void): void;
|
|
58
|
+
constructor(storage: Storage, options?: ReactiveStorageOptions);
|
|
59
|
+
/**
|
|
60
|
+
* Reactive access to the number of keys in Storage
|
|
61
|
+
*/
|
|
62
|
+
get length(): number;
|
|
63
|
+
/**
|
|
64
|
+
* Non-reactive way to peek the current value of a key in Storage
|
|
65
|
+
*/
|
|
66
|
+
peekItem(key: string): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* Reactive access to Storage contents
|
|
69
|
+
*/
|
|
70
|
+
getItem(key: string): string | null;
|
|
71
|
+
/**
|
|
72
|
+
* Set a value in Storage, triggering reactivity
|
|
73
|
+
*/
|
|
74
|
+
setItem(key: string, value: string): void;
|
|
75
|
+
/**
|
|
76
|
+
* Remove a value from Storage, triggering reactivity
|
|
77
|
+
*/
|
|
78
|
+
removeItem(key: string): void;
|
|
79
|
+
/**
|
|
80
|
+
* Clears all keys from Storage, triggering reactivity
|
|
81
|
+
*/
|
|
82
|
+
clear(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Reactive access to the key at the given index
|
|
85
|
+
*/
|
|
86
|
+
key(index: number): string | null;
|
|
87
|
+
}
|
|
88
|
+
export type { ReactiveStorage };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./storage/storage.js";
|
|
2
|
+
export type * from "./storage/storage.js";
|
|
3
|
+
export * from "./storage/storage-resource.js";
|
|
4
|
+
export type * from "./storage/storage-resource.js";
|
|
5
|
+
export * from "./storage/cache.js";
|
|
6
|
+
export type * from "./storage/cache.js";
|
|
7
|
+
export { initMeta as _initMeta, getParamCompanion as _getParamCompanion } from "./storage/-private/storage-infra.js";
|