@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/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
- // src/context/async-iterator.ts
4
+ //#region src/context/async-iterator.ts
12
5
  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;
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
- return value !== null && typeof value === "object" && "next" in value && typeof value.next === "function";
15
+ return value !== null && typeof value === "object" && "next" in value && typeof value.next === "function";
23
16
  }
24
17
 
25
- // src/context/context.ts
26
- var resolverPayloadStorage = new AsyncLocalStorage();
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
- const payload = resolverPayloadStorage.getStore();
29
- if (payload === void 0 || isOnlyMemoryPayload(payload)) return;
30
- return payload;
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
- return useResolverPayload()?.context;
38
+ return useResolverPayload()?.context;
34
39
  }
40
+ /**
41
+ * use the MemoizationMap of the current context
42
+ */
35
43
  function useMemoizationMap() {
36
- const payload = resolverPayloadStorage.getStore();
37
- if (payload == null) return;
38
- return getMemoizationMap(payload);
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
- * 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
- }
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
- 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
- }
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
- 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
- });
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
- 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
- });
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
- 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
- };
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
- 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
- run: async (resolve, ...keyValues) => {
211
- const store = onlyMemoization();
212
- const map = getMemoizationMap(store);
213
- for (const [key, value] of keyValues) {
214
- map.set(key, value);
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
- // src/context/use-resolving-fields.ts
222
- var useResolvingFields = createContext(
223
- () => {
224
- const payload = useResolverPayload();
225
- if (!payload) return;
226
- return getResolvingFields(payload);
227
- }
228
- );
229
- export {
230
- ContextMemoization,
231
- InjectableContext,
232
- asyncContextProvider,
233
- bindAsyncIterator,
234
- createContext,
235
- createMemoization,
236
- isAsyncIterator,
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 };