@mearie/core 0.6.4 → 0.6.5

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/index.cjs CHANGED
@@ -272,7 +272,10 @@ const dedupExchange = () => {
272
272
  else operations.set(dedupKey, new Set([op.key]));
273
273
  if (!isInflight) resolved.delete(dedupKey);
274
274
  return (op.metadata.dedup?.skip ?? false) || !isInflight;
275
- }), delay(0)), require_make.pipe(ops$, require_make.filter((op) => op.variant === "teardown"), require_make.filter((teardown) => {
275
+ }), delay(0), require_make.filter((op) => {
276
+ const dedupKey = makeDedupKey(op);
277
+ return operations.get(dedupKey)?.has(op.key) ?? false;
278
+ })), require_make.pipe(ops$, require_make.filter((op) => op.variant === "teardown"), require_make.filter((teardown) => {
276
279
  for (const [dedupKey, subs] of operations.entries()) if (subs.delete(teardown.key)) {
277
280
  if (subs.size === 0) {
278
281
  operations.delete(dedupKey);
@@ -559,6 +562,13 @@ const resolveTypename = (selections, data) => {
559
562
  return data.__typename;
560
563
  };
561
564
  const normalize = (schemaMeta, selections, storage, data, variables, accessor) => {
565
+ const accessorNotified = accessor ? /* @__PURE__ */ new Map() : void 0;
566
+ const callAccessor = (storageKey, fieldKey, oldValue, newValue) => {
567
+ const key = `${storageKey}\0${fieldKey}`;
568
+ if (accessorNotified.has(key) && isEqual(accessorNotified.get(key), newValue)) return;
569
+ accessorNotified.set(key, newValue);
570
+ accessor(storageKey, fieldKey, oldValue, newValue);
571
+ };
562
572
  const resolveEntityKey = (typename, data) => {
563
573
  if (!typename) return null;
564
574
  const entityMeta = schemaMeta.entities[typename];
@@ -585,9 +595,9 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
585
595
  if (fieldTypename && schemaMeta.entities[fieldTypename] && !resolveEntityKey(fieldTypename, fieldValue) && isEntityLink(storage[storageKey]?.[fieldKey])) continue;
586
596
  }
587
597
  const oldValue = storageKey === null ? void 0 : storage[storageKey]?.[fieldKey];
588
- if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) accessor?.(storageKey, fieldKey, oldValue, fieldValue);
598
+ if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) callAccessor?.(storageKey, fieldKey, oldValue, fieldValue);
589
599
  fields[fieldKey] = selection.selections ? normalizeField(null, selection.selections, fieldValue, selection.type) : fieldValue;
590
- if (storageKey !== null && selection.selections && !isNullish(oldValue) && !isNullish(fieldValue) && !isEntityLink(fields[fieldKey]) && !isEqual(oldValue, fields[fieldKey])) accessor?.(storageKey, fieldKey, oldValue, fields[fieldKey]);
600
+ if (storageKey !== null && selection.selections && !isNullish(oldValue) && !isNullish(fieldValue) && !isEntityLink(fields[fieldKey]) && !isEqual(oldValue, fields[fieldKey])) callAccessor?.(storageKey, fieldKey, oldValue, fields[fieldKey]);
591
601
  } else if (selection.kind === "FragmentSpread" || selection.kind === "InlineFragment" && selection.on === typename) {
592
602
  const inner = normalizeField(storageKey, selection.selections, value);
593
603
  if (!isEntityLink(inner)) mergeFields(fields, inner);
@@ -602,10 +612,7 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
602
612
  return fields;
603
613
  };
604
614
  const fields = normalizeField(RootFieldKey, selections, data);
605
- storage[RootFieldKey] = {
606
- ...storage[RootFieldKey],
607
- ...fields
608
- };
615
+ mergeFields(storage[RootFieldKey], fields);
609
616
  };
610
617
 
611
618
  //#endregion
@@ -969,7 +976,7 @@ const diffSnapshots = (oldData, newData, entityArrayChanges) => {
969
976
  patches.push(...arrayPatches);
970
977
  for (const [i, item] of cur.entries()) {
971
978
  const entityKey = newKeys[i];
972
- diff(entityKey ? oldByKey.get(entityKey) : void 0, item, [...path, i]);
979
+ if (entityKey && oldByKey.has(entityKey)) diff(oldByKey.get(entityKey), item, [...path, i]);
973
980
  }
974
981
  };
975
982
  diff(oldData, newData, []);
@@ -1015,7 +1022,7 @@ const processScalarChanges = (changes, registry, subscriptions, storage) => {
1015
1022
  let patchValue = change.newValue;
1016
1023
  if (entry.selections && (isNormalizedRecord(change.newValue) || Array.isArray(change.newValue) && change.newValue.some((v) => isNormalizedRecord(v)))) {
1017
1024
  const mergedValue = storage[change.storageKey]?.[change.fieldKey] ?? change.newValue;
1018
- const { data } = denormalize(entry.selections, {}, mergedValue, sub.variables);
1025
+ const { data } = denormalize(entry.selections, storage, mergedValue, sub.variables);
1019
1026
  patchValue = data;
1020
1027
  }
1021
1028
  const patches = result.get(entry.subscriptionId) ?? [];
@@ -1591,10 +1598,11 @@ var Cache = class {
1591
1598
  */
1592
1599
  hydrate(snapshot) {
1593
1600
  const { storage } = snapshot;
1594
- for (const [key, fields] of Object.entries(storage)) this.#storage[key] = {
1595
- ...this.#storage[key],
1596
- ...fields
1597
- };
1601
+ for (const [key, fields] of Object.entries(storage)) {
1602
+ const existing = this.#storage[key];
1603
+ if (existing) mergeFields(existing, fields, true);
1604
+ else this.#storage[key] = fields;
1605
+ }
1598
1606
  }
1599
1607
  /**
1600
1608
  * Clears all cache data.
package/dist/index.mjs CHANGED
@@ -271,7 +271,10 @@ const dedupExchange = () => {
271
271
  else operations.set(dedupKey, new Set([op.key]));
272
272
  if (!isInflight) resolved.delete(dedupKey);
273
273
  return (op.metadata.dedup?.skip ?? false) || !isInflight;
274
- }), delay(0)), pipe(ops$, filter((op) => op.variant === "teardown"), filter((teardown) => {
274
+ }), delay(0), filter((op) => {
275
+ const dedupKey = makeDedupKey(op);
276
+ return operations.get(dedupKey)?.has(op.key) ?? false;
277
+ })), pipe(ops$, filter((op) => op.variant === "teardown"), filter((teardown) => {
275
278
  for (const [dedupKey, subs] of operations.entries()) if (subs.delete(teardown.key)) {
276
279
  if (subs.size === 0) {
277
280
  operations.delete(dedupKey);
@@ -558,6 +561,13 @@ const resolveTypename = (selections, data) => {
558
561
  return data.__typename;
559
562
  };
560
563
  const normalize = (schemaMeta, selections, storage, data, variables, accessor) => {
564
+ const accessorNotified = accessor ? /* @__PURE__ */ new Map() : void 0;
565
+ const callAccessor = (storageKey, fieldKey, oldValue, newValue) => {
566
+ const key = `${storageKey}\0${fieldKey}`;
567
+ if (accessorNotified.has(key) && isEqual(accessorNotified.get(key), newValue)) return;
568
+ accessorNotified.set(key, newValue);
569
+ accessor(storageKey, fieldKey, oldValue, newValue);
570
+ };
561
571
  const resolveEntityKey = (typename, data) => {
562
572
  if (!typename) return null;
563
573
  const entityMeta = schemaMeta.entities[typename];
@@ -584,9 +594,9 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
584
594
  if (fieldTypename && schemaMeta.entities[fieldTypename] && !resolveEntityKey(fieldTypename, fieldValue) && isEntityLink(storage[storageKey]?.[fieldKey])) continue;
585
595
  }
586
596
  const oldValue = storageKey === null ? void 0 : storage[storageKey]?.[fieldKey];
587
- if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) accessor?.(storageKey, fieldKey, oldValue, fieldValue);
597
+ if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) callAccessor?.(storageKey, fieldKey, oldValue, fieldValue);
588
598
  fields[fieldKey] = selection.selections ? normalizeField(null, selection.selections, fieldValue, selection.type) : fieldValue;
589
- if (storageKey !== null && selection.selections && !isNullish(oldValue) && !isNullish(fieldValue) && !isEntityLink(fields[fieldKey]) && !isEqual(oldValue, fields[fieldKey])) accessor?.(storageKey, fieldKey, oldValue, fields[fieldKey]);
599
+ if (storageKey !== null && selection.selections && !isNullish(oldValue) && !isNullish(fieldValue) && !isEntityLink(fields[fieldKey]) && !isEqual(oldValue, fields[fieldKey])) callAccessor?.(storageKey, fieldKey, oldValue, fields[fieldKey]);
590
600
  } else if (selection.kind === "FragmentSpread" || selection.kind === "InlineFragment" && selection.on === typename) {
591
601
  const inner = normalizeField(storageKey, selection.selections, value);
592
602
  if (!isEntityLink(inner)) mergeFields(fields, inner);
@@ -601,10 +611,7 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
601
611
  return fields;
602
612
  };
