houdini 1.2.1 → 1.2.3

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.
Files changed (35) hide show
  1. package/build/cmd-cjs/index.js +149 -104
  2. package/build/cmd-esm/index.js +149 -104
  3. package/build/codegen/generators/artifacts/selection.d.ts +2 -1
  4. package/build/codegen/utils/flattenSelections.d.ts +2 -4
  5. package/build/codegen-cjs/index.js +147 -102
  6. package/build/codegen-esm/index.js +147 -102
  7. package/build/lib-cjs/index.js +103 -45
  8. package/build/lib-esm/index.js +103 -45
  9. package/build/runtime/cache/cache.d.ts +3 -1
  10. package/build/runtime/cache/lists.d.ts +3 -3
  11. package/build/runtime/cache/storage.d.ts +2 -2
  12. package/build/runtime/cache/subscription.d.ts +1 -0
  13. package/build/runtime-cjs/cache/cache.d.ts +3 -1
  14. package/build/runtime-cjs/cache/cache.js +78 -27
  15. package/build/runtime-cjs/cache/lists.d.ts +3 -3
  16. package/build/runtime-cjs/cache/lists.js +7 -7
  17. package/build/runtime-cjs/cache/storage.d.ts +2 -2
  18. package/build/runtime-cjs/cache/storage.js +5 -5
  19. package/build/runtime-cjs/cache/subscription.d.ts +1 -0
  20. package/build/runtime-cjs/cache/subscription.js +3 -0
  21. package/build/runtime-cjs/client/plugins/mutation.js +10 -6
  22. package/build/runtime-esm/cache/cache.d.ts +3 -1
  23. package/build/runtime-esm/cache/cache.js +78 -27
  24. package/build/runtime-esm/cache/lists.d.ts +3 -3
  25. package/build/runtime-esm/cache/lists.js +7 -7
  26. package/build/runtime-esm/cache/storage.d.ts +2 -2
  27. package/build/runtime-esm/cache/storage.js +5 -5
  28. package/build/runtime-esm/cache/subscription.d.ts +1 -0
  29. package/build/runtime-esm/cache/subscription.js +3 -0
  30. package/build/runtime-esm/client/plugins/mutation.js +10 -6
  31. package/build/test-cjs/index.js +147 -102
  32. package/build/test-esm/index.js +147 -102
  33. package/build/vite-cjs/index.js +147 -102
  34. package/build/vite-esm/index.js +147 -102
  35. package/package.json +1 -1
@@ -58,20 +58,7 @@ class Cache {
58
58
  }) {
59
59
  const layer = layerID ? this._internal_unstable.storage.getLayer(layerID) : this._internal_unstable.storage.topLayer;
60
60
  const subscribers = this._internal_unstable.writeSelection({ ...args, layer }).map((sub) => sub[0]);
61
- const notified = [];
62
- for (const spec of subscribers.concat(notifySubscribers)) {
63
- if (!notified.includes(spec.set)) {
64
- notified.push(spec.set);
65
- spec.set(
66
- this._internal_unstable.getSelection({
67
- parent: spec.parentID || rootID,
68
- selection: spec.selection,
69
- variables: spec.variables?.() || {},
70
- ignoreMasking: false
71
- }).data
72
- );
73
- }
74
- }
61
+ this.#notifySubscribers(subscribers.concat(notifySubscribers));
75
62
  return subscribers;
76
63
  }
77
64
  read(...args) {
@@ -110,10 +97,10 @@ class Cache {
110
97
  }
111
98
  return handler;
112
99
  }
