edges-svelte 0.5.3 → 0.5.5
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 +34 -4
- package/dist/provider/Provider.js +0 -1
- package/dist/store/State.svelte.js +14 -7
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# EdgeS
|
2
2
|
|
3
3
|
### A blazing-fast, extremely lightweight and SSR-friendly store for Svelte.
|
4
4
|
|
5
|
-
**
|
5
|
+
**EdgeS** brings seamless, per-request state management to Svelte apps — fully reactive, server-aware, and serialization-safe by default.
|
6
6
|
|
7
7
|
No context boilerplate. No hydration headaches. Just drop-in SSR-compatible state primitives with built-in support for client-side reactivity and server-side isolation.
|
8
8
|
|
@@ -26,7 +26,7 @@ npm install edges-svelte
|
|
26
26
|
|
27
27
|
## Setup
|
28
28
|
|
29
|
-
To enable **
|
29
|
+
To enable **EdgeS**, wrap your SvelteKit `handle` hook and serialize the state in `transformPageChunks`:
|
30
30
|
|
31
31
|
```ts
|
32
32
|
// hooks.server.ts
|
@@ -97,6 +97,36 @@ const myProvider = createProvider({
|
|
97
97
|
|
98
98
|
---
|
99
99
|
|
100
|
+
## Provider Caching with `cacheKey`
|
101
|
+
|
102
|
+
To improve performance and avoid redundant computations, **EdgeS** supports caching providers by a unique `cacheKey`.
|
103
|
+
|
104
|
+
### What is `cacheKey`?
|
105
|
+
|
106
|
+
- A `cacheKey` is a string uniquely identifying a specific provider invocation.
|
107
|
+
- It enables **caching the result of the provider’s factory function**
|
108
|
+
- Prevents repeated calls to the same provider with identical parameters.
|
109
|
+
With caching:
|
110
|
+
|
111
|
+
- The provider is called **only once per unique `cacheKey`** within a request.
|
112
|
+
- Subsequent calls with the same key return the cached result instantly.
|
113
|
+
|
114
|
+
### How to use `cacheKey`
|
115
|
+
|
116
|
+
```ts
|
117
|
+
import { createProvider } from 'edges-svelte';
|
118
|
+
|
119
|
+
const myProvider = createProvider({
|
120
|
+
cacheKey: (params) => params.userId,
|
121
|
+
factory: ({ createState }, params) => {
|
122
|
+
const userData = createState('userData', () => fetchUserData(params.userId));
|
123
|
+
return { userData };
|
124
|
+
}
|
125
|
+
});
|
126
|
+
```
|
127
|
+
|
128
|
+
---
|
129
|
+
|
100
130
|
## Core Concepts
|
101
131
|
|
102
132
|
### SSR-safe state access
|
@@ -213,7 +243,7 @@ type EdgesHandle = (
|
|
213
243
|
|
214
244
|
### Why not just use `writable, derived`?
|
215
245
|
|
216
|
-
Because `writable` and `derived` shares state between requests when used on the server. That means users could leak state into each other’s responses. **
|
246
|
+
Because `writable` and `derived` shares state between requests when used on the server. That means users could leak state into each other’s responses. **EdgeS** solves that by isolating state per request.
|
217
247
|
|
218
248
|
### Difference between `createState` and `createRawState`
|
219
249
|
|
@@ -27,7 +27,6 @@ export const createProvider = (options) => {
|
|
27
27
|
if (!context.data.providers) {
|
28
28
|
context.data.providers = new Map();
|
29
29
|
}
|
30
|
-
console.log(RequestContext.current().data.providers);
|
31
30
|
const map = context.data.providers;
|
32
31
|
if (!map.has(cacheKey)) {
|
33
32
|
map.set(cacheKey, options.factory(deps));
|
@@ -86,15 +86,22 @@ export const createDerivedState = (stores, deriveFn) => {
|
|
86
86
|
if (browser) {
|
87
87
|
return derived(stores, deriveFn);
|
88
88
|
}
|
89
|
-
const values = stores.map((s) => {
|
90
|
-
let value;
|
91
|
-
s.subscribe((v) => (value = v))();
|
92
|
-
return value;
|
93
|
-
});
|
94
89
|
return {
|
95
90
|
subscribe(run) {
|
96
|
-
|
97
|
-
|
91
|
+
const values = new Array(stores.length);
|
92
|
+
let initializedCount = 0;
|
93
|
+
const unsubscribers = stores.map((store, i) => store.subscribe((value) => {
|
94
|
+
values[i] = value;
|
95
|
+
if (initializedCount < stores.length) {
|
96
|
+
initializedCount++;
|
97
|
+
}
|
98
|
+
if (initializedCount >= stores.length) {
|
99
|
+
run(deriveFn(values));
|
100
|
+
}
|
101
|
+
}));
|
102
|
+
return () => {
|
103
|
+
unsubscribers.forEach((unsub) => unsub());
|
104
|
+
};
|
98
105
|
}
|
99
106
|
};
|
100
107
|
};
|