@gqloom/core 0.8.4 → 0.9.1
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 +16 -3
- package/dist/chunk-BQAPYNA7.js +220 -0
- package/dist/context-DsvT0-C6.d.cts +766 -0
- package/dist/context-DsvT0-C6.d.ts +766 -0
- package/dist/context.cjs +453 -0
- package/dist/context.d.cts +137 -0
- package/dist/context.d.ts +137 -0
- package/dist/context.js +234 -0
- package/dist/index.cjs +683 -379
- package/dist/index.d.cts +138 -542
- package/dist/index.d.ts +138 -542
- package/dist/index.js +448 -330
- package/package.json +15 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { P as OnlyMemoizationPayload, R as ResolverPayload, B as BaseField, v as Middleware, K as ResolvingFields } from './context-DsvT0-C6.js';
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import 'graphql';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* the AsyncLocalStorage instance to store the resolver payload
|
|
7
|
+
*/
|
|
8
|
+
declare const resolverPayloadStorage: AsyncLocalStorage<OnlyMemoizationPayload | ResolverPayload<object, BaseField>>;
|
|
9
|
+
/**
|
|
10
|
+
* use detailed payload of the current resolver
|
|
11
|
+
* @returns the resolver payload
|
|
12
|
+
*/
|
|
13
|
+
declare function useResolverPayload(): ResolverPayload | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* use context of the current resolver
|
|
16
|
+
* @returns the context of the current resolver
|
|
17
|
+
*/
|
|
18
|
+
declare function useContext<TContextType = object>(): TContextType;
|
|
19
|
+
/**
|
|
20
|
+
* use the MemoizationMap of the current context
|
|
21
|
+
*/
|
|
22
|
+
declare function useMemoizationMap(): WeakMap<WeakKey, any> | undefined;
|
|
23
|
+
interface ContextOptions {
|
|
24
|
+
getContextMap: () => WeakMap<WeakKey, any> | undefined;
|
|
25
|
+
key: WeakKey;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A class that provides dependency injection and context sharing capabilities.
|
|
29
|
+
* It allows you to create injectable dependencies that can be shared across different parts of your application.
|
|
30
|
+
*
|
|
31
|
+
* @template T The type of the value that will be injected
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
declare class InjectableContext<T> implements ContextOptions {
|
|
35
|
+
readonly getter: () => T;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new instance of InjectableContext.
|
|
38
|
+
*
|
|
39
|
+
* @param getter - A function that returns the default value when no custom implementation is provided
|
|
40
|
+
* @param options - Optional configuration for the context
|
|
41
|
+
* @param options.getContextMap - A function that returns the WeakMap used to store context values. Defaults to useMemoizationMap
|
|
42
|
+
* @param options.key - A unique key used to identify this context in the WeakMap. Defaults to the getter function
|
|
43
|
+
*/
|
|
44
|
+
constructor(getter: () => T, options?: Partial<ContextOptions>);
|
|
45
|
+
/**
|
|
46
|
+
* A function that returns the WeakMap used to store context values.
|
|
47
|
+
* This can be customized to use different storage mechanisms.
|
|
48
|
+
*/
|
|
49
|
+
getContextMap: () => WeakMap<WeakKey, any> | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* A unique key used to identify this context in the WeakMap.
|
|
52
|
+
* This is used to store and retrieve values from the context map.
|
|
53
|
+
*/
|
|
54
|
+
readonly key: WeakKey;
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves the value from the context.
|
|
57
|
+
* If a custom implementation is provided, it will be used.
|
|
58
|
+
* Otherwise, the default getter function will be called.
|
|
59
|
+
*
|
|
60
|
+
* @returns The value of type T
|
|
61
|
+
*/
|
|
62
|
+
get(): T;
|
|
63
|
+
/**
|
|
64
|
+
* Provides a new implementation for this context.
|
|
65
|
+
*
|
|
66
|
+
* @param getter - A function that returns the new value
|
|
67
|
+
* @returns A tuple containing the key and the new getter function
|
|
68
|
+
*/
|
|
69
|
+
provide(getter: () => T): [WeakKey, () => T];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a memoization in context to store the result of a getter function
|
|
73
|
+
*/
|
|
74
|
+
declare class ContextMemoization<T> implements ContextOptions {
|
|
75
|
+
readonly getter: () => T;
|
|
76
|
+
constructor(getter: () => T, options?: Partial<ContextOptions>);
|
|
77
|
+
getContextMap: () => WeakMap<WeakKey, any> | undefined;
|
|
78
|
+
readonly key: WeakKey;
|
|
79
|
+
/**
|
|
80
|
+
* Get the value in memoization or call the getter function
|
|
81
|
+
* @returns the value of the getter function
|
|
82
|
+
*/
|
|
83
|
+
get(): T;
|
|
84
|
+
/**
|
|
85
|
+
* Clear the memoization
|
|
86
|
+
* @returns true if the memoization is cleared, undefined if the context is not found
|
|
87
|
+
*/
|
|
88
|
+
clear(): boolean | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Check if the memoization exists
|
|
91
|
+
* @returns true if the memoization exists, undefined if the context is not found
|
|
92
|
+
*/
|
|
93
|
+
exists(): boolean | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Set a new value to the memoization
|
|
96
|
+
* @param value the new value to set
|
|
97
|
+
* @returns the memoization map or undefined if the context is not found
|
|
98
|
+
*/
|
|
99
|
+
set(value: T): WeakMap<WeakKey, any> | undefined;
|
|
100
|
+
provide(value: T): [WeakKey, T];
|
|
101
|
+
}
|
|
102
|
+
interface CallableContext<T> extends InjectableContext<T> {
|
|
103
|
+
(): T;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create a callable context
|
|
107
|
+
* @param args - The arguments to pass to the InjectableContext constructor
|
|
108
|
+
* @returns A callable context
|
|
109
|
+
*/
|
|
110
|
+
declare function createContext<T>(...args: ConstructorParameters<typeof InjectableContext<T>>): CallableContext<T>;
|
|
111
|
+
/**
|
|
112
|
+
* Async Memoization with a callable function
|
|
113
|
+
*/
|
|
114
|
+
interface CallableContextMemoization<T> extends ContextMemoization<T> {
|
|
115
|
+
(): T;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a memoization in context to store the result of a getter function
|
|
119
|
+
*/
|
|
120
|
+
declare function createMemoization<T>(...args: ConstructorParameters<typeof ContextMemoization<T>>): CallableContextMemoization<T>;
|
|
121
|
+
interface AsyncContextProvider extends Middleware {
|
|
122
|
+
with: (...keyValues: [WeakKey, any][]) => Middleware;
|
|
123
|
+
}
|
|
124
|
+
declare const asyncContextProvider: AsyncContextProvider;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A hook that analyzes and processes field resolution in a GraphQL query.
|
|
128
|
+
*
|
|
129
|
+
* @returns An object containing sets of different field types,
|
|
130
|
+
* or undefined if no resolver payload is available
|
|
131
|
+
*/
|
|
132
|
+
declare const useResolvingFields: CallableContext<ResolvingFields | undefined>;
|
|
133
|
+
|
|
134
|
+
declare function bindAsyncIterator<TAsyncLocalStorage extends AsyncLocalStorage<unknown>, TAsyncIterator extends AsyncIterator<unknown, unknown, unknown>>(storage: TAsyncLocalStorage, generator: TAsyncIterator): TAsyncIterator;
|
|
135
|
+
declare function isAsyncIterator(value: unknown): value is AsyncIterator<unknown, unknown, unknown>;
|
|
136
|
+
|
|
137
|
+
export { type AsyncContextProvider, type CallableContext, type CallableContextMemoization, ContextMemoization, InjectableContext, asyncContextProvider, bindAsyncIterator, createContext, createMemoization, isAsyncIterator, resolverPayloadStorage, useContext, useMemoizationMap, useResolverPayload, useResolvingFields };
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getMemoizationMap,
|
|
3
|
+
getResolvingFields,
|
|
4
|
+
isOnlyMemoryPayload,
|
|
5
|
+
onlyMemoization
|
|
6
|
+
} from "./chunk-BQAPYNA7.js";
|
|
7
|
+
|
|
8
|
+
// src/context/context.ts
|
|
9
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
+
|
|
11
|
+
// src/context/async-iterator.ts
|
|
12
|
+
function bindAsyncIterator(storage, generator) {
|
|
13
|
+
const store = storage.getStore();
|
|
14
|
+
const next = generator.next;
|
|
15
|
+
Object.defineProperty(generator, "next", {
|
|
16
|
+
value: (...args) => storage.run(store, () => next.apply(generator, args)),
|
|
17
|
+
writable: false
|
|
18
|
+
});
|
|
19
|
+
return generator;
|
|
20
|
+
}
|
|
21
|
+
function isAsyncIterator(value) {
|
|
22
|
+
return value !== null && typeof value === "object" && "next" in value && typeof value.next === "function";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/context/context.ts
|
|
26
|
+
var resolverPayloadStorage = new AsyncLocalStorage();
|
|
27
|
+
function useResolverPayload() {
|
|
28
|
+
const payload = resolverPayloadStorage.getStore();
|
|
29
|
+
if (payload === void 0 || isOnlyMemoryPayload(payload)) return;
|
|
30
|
+
return payload;
|
|
31
|
+
}
|
|
32
|
+
function useContext() {
|
|
33
|
+
return useResolverPayload()?.context;
|
|
34
|
+
}
|
|
35
|
+
function useMemoizationMap() {
|
|
36
|
+
const payload = resolverPayloadStorage.getStore();
|
|
37
|
+
if (payload == null) return;
|
|
38
|
+
return getMemoizationMap(payload);
|
|
39
|
+
}
|
|
40
|
+
var InjectableContext = class {
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new instance of InjectableContext.
|
|
43
|
+
*
|
|
44
|
+
* @param getter - A function that returns the default value when no custom implementation is provided
|
|
45
|
+
* @param options - Optional configuration for the context
|
|
46
|
+
* @param options.getContextMap - A function that returns the WeakMap used to store context values. Defaults to useMemoizationMap
|
|
47
|
+
* @param options.key - A unique key used to identify this context in the WeakMap. Defaults to the getter function
|
|
48
|
+
*/
|
|
49
|
+
constructor(getter, options = {}) {
|
|
50
|
+
this.getter = getter;
|
|
51
|
+
this.getter = getter;
|
|
52
|
+
this.getContextMap = options.getContextMap ?? useMemoizationMap;
|
|
53
|
+
this.key = options.key ?? this.getter;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A function that returns the WeakMap used to store context values.
|
|
57
|
+
* This can be customized to use different storage mechanisms.
|
|
58
|
+
*/
|
|
59
|
+
getContextMap;
|
|
60
|
+
/**
|
|
61
|
+
* A unique key used to identify this context in the WeakMap.
|
|
62
|
+
* This is used to store and retrieve values from the context map.
|
|
63
|
+
*/
|
|
64
|
+
key;
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves the value from the context.
|
|
67
|
+
* If a custom implementation is provided, it will be used.
|
|
68
|
+
* Otherwise, the default getter function will be called.
|
|
69
|
+
*
|
|
70
|
+
* @returns The value of type T
|
|
71
|
+
*/
|
|
72
|
+
get() {
|
|
73
|
+
const getter = this.getContextMap()?.get(this.key) ?? this.getter;
|
|
74
|
+
if (typeof getter === "function") return getter();
|
|
75
|
+
return getter;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Provides a new implementation for this context.
|
|
79
|
+
*
|
|
80
|
+
* @param getter - A function that returns the new value
|
|
81
|
+
* @returns A tuple containing the key and the new getter function
|
|
82
|
+
*/
|
|
83
|
+
provide(getter) {
|
|
84
|
+
return [this.key, getter];
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var ContextMemoization = class {
|
|
88
|
+
constructor(getter, options = {}) {
|
|
89
|
+
this.getter = getter;
|
|
90
|
+
this.getter = getter;
|
|
91
|
+
this.getContextMap = options.getContextMap ?? useMemoizationMap;
|
|
92
|
+
this.key = options.key ?? this.getter;
|
|
93
|
+
}
|
|
94
|
+
getContextMap;
|
|
95
|
+
key;
|
|
96
|
+
/**
|
|
97
|
+
* Get the value in memoization or call the getter function
|
|
98
|
+
* @returns the value of the getter function
|
|
99
|
+
*/
|
|
100
|
+
get() {
|
|
101
|
+
const map = this.getContextMap();
|
|
102
|
+
if (!map) return this.getter();
|
|
103
|
+
if (!map.has(this.key)) {
|
|
104
|
+
map.set(this.key, this.getter());
|
|
105
|
+
}
|
|
106
|
+
return map.get(this.key);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Clear the memoization
|
|
110
|
+
* @returns true if the memoization is cleared, undefined if the context is not found
|
|
111
|
+
*/
|
|
112
|
+
clear() {
|
|
113
|
+
const map = this.getContextMap();
|
|
114
|
+
if (!map) return;
|
|
115
|
+
return map.delete(this.key);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Check if the memoization exists
|
|
119
|
+
* @returns true if the memoization exists, undefined if the context is not found
|
|
120
|
+
*/
|
|
121
|
+
exists() {
|
|
122
|
+
const map = this.getContextMap();
|
|
123
|
+
if (!map) return;
|
|
124
|
+
return map.has(this.key);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Set a new value to the memoization
|
|
128
|
+
* @param value the new value to set
|
|
129
|
+
* @returns the memoization map or undefined if the context is not found
|
|
130
|
+
*/
|
|
131
|
+
set(value) {
|
|
132
|
+
const map = this.getContextMap();
|
|
133
|
+
if (!map) return;
|
|
134
|
+
return map.set(this.key, value);
|
|
135
|
+
}
|
|
136
|
+
provide(value) {
|
|
137
|
+
return [this.key, value];
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
function createContext(...args) {
|
|
141
|
+
const context = new InjectableContext(...args);
|
|
142
|
+
const callable = () => context.get();
|
|
143
|
+
Object.defineProperty(context, "key", {
|
|
144
|
+
value: callable,
|
|
145
|
+
writable: false,
|
|
146
|
+
configurable: false
|
|
147
|
+
});
|
|
148
|
+
return Object.assign(callable, {
|
|
149
|
+
key: context.key,
|
|
150
|
+
get: () => context.get(),
|
|
151
|
+
provide: (getter) => context.provide(getter),
|
|
152
|
+
getContextMap: () => context.getContextMap(),
|
|
153
|
+
getter: context.getter
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function createMemoization(...args) {
|
|
157
|
+
const memoization = new ContextMemoization(...args);
|
|
158
|
+
const callable = () => memoization.get();
|
|
159
|
+
Object.defineProperty(memoization, "key", {
|
|
160
|
+
value: callable,
|
|
161
|
+
writable: false,
|
|
162
|
+
configurable: false
|
|
163
|
+
});
|
|
164
|
+
return Object.assign(callable, {
|
|
165
|
+
key: memoization.key,
|
|
166
|
+
get: () => memoization.get(),
|
|
167
|
+
set: (value) => memoization.set(value),
|
|
168
|
+
clear: () => memoization.clear(),
|
|
169
|
+
exists: () => memoization.exists(),
|
|
170
|
+
getter: memoization.getter,
|
|
171
|
+
provide: (value) => memoization.provide(value),
|
|
172
|
+
getContextMap: () => memoization.getContextMap()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
var createProvider = (...keyValues) => {
|
|
176
|
+
return ({ next, payload, operation }) => {
|
|
177
|
+
const store = payload ?? onlyMemoization();
|
|
178
|
+
const map = getMemoizationMap(store);
|
|
179
|
+
if (map) {
|
|
180
|
+
for (const [key, value] of keyValues) {
|
|
181
|
+
map.set(key, value);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (operation === "subscription.subscribe") {
|
|
185
|
+
return resolverPayloadStorage.run(store, async () => {
|
|
186
|
+
let result = await next();
|
|
187
|
+
if (isAsyncIterator(result)) {
|
|
188
|
+
result = bindAsyncIterator(resolverPayloadStorage, result);
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return resolverPayloadStorage.run(store, next);
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
var asyncContextProvider = Object.assign(
|
|
197
|
+
createProvider(),
|
|
198
|
+
{
|
|
199
|
+
operations: [
|
|
200
|
+
"query",
|
|
201
|
+
"mutation",
|
|
202
|
+
"field",
|
|
203
|
+
"subscription.resolve",
|
|
204
|
+
"subscription.subscribe",
|
|
205
|
+
"resolveReference"
|
|
206
|
+
],
|
|
207
|
+
with: (...keyValues) => {
|
|
208
|
+
return createProvider(...keyValues);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
// src/context/use-resolving-fields.ts
|
|
214
|
+
var useResolvingFields = createContext(
|
|
215
|
+
() => {
|
|
216
|
+
const payload = useResolverPayload();
|
|
217
|
+
if (!payload) return;
|
|
218
|
+
return getResolvingFields(payload);
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
export {
|
|
222
|
+
ContextMemoization,
|
|
223
|
+
InjectableContext,
|
|
224
|
+
asyncContextProvider,
|
|
225
|
+
bindAsyncIterator,
|
|
226
|
+
createContext,
|
|
227
|
+
createMemoization,
|
|
228
|
+
isAsyncIterator,
|
|
229
|
+
resolverPayloadStorage,
|
|
230
|
+
useContext,
|
|
231
|
+
useMemoizationMap,
|
|
232
|
+
useResolverPayload,
|
|
233
|
+
useResolvingFields
|
|
234
|
+
};
|