edges-svelte 1.0.3 → 1.0.4
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.
@@ -10,6 +10,7 @@ type StoreDeps = {
|
|
10
10
|
type NoConflict<I> = {
|
11
11
|
[K in keyof I]: K extends keyof StoreDeps ? never : I[K];
|
12
12
|
};
|
13
|
+
export declare const clearProviderCache: (pattern?: string) => void;
|
13
14
|
export declare const createProvider: <T, I extends Record<string, unknown> = Record<string, unknown>>(name: string, factory: (args: StoreDeps & NoConflict<I>) => T, inject?: I) => (() => T);
|
14
15
|
export declare const createProviderFactory: <I extends Record<string, unknown>>(inject: I) => <T>(name: string, factory: (args: StoreDeps & NoConflict<I>) => T) => () => T;
|
15
16
|
export {};
|
@@ -2,6 +2,20 @@ import { createState as BaseCreateState, createDerivedState as BaseCreateDerived
|
|
2
2
|
import { RequestContext } from '../context/index.js';
|
3
3
|
import { browser } from '$app/environment';
|
4
4
|
const globalClientCache = new Map();
|
5
|
+
export const clearProviderCache = (pattern) => {
|
6
|
+
if (browser) {
|
7
|
+
if (pattern) {
|
8
|
+
for (const [key] of globalClientCache) {
|
9
|
+
if (key.includes(pattern)) {
|
10
|
+
globalClientCache.delete(key);
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
else {
|
15
|
+
globalClientCache.clear();
|
16
|
+
}
|
17
|
+
}
|
18
|
+
};
|
5
19
|
export const createProvider = (name, factory, inject) => {
|
6
20
|
const cacheKey = name;
|
7
21
|
return () => {
|
@@ -17,7 +31,10 @@ export const createProvider = (name, factory, inject) => {
|
|
17
31
|
contextMap = context.data.providers;
|
18
32
|
}
|
19
33
|
if (cacheKey && contextMap.has(cacheKey)) {
|
20
|
-
|
34
|
+
const cached = contextMap.get(cacheKey);
|
35
|
+
if (cached !== undefined) {
|
36
|
+
return cached;
|
37
|
+
}
|
21
38
|
}
|
22
39
|
let stateCounter = 0;
|
23
40
|
const autoKeyDeps = {
|
@@ -42,7 +42,7 @@ export const createRawState = (key, initial) => {
|
|
42
42
|
return {
|
43
43
|
get value() {
|
44
44
|
if (!map)
|
45
|
-
return
|
45
|
+
return initial();
|
46
46
|
if (!map.has(key))
|
47
47
|
map.set(key, structuredClone(initial()));
|
48
48
|
return map.get(key);
|
@@ -63,22 +63,22 @@ export const createState = (key, initial) => {
|
|
63
63
|
throw new Error('No RequestContext available');
|
64
64
|
if (!map.has(key))
|
65
65
|
map.set(key, structuredClone(initial()));
|
66
|
-
const subscribers = new Set();
|
66
|
+
//const subscribers: Set<(val: T) => void> = new Set();
|
67
67
|
return {
|
68
68
|
subscribe(run) {
|
69
69
|
run(map.get(key));
|
70
|
-
subscribers.add(run);
|
71
|
-
return () =>
|
70
|
+
//subscribers.add(run);
|
71
|
+
return () => { };
|
72
72
|
},
|
73
73
|
set(val) {
|
74
74
|
map.set(key, val);
|
75
|
-
subscribers.forEach((fn) => fn(val));
|
75
|
+
//subscribers.forEach((fn) => fn(val));
|
76
76
|
},
|
77
77
|
update(updater) {
|
78
78
|
const oldVal = map.get(key);
|
79
79
|
const newVal = updater(oldVal);
|
80
80
|
map.set(key, newVal);
|
81
|
-
subscribers.forEach((fn) => fn(newVal));
|
81
|
+
//subscribers.forEach((fn) => fn(newVal));
|
82
82
|
}
|
83
83
|
};
|
84
84
|
};
|
@@ -90,14 +90,19 @@ export const createDerivedState = (stores, deriveFn) => {
|
|
90
90
|
subscribe(run) {
|
91
91
|
const values = new Array(stores.length);
|
92
92
|
let initializedCount = 0;
|
93
|
+
let isInitialized = false;
|
94
|
+
const checkAndRun = () => {
|
95
|
+
if (initializedCount >= stores.length && !isInitialized) {
|
96
|
+
isInitialized = true;
|
97
|
+
run(deriveFn(values));
|
98
|
+
}
|
99
|
+
};
|
93
100
|
const unsubscribers = stores.map((store, i) => store.subscribe((value) => {
|
94
101
|
values[i] = value;
|
95
102
|
if (initializedCount < stores.length) {
|
96
103
|
initializedCount++;
|
97
104
|
}
|
98
|
-
|
99
|
-
run(deriveFn(values));
|
100
|
-
}
|
105
|
+
checkAndRun();
|
101
106
|
}));
|
102
107
|
return () => {
|
103
108
|
unsubscribers.forEach((unsub) => unsub());
|