edges-svelte 0.4.1 → 0.5.2

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 CHANGED
@@ -30,6 +30,7 @@ To enable **Edge-S**, wrap your SvelteKit `handle` hook and serialize the state
30
30
 
31
31
  ```ts
32
32
  // hooks.server.ts
33
+ import { dev } from '$app/environment';
33
34
  import { edgesHandle } from 'edges-svelte/server';
34
35
 
35
36
  export const handle: Handle = async ({ event, resolve }) => {
@@ -39,7 +40,7 @@ export const handle: Handle = async ({ event, resolve }) => {
39
40
  //...Your handle code, use edgesEvent as a default svelte event (RequestEvent)
40
41
  return resolve(edgesEvent, { transformPageChunk: ({ html }) => serialize(html) });
41
42
  },
42
- true
43
+ dev
43
44
  );
44
45
  };
45
46
  ```
@@ -2,6 +2,10 @@ import type { RequestEvent } from '@sveltejs/kit';
2
2
  export interface ContextData {
3
3
  event?: RequestEvent;
4
4
  symbol?: symbol;
5
+ data: {
6
+ providers?: Map<symbol, unknown>;
7
+ [p: string]: unknown;
8
+ };
5
9
  }
6
10
  declare const _default: {
7
11
  current(): ContextData;
@@ -4,6 +4,6 @@ export default {
4
4
  if (!browser) {
5
5
  throw new Error('AsyncLocalStorage has not been initialized');
6
6
  }
7
- return {};
7
+ return { data: {} };
8
8
  }
9
9
  };
@@ -6,6 +6,7 @@ type StoreDeps = {
6
6
  };
7
7
  interface CreateProviderOptions<T, I extends Record<string, unknown> = Record<string, unknown>> {
8
8
  inject?: I;
9
+ cache?: boolean;
9
10
  factory: (args: StoreDeps & I) => T;
10
11
  }
11
12
  export declare const createProvider: <T, I extends Record<string, unknown> = Record<string, unknown>>(options: CreateProviderOptions<T, I>) => (() => T);
@@ -1,4 +1,7 @@
1
1
  import { createState, createDerivedState, createRawState } from '../store/index.js';
2
+ import { RequestContext } from '../context/index.js';
3
+ import { browser } from '$app/environment';
4
+ const globalClientCache = new Map();
2
5
  export const createProvider = (options) => {
3
6
  const deps = {
4
7
  ...{
@@ -8,7 +11,32 @@ export const createProvider = (options) => {
8
11
  },
9
12
  ...(options.inject || {})
10
13
  };
11
- return () => options.factory(deps);
14
+ const cacheKey = Symbol();
15
+ const cache = options.cache ?? true;
16
+ console.log('cache-key', cacheKey);
17
+ return () => {
18
+ if (!cache) {
19
+ return options.factory(deps);
20
+ }
21
+ if (browser) {
22
+ if (!globalClientCache.has(cacheKey)) {
23
+ globalClientCache.set(cacheKey, options.factory(deps));
24
+ }
25
+ return globalClientCache.get(cacheKey);
26
+ }
27
+ else {
28
+ const context = RequestContext.current();
29
+ if (!context.data.providers) {
30
+ context.data.providers = new Map();
31
+ }
32
+ console.log(RequestContext.current().data.providers);
33
+ const map = context.data.providers;
34
+ if (!map.has(cacheKey)) {
35
+ map.set(cacheKey, options.factory(deps));
36
+ }
37
+ return map.get(cacheKey);
38
+ }
39
+ };
12
40
  };
13
41
  export const createProviderFactory = (inject) => {
14
42
  return function createInjectedProvider(options) {
@@ -14,7 +14,7 @@ const storage = new AsyncLocalStorage();
14
14
  * and returns a 204 No Content response instead of a 404 error.
15
15
  */
16
16
  export const edgesHandle = async (event, callback, silentChromeDevtools = false) => {
17
- return await storage.run({ event: event, symbol: Symbol() }, async () => {
17
+ return await storage.run({ event: event, symbol: Symbol(), data: { providers: new Map() } }, async () => {
18
18
  RequestContext.current = () => {
19
19
  const context = storage.getStore();
20
20
  if (context === undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "edges-svelte",
3
- "version": "0.4.1",
3
+ "version": "0.5.2",
4
4
  "license": "MIT",
5
5
  "author": "Pixel1917",
6
6
  "description": "A blazing-fast, extremely lightweight and SSR-friendly store for Svelte",