113
- delete(id) {
100
+ delete(id, layer) {
114
101
  this._internal_unstable.subscriptions.removeAllSubscribers(id);
115
- this._internal_unstable.lists.removeIDFromAllLists(id);
116
- this._internal_unstable.storage.delete(id);
102
+ this._internal_unstable.lists.removeIDFromAllLists(id, layer);
103
+ this._internal_unstable.storage.delete(id, layer);
117
104
  }
118
105
  setConfig(config) {
119
106
  this._internal_unstable.setConfig(config);
@@ -145,6 +132,70 @@ class Cache {
145
132
  config() {
146
133
  return this._internal_unstable.config;
147
134
  }
135
+ clearLayer(layerID) {
136
+ const layer = this._internal_unstable.storage.getLayer(layerID);
137
+ if (!layer) {
138
+ throw new Error("Cannot find layer with id: " + layerID);
139
+ }
140
+ const toNotify = [];
141
+ const allFields = [];
142
+ for (const target of [layer.fields, layer.links]) {
143
+ for (const [id, fields] of Object.entries(target)) {
144
+ allFields.push(
145
+ ...Object.entries(fields).map(([field, value]) => ({ id, field, value }))
146
+ );
147
+ }
148
+ }
149
+ const displayFields = [];
150
+ for (const pair of allFields) {
151
+ const { displayLayers } = this._internal_unstable.storage.get(pair.id, pair.field);
152
+ if (!displayLayers.includes(layerID)) {
153
+ continue;
154
+ }
155
+ displayFields.push(pair);
156
+ }
157
+ for (const [id, operation] of Object.entries(layer.operations)) {
158
+ if (operation.deleted) {
159
+ displayFields.push(
160
+ ...this._internal_unstable.subscriptions.activeFields(id).map((field) => ({ id, field }))
161
+ );
162
+ }
163
+ const fields = Object.keys(operation.fields ?? {});
164
+ if (fields.length > 0) {
165
+ displayFields.push(...fields.map((field) => ({ id, field })));
166
+ }
167
+ }
168
+ layer.clear();
169
+ for (const display of displayFields) {
170
+ const { field, id } = display;
171
+ const notify = !("value" in display) || this._internal_unstable.storage.get(id, field).value !== display.value;
172
+ if (notify) {
173
+ toNotify.push(
174
+ ...this._internal_unstable.subscriptions.get(id, field).map((sub) => sub[0])
175
+ );
176
+ }
177
+ }
178
+ this.#notifySubscribers(toNotify);
179
+ }
180
+ #notifySubscribers(subs) {
181
+ if (subs.length === 0) {
182
+ return;
183
+ }
184
+ const notified = [];
185
+ for (const spec of subs) {
186
+ if (!notified.includes(spec.set)) {
187
+ notified.push(spec.set);
188
+ spec.set(
189
+ this._internal_unstable.getSelection({
190
+ parent: spec.parentID || rootID,
191
+ selection: spec.selection,
192
+ variables: spec.variables?.() || {},
193
+ ignoreMasking: false
194
+ }).data
195
+ );
196
+ }
197
+ }
198
+ }
148
199
  }
149
200
  class CacheInternal {
150
201
  _disabled = false;
@@ -432,8 +483,16 @@ class CacheInternal {
432
483
  operation.position || "last",
433
484
  layer
434
485
  );
486
+ } else if (operation.action === "toggle" && target instanceof Object && fieldSelection && operation.list) {
487
+ this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).toggleElement({
488
+ selection: fieldSelection,
489
+ data: target,
490
+ variables,
491
+ where: operation.position || "last",
492
+ layer
493
+ });
435
494
  } else if (operation.action === "remove" && target instanceof Object && fieldSelection && operation.list) {
436
- this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).remove(target, variables);
495
+ this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).remove(target, variables, layer);
437
496
  } else if (operation.action === "delete" && operation.type) {
438
497
  if (typeof target !== "string") {
439
498
  throw new Error("Cannot delete a record with a non-string ID");
@@ -442,15 +501,7 @@ class CacheInternal {
442
501
  if (!targetID) {
443
502
  continue;
444
503
  }
445
- this.cache.delete(targetID);
446
- } else if (operation.action === "toggle" && target instanceof Object && fieldSelection && operation.list) {
447
- this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).toggleElement({
448
- selection: fieldSelection,
449
- data: target,
450
- variables,
451
- where: operation.position || "last",
452
- layer
453
- });
504
+ this.cache.delete(targetID, layer);
454
505
  }
455
506
  }
