houdini 1.0.2 → 1.0.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.
@@ -15480,109 +15480,6 @@ var require_minimatch = __commonJS({
15480
15480
  }
15481
15481
  });
15482
15482
 
15483
- // ../../node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js
15484
- var require_cjs = __commonJS({
15485
- "../../node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js"(exports, module) {
15486
- "use strict";
15487
- var isMergeableObject = function isMergeableObject2(value) {
15488
- return isNonNullObject(value) && !isSpecial(value);
15489
- };
15490
- function isNonNullObject(value) {
15491
- return !!value && typeof value === "object";
15492
- }
15493
- function isSpecial(value) {
15494
- var stringValue = Object.prototype.toString.call(value);
15495
- return stringValue === "[object RegExp]" || stringValue === "[object Date]" || isReactElement(value);
15496
- }
15497
- var canUseSymbol = typeof Symbol === "function" && Symbol.for;
15498
- var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for("react.element") : 60103;
15499
- function isReactElement(value) {
15500
- return value.$$typeof === REACT_ELEMENT_TYPE;
15501
- }
15502
- function emptyTarget(val) {
15503
- return Array.isArray(val) ? [] : {};
15504
- }
15505
- function cloneUnlessOtherwiseSpecified(value, options) {
15506
- return options.clone !== false && options.isMergeableObject(value) ? deepmerge(emptyTarget(value), value, options) : value;
15507
- }
15508
- function defaultArrayMerge(target, source, options) {
15509
- return target.concat(source).map(function(element) {
15510
- return cloneUnlessOtherwiseSpecified(element, options);
15511
- });
15512
- }
15513
- function getMergeFunction(key, options) {
15514
- if (!options.customMerge) {
15515
- return deepmerge;
15516
- }
15517
- var customMerge = options.customMerge(key);
15518
- return typeof customMerge === "function" ? customMerge : deepmerge;
15519
- }
15520
- function getEnumerableOwnPropertySymbols(target) {
15521
- return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter(function(symbol) {
15522
- return target.propertyIsEnumerable(symbol);
15523
- }) : [];
15524
- }
15525
- function getKeys(target) {
15526
- return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target));
15527
- }
15528
- function propertyIsOnObject(object, property) {
15529
- try {
15530
- return property in object;
15531
- } catch (_) {
15532
- return false;
15533
- }
15534
- }
15535
- function propertyIsUnsafe(target, key) {
15536
- return propertyIsOnObject(target, key) && !(Object.hasOwnProperty.call(target, key) && Object.propertyIsEnumerable.call(target, key));
15537
- }
15538
- function mergeObject(target, source, options) {
15539
- var destination = {};
15540
- if (options.isMergeableObject(target)) {
15541
- getKeys(target).forEach(function(key) {
15542
- destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
15543
- });
15544
- }
15545
- getKeys(source).forEach(function(key) {
15546
- if (propertyIsUnsafe(target, key)) {
15547
- return;
15548
- }
15549
- if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) {
15550
- destination[key] = getMergeFunction(key, options)(target[key], source[key], options);
15551
- } else {
15552
- destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
15553
- }
15554
- });
15555
- return destination;
15556
- }
15557
- function deepmerge(target, source, options) {
15558
- options = options || {};
15559
- options.arrayMerge = options.arrayMerge || defaultArrayMerge;
15560
- options.isMergeableObject = options.isMergeableObject || isMergeableObject;
15561
- options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified;
15562
- var sourceIsArray = Array.isArray(source);
15563
- var targetIsArray = Array.isArray(target);
15564
- var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
15565
- if (!sourceAndTargetTypesMatch) {
15566
- return cloneUnlessOtherwiseSpecified(source, options);
15567
- } else if (sourceIsArray) {
15568
- return options.arrayMerge(target, source, options);
15569
- } else {
15570
- return mergeObject(target, source, options);
15571
- }
15572
- }
15573
- deepmerge.all = function deepmergeAll(array, options) {
15574
- if (!Array.isArray(array)) {
15575
- throw new Error("first argument should be an array");
15576
- }
15577
- return array.reduce(function(prev, next) {
15578
- return deepmerge(prev, next, options);
15579
- }, {});
15580
- };
15581
- var deepmerge_1 = deepmerge;
15582
- module.exports = deepmerge_1;
15583
- }
15584
- });
15585
-
15586
15483
  // ../../node_modules/.pnpm/universalify@2.0.0/node_modules/universalify/index.js