603
613
  const fields = normalizeField(RootFieldKey, selections, data);
604
- storage[RootFieldKey] = {
605
- ...storage[RootFieldKey],
606
- ...fields
607
- };
614
+ mergeFields(storage[RootFieldKey], fields);
608
615
  };
609
616
 
610
617
  //#endregion
@@ -968,7 +975,7 @@ const diffSnapshots = (oldData, newData, entityArrayChanges) => {
968
975
  patches.push(...arrayPatches);
969
976
  for (const [i, item] of cur.entries()) {
970
977
  const entityKey = newKeys[i];
971
- diff(entityKey ? oldByKey.get(entityKey) : void 0, item, [...path, i]);
978
+ if (entityKey && oldByKey.has(entityKey)) diff(oldByKey.get(entityKey), item, [...path, i]);
972
979
  }
973
980
  };
974
981
  diff(oldData, newData, []);
@@ -1014,7 +1021,7 @@ const processScalarChanges = (changes, registry, subscriptions, storage) => {
1014
1021
  let patchValue = change.newValue;
1015
1022
  if (entry.selections && (isNormalizedRecord(change.newValue) || Array.isArray(change.newValue) && change.newValue.some((v) => isNormalizedRecord(v)))) {
1016
1023
  const mergedValue = storage[change.storageKey]?.[change.fieldKey] ?? change.newValue;
1017
- const { data } = denormalize(entry.selections, {}, mergedValue, sub.variables);
1024
+ const { data } = denormalize(entry.selections, storage, mergedValue, sub.variables);
1018
1025
  patchValue = data;
1019
1026
  }
1020
1027
  const patches = result.get(entry.subscriptionId) ?? [];
@@ -1590,10 +1597,11 @@ var Cache = class {
1590
1597
  */
1591
1598
  hydrate(snapshot) {
1592
1599
  const { storage } = snapshot;
1593
- for (const [key, fields] of Object.entries(storage)) this.#storage[key] = {
1594
- ...this.#storage[key],
1595
- ...fields
1596
- };
1600
+ for (const [key, fields] of Object.entries(storage)) {
1601
+ const existing = this.#storage[key];
1602
+ if (existing) mergeFields(existing, fields, true);
1603
+ else this.#storage[key] = fields;
1604
+ }
1597
1605
  }
1598
1606
  /**
1599
1607
  * Clears all cache data.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearie/core",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "Type-safe, zero-overhead GraphQL client",
5
5
  "keywords": [
6
6
  "graphql",