@graphql-box/worker-client 5.4.2 → 5.4.4

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/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.2",
4
+ "version": "5.4.4",
5
5
  "author": "Dylan Aubrey",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/graphql-box",
package/src/main.ts CHANGED
@@ -102,7 +102,6 @@ export class WorkerClient {
102
102
  private _experimentalDeferStreamSupport: boolean;
103
103
  private _messageQueue: MessageRequestPayload[] = [];
104
104
  private _pending: PendingTracker = new Map();
105
-
106
105
  private _worker: Worker | undefined;
107
106
 
108
107
  constructor(options: UserOptions) {
@@ -116,7 +115,7 @@ export class WorkerClient {
116
115
  errors.push(new ArgsError('@graphql-box/worker-client expected options.cache.'));
117
116
  }
118
117
 
119
- if (!('worker' in options)) {
118
+ if (!options.lazyWorkerInit && !('worker' in options)) {
120
119
  errors.push(new ArgsError('@graphql-box/worker-client expected options.worker.'));
121
120
  }
122
121
 
@@ -130,8 +129,7 @@ export class WorkerClient {
130
129
  this._experimentalDeferStreamSupport = options.experimentalDeferStreamSupport ?? false;
131
130
 
132
131
  if (typeof options.worker === 'function') {
133
- options
134
- .worker()
132
+ Promise.resolve(options.worker())
135
133
  .then(worker => {
136
134
  this._worker = worker;
137
135
  this._addEventListener();
@@ -140,7 +138,7 @@ export class WorkerClient {
140
138
  .catch((error: unknown) => {
141
139
  throw error;
142
140
  });
143
- } else {
141
+ } else if (options.worker) {
144
142
  this._worker = options.worker;
145
143
  this._addEventListener();
146
144
  }
@@ -166,6 +164,12 @@ export class WorkerClient {
166
164
  return this._subscribe(request, options, this._getRequestContext(OperationTypeNode.SUBSCRIPTION, request));
167
165
  }
168
166
 
167
+ public set worker(worker: Worker) {
168
+ this._worker = worker;
169
+ this._addEventListener();
170
+ this._releaseMessageQueue();
171
+ }
172
+
169
173
  private _addEventListener(): void {
170
174
  if (!this._worker) {
171
175
  throw new Error('A worker is required for the WorkerClient to work correctly.');
package/src/types.ts CHANGED
@@ -12,22 +12,24 @@ export interface UserOptions {
12
12
  * The cache.
13
13
  */
14
14
  cache: CoreWorker;
15
-
16
15
  /**
17
16
  * The debug manager.
18
17
  */
19
18
  debugManager?: DebugManagerDef;
20
-
21
19
  /**
22
20
  * Enable support for defer and stream directives. Based on version
23
21
  * of spec in 16.1.0-experimental-stream-defer.6
24
22
  */
25
23
  experimentalDeferStreamSupport?: boolean;
26
-
24
+ /**
25
+ * Must be passed in as true if you are going to
26
+ * initialise the worker after the constructor.
27
+ */
28
+ lazyWorkerInit?: boolean;
27
29
  /**
28
30
  * The web worker instance.
29
31
  */
30
- worker: Worker | (() => Promise<Worker>);
32
+ worker?: Worker | (() => Worker | Promise<Worker>);
31
33
  }
32
34
 
33
35
  export type MethodNames = 'request' | 'subscribe';