@slates/provider 1.0.0-rc.11 → 1.0.0-rc.12
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/index.cjs +4 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.module.js +4 -1
- package/package.json +2 -2
- package/src/auth/types.ts +1 -0
- package/src/context/hook.test.ts +21 -0
- package/src/context/hook.ts +10 -1
package/dist/index.cjs
CHANGED
|
@@ -126,7 +126,10 @@ var import_axios = require("axios");
|
|
|
126
126
|
|
|
127
127
|
// src/context/hook.ts
|
|
128
128
|
var import_async_hooks = require("async_hooks");
|
|
129
|
-
var
|
|
129
|
+
var slateContextStorageKey = /* @__PURE__ */ Symbol.for("slates.provider.context.asyncLocalStorage");
|
|
130
|
+
var globalWithSlateContext = globalThis;
|
|
131
|
+
var asyncLocalStorage = globalWithSlateContext[slateContextStorageKey] ?? new import_async_hooks.AsyncLocalStorage();
|
|
132
|
+
globalWithSlateContext[slateContextStorageKey] = asyncLocalStorage;
|
|
130
133
|
var runWithContext = (context, fn) => {
|
|
131
134
|
return asyncLocalStorage.run(context, fn);
|
|
132
135
|
};
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.module.js
CHANGED
|
@@ -55,7 +55,10 @@ import { isAxiosError } from "axios";
|
|
|
55
55
|
|
|
56
56
|
// src/context/hook.ts
|
|
57
57
|
import { AsyncLocalStorage } from "async_hooks";
|
|
58
|
-
var
|
|
58
|
+
var slateContextStorageKey = /* @__PURE__ */ Symbol.for("slates.provider.context.asyncLocalStorage");
|
|
59
|
+
var globalWithSlateContext = globalThis;
|
|
60
|
+
var asyncLocalStorage = globalWithSlateContext[slateContextStorageKey] ?? new AsyncLocalStorage();
|
|
61
|
+
globalWithSlateContext[slateContextStorageKey] = asyncLocalStorage;
|
|
59
62
|
var runWithContext = (context, fn) => {
|
|
60
63
|
return asyncLocalStorage.run(context, fn);
|
|
61
64
|
};
|
package/package.json
CHANGED
package/src/auth/types.ts
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { SlateContext } from './context';
|
|
3
|
+
import { runWithContext } from './hook';
|
|
4
|
+
|
|
5
|
+
describe('slate context hook', () => {
|
|
6
|
+
it('shares context storage across duplicate module instances', async () => {
|
|
7
|
+
// @ts-expect-error Vite treats the query as a distinct module instance.
|
|
8
|
+
let duplicateHook: typeof import('./hook') = await import('./hook?duplicate-instance');
|
|
9
|
+
let context = new SlateContext({}, {}, {}, {} as any, {
|
|
10
|
+
debug: () => {},
|
|
11
|
+
error: () => {},
|
|
12
|
+
info: () => {},
|
|
13
|
+
progress: () => {},
|
|
14
|
+
warn: () => {}
|
|
15
|
+
} as any);
|
|
16
|
+
|
|
17
|
+
let result = await runWithContext(context, async () => duplicateHook.getCurrentContext());
|
|
18
|
+
|
|
19
|
+
expect(result).toBe(context);
|
|
20
|
+
});
|
|
21
|
+
});
|
package/src/context/hook.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
2
|
import type { SlateContext } from './context';
|
|
3
3
|
|
|
4
|
-
let
|
|
4
|
+
let slateContextStorageKey = Symbol.for('slates.provider.context.asyncLocalStorage');
|
|
5
|
+
let globalWithSlateContext = globalThis as typeof globalThis & {
|
|
6
|
+
[key: symbol]: AsyncLocalStorage<SlateContext<any, any, any>> | undefined;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
let asyncLocalStorage =
|
|
10
|
+
globalWithSlateContext[slateContextStorageKey] ??
|
|
11
|
+
new AsyncLocalStorage<SlateContext<any, any, any>>();
|
|
12
|
+
|
|
13
|
+
globalWithSlateContext[slateContextStorageKey] = asyncLocalStorage;
|
|
5
14
|
|
|
6
15
|
export let runWithContext = <
|
|
7
16
|
ConfigType extends {},
|