15587
15484
  var require_universalify = __commonJS({
15588
15485
  "../../node_modules/.pnpm/universalify@2.0.0/node_modules/universalify/index.js"(exports) {
@@ -54005,11 +53902,11 @@ var require_esprima2 = __commonJS({
54005
53902
  case "}":
54006
53903
  regex = false;
54007
53904
  if (this.values[this.curly - 3] === "function") {
54008
- var check = this.values[this.curly - 4];
54009
- regex = check ? !this.beforeFunctionExpression(check) : false;
53905
+ var check2 = this.values[this.curly - 4];
53906
+ regex = check2 ? !this.beforeFunctionExpression(check2) : false;
54010
53907
  } else if (this.values[this.curly - 4] === "function") {
54011
- var check = this.values[this.curly - 5];
54012
- regex = check ? !this.beforeFunctionExpression(check) : true;
53908
+ var check2 = this.values[this.curly - 5];
53909
+ regex = check2 ? !this.beforeFunctionExpression(check2) : true;
54013
53910
  }
54014
53911
  break;
54015
53912
  default:
@@ -58392,6 +58289,109 @@ var require_main2 = __commonJS({
58392
58289
  }
58393
58290
  });
58394
58291
 
58292
+ // ../../node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js
58293
+ var require_cjs = __commonJS({
58294
+ "../../node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js"(exports, module) {
58295
+ "use strict";
58296
+ var isMergeableObject = function isMergeableObject2(value) {
58297
+ return isNonNullObject(value) && !isSpecial(value);
58298
+ };
58299
+ function isNonNullObject(value) {
58300
+ return !!value && typeof value === "object";
58301
+ }
58302
+ function isSpecial(value) {
58303
+ var stringValue = Object.prototype.toString.call(value);
58304
+ return stringValue === "[object RegExp]" || stringValue === "[object Date]" || isReactElement(value);
58305
+ }
58306
+ var canUseSymbol = typeof Symbol === "function" && Symbol.for;
58307
+ var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for("react.element") : 60103;
58308
+ function isReactElement(value) {
58309
+ return value.$$typeof === REACT_ELEMENT_TYPE;
58310
+ }
58311
+ function emptyTarget(val) {
58312
+ return Array.isArray(val) ? [] : {};
58313
+ }
58314
+ function cloneUnlessOtherwiseSpecified(value, options) {
58315
+ return options.clone !== false && options.isMergeableObject(value) ? deepmerge(emptyTarget(value), value, options) : value;
58316
+ }
58317
+ function defaultArrayMerge(target, source, options) {
58318
+ return target.concat(source).map(function(element) {
58319
+ return cloneUnlessOtherwiseSpecified(element, options);
58320
+ });
58321
+ }
58322
+ function getMergeFunction(key, options) {
58323
+ if (!options.customMerge) {
58324
+ return deepmerge;
58325
+ }
58326
+ var customMerge = options.customMerge(key);
58327
+ return typeof customMerge === "function" ? customMerge : deepmerge;
58328
+ }
58329
+ function getEnumerableOwnPropertySymbols(target) {
58330
+ return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter(function(symbol) {
58331
+ return target.propertyIsEnumerable(symbol);
58332
+ }) : [];
58333
+ }
58334
+ function getKeys(target) {
58335
+ return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target));
58336
+ }
58337
+ function propertyIsOnObject(object, property) {
58338
+ try {
58339
+ return property in object;
58340
+ } catch (_) {
58341
+ return false;
58342
+ }
58343
+ }
58344
+ function propertyIsUnsafe(target, key) {
58345
+ return propertyIsOnObject(target, key) && !(Object.hasOwnProperty.call(target, key) && Object.propertyIsEnumerable.call(target, key));
58346
+ }
58347
+ function mergeObject(target, source, options) {
58348
+ var destination = {};
58349
+ if (options.isMergeableObject(target)) {
58350
+ getKeys(target).forEach(function(key) {
58351
+ destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
58352
+ });
58353
+ }
58354
+ getKeys(source).forEach(function(key) {
58355
+ if (propertyIsUnsafe(target, key)) {
58356
+ return;
58357
+ }
58358
+ if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) {
58359
+ destination[key] = getMergeFunction(key, options)(target[key], source[key], options);
58360
+ } else {
58361
+ destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
58362
+ }
58363
+ });
58364
+ return destination;
58365
+ }
58366
+ function deepmerge(target, source, options) {
58367
+ options = options || {};
58368
+ options.arrayMerge = options.arrayMerge || defaultArrayMerge;
58369
+ options.isMergeableObject = options.isMergeableObject || isMergeableObject;
58370
+ options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified;
58371
+ var sourceIsArray = Array.isArray(source);
58372
+ var targetIsArray = Array.isArray(target);
58373
+ var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
58374
+ if (!sourceAndTargetTypesMatch) {
58375
+ return cloneUnlessOtherwiseSpecified(source, options);
58376
+ } else if (sourceIsArray) {
58377
+ return options.arrayMerge(target, source, options);
58378
+ } else {
58379
+ return mergeObject(target, source, options);
58380
+ }
58381
+ }
58382
+ deepmerge.all = function deepmergeAll(array, options) {
58383
+ if (!Array.isArray(array)) {
58384
+ throw new Error("first argument should be an array");
58385
+ }
58386
+ return array.reduce(function(prev, next) {
58387
+ return deepmerge(prev, next, options);
58388
+ }, {});
58389
+ };
58390
+ var deepmerge_1 = deepmerge;
58391
+ module.exports = deepmerge_1;
58392
+ }
58393
+ });
58394
+
58395
58395
  // src/lib/pipeline.ts
58396
58396
  async function runPipeline(config, pipeline2, target) {
58397
58397
  for (const transform of pipeline2) {
@@ -61849,9 +61849,6 @@ var houdini_mode = {
61849
61849
  }
61850
61850
  };
61851
61851
 
61852
- // src/lib/deepMerge.ts
61853
- var import_deepmerge = __toESM(require_cjs(), 1);
61854
-
61855
61852
  // src/lib/error.ts
61856
61853
  var HoudiniError = class extends Error {
61857
61854
  filepath = null;
@@ -61871,20 +61868,6 @@ var HoudiniError = class extends Error {
61871
61868
  }
61872
61869
  };
61873
61870
 
61874
- // src/lib/deepMerge.ts
61875
- function deepMerge(filepath, ...targets) {
61876
- try {
61877
- if (targets.length === 1) {
61878
- return targets[0];
61879
- } else if (targets.length === 2) {
61880
- return (0, import_deepmerge.default)(targets[0], targets[1]);
61881
- }
61882
- return deepMerge(filepath, targets[0], deepMerge(filepath, ...targets.slice(1)));
61883
- } catch (e2) {
61884
- throw new HoudiniError({ filepath, message: "could not merge: " + targets });
61885
- }
61886
- }
61887
-
61888
61871
  // src/lib/fs.ts
61889
61872
  var fs_exports = {};
61890
61873
  __export(fs_exports, {
@@ -63438,14 +63421,14 @@ function flatten(source) {
63438
63421
  var fetch2 = (target) => {
63439
63422
  return () => {
63440
63423
  return {
63441
- async network(ctx, { client, resolve: resolve2, marshalVariables: marshalVariables2 }) {
63424
+ async network(ctx, { client: client2, resolve: resolve2, marshalVariables: marshalVariables2 }) {
63442
63425
  const fetch3 = ctx.fetch ?? globalThis.fetch;
63443
63426
  const fetchParams2 = {
63444
63427
  text: ctx.text,
63445
63428
  hash: ctx.hash,
63446
63429
  variables: marshalVariables2(ctx)
63447
63430
  };
63448
- let fetchFn = defaultFetch(client.url, ctx.fetchParams);
63431
+ let fetchFn = defaultFetch(client2.url, ctx.fetchParams);
63449
63432
  if (target) {
63450
63433
  if (typeof target === "string") {
63451
63434
  fetchFn = defaultFetch(target, ctx.fetchParams);
@@ -65624,11 +65607,10 @@ var mutation = documentPlugin(ArtifactKind.Mutation, () => {
65624
65607
  });
65625
65608
 
65626
65609
  // src/runtime/client/plugins/subscription.ts
65610
+ var check = null;
65627
65611
  function subscription(factory) {
65628
65612
  return documentPlugin(ArtifactKind.Subscription, () => {
65629
65613
  let clearSubscription = null;
65630
- let socketClient = null;
65631
- let check = null;
65632
65614
  return {
65633
65615
  start(ctx, { resolve: resolve2, next, initialValue }) {
65634
65616
  if (typeof globalThis.window === "undefined") {
@@ -65637,7 +65619,7 @@ function subscription(factory) {
65637
65619
  }
65638
65620
  next(ctx);
65639
65621
  },
65640
- network(ctx, { resolve: resolve2, initialValue, variablesChanged: variablesChanged2, marshalVariables: marshalVariables2 }) {
65622
+ async network(ctx, { resolve: resolve2, initialValue, variablesChanged: variablesChanged2, marshalVariables: marshalVariables2 }) {
65641
65623
  const checkValue = {
65642
65624
  fetchParams: ctx.fetchParams ?? {},
65643
65625
  session: ctx.session ?? {},
@@ -65650,11 +65632,11 @@ function subscription(factory) {
65650
65632
  return;
65651
65633
  }
65652
65634
  check = checkValue;
65653
- clearSubscription?.();
65654
- if (!socketClient || sessionChange) {
65655
- socketClient = factory(ctx);
65635
+ if (sessionChange) {
65636
+ await loadClient(ctx, factory);
65656
65637
  }
65657
- clearSubscription = socketClient.subscribe(
65638
+ clearSubscription?.();
65639
+ clearSubscription = client.subscribe(
65658
65640
  {
65659
65641
  query: ctx.artifact.raw,
65660
65642
  variables: marshalVariables2(ctx)
@@ -65694,6 +65676,19 @@ function subscription(factory) {
65694
65676
  };
65695
65677
  });
65696
65678
  }
65679
+ var pendingCreate = null;
65680
+ var client;
65681
+ function loadClient(ctx, factory) {
65682
+ if (pendingCreate) {
65683
+ return pendingCreate;
65684
+ }
65685
+ pendingCreate = new Promise((resolve2) => {
65686
+ client = factory(ctx);
65687
+ resolve2();
65688
+ pendingCreate = null;
65689
+ });
65690
+ return pendingCreate;
65691
+ }
65697
65692
 
65698
65693
  // src/runtime/client/plugins/throwOnError.ts
65699
65694
  var throwOnError = ({ operations, error }) => () => {
@@ -65752,7 +65747,7 @@ var DocumentStore = class extends Writable {
65752
65747
  artifact,
65753
65748
  plugins: plugins2,
65754
65749
  pipeline: pipeline2,
65755
- client,
65750
+ client: client2,
65756
65751
  cache = true,
65757
65752
  initialValue,
65758
65753
  fetching
@@ -65770,13 +65765,11 @@ var DocumentStore = class extends Writable {
65770
65765
  super(initialState, () => {
65771
65766
  return () => {
65772
65767
  this.#lastVariables = null;
65773
- for (const plugin2 of this.#plugins) {
65774
- plugin2.cleanup?.(this.#lastContext);
65775
- }
65768
+ this.cleanup();
65776
65769
  };
65777
65770
  });
65778
65771
  this.#artifact = artifact;
65779
- this.#client = client;
65772
+ this.#client = client2;
65780
65773
  this.#lastVariables = null;
65781
65774
  this.#configFile = getCurrentConfig();
65782
65775
  this.#plugins = pipeline2 ?? [
@@ -65838,6 +65831,11 @@ var DocumentStore = class extends Writable {
65838
65831
  this.#step("forward", state);
65839
65832
  });
65840
65833
  }
65834
+ async cleanup() {
65835
+ for (const plugin2 of this.#plugins) {
65836
+ plugin2.cleanup?.(this.#lastContext);
65837
+ }
65838
+ }
65841
65839
  #step(direction, ctx, value) {
65842
65840
  const hook = direction === "error" ? "catch" : steps[direction][ctx.currentStep];
65843
65841
  let valid = (i2) => i2 <= this.#plugins.length;
@@ -66687,11 +66685,12 @@ async function getConfig({
66687
66685
  for (const plugin2 of plugins2) {
66688
66686
  if (plugin2.config) {
66689
66687
  try {
66690
- const configFactory = await import(plugin2.config);
66691
- const newValue = typeof configFactory === "function" ? configFactory(configFile) : configFactory;
66692
- configFile = deepMerge(configPath, configFile, newValue);
66688
+ const configFactory = (await import(plugin2.config)).default;
66689
+ if (configFactory) {
66690
+ configFile = typeof configFactory === "function" ? configFactory(configFile) : configFactory;
66691
+ }
66693
66692
  } catch {
66694
- console.log("could not load config file" + plugin2.config);
66693
+ console.log("could not load config file " + plugin2.config);
66695
66694
  }
66696
66695
  }
66697
66696
  }
@@ -67099,6 +67098,21 @@ async function cleanupFiles(pathFolder, listOfObj) {
67099
67098
  return allFilesNotInList;
67100
67099
  }
67101
67100
 
67101
+ // src/lib/deepMerge.ts
67102
+ var import_deepmerge = __toESM(require_cjs(), 1);
67103
+ function deepMerge(filepath, ...targets) {
67104
+ try {
67105
+ if (targets.length === 1) {
67106
+ return targets[0];
67107
+ } else if (targets.length === 2) {
67108
+ return (0, import_deepmerge.default)(targets[0], targets[1]);
67109
+ }
67110
+ return deepMerge(filepath, targets[0], deepMerge(filepath, ...targets.slice(1)));
67111
+ } catch (e2) {
67112
+ throw new HoudiniError({ filepath, message: "could not merge: " + targets });
67113
+ }
67114
+ }
67115
+
67102
67116
  // ../../node_modules/.pnpm/estree-walker@3.0.1/node_modules/estree-walker/src/walker.js
67103
67117
  var WalkerBase = class {
67104
67118
  constructor() {
@@ -15,6 +15,7 @@ export declare class DocumentStore<_Data extends GraphQLObject, _Input extends R
15
15
  fetching?: boolean;
16
16
  });
17
17
  send({ metadata, session, fetch, variables, policy, stuff, cacheParams, setup, silenceEcho, }?: SendParams): Promise<QueryResult<_Data, _Input>>;
18
+ cleanup(): Promise<void>;
18
19
  }
19
20
  declare function marshalVariables<_Data extends GraphQLObject, _Input extends {}>(ctx: ClientPluginContext): Record<string, any>;
20
21
  export type ClientPlugin = () => ClientHooks | null | (ClientHooks | ClientPlugin | null)[];
@@ -1,6 +1,7 @@
1
1
  import type { ClientPluginContext } from '../documentStore';
2
2
  export declare function subscription(factory: SubscriptionHandler): import("../documentStore").ClientPlugin;
3
- export type SubscriptionHandler = (ctx: ClientPluginContext) => {
3
+ export type SubscriptionHandler = (ctx: ClientPluginContext) => SubscriptionClient;
4
+ export type SubscriptionClient = {
4
5
  subscribe: (payload: {
5
6
  query: string;
6
7
  variables?: {};
@@ -15,6 +15,7 @@ export declare class DocumentStore<_Data extends GraphQLObject, _Input extends R
15
15
  fetching?: boolean;
16
16
  });
17
17
  send({ metadata, session, fetch, variables, policy, stuff, cacheParams, setup, silenceEcho, }?: SendParams): Promise<QueryResult<_Data, _Input>>;
18
+ cleanup(): Promise<void>;
18
19
  }
19
20
  declare function marshalVariables<_Data extends GraphQLObject, _Input extends {}>(ctx: ClientPluginContext): Record<string, any>;
20
21
  export type ClientPlugin = () => ClientHooks | null | (ClientHooks | ClientPlugin | null)[];
@@ -60,9 +60,7 @@ class DocumentStore extends import_store.Writable {
60
60
  super(initialState, () => {
61
61
  return () => {
62
62
  this.#lastVariables = null;
63
- for (const plugin of this.#plugins) {
64
- plugin.cleanup?.(this.#lastContext);
65
- }
63
+ this.cleanup();
66
64
  };
67
65
  });
68
66
  this.#artifact = artifact;
@@ -128,6 +126,11 @@ class DocumentStore extends import_store.Writable {
128
126
  this.#step("forward", state);
129
127
  });
130
128
  }
129
+ async cleanup() {
130
+ for (const plugin of this.#plugins) {
131
+ plugin.cleanup?.(this.#lastContext);
132
+ }
133
+ }
131
134
  #step(direction, ctx, value) {
132
135
  const hook = direction === "error" ? "catch" : steps[direction][ctx.currentStep];
133
136
  let valid = (i) => i <= this.#plugins.length;
@@ -1,6 +1,7 @@
1
1
  import type { ClientPluginContext } from '../documentStore';
2
2
  export declare function subscription(factory: SubscriptionHandler): import("../documentStore").ClientPlugin;
3
- export type SubscriptionHandler = (ctx: ClientPluginContext) => {
3
+ export type SubscriptionHandler = (ctx: ClientPluginContext) => SubscriptionClient;
4
+ export type SubscriptionClient = {
4
5
  subscribe: (payload: {
5
6
  query: string;
6
7
  variables?: {};
@@ -24,11 +24,10 @@ module.exports = __toCommonJS(subscription_exports);
24
24
  var import_deepEquals = require("../../lib/deepEquals");
25
25
  var import_types = require("../../lib/types");
26
26
  var import_utils = require("../utils");
27
+ let check = null;
27
28
  function subscription(factory) {
28
29
  return (0, import_utils.documentPlugin)(import_types.ArtifactKind.Subscription, () => {
29
30
  let clearSubscription = null;
30
- let socketClient = null;
31
- let check = null;
32
31
  return {
33
32
  start(ctx, { resolve, next, initialValue }) {
34
33
  if (typeof globalThis.window === "undefined") {
@@ -37,7 +36,7 @@ function subscription(factory) {
37
36
  }
38
37
  next(ctx);
39
38
  },
40
- network(ctx, { resolve, initialValue, variablesChanged, marshalVariables }) {
39
+ async network(ctx, { resolve, initialValue, variablesChanged, marshalVariables }) {
41
40
  const checkValue = {
42
41
  fetchParams: ctx.fetchParams ?? {},
43
42
  session: ctx.session ?? {},
@@ -50,11 +49,11 @@ function subscription(factory) {
50
49
  return;
51
50
  }
52
51
  check = checkValue;
53
- clearSubscription?.();
54
- if (!socketClient || sessionChange) {
55
- socketClient = factory(ctx);
52
+ if (sessionChange) {
53
+ await loadClient(ctx, factory);
56
54
  }
57
- clearSubscription = socketClient.subscribe(
55
+ clearSubscription?.();
56
+ clearSubscription = client.subscribe(
58
57
  {
59
58
  query: ctx.artifact.raw,
60
59
  variables: marshalVariables(ctx)
@@ -94,6 +93,19 @@ function subscription(factory) {
94
93
  };
95
94
  });
96
95
  }
96
+ let pendingCreate = null;
97
+ let client;
98
+ function loadClient(ctx, factory) {
99
+ if (pendingCreate) {
100
+ return pendingCreate;
101
+ }
102
+ pendingCreate = new Promise((resolve) => {
103
+ client = factory(ctx);
104
+ resolve();
105
+ pendingCreate = null;
106
+ });
107
+ return pendingCreate;
108
+ }
97
109
  // Annotate the CommonJS export names for ESM import in node:
98
110
  0 && (module.exports = {
99
111
  subscription
@@ -15,6 +15,7 @@ export declare class DocumentStore<_Data extends GraphQLObject, _Input extends R
15
15
  fetching?: boolean;
16
16
  });
17
17
  send({ metadata, session, fetch, variables, policy, stuff, cacheParams, setup, silenceEcho, }?: SendParams): Promise<QueryResult<_Data, _Input>>;
18
+ cleanup(): Promise<void>;
18
19
  }
19
20
  declare function marshalVariables<_Data extends GraphQLObject, _Input extends {}>(ctx: ClientPluginContext): Record<string, any>;
20
21
  export type ClientPlugin = () => ClientHooks | null | (ClientHooks | ClientPlugin | null)[];
@@ -37,9 +37,7 @@ class DocumentStore extends Writable {
37
37
  super(initialState, () => {
38
38
  return () => {
39
39
  this.#lastVariables = null;
40
- for (const plugin of this.#plugins) {
41
- plugin.cleanup?.(this.#lastContext);
42
- }
40
+ this.cleanup();
43
41
  };
44
42
  });
45
43
  this.#artifact = artifact;
@@ -105,6 +103,11 @@ class DocumentStore extends Writable {
105
103
  this.#step("forward", state);
106
104
  });
107
105
  }
106
+ async cleanup() {
107
+ for (const plugin of this.#plugins) {
108
+ plugin.cleanup?.(this.#lastContext);
109
+ }
110
+ }
108
111
  #step(direction, ctx, value) {
109
112
  const hook = direction === "error" ? "catch" : steps[direction][ctx.currentStep];
110
113
  let valid = (i) => i <= this.#plugins.length;
@@ -1,6 +1,7 @@
1
1
  import type { ClientPluginContext } from '../documentStore';
2
2
  export declare function subscription(factory: SubscriptionHandler): import("../documentStore").ClientPlugin;
3
- export type SubscriptionHandler = (ctx: ClientPluginContext) => {
3
+ export type SubscriptionHandler = (ctx: ClientPluginContext) => SubscriptionClient;
4
+ export type SubscriptionClient = {
4
5
  subscribe: (payload: {
5
6
  query: string;
6
7
  variables?: {};
@@ -1,11 +1,10 @@
1
1
  import { deepEquals } from "../../lib/deepEquals";
2
2
  import { ArtifactKind, DataSource } from "../../lib/types";
3
3
  import { documentPlugin } from "../utils";
4
+ let check = null;
4
5
  function subscription(factory) {
5
6
  return documentPlugin(ArtifactKind.Subscription, () => {
6
7
  let clearSubscription = null;
7
- let socketClient = null;
8
- let check = null;
9
8
  return {
10
9
  start(ctx, { resolve, next, initialValue }) {
11
10
  if (typeof globalThis.window === "undefined") {
@@ -14,7 +13,7 @@ function subscription(factory) {
14
13
  }
15
14
  next(ctx);
16
15
  },
17
- network(ctx, { resolve, initialValue, variablesChanged, marshalVariables }) {
16
+ async network(ctx, { resolve, initialValue, variablesChanged, marshalVariables }) {
18
17
  const checkValue = {
19
18
  fetchParams: ctx.fetchParams ?? {},
20
19
  session: ctx.session ?? {},
@@ -27,11 +26,11 @@ function subscription(factory) {
27
26
  return;
28
27
  }
29
28
  check = checkValue;
30
- clearSubscription?.();
31
- if (!socketClient || sessionChange) {
32
- socketClient = factory(ctx);
29
+ if (sessionChange) {
30
+ await loadClient(ctx, factory);
33
31
  }
34
- clearSubscription = socketClient.subscribe(
32
+ clearSubscription?.();
33
+ clearSubscription = client.subscribe(
35
34
  {
36
35
  query: ctx.artifact.raw,
37
36
  variables: marshalVariables(ctx)
@@ -71,6 +70,19 @@ function subscription(factory) {
71
70
  };
72
71
  });
73
72
  }
73
+ let pendingCreate = null;
74
+ let client;
75
+ function loadClient(ctx, factory) {
76
+ if (pendingCreate) {
77
+ return pendingCreate;
78
+ }
79
+ pendingCreate = new Promise((resolve) => {
80
+ client = factory(ctx);
81
+ resolve();
82
+ pendingCreate = null;
83
+ });
84
+ return pendingCreate;
85
+ }
74
86
  export {
75
87
  subscription
76
88
  };