456
507
  }
@@ -21,7 +21,7 @@ export declare class ListManager {
21
21
  filters?: List['filters'];
22
22
  abstract?: boolean;
23
23
  }): void;
24
- removeIDFromAllLists(id: string): void;
24
+ removeIDFromAllLists(id: string, layer?: Layer): void;
25
25
  deleteField(parentID: string, field: string): void;
26
26
  }
27
27
  export declare class List {
@@ -54,8 +54,8 @@ export declare class List {
54
54
  layer?: Layer;
55
55
  }): void;
56
56
  addToList(selection: SubscriptionSelection, data: {}, variables: {} | undefined, where: 'first' | 'last', layer?: Layer): void;
57
- removeID(id: string, variables?: {}): true | undefined;
58
- remove(data: {}, variables?: {}): true | undefined;
57
+ removeID(id: string, variables?: {}, layer?: Layer): true | undefined;
58
+ remove(data: {}, variables?: {}, layer?: Layer): true | undefined;
59
59
  listType(data: {
60
60
  __typename?: string;
61
61
  }): string;
@@ -89,10 +89,10 @@ class ListManager {
89
89
  this.lists.get(list.name).get(parentID).lists.push(handler);
90
90
  this.listsByField.get(parentID).get(list.key).push(handler);
91
91
  }
92
- removeIDFromAllLists(id) {
92
+ removeIDFromAllLists(id, layer) {
93
93
  for (const fieldMap of this.lists.values()) {
94
94
  for (const list of fieldMap.values()) {
95
- list.removeID(id);
95
+ list.removeID(id, void 0, layer);
96
96
  }
97
97
  }
98
98
  }
@@ -249,7 +249,7 @@ class List {
249
249
  layer: layer?.id
250
250
  });
251
251
  }
