@friendofsvelte/state 0.0.2 → 0.0.3
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/dist/state.svelte.d.ts +1 -1
- package/dist/state.svelte.js +20 -8
- package/package.json +1 -1
package/dist/state.svelte.d.ts
CHANGED
|
@@ -13,5 +13,5 @@ type GetTypeFromRegistry<K extends keyof PodTypeRegistry> = PodTypeRegistry[K] e
|
|
|
13
13
|
* @param context - The initial state or override.
|
|
14
14
|
* @param override - Whether to override the stored value with the provided context.
|
|
15
15
|
*/
|
|
16
|
-
export declare function pod<K extends keyof PodTypeRegistry>(key: K, storage
|
|
16
|
+
export declare function pod<K extends keyof PodTypeRegistry>(key: K, storage: StorageType, context?: GetTypeFromRegistry<K>, override?: boolean): GetTypeFromRegistry<K>;
|
|
17
17
|
export {};
|
package/dist/state.svelte.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { untrack } from 'svelte';
|
|
1
|
+
import { getContext, setContext, untrack } from 'svelte';
|
|
2
|
+
const EMPTY = Symbol('___empty____');
|
|
3
|
+
function makeContextKey(key, storage) {
|
|
4
|
+
return `${key}__${storage}__pod`;
|
|
5
|
+
}
|
|
2
6
|
function track(key, storage, context, override = false) {
|
|
3
|
-
|
|
7
|
+
const contextKey = makeContextKey(key, storage);
|
|
8
|
+
if (context === EMPTY) {
|
|
9
|
+
return getContext(contextKey);
|
|
10
|
+
}
|
|
11
|
+
let state = $state(context);
|
|
4
12
|
if (typeof window !== 'undefined') {
|
|
5
13
|
const storedValue = untrack(() => window[storage].getItem(String(key)));
|
|
6
14
|
if (storedValue && !override) {
|
|
@@ -16,12 +24,14 @@ function track(key, storage, context, override = false) {
|
|
|
16
24
|
const json = JSON.stringify($state.snapshot(state));
|
|
17
25
|
window[storage].setItem(String(key), json);
|
|
18
26
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
if (context !== EMPTY) {
|
|
28
|
+
$effect.pre(() => {
|
|
29
|
+
const json = JSON.stringify($state.snapshot(state));
|
|
30
|
+
window[storage].setItem(String(key), json);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
23
33
|
}
|
|
24
|
-
return state;
|
|
34
|
+
return setContext(contextKey, state);
|
|
25
35
|
}
|
|
26
36
|
/**
|
|
27
37
|
* Get a persistent state from storage, or initialize with an optional context.
|
|
@@ -30,6 +40,8 @@ function track(key, storage, context, override = false) {
|
|
|
30
40
|
* @param context - The initial state or override.
|
|
31
41
|
* @param override - Whether to override the stored value with the provided context.
|
|
32
42
|
*/
|
|
33
|
-
export function pod(key, storage
|
|
43
|
+
export function pod(key, storage,
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
context = EMPTY, override = false) {
|
|
34
46
|
return track(key, storage, context, override);
|
|
35
47
|
}
|