@graphql-box/worker-client 5.4.0 → 5.4.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/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/production.analysis.txt +22 -22
- package/dist/types/cjs/main.d.cts +2 -0
- package/dist/types/cjs/main.d.cts.map +1 -1
- package/dist/types/cjs/types.d.cts +1 -1
- package/dist/types/cjs/types.d.cts.map +1 -1
- package/dist/types/esm/main.d.ts +2 -0
- package/dist/types/esm/main.d.ts.map +1 -1
- package/dist/types/esm/types.d.ts +1 -1
- package/dist/types/esm/types.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/main.ts +68 -17
- package/src/registerWorker.ts +1 -1
- package/src/types.ts +1 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-box/worker-client",
|
|
3
3
|
"description": "The GraphQL Box web worker client module.",
|
|
4
|
-
"version": "5.4.
|
|
4
|
+
"version": "5.4.2",
|
|
5
5
|
"author": "Dylan Aubrey",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/badbatch/graphql-box",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"iterall": "^1.3.0",
|
|
42
42
|
"lodash-es": "^4.17.21",
|
|
43
43
|
"uuid": "^11.0.3",
|
|
44
|
-
"@graphql-box/
|
|
45
|
-
"@graphql-box/
|
|
44
|
+
"@graphql-box/helpers": "5.4.0",
|
|
45
|
+
"@graphql-box/core": "5.4.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"graphql": "<17",
|
package/src/main.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { logRequest } from './debug/logRequest.ts';
|
|
|
26
26
|
import { logSubscription } from './debug/logSubscription.ts';
|
|
27
27
|
import {
|
|
28
28
|
type MessageContext,
|
|
29
|
+
type MessageRequestPayload,
|
|
29
30
|
type MessageResponsePayload,
|
|
30
31
|
type PendingResolver,
|
|
31
32
|
type PendingTracker,
|
|
@@ -99,8 +100,10 @@ export class WorkerClient {
|
|
|
99
100
|
private _debugManager: DebugManagerDef | null;
|
|
100
101
|
private _eventEmitter: EventEmitter;
|
|
101
102
|
private _experimentalDeferStreamSupport: boolean;
|
|
103
|
+
private _messageQueue: MessageRequestPayload[] = [];
|
|
102
104
|
private _pending: PendingTracker = new Map();
|
|
103
|
-
|
|
105
|
+
|
|
106
|
+
private _worker: Worker | undefined;
|
|
104
107
|
|
|
105
108
|
constructor(options: UserOptions) {
|
|
106
109
|
const errors: ArgsError[] = [];
|
|
@@ -125,8 +128,22 @@ export class WorkerClient {
|
|
|
125
128
|
this._debugManager = options.debugManager ?? null;
|
|
126
129
|
this._eventEmitter = new EventEmitter();
|
|
127
130
|
this._experimentalDeferStreamSupport = options.experimentalDeferStreamSupport ?? false;
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
|
|
132
|
+
if (typeof options.worker === 'function') {
|
|
133
|
+
options
|
|
134
|
+
.worker()
|
|
135
|
+
.then(worker => {
|
|
136
|
+
this._worker = worker;
|
|
137
|
+
this._addEventListener();
|
|
138
|
+
this._releaseMessageQueue();
|
|
139
|
+
})
|
|
140
|
+
.catch((error: unknown) => {
|
|
141
|
+
throw error;
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
this._worker = options.worker;
|
|
145
|
+
this._addEventListener();
|
|
146
|
+
}
|
|
130
147
|
}
|
|
131
148
|
|
|
132
149
|
get cache(): CoreWorker {
|
|
@@ -150,6 +167,10 @@ export class WorkerClient {
|
|
|
150
167
|
}
|
|
151
168
|
|
|
152
169
|
private _addEventListener(): void {
|
|
170
|
+
if (!this._worker) {
|
|
171
|
+
throw new Error('A worker is required for the WorkerClient to work correctly.');
|
|
172
|
+
}
|
|
173
|
+
|
|
153
174
|
this._worker.addEventListener(MESSAGE, this._onMessage);
|
|
154
175
|
}
|
|
155
176
|
|
|
@@ -176,6 +197,16 @@ export class WorkerClient {
|
|
|
176
197
|
};
|
|
177
198
|
}
|
|
178
199
|
|
|
200
|
+
private _releaseMessageQueue(): void {
|
|
201
|
+
if (!this._worker) {
|
|
202
|
+
throw new Error('A worker is required for the WorkerClient to work correctly.');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (const message of this._messageQueue) {
|
|
206
|
+
this._worker.postMessage(message);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
179
210
|
@logRequest()
|
|
180
211
|
private async _request(
|
|
181
212
|
request: string,
|
|
@@ -184,13 +215,23 @@ export class WorkerClient {
|
|
|
184
215
|
): Promise<PartialRequestResult | AsyncIterableIterator<PartialRequestResult | undefined>> {
|
|
185
216
|
try {
|
|
186
217
|
return await new Promise((resolve: PendingResolver) => {
|
|
187
|
-
this._worker
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
218
|
+
if (this._worker) {
|
|
219
|
+
this._worker.postMessage({
|
|
220
|
+
context: WorkerClient._getMessageContext(context),
|
|
221
|
+
method: REQUEST,
|
|
222
|
+
options,
|
|
223
|
+
request,
|
|
224
|
+
type: GRAPHQL_BOX,
|
|
225
|
+
});
|
|
226
|
+
} else {
|
|
227
|
+
this._messageQueue.push({
|
|
228
|
+
context: WorkerClient._getMessageContext(context),
|
|
229
|
+
method: REQUEST,
|
|
230
|
+
options,
|
|
231
|
+
request,
|
|
232
|
+
type: GRAPHQL_BOX,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
194
235
|
|
|
195
236
|
this._pending.set(context.requestID, { resolve });
|
|
196
237
|
});
|
|
@@ -210,13 +251,23 @@ export class WorkerClient {
|
|
|
210
251
|
context: RequestContext,
|
|
211
252
|
): Promise<PartialRequestResult | AsyncIterableIterator<PartialRequestResult | undefined>> {
|
|
212
253
|
try {
|
|
213
|
-
this._worker
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
254
|
+
if (this._worker) {
|
|
255
|
+
this._worker.postMessage({
|
|
256
|
+
context: WorkerClient._getMessageContext(context),
|
|
257
|
+
method: SUBSCRIBE,
|
|
258
|
+
options,
|
|
259
|
+
request,
|
|
260
|
+
type: GRAPHQL_BOX,
|
|
261
|
+
});
|
|
262
|
+
} else {
|
|
263
|
+
this._messageQueue.push({
|
|
264
|
+
context: WorkerClient._getMessageContext(context),
|
|
265
|
+
method: SUBSCRIBE,
|
|
266
|
+
options,
|
|
267
|
+
request,
|
|
268
|
+
type: GRAPHQL_BOX,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
220
271
|
|
|
221
272
|
const eventAsyncIterator = new EventAsyncIterator<PartialRequestResult>(this._eventEmitter, context.requestID);
|
|
222
273
|
return Promise.resolve(eventAsyncIterator.getIterator());
|
package/src/registerWorker.ts
CHANGED
|
@@ -96,7 +96,7 @@ export const registerWorker = ({ client }: RegisterWorkerOptions): void => {
|
|
|
96
96
|
const onMessage = ({ data }: MessageEvent<MessageRequestPayload | CachemapMessageRequestPayload>): void => {
|
|
97
97
|
if (isGraphqlBoxMessageRequestPayload(data)) {
|
|
98
98
|
handleMessage(data, client);
|
|
99
|
-
} else {
|
|
99
|
+
} else if (client.cache) {
|
|
100
100
|
void handleCachemapMessage(data, client.cache);
|
|
101
101
|
}
|
|
102
102
|
};
|