252
- removeID(id, variables = {}) {
252
+ removeID(id, variables = {}, layer) {
253
253
  if (!this.validateWhen()) {
254
254
  return;
255
255
  }
@@ -296,7 +296,7 @@ class List {
296
296
  subscribers.map((sub) => sub[0]),
297
297
  variables
298
298
  );
299
- this.cache._internal_unstable.storage.remove(parentID, targetKey, targetID);
299
+ this.cache._internal_unstable.storage.remove(parentID, targetKey, targetID, layer);
300
300
  for (const [spec] of subscribers) {
301
301
  spec.set(
302
302
  this.cache._internal_unstable.getSelection({
@@ -309,12 +309,12 @@ class List {
309
309
  }
310
310
  return true;
311
311
  }
312
- remove(data, variables = {}) {
312
+ remove(data, variables = {}, layer) {
313
313
  const targetID = this.cache._internal_unstable.id(this.listType(data), data);
314
314
  if (!targetID) {
315
315
  return;
316
316
  }
317
- return this.removeID(targetID, variables);
317
+ return this.removeID(targetID, variables, layer);
318
318
  }
319
319
  listType(data) {
320
320
  return data.__typename || this.type;
@@ -346,7 +346,7 @@ class List {
346
346
  layer,
347
347
  where
348
348
  }) {
349
- if (!this.remove(data, variables)) {
349
+ if (!this.remove(data, variables, layer)) {
350
350
  this.addToList(selection, data, variables, where, layer);
351
351
  }
352
352
  }
@@ -8,8 +8,8 @@ export declare class InMemoryStorage {
8
8
  get nextRank(): number;
9
9
  createLayer(optimistic?: boolean): Layer;
10
10
  insert(id: string, field: string, location: OperationLocations, target: string): void;
11
- remove(id: string, field: string, target: string): void;
12
- delete(id: string): void;
11
+ remove(id: string, field: string, target: string, layerToUser?: Layer): void;
12
+ delete(id: string, layerToUser?: Layer): void;
13
13
  deleteField(id: string, field: string): void;
14
14
  getLayer(id: number): Layer;
15
15
  replaceID(replacement: {
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(storage_exports);
27
27
  var import_flatten = require("../lib/flatten");
28
28
  class InMemoryStorage {
29
29
  data;
30
- idCount = 0;
30
+ idCount = 1;
31
31
  rank = 0;
32
32
  constructor() {
33
33
  this.data = [];
@@ -47,11 +47,11 @@ class InMemoryStorage {
47
47
  insert(id, field, location, target) {
48
48
  return this.topLayer.insert(id, field, location, target);
49
49
  }
50
- remove(id, field, target) {
51
- return this.topLayer.remove(id, field, target);
50
+ remove(id, field, target, layerToUser = this.topLayer) {
51
+ return layerToUser.remove(id, field, target);
52
52
  }
53
- delete(id) {
54
- return this.topLayer.delete(id);
53
+ delete(id, layerToUser = this.topLayer) {
54
+ return layerToUser.delete(id);
55
55
  }
56
56
  deleteField(id, field) {
57
57
  return this.topLayer.deleteField(id, field);
@@ -10,6 +10,7 @@ export declare class InMemorySubscriptions {
10
10
  private subscribers;
11
11
  private referenceCounts;
12
12
  private keyVersions;
13
+ activeFields(parent: string): string[];
13
14
  add({ parent, spec, selection, variables, parentType, }: {
14
15
  parent: string;
15
16
  parentType?: string;
@@ -32,6 +32,9 @@ class InMemorySubscriptions {
32
32
  subscribers = {};
33
33
  referenceCounts = {};
34
34
  keyVersions = {};
35
+ activeFields(parent) {
36
+ return Object.keys(this.subscribers[parent] || {});
37
+ }
35
38
  add({
36
39
  parent,
37
40
  spec,
@@ -34,7 +34,7 @@ var import_utils = require("../utils");
34
34
  const mutation = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Mutation, () => {
35
35
  return {
36
36
  async start(ctx, { next, marshalVariables }) {
37
- const layer = import_cache.default._internal_unstable.storage.createLayer(true);
37
+ const layerOptimistic = import_cache.default._internal_unstable.storage.createLayer(true);
38
38
  const optimisticResponse = ctx.stuff.optimisticResponse;
39
39
  let toNotify = [];
40
40
  if (optimisticResponse) {
@@ -45,25 +45,29 @@ const mutation = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Muta
45
45
  data: optimisticResponse
46
46
  }),
47
47
  variables: marshalVariables(ctx),
48
- layer: layer.id
48
+ layer: layerOptimistic.id
49
49
  });
50
50
  }
51
51
  ctx.cacheParams = {
52
52
  ...ctx.cacheParams,
53
- layer,
53
+ layer: layerOptimistic,
54
54
  notifySubscribers: toNotify,
55
55
  forceNotify: true
56
56
  };
57
57
  next(ctx);
58
58
  },
59
59
  afterNetwork(ctx, { resolve }) {
60
- ctx.cacheParams?.layer?.clear();
60
+ if (ctx.cacheParams?.layer) {
61
+ import_cache.default.clearLayer(ctx.cacheParams.layer.id);
62
+ }
61
63
  resolve(ctx);
62
64
  },
63
65
  end(ctx, { resolve, value }) {
64
66
  const hasErrors = value.errors && value.errors.length > 0;
65
67
  if (hasErrors) {
66
- ctx.cacheParams?.layer?.clear();
68
+ if (ctx.cacheParams?.layer) {
69
+ import_cache.default.clearLayer(ctx.cacheParams.layer.id);
70
+ }
67
71
  }
68
72
  if (ctx.cacheParams?.layer) {
69
73
  import_cache.default._internal_unstable.storage.resolveLayer(ctx.cacheParams.layer.id);
@@ -73,7 +77,7 @@ const mutation = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Muta
73
77
  catch(ctx, { error }) {
74
78
  if (ctx.cacheParams?.layer) {
75
79
  const { layer } = ctx.cacheParams;
76
- layer.clear();
80
+ import_cache.default.clearLayer(layer.id);
77
81
  import_cache.default._internal_unstable.storage.resolveLayer(layer.id);
78
82
  }
79
83
  throw error;
@@ -8,6 +8,7 @@ import type { Layer, LayerID } from './storage';
8
8
  import { InMemoryStorage } from './storage';
9
9
  import { InMemorySubscriptions, type FieldSelection } from './subscription';
10
10
  export declare class Cache {
11
+ #private;
11
12
  _internal_unstable: CacheInternal;
12
13
  constructor({ disabled, ...config }?: ConfigFile & {
13
14
  disabled?: boolean;
@@ -33,7 +34,7 @@ export declare class Cache {
33
34
  subscribe(spec: SubscriptionSpec, variables?: {}): void;
34
35
  unsubscribe(spec: SubscriptionSpec, variables?: {}): void;
35
36
  list(name: string, parentID?: string, allLists?: boolean): ListCollection;
36
- delete(id: string): void;
37
+ delete(id: string, layer?: Layer): void;
37
38
  setConfig(config: ConfigFile): void;
38
39
  markTypeStale(options?: {
39
40
  type: string;
@@ -46,6 +47,7 @@ export declare class Cache {
46
47
  }): void;
47
48
  getFieldTime(id: string, field: string): number | null | undefined;
48
49
  config(): ConfigFile;
50
+ clearLayer(layerID: Layer['id']): void;
49
51
  }
50
52
  declare class CacheInternal {
51
53
  private _disabled;
@@ -33,20 +33,7 @@ class Cache {
33
33
  }) {
34
34
  const layer = layerID ? this._internal_unstable.storage.getLayer(layerID) : this._internal_unstable.storage.topLayer;
35
35
  const subscribers = this._internal_unstable.writeSelection({ ...args, layer }).map((sub) => sub[0]);
36
- const notified = [];
37
- for (const spec of subscribers.concat(notifySubscribers)) {
38
- if (!notified.includes(spec.set)) {
39
- notified.push(spec.set);
40
- spec.set(
41
- this._internal_unstable.getSelection({
42
- parent: spec.parentID || rootID,
43
- selection: spec.selection,
44
- variables: spec.variables?.() || {},
45
- ignoreMasking: false
46
- }).data
47
- );
48
- }
49
- }
36
+ this.#notifySubscribers(subscribers.concat(notifySubscribers));
50
37
  return subscribers;
51
38
  }
52
39
  read(...args) {
@@ -85,10 +72,10 @@ class Cache {
85
72
  }
86
73
  return handler;
87
74
  }
88
- delete(id) {
75
+ delete(id, layer) {
89
76
  this._internal_unstable.subscriptions.removeAllSubscribers(id);
90
- this._internal_unstable.lists.removeIDFromAllLists(id);
91
- this._internal_unstable.storage.delete(id);
77
+ this._internal_unstable.lists.removeIDFromAllLists(id, layer);
78
+ this._internal_unstable.storage.delete(id, layer);
92
79
  }
93
80
  setConfig(config) {
94
81
  this._internal_unstable.setConfig(config);
@@ -120,6 +107,70 @@ class Cache {
120
107
  config() {
121
108
  return this._internal_unstable.config;
122
109
  }
110
+ clearLayer(layerID) {
111
+ const layer = this._internal_unstable.storage.getLayer(layerID);
112
+ if (!layer) {
113
+ throw new Error("Cannot find layer with id: " + layerID);
114
+ }
115
+ const toNotify = [];
116
+ const allFields = [];
117
+ for (const target of [layer.fields, layer.links]) {
118
+ for (const [id, fields] of Object.entries(target)) {
119
+ allFields.push(
120
+ ...Object.entries(fields).map(([field, value]) => ({ id, field, value }))
121
+ );
122
+ }
123
+ }
124
+ const displayFields = [];
125
+ for (const pair of allFields) {
126
+ const { displayLayers } = this._internal_unstable.storage.get(pair.id, pair.field);
127
+ if (!displayLayers.includes(layerID)) {
128
+ continue;
129
+ }
130
+ displayFields.push(pair);
131
+ }
132
+ for (const [id, operation] of Object.entries(layer.operations)) {
133
+ if (operation.deleted) {
134
+ displayFields.push(
135
+ ...this._internal_unstable.subscriptions.activeFields(id).map((field) => ({ id, field }))
136
+ );
137
+ }
138
+ const fields = Object.keys(operation.fields ?? {});
139
+ if (fields.length > 0) {
140
+ displayFields.push(...fields.map((field) => ({ id, field })));
141
+ }
142
+ }
143
+ layer.clear();
144
+ for (const display of displayFields) {
145
+ const { field, id } = display;
146
+ const notify = !("value" in display) || this._internal_unstable.storage.get(id, field).value !== display.value;
147
+ if (notify) {
148
+ toNotify.push(
149
+ ...this._internal_unstable.subscriptions.get(id, field).map((sub) => sub[0])
150
+ );
151
+ }
152
+ }
153
+ this.#notifySubscribers(toNotify);
154
+ }
155
+ #notifySubscribers(subs) {
156
+ if (subs.length === 0) {
157
+ return;
158
+ }
159
+ const notified = [];
160
+ for (const spec of subs) {
161
+ if (!notified.includes(spec.set)) {
162
+ notified.push(spec.set);
163
+ spec.set(
164
+ this._internal_unstable.getSelection({
165
+ parent: spec.parentID || rootID,
166
+ selection: spec.selection,
167
+ variables: spec.variables?.() || {},
168
+ ignoreMasking: false
169
+ }).data
170
+ );
171
+ }
172
+ }
173
+ }
123
174
  }
124
175
  class CacheInternal {
125
176
  _disabled = false;
@@ -407,8 +458,16 @@ class CacheInternal {
407
458
  operation.position || "last",
408
459
  layer
409
460
  );
461
+ } else if (operation.action === "toggle" && target instanceof Object && fieldSelection && operation.list) {
462
+ this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).toggleElement({
463
+ selection: fieldSelection,
464
+ data: target,
465
+ variables,
466
+ where: operation.position || "last",
467
+ layer
468
+ });
410
469
  } else if (operation.action === "remove" && target instanceof Object && fieldSelection && operation.list) {
411
- this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).remove(target, variables);
470
+ this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).remove(target, variables, layer);
412
471
  } else if (operation.action === "delete" && operation.type) {
413
472
  if (typeof target !== "string") {
414
473
  throw new Error("Cannot delete a record with a non-string ID");
@@ -417,15 +476,7 @@ class CacheInternal {
417
476
  if (!targetID) {
418
477
  continue;
419
478
  }
420
- this.cache.delete(targetID);
421
- } else if (operation.action === "toggle" && target instanceof Object && fieldSelection && operation.list) {
422
- this.cache.list(operation.list, parentID, operation.target === "all").when(operation.when).toggleElement({
423
- selection: fieldSelection,
424
- data: target,
425
- variables,
426
- where: operation.position || "last",
427
- layer
428
- });
479
+ this.cache.delete(targetID, layer);
429
480
  }
430
481
  }
431
482
  }
@@ -21,7 +21,7 @@ export declare class ListManager {
21
21
  filters?: List['filters'];
22
22
  abstract?: boolean;
23
23
  }): void;
24
- removeIDFromAllLists(id: string): void;
24
+ removeIDFromAllLists(id: string, layer?: Layer): void;
25
25
  deleteField(parentID: string, field: string): void;
26
26
  }
27
27
  export declare class List {
@@ -54,8 +54,8 @@ export declare class List {
54
54
  layer?: Layer;
55
55
  }): void;
56
56
  addToList(selection: SubscriptionSelection, data: {}, variables: {} | undefined, where: 'first' | 'last', layer?: Layer): void;
57
- removeID(id: string, variables?: {}): true | undefined;
58
- remove(data: {}, variables?: {}): true | undefined;
57
+ removeID(id: string, variables?: {}, layer?: Layer): true | undefined;
58
+ remove(data: {}, variables?: {}, layer?: Layer): true | undefined;
59
59
  listType(data: {
60
60
  __typename?: string;
61
61
  }): string;
@@ -64,10 +64,10 @@ class ListManager {
64
64
  this.lists.get(list.name).get(parentID).lists.push(handler);
65
65
  this.listsByField.get(parentID).get(list.key).push(handler);
66
66
  }
67
- removeIDFromAllLists(id) {
67
+ removeIDFromAllLists(id, layer) {
68
68
  for (const fieldMap of this.lists.values()) {
69
69
  for (const list of fieldMap.values()) {
70
- list.removeID(id);
70
+ list.removeID(id, void 0, layer);
71
71
  }
72
72
  }
73
73
  }
@@ -224,7 +224,7 @@ class List {
224
224
  layer: layer?.id
225
225
  });
226
226
  }
227
- removeID(id, variables = {}) {
227
+ removeID(id, variables = {}, layer) {
228
228
  if (!this.validateWhen()) {
229
229
  return;
230
230
  }
@@ -271,7 +271,7 @@ class List {
271
271
  subscribers.map((sub) => sub[0]),
272
272
  variables
273
273
  );
274
- this.cache._internal_unstable.storage.remove(parentID, targetKey, targetID);
274
+ this.cache._internal_unstable.storage.remove(parentID, targetKey, targetID, layer);
275
275
  for (const [spec] of subscribers) {
276
276
  spec.set(
277
277
  this.cache._internal_unstable.getSelection({
@@ -284,12 +284,12 @@ class List {
284
284
  }
285
285
  return true;
286
286
  }
287
- remove(data, variables = {}) {
287
+ remove(data, variables = {}, layer) {
288
288
  const targetID = this.cache._internal_unstable.id(this.listType(data), data);
289
289
  if (!targetID) {
290
290
  return;
291
291
  }
292
- return this.removeID(targetID, variables);
292
+ return this.removeID(targetID, variables, layer);
293
293
  }
294
294
  listType(data) {
295
295
  return data.__typename || this.type;
@@ -321,7 +321,7 @@ class List {
321
321
  layer,
322
322
  where
323
323
  }) {
324
- if (!this.remove(data, variables)) {
324
+ if (!this.remove(data, variables, layer)) {
325
325
  this.addToList(selection, data, variables, where, layer);
326
326
  }
327
327
  }
@@ -8,8 +8,8 @@ export declare class InMemoryStorage {
8
8
  get nextRank(): number;
9
9
  createLayer(optimistic?: boolean): Layer;
10
10
  insert(id: string, field: string, location: OperationLocations, target: string): void;
11
- remove(id: string, field: string, target: string): void;
12
- delete(id: string): void;
11
+ remove(id: string, field: string, target: string, layerToUser?: Layer): void;
12
+ delete(id: string, layerToUser?: Layer): void;
13
13
  deleteField(id: string, field: string): void;
14
14
  getLayer(id: number): Layer;
15
15
  replaceID(replacement: {
@@ -1,7 +1,7 @@
1
1
  import { flatten } from "../lib/flatten";
2
2
  class InMemoryStorage {
3
3
  data;
4
- idCount = 0;
4
+ idCount = 1;
5
5
  rank = 0;
6
6
  constructor() {
7
7
  this.data = [];
@@ -21,11 +21,11 @@ class InMemoryStorage {
21
21
  insert(id, field, location, target) {
22
22
  return this.topLayer.insert(id, field, location, target);
23
23
  }
24
- remove(id, field, target) {
25
- return this.topLayer.remove(id, field, target);
24
+ remove(id, field, target, layerToUser = this.topLayer) {
25
+ return layerToUser.remove(id, field, target);
26
26
  }
27
- delete(id) {
28
- return this.topLayer.delete(id);
27
+ delete(id, layerToUser = this.topLayer) {
28
+ return layerToUser.delete(id);
29
29
  }
30
30
  deleteField(id, field) {
31
31
  return this.topLayer.deleteField(id, field);
@@ -10,6 +10,7 @@ export declare class InMemorySubscriptions {
10
10
  private subscribers;
11
11
  private referenceCounts;
12
12
  private keyVersions;
13
+ activeFields(parent: string): string[];
13
14
  add({ parent, spec, selection, variables, parentType, }: {
14
15
  parent: string;
15
16
  parentType?: string;
@@ -9,6 +9,9 @@ class InMemorySubscriptions {
9
9
  subscribers = {};
10
10
  referenceCounts = {};
11
11
  keyVersions = {};
12
+ activeFields(parent) {
13
+ return Object.keys(this.subscribers[parent] || {});
14
+ }
12
15
  add({
13
16
  parent,
14
17
  spec,
@@ -5,7 +5,7 @@ import { documentPlugin } from "../utils";
5
5
  const mutation = documentPlugin(ArtifactKind.Mutation, () => {
6
6
  return {
7
7
  async start(ctx, { next, marshalVariables }) {
8
- const layer = cache._internal_unstable.storage.createLayer(true);
8
+ const layerOptimistic = cache._internal_unstable.storage.createLayer(true);
9
9
  const optimisticResponse = ctx.stuff.optimisticResponse;
10
10
  let toNotify = [];
11
11
  if (optimisticResponse) {
@@ -16,25 +16,29 @@ const mutation = documentPlugin(ArtifactKind.Mutation, () => {
16
16
  data: optimisticResponse
17
17
  }),
18
18
  variables: marshalVariables(ctx),
19
- layer: layer.id
19
+ layer: layerOptimistic.id
20
20
  });
21
21
  }
22
22
  ctx.cacheParams = {
23
23
  ...ctx.cacheParams,
24
- layer,
24
+ layer: layerOptimistic,
25
25
  notifySubscribers: toNotify,
26
26
  forceNotify: true
27
27
  };
28
28
  next(ctx);
29
29
  },
30
30
  afterNetwork(ctx, { resolve }) {
31
- ctx.cacheParams?.layer?.clear();
31
+ if (ctx.cacheParams?.layer) {
32
+ cache.clearLayer(ctx.cacheParams.layer.id);
33
+ }
32
34
  resolve(ctx);
33
35
  },
34
36
  end(ctx, { resolve, value }) {
35
37
  const hasErrors = value.errors && value.errors.length > 0;
36
38
  if (hasErrors) {
37
- ctx.cacheParams?.layer?.clear();
39
+ if (ctx.cacheParams?.layer) {
40
+ cache.clearLayer(ctx.cacheParams.layer.id);
41
+ }
38
42
  }
39
43
  if (ctx.cacheParams?.layer) {
40
44
  cache._internal_unstable.storage.resolveLayer(ctx.cacheParams.layer.id);
@@ -44,7 +48,7 @@ const mutation = documentPlugin(ArtifactKind.Mutation, () => {
44
48
  catch(ctx, { error }) {
45
49
  if (ctx.cacheParams?.layer) {
46
50
  const { layer } = ctx.cacheParams;
47
- layer.clear();
51
+ cache.clearLayer(layer.id);
48
52
  cache._internal_unstable.storage.resolveLayer(layer.id);
49
53
  }
50
54
  throw error;