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