react-native-cloud-sync 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  <img src="https://img.shields.io/badge/Expo-compatible-000020.svg" alt="Expo compatible" />
8
8
  <img src="https://img.shields.io/badge/New%20Architecture-supported-success.svg" alt="New Architecture supported" />
9
9
  <img src="https://img.shields.io/badge/Legacy%20Architecture-supported-success.svg" alt="Legacy Architecture supported" />
10
+ <a href="https://github.com/sponsors/kesha-antonov"><img src="https://img.shields.io/badge/sponsor-%E2%9D%A4-ea4aaa.svg?logo=github-sponsors" alt="Sponsor this project" /></a>
10
11
  </p>
11
12
 
12
13
  <h1 align="center">react-native-cloud-sync</h1>
@@ -345,7 +346,11 @@ yarn installDevBuild:ios # or :android
345
346
  yarn start:web
346
347
  ```
347
348
 
348
- Tabs: **Sync** (shared counter across devices), **iCloud KV**, **CloudKit**, **Drive**, **Files** (large-file backup/restore, including a working `GoogleDriveFileAdapter`), **Store** and **Faults**.
349
+ Tabs: **Sync** (shared counter across devices, including a one-tap mirror mode that writes to iCloud and Drive at once), **iCloud KV**, **CloudKit**, **Drive**, **Files** (large-file backup/restore, including a working `GoogleDriveFileAdapter`), **Store** and **Faults**.
350
+
351
+ <p align="center">
352
+ <img src="assets/screenshots/sync.png" width="18%" alt="Sync demo tab - shared counter with mirror provider selection" />&nbsp;&nbsp;<img src="assets/screenshots/icloud-kv.png" width="18%" alt="iCloud key-value store tab" />&nbsp;&nbsp;<img src="assets/screenshots/cloudkit.png" width="18%" alt="CloudKit tab - record read/write and oversized-write handling" />&nbsp;&nbsp;<img src="assets/screenshots/drive.png" width="18%" alt="Google Drive tab - OAuth token configuration and file operations" />&nbsp;&nbsp;<img src="assets/screenshots/store.png" width="18%" alt="Store facade tab - size tiering across providers" />
353
+ </p>
349
354
 
350
355
  ## 🤝 Contributing
351
356
 
@@ -817,13 +817,36 @@ import Foundation
817
817
  ) {
818
818
  let operation = CKFetchRecordsOperation(recordIDs: [id])
819
819
  operation.desiredKeys = [Self.sizeField(fieldName)]
820
+
821
+ // Best-effort means exactly one call to `completion`, from whichever
822
+ // block actually fires - never zero. Only `perRecordResultBlock` was
823
+ // wired here, but CloudKit does not always get that far: a failure at
824
+ // the OPERATION level (no signed-in account, before evaluating a
825
+ // single record) fires the operation-level completion block instead
826
+ // and never touches `perRecordResultBlock` at all. Without a fallback
827
+ // there, `completion` was silently never called - which means
828
+ // `ckFetchAsset`'s continuation into `fetchAsset` never runs, and the
829
+ // whole promise hangs forever with no error, no timeout, and no way
830
+ // for the caller to tell a stuck restore from a slow one.
831
+ var settled = false
832
+ let settle = { (size: Int) in
833
+ if settled { return }
834
+ settled = true
835
+ completion(size)
836
+ }
837
+
820
838
  operation.perRecordResultBlock = { _, result in
821
839
  guard case let .success(record) = result,
822
840
  let size = record[Self.sizeField(fieldName)] as? Int else {
823
- completion(0)
841
+ settle(0)
824
842
  return
825
843
  }
826
- completion(size)
844
+ settle(size)
845
+ }
846
+ operation.fetchRecordsResultBlock = { _ in
847
+ // Only reaches here without having settled when no per-record
848
+ // block fired at all - the operation-level failure case.
849
+ settle(0)
827
850
  }
828
851
  database.add(operation)
829
852
  }
@@ -207,6 +207,12 @@ Object.defineProperty(exports, "resolveByTimestamp", {
207
207
  return _resolvers.resolveByTimestamp;
208
208
  }
209
209
  });
210
+ Object.defineProperty(exports, "resolveByUnion", {
211
+ enumerable: true,
212
+ get: function () {
213
+ return _resolvers.resolveByUnion;
214
+ }
215
+ });
210
216
  Object.defineProperty(exports, "resolveFirstOf", {
211
217
  enumerable: true,
212
218
  get: function () {
@@ -1 +1 @@
1
- {"version":3,"names":["_icloudKV","require","_cloudKit","_cloudKitBackup","_cloudKitEncrypted","_icloudDocuments","_googleDrive","_googleDriveFiles","_memory","_store","_resolvers","_keys","_config","_base","_errors","_types","_logger"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAAA,SAAA,GAAAC,OAAA;AAKA,IAAAC,SAAA,GAAAD,OAAA;AAMA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,kBAAA,GAAAH,OAAA;AACA,IAAAI,gBAAA,GAAAJ,OAAA;AACA,IAAAK,YAAA,GAAAL,OAAA;AACA,IAAAM,iBAAA,GAAAN,OAAA;AACA,IAAAO,OAAA,GAAAP,OAAA;AAGA,IAAAQ,MAAA,GAAAR,OAAA;AAQA,IAAAS,UAAA,GAAAT,OAAA;AAQA,IAAAU,KAAA,GAAAV,OAAA;AAWA,IAAAW,OAAA,GAAAX,OAAA;AAgBA,IAAAY,KAAA,GAAAZ,OAAA;AAGA,IAAAa,OAAA,GAAAb,OAAA;AAqCA,IAAAc,MAAA,GAAAd,OAAA;AAEA,IAAAe,OAAA,GAAAf,OAAA","ignoreList":[]}
1
+ {"version":3,"names":["_icloudKV","require","_cloudKit","_cloudKitBackup","_cloudKitEncrypted","_icloudDocuments","_googleDrive","_googleDriveFiles","_memory","_store","_resolvers","_keys","_config","_base","_errors","_types","_logger"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAAA,SAAA,GAAAC,OAAA;AAKA,IAAAC,SAAA,GAAAD,OAAA;AAMA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,kBAAA,GAAAH,OAAA;AACA,IAAAI,gBAAA,GAAAJ,OAAA;AACA,IAAAK,YAAA,GAAAL,OAAA;AACA,IAAAM,iBAAA,GAAAN,OAAA;AACA,IAAAO,OAAA,GAAAP,OAAA;AAGA,IAAAQ,MAAA,GAAAR,OAAA;AAQA,IAAAS,UAAA,GAAAT,OAAA;AASA,IAAAU,KAAA,GAAAV,OAAA;AAWA,IAAAW,OAAA,GAAAX,OAAA;AAgBA,IAAAY,KAAA,GAAAZ,OAAA;AAGA,IAAAa,OAAA,GAAAb,OAAA;AAqCA,IAAAc,MAAA,GAAAd,OAAA;AAEA,IAAAe,OAAA,GAAAf,OAAA","ignoreList":[]}
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.resolveByModifiedAt = resolveByModifiedAt;
7
7
  exports.resolveByPreferenceOrder = void 0;
8
8
  exports.resolveByTimestamp = resolveByTimestamp;
9
+ exports.resolveByUnion = resolveByUnion;
9
10
  exports.resolveFirstOf = resolveFirstOf;
10
11
  /**
11
12
  * Resolves by a numeric timestamp inside a JSON value.
@@ -106,6 +107,73 @@ function resolveByModifiedAt(options = {}) {
106
107
  };
107
108
  }
108
109
 
110
+ /**
111
+ * Resolves by merging JSON arrays across every candidate - the union of
112
+ * elements, not a winner-take-all pick.
113
+ *
114
+ * The case {@link resolveByTimestamp} and {@link resolveByModifiedAt} do not
115
+ * cover: a list of favorited item ids, a set of dismissed-tip ids, anything
116
+ * where two devices adding *different* elements between syncs should both
117
+ * survive rather than one write clobbering the other.
118
+ *
119
+ * Deletions do not propagate. Removing an element on one device and syncing
120
+ * does not remove it from the merged result, because nothing in a plain union
121
+ * distinguishes "never added" from "removed" - a plain array carries no
122
+ * tombstones. Track removals yourself (a separate `removedIds` set, synced the
123
+ * same way) if that matters for your data.
124
+ *
125
+ * A candidate that is not valid JSON, or whose JSON is not an array, is
126
+ * dropped rather than treated as empty - a value you cannot read as a list is
127
+ * not evidence that the list is empty. If every candidate drops out this way,
128
+ * falls back to preference order rather than returning `[]` and reading as
129
+ * "nothing favorited" on every device.
130
+ *
131
+ * Elements are deduplicated and ordered by first appearance across candidates
132
+ * in provider order, so the merged result is stable rather than reshuffling on
133
+ * every resolve. When two candidates hold the "same" element with different
134
+ * fields (an object whose non-key fields differ), the first-seen copy wins -
135
+ * this merges *membership*, not per-element conflicts. Primitives are
136
+ * deduplicated by value; supply `key` for arrays of objects.
137
+ *
138
+ * ```ts
139
+ * createCloudStore({
140
+ * providers: ['icloudKV', 'googleDrive'],
141
+ * writeMode: 'mirror',
142
+ * resolve: resolveByUnion({ key: item => item.id }),
143
+ * })
144
+ * ```
145
+ */
146
+ function resolveByUnion(options = {}) {
147
+ const keyOf = options.key ?? (item => JSON.stringify(item));
148
+ return candidates => {
149
+ if (candidates.length === 0) return null;
150
+ const merged = new Map();
151
+ let sawArray = false;
152
+ for (const candidate of candidates) {
153
+ const items = arrayOf(candidate.value);
154
+ if (items == null) continue;
155
+ sawArray = true;
156
+ for (const item of items) {
157
+ const key = keyOf(item);
158
+ if (!merged.has(key)) merged.set(key, item);
159
+ }
160
+ }
161
+
162
+ // Nothing was a readable array: defer to preference order rather than
163
+ // manufacturing an empty list.
164
+ if (!sawArray) return candidates[0].value;
165
+ return JSON.stringify([...merged.values()]);
166
+ };
167
+ }
168
+ function arrayOf(value) {
169
+ try {
170
+ const parsed = JSON.parse(value);
171
+ return Array.isArray(parsed) ? parsed : null;
172
+ } catch {
173
+ return null;
174
+ }
175
+ }
176
+
109
177
  /**
110
178
  * Tries each resolver in turn and takes the first non-null answer.
111
179
  *
@@ -1 +1 @@
1
- {"version":3,"names":["resolveByTimestamp","field","candidates","length","best","bestAt","Number","NEGATIVE_INFINITY","candidate","at","timestampOf","value","parsed","JSON","parse","raw","isFinite","parsedDate","Date","isNaN","resolveByPreferenceOrder","exports","resolveByModifiedAt","options","fallback","dated","filter","c","modifiedAt","resolveFirstOf","resolvers","resolve","answer"],"sourceRoot":"../../src","sources":["resolvers.ts"],"mappings":";;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,kBAAkBA,CAACC,KAAK,GAAG,WAAW,EAAa;EACjE,OAAQC,UAA8B,IAAoB;IACxD,IAAIA,UAAU,CAACC,MAAM,KAAK,CAAC,EAAE,OAAO,IAAI;IAExC,IAAIC,IAA6B,GAAG,IAAI;IACxC,IAAIC,MAAM,GAAGC,MAAM,CAACC,iBAAiB;IAErC,KAAK,MAAMC,SAAS,IAAIN,UAAU,EAAE;MAClC,MAAMO,EAAE,GAAGC,WAAW,CAACF,SAAS,CAACG,KAAK,EAAEV,KAAK,CAAC;MAC9C;MACA;MACA,IAAIQ,EAAE,GAAGJ,MAAM,EAAE;QACfD,IAAI,GAAGI,SAAS;QAChBH,MAAM,GAAGI,EAAE;MACb;IACF;;IAEA;IACA,IAAIL,IAAI,IAAI,IAAI,EAAE,OAAOF,UAAU,CAAC,CAAC,CAAC,CAACS,KAAK;IAC5C,OAAOP,IAAI,CAACO,KAAK;EACnB,CAAC;AACH;AAEA,SAASD,WAAWA,CAACC,KAAa,EAAEV,KAAa,EAAU;EACzD,IAAI;IACF,MAAMW,MAAe,GAAGC,IAAI,CAACC,KAAK,CAACH,KAAK,CAAC;IACzC,IAAIC,MAAM,IAAI,IAAI,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE,OAAON,MAAM,CAACC,iBAAiB;IACjF,MAAMQ,GAAG,GAAIH,MAAM,CAA6BX,KAAK,CAAC;IACtD,IAAI,OAAOc,GAAG,KAAK,QAAQ,IAAIT,MAAM,CAACU,QAAQ,CAACD,GAAG,CAAC,EAAE,OAAOA,GAAG;IAC/D;IACA,IAAI,OAAOA,GAAG,KAAK,QAAQ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,OAAOJ,IAAI,CAACO,KAAK;EACnB,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASkB,cAAcA,CAAC,GAAGC,SAAsB,EAAa;EACnE,OAAQ5B,UAA8B,IAAoB;IACxD,KAAK,MAAM6B,OAAO,IAAID,SAAS,EAAE;MAC/B,MAAME,MAAM,GAAGD,OAAO,CAAC7B,UAAU,CAAC;MAClC,IAAI8B,MAAM,IAAI,IAAI,EAAE,OAAOA,MAAM;IACnC;IACA,OAAO,IAAI;EACb,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["resolveByTimestamp","field","candidates","length","best","bestAt","Number","NEGATIVE_INFINITY","candidate","at","timestampOf","value","parsed","JSON","parse","raw","isFinite","parsedDate","Date","isNaN","resolveByPreferenceOrder","exports","resolveByModifiedAt","options","fallback","dated","filter","c","modifiedAt","resolveByUnion","keyOf","key","item","stringify","merged","Map","sawArray","items","arrayOf","has","set","values","Array","isArray","resolveFirstOf","resolvers","resolve","answer"],"sourceRoot":"../../src","sources":["resolvers.ts"],"mappings":";;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,kBAAkBA,CAACC,KAAK,GAAG,WAAW,EAAa;EACjE,OAAQC,UAA8B,IAAoB;IACxD,IAAIA,UAAU,CAACC,MAAM,KAAK,CAAC,EAAE,OAAO,IAAI;IAExC,IAAIC,IAA6B,GAAG,IAAI;IACxC,IAAIC,MAAM,GAAGC,MAAM,CAACC,iBAAiB;IAErC,KAAK,MAAMC,SAAS,IAAIN,UAAU,EAAE;MAClC,MAAMO,EAAE,GAAGC,WAAW,CAACF,SAAS,CAACG,KAAK,EAAEV,KAAK,CAAC;MAC9C;MACA;MACA,IAAIQ,EAAE,GAAGJ,MAAM,EAAE;QACfD,IAAI,GAAGI,SAAS;QAChBH,MAAM,GAAGI,EAAE;MACb;IACF;;IAEA;IAC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IAAI0B,KAAK,IAAI,IAAI,EAAE;MACnBD,QAAQ,GAAG,IAAI;MACf,KAAK,MAAMJ,IAAI,IAAIK,KAAK,EAAE;QACxB,MAAMN,GAAG,GAAGD,KAAK,CAACE,IAAI,CAAC;QACvB,IAAI,CAACE,MAAM,CAACK,GAAG,CAACR,GAAG,CAAC,EAAEG,MAAM,CAACM,GAAG,CAACT,GAAG,EAAEC,IAAI,CAAC;MAC7C;IACF;;IAEA;IACA;IACA,IAAI,CAACI,QAAQ,EAAE,OAAOlC,UAAU,CAAC,CAAC,CAAC,CAACS,KAAK;IAEzC,OAAOE,IAAI,CAACoB,SAAS,CAAC,CAAC,GAAGC,MAAM,CAACO,MAAM,CAAC,CAAC,CAAC,CAAC;EAC7C,CAAC;AACH;AAEA,SAASH,OAAOA,CAAI3B,KAAa,EAAc;EAC7C,IAAI;IACF,MAAMC,MAAe,GAAGC,IAAI,CAACC,KAAK,CAACH,KAAK,CAAC;IACzC,OAAO+B,KAAK,CAACC,OAAO,CAAC/B,MAAM,CAAC,GAAIA,MAAM,GAAW,IAAI;EACvD,CAAC,CACD,MAAM;IACJ,OAAO,IAAI;EACb;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASgC,cAAcA,CAAC,GAAGC,SAAsB,EAAa;EACnE,OAAQ3C,UAA8B,IAAoB;IACxD,KAAK,MAAM4C,OAAO,IAAID,SAAS,EAAE;MAC/B,MAAME,MAAM,GAAGD,OAAO,CAAC5C,UAAU,CAAC;MAClC,IAAI6C,MAAM,IAAI,IAAI,EAAE,OAAOA,MAAM;IACnC;IACA,OAAO,IAAI;EACb,CAAC;AACH","ignoreList":[]}
@@ -19,7 +19,7 @@ export { createMemoryProvider } from "./providers/memory.js";
19
19
 
20
20
  // The facade over them.
21
21
  export { createCloudStore } from "./store.js";
22
- export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveFirstOf } from "./resolvers.js";
22
+ export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveByUnion, resolveFirstOf } from "./resolvers.js";
23
23
 
24
24
  // Key rules, for callers whose keys come from somewhere they do not control.
25
25
  export { checkKey, sanitizeKey } from "./internal/keys.js";
@@ -1 +1 @@
1
- {"version":3,"names":["icloudKV","getAllItems","icloudKVGetAllItems","sync","icloudKVSync","cloudKit","createCloudKitProvider","zones","cloudKitZones","assets","cloudKitAssets","cloudKitBackup","cloudKitEncrypted","icloudDocuments","googleDrive","createGoogleDriveProvider","googleDriveFiles","createMemoryProvider","createCloudStore","resolveByTimestamp","resolveByModifiedAt","resolveByPreferenceOrder","resolveFirstOf","checkKey","sanitizeKey","configureCloudKit","configureGoogleDrive","configureGoogleDriveFiles","isCloudKitConfigured","isGoogleDriveConfigured","isGoogleDriveFilesConfigured","base64ToBytes","bytesToBase64","CloudSyncError","ErrorCode","isCancelled","isCloudSyncError","isRetryable","requiresUserAction","DEFAULT_TIERING","setLogsEnabled"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA,SACEA,QAAQ,EACRC,WAAW,IAAIC,mBAAmB,EAClCC,IAAI,IAAIC,YAAY,QACf,sBAAsB;AAC7B,SACEC,QAAQ,EACRC,sBAAsB,EACtBC,KAAK,IAAIC,aAAa,EACtBC,MAAM,IAAIC,cAAc,QACnB,yBAAsB;AAC7B,SAASC,cAAc,QAAQ,+BAA4B;AAC3D,SAASC,iBAAiB,QAAQ,kCAA+B;AACjE,SAASC,eAAe,QAAQ,gCAA6B;AAC7D,SAASC,WAAW,EAAEC,yBAAyB,QAAQ,4BAAyB;AAChF,SAASC,gBAAgB,QAAQ,iCAA8B;AAC/D,SAASC,oBAAoB,QAAQ,uBAAoB;;AAEzD;AACA,SACEC,gBAAgB,QAMX,YAAS;AAChB,SACEC,kBAAkB,EAClBC,mBAAmB,EACnBC,wBAAwB,EACxBC,cAAc,QACT,gBAAa;;AAEpB;AACA,SAASC,QAAQ,EAAEC,WAAW,QAAQ,oBAAiB;;AAEvD;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA,SACEC,iBAAiB,EACjBC,oBAAoB,EACpBC,yBAAyB,EACzBC,oBAAoB,EACpBC,uBAAuB,EACvBC,4BAA4B,QACvB,aAAU;AAKjB;AACA;AACA;AACA;AACA,SAASC,aAAa,EAAEC,aAAa,QAAQ,sBAAmB;;AAEhE;AACA,SACEC,cAAc,EACdC,SAAS,EACTC,WAAW,EACXC,gBAAgB,EAChBC,WAAW,EACXC,kBAAkB,QACb,aAAU;AA8BjB,SAASC,eAAe,QAAQ,YAAS;AAEzC,SAASC,cAAc,QAAQ,sBAAmB","ignoreList":[]}
1
+ {"version":3,"names":["icloudKV","getAllItems","icloudKVGetAllItems","sync","icloudKVSync","cloudKit","createCloudKitProvider","zones","cloudKitZones","assets","cloudKitAssets","cloudKitBackup","cloudKitEncrypted","icloudDocuments","googleDrive","createGoogleDriveProvider","googleDriveFiles","createMemoryProvider","createCloudStore","resolveByTimestamp","resolveByModifiedAt","resolveByPreferenceOrder","resolveByUnion","resolveFirstOf","checkKey","sanitizeKey","configureCloudKit","configureGoogleDrive","configureGoogleDriveFiles","isCloudKitConfigured","isGoogleDriveConfigured","isGoogleDriveFilesConfigured","base64ToBytes","bytesToBase64","CloudSyncError","ErrorCode","isCancelled","isCloudSyncError","isRetryable","requiresUserAction","DEFAULT_TIERING","setLogsEnabled"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA,SACEA,QAAQ,EACRC,WAAW,IAAIC,mBAAmB,EAClCC,IAAI,IAAIC,YAAY,QACf,sBAAsB;AAC7B,SACEC,QAAQ,EACRC,sBAAsB,EACtBC,KAAK,IAAIC,aAAa,EACtBC,MAAM,IAAIC,cAAc,QACnB,yBAAsB;AAC7B,SAASC,cAAc,QAAQ,+BAA4B;AAC3D,SAASC,iBAAiB,QAAQ,kCAA+B;AACjE,SAASC,eAAe,QAAQ,gCAA6B;AAC7D,SAASC,WAAW,EAAEC,yBAAyB,QAAQ,4BAAyB;AAChF,SAASC,gBAAgB,QAAQ,iCAA8B;AAC/D,SAASC,oBAAoB,QAAQ,uBAAoB;;AAEzD;AACA,SACEC,gBAAgB,QAMX,YAAS;AAChB,SACEC,kBAAkB,EAClBC,mBAAmB,EACnBC,wBAAwB,EACxBC,cAAc,EACdC,cAAc,QACT,gBAAa;;AAEpB;AACA,SAASC,QAAQ,EAAEC,WAAW,QAAQ,oBAAiB;;AAEvD;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA,SACEC,iBAAiB,EACjBC,oBAAoB,EACpBC,yBAAyB,EACzBC,oBAAoB,EACpBC,uBAAuB,EACvBC,4BAA4B,QACvB,aAAU;AAKjB;AACA;AACA;AACA;AACA,SAASC,aAAa,EAAEC,aAAa,QAAQ,sBAAmB;;AAEhE;AACA,SACEC,cAAc,EACdC,SAAS,EACTC,WAAW,EACXC,gBAAgB,EAChBC,WAAW,EACXC,kBAAkB,QACb,aAAU;AA8BjB,SAASC,eAAe,QAAQ,YAAS;AAEzC,SAASC,cAAc,QAAQ,sBAAmB","ignoreList":[]}
@@ -98,6 +98,73 @@ export function resolveByModifiedAt(options = {}) {
98
98
  };
99
99
  }
100
100
 
101
+ /**
102
+ * Resolves by merging JSON arrays across every candidate - the union of
103
+ * elements, not a winner-take-all pick.
104
+ *
105
+ * The case {@link resolveByTimestamp} and {@link resolveByModifiedAt} do not
106
+ * cover: a list of favorited item ids, a set of dismissed-tip ids, anything
107
+ * where two devices adding *different* elements between syncs should both
108
+ * survive rather than one write clobbering the other.
109
+ *
110
+ * Deletions do not propagate. Removing an element on one device and syncing
111
+ * does not remove it from the merged result, because nothing in a plain union
112
+ * distinguishes "never added" from "removed" - a plain array carries no
113
+ * tombstones. Track removals yourself (a separate `removedIds` set, synced the
114
+ * same way) if that matters for your data.
115
+ *
116
+ * A candidate that is not valid JSON, or whose JSON is not an array, is
117
+ * dropped rather than treated as empty - a value you cannot read as a list is
118
+ * not evidence that the list is empty. If every candidate drops out this way,
119
+ * falls back to preference order rather than returning `[]` and reading as
120
+ * "nothing favorited" on every device.
121
+ *
122
+ * Elements are deduplicated and ordered by first appearance across candidates
123
+ * in provider order, so the merged result is stable rather than reshuffling on
124
+ * every resolve. When two candidates hold the "same" element with different
125
+ * fields (an object whose non-key fields differ), the first-seen copy wins -
126
+ * this merges *membership*, not per-element conflicts. Primitives are
127
+ * deduplicated by value; supply `key` for arrays of objects.
128
+ *
129
+ * ```ts
130
+ * createCloudStore({
131
+ * providers: ['icloudKV', 'googleDrive'],
132
+ * writeMode: 'mirror',
133
+ * resolve: resolveByUnion({ key: item => item.id }),
134
+ * })
135
+ * ```
136
+ */
137
+ export function resolveByUnion(options = {}) {
138
+ const keyOf = options.key ?? (item => JSON.stringify(item));
139
+ return candidates => {
140
+ if (candidates.length === 0) return null;
141
+ const merged = new Map();
142
+ let sawArray = false;
143
+ for (const candidate of candidates) {
144
+ const items = arrayOf(candidate.value);
145
+ if (items == null) continue;
146
+ sawArray = true;
147
+ for (const item of items) {
148
+ const key = keyOf(item);
149
+ if (!merged.has(key)) merged.set(key, item);
150
+ }
151
+ }
152
+
153
+ // Nothing was a readable array: defer to preference order rather than
154
+ // manufacturing an empty list.
155
+ if (!sawArray) return candidates[0].value;
156
+ return JSON.stringify([...merged.values()]);
157
+ };
158
+ }
159
+ function arrayOf(value) {
160
+ try {
161
+ const parsed = JSON.parse(value);
162
+ return Array.isArray(parsed) ? parsed : null;
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
167
+
101
168
  /**
102
169
  * Tries each resolver in turn and takes the first non-null answer.
103
170
  *
@@ -1 +1 @@
1
- {"version":3,"names":["resolveByTimestamp","field","candidates","length","best","bestAt","Number","NEGATIVE_INFINITY","candidate","at","timestampOf","value","parsed","JSON","parse","raw","isFinite","parsedDate","Date","isNaN","resolveByPreferenceOrder","resolveByModifiedAt","options","fallback","dated","filter","c","modifiedAt","resolveFirst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AAcA,CAAC,GAAGC,SAAsB,EAAa;EACnE,OAAQ3B,UAA8B,IAAoB;IACxD,KAAK,MAAM4B,OAAO,IAAID,SAAS,EAAE;MAC/B,MAAME,MAAM,GAAGD,OAAO,CAAC5B,UAAU,CAAC;MAClC,IAAI6B,MAAM,IAAI,IAAI,EAAE,OAAOA,MAAM;IACnC;IACA,OAAO,IAAI;EACb,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["resolveByTimestamp","field","candidates","length","best","bestAt","Number","NEGATIVE_INFINITY","candidate","at","timestampOf","value","parsed","JSON","parse","raw","isFinite","parsedDate","Date","isNaN","resolveByPreferenceOrder","resolveByModifiedAt","options","fallback","dated","filter","c","modifiedAt","resolveByUnion","keyOf","key","item","stringify","merged","Map","sawArray","items","arrayOf","has","set","values","Array","isArray","resolveFirstOf","resolvers","resolve","answer"],"sourceRoot":"../../src","sources":["resolvers.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,kBAAkBA,CAACC,KAAK,GAAG,WAAW,EAAa;EACjE,OAAQC,UAA8B,IAAoB;IACxD,IAAIA,UAAU,CAACC,MAAM,KAAK,CAAC,EAAE,OAAO,IAAI;IAExC,IAAIC,IAA6B,GAAG,IAAI;IACxC,IAAIC,MAAM,GAAGC,MAAM,CAACC,iBAAiB;IAErC,KAAK,MAAMC,SAAS,IAAIN,UAAU,EAAE;MAClC,MAAMO,EAAE,GAAGC,WAAW,CAACF,SAAS,CAACG,KAAK,EAAEV,KAAK,CAAC;MAC9C;MACA;MACA,IAAIQ,EAAE,GAAGJ,MAAM,EAAE;QACfD,IAAI,GAAGI,SAAS;QAChBH,MAAM,GAAGI,EAAE;MACb;IACF;;IAEA;IACA,IAAIL,IAAI,IAAI,IAAI,EAAE,OAAOF,UAAU,CAAC,CAAC,CAAC,CAACS,KAAK;IAC5C,OAAOP,IAAI,CAACO,KAAK;EACnB,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IAAI,CAAC;QACvB,IAAI,CAACE,MAAM,CAACK,GAAG,CAACR,GAAG,CAAC,EAAEG,MAAM,CAACM,GAAG,CAACT,GAAG,EAAEC,IAAI,CAAC;MAC7C;IACF;;IAEA;IACA;IACA,IAAI,CAACI,QAAQ,EAAE,OAAOjC,UAAU,CAAC,CAAC,CAAC,CAACS,KAAK;IAEzC,OAAOE,IAAI,CAACmB,SAAS,CAAC,CAAC,GAAGC,MAAM,CAACO,MAAM,CAAC,CAAC,CAAC,CAAC;EAC7C,CAAC;AACH;AAEA,SAASH,OAAOA,CAAI1B,KAAa,EAAc;EAC7C,IAAI;IACF,MAAMC,MAAe,GAAGC,IAAI,CAACC,KAAK,CAACH,KAAK,CAAC;IACzC,OAAO8B,KAAK,CAACC,OAAO,CAAC9B,MAAM,CAAC,GAAIA,MAAM,GAAW,IAAI;EACvD,CAAC,CACD,MAAM;IACJ,OAAO,IAAI;EACb;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAAS+B,cAAcA,CAAC,GAAGC,SAAsB,EAAa;EACnE,OAAQ1C,UAA8B,IAAoB;IACxD,KAAK,MAAM2C,OAAO,IAAID,SAAS,EAAE;MAC/B,MAAME,MAAM,GAAGD,OAAO,CAAC3C,UAAU,CAAC;MAClC,IAAI4C,MAAM,IAAI,IAAI,EAAE,OAAOA,MAAM;IACnC;IACA,OAAO,IAAI;EACb,CAAC;AACH","ignoreList":[]}
@@ -13,7 +13,7 @@ export { googleDrive, createGoogleDriveProvider } from './providers/googleDrive'
13
13
  export { googleDriveFiles } from './providers/googleDriveFiles';
14
14
  export { createMemoryProvider } from './providers/memory';
15
15
  export { createCloudStore, type CloudStore, type FlushResult, type MigrateOptions, type MigrateResult, type OutboxStorage } from './store';
16
- export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveFirstOf } from './resolvers';
16
+ export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveByUnion, resolveFirstOf } from './resolvers';
17
17
  export { checkKey, sanitizeKey } from './internal/keys';
18
18
  /**
19
19
  * The abort-signal shape the cancellable APIs accept.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,QAAQ,EACR,WAAW,IAAI,mBAAmB,EAClC,IAAI,IAAI,YAAY,EACrB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EACL,QAAQ,EACR,sBAAsB,EACtB,KAAK,IAAI,aAAa,EACtB,MAAM,IAAI,cAAc,EACzB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AAC/D,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAGzD,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,aAAa,EACnB,MAAM,SAAS,CAAA;AAChB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB,EACxB,cAAc,EACf,MAAM,aAAa,CAAA;AAGpB,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAEvD;;;;;GAKG;AACH,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAGnD,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,uBAAuB,EACvB,4BAA4B,EAC7B,MAAM,UAAU,CAAA;AAEjB,YAA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AAA;AACpF,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AACtF,YAAY,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AAEvH,YAAY,EACV,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACV,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA"}
@@ -46,6 +46,45 @@ export declare const resolveByPreferenceOrder: ResolveFn;
46
46
  export declare function resolveByModifiedAt(options?: {
47
47
  fallback?: ResolveFn;
48
48
  }): ResolveFn;
49
+ /**
50
+ * Resolves by merging JSON arrays across every candidate - the union of
51
+ * elements, not a winner-take-all pick.
52
+ *
53
+ * The case {@link resolveByTimestamp} and {@link resolveByModifiedAt} do not
54
+ * cover: a list of favorited item ids, a set of dismissed-tip ids, anything
55
+ * where two devices adding *different* elements between syncs should both
56
+ * survive rather than one write clobbering the other.
57
+ *
58
+ * Deletions do not propagate. Removing an element on one device and syncing
59
+ * does not remove it from the merged result, because nothing in a plain union
60
+ * distinguishes "never added" from "removed" - a plain array carries no
61
+ * tombstones. Track removals yourself (a separate `removedIds` set, synced the
62
+ * same way) if that matters for your data.
63
+ *
64
+ * A candidate that is not valid JSON, or whose JSON is not an array, is
65
+ * dropped rather than treated as empty - a value you cannot read as a list is
66
+ * not evidence that the list is empty. If every candidate drops out this way,
67
+ * falls back to preference order rather than returning `[]` and reading as
68
+ * "nothing favorited" on every device.
69
+ *
70
+ * Elements are deduplicated and ordered by first appearance across candidates
71
+ * in provider order, so the merged result is stable rather than reshuffling on
72
+ * every resolve. When two candidates hold the "same" element with different
73
+ * fields (an object whose non-key fields differ), the first-seen copy wins -
74
+ * this merges *membership*, not per-element conflicts. Primitives are
75
+ * deduplicated by value; supply `key` for arrays of objects.
76
+ *
77
+ * ```ts
78
+ * createCloudStore({
79
+ * providers: ['icloudKV', 'googleDrive'],
80
+ * writeMode: 'mirror',
81
+ * resolve: resolveByUnion({ key: item => item.id }),
82
+ * })
83
+ * ```
84
+ */
85
+ export declare function resolveByUnion<T = unknown>(options?: {
86
+ key?: (item: T) => string | number;
87
+ }): ResolveFn;
49
88
  /**
50
89
  * Tries each resolver in turn and takes the first non-null answer.
51
90
  *
@@ -1 +1 @@
1
- {"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../../src/resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,SAAS,EAAE,MAAM,SAAS,CAAA;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAc,GAAG,SAAS,CAqBjE;AAoBD,+EAA+E;AAC/E,eAAO,MAAM,wBAAwB,EAAE,SACa,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAO,GACrC,SAAS,CAsBX;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,SAAS,EAAE,SAAS,EAAE,GAAG,SAAS,CAQnE"}
1
+ {"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../../src/resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,SAAS,EAAE,MAAM,SAAS,CAAA;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAc,GAAG,SAAS,CAqBjE;AAoBD,+EAA+E;AAC/E,eAAO,MAAM,wBAAwB,EAAE,SACa,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAO,GACrC,SAAS,CAsBX;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,cAAc,CAAC,CAAC,GAAG,OAAO,EACxC,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,CAAA;CAAO,GACnD,SAAS,CAyBX;AAYD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,SAAS,EAAE,SAAS,EAAE,GAAG,SAAS,CAQnE"}
@@ -13,7 +13,7 @@ export { googleDrive, createGoogleDriveProvider } from './providers/googleDrive.
13
13
  export { googleDriveFiles } from './providers/googleDriveFiles.js';
14
14
  export { createMemoryProvider } from './providers/memory.js';
15
15
  export { createCloudStore, type CloudStore, type FlushResult, type MigrateOptions, type MigrateResult, type OutboxStorage } from './store.js';
16
- export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveFirstOf } from './resolvers.js';
16
+ export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveByUnion, resolveFirstOf } from './resolvers.js';
17
17
  export { checkKey, sanitizeKey } from './internal/keys.js';
18
18
  /**
19
19
  * The abort-signal shape the cancellable APIs accept.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,QAAQ,EACR,WAAW,IAAI,mBAAmB,EAClC,IAAI,IAAI,YAAY,EACrB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EACL,QAAQ,EACR,sBAAsB,EACtB,KAAK,IAAI,aAAa,EACtB,MAAM,IAAI,cAAc,EACzB,MAAM,yBAAsB,C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X,UAAU,EACV,SAAS,EACV,MAAM,YAAS,CAAA;AAChB,OAAO,EAAE,eAAe,EAAE,MAAM,YAAS,CAAA;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAmB,CAAA"}
@@ -46,6 +46,45 @@ export declare const resolveByPreferenceOrder: ResolveFn;
46
46
  export declare function resolveByModifiedAt(options?: {
47
47
  fallback?: ResolveFn;
48
48
  }): ResolveFn;
49
+ /**
50
+ * Resolves by merging JSON arrays across every candidate - the union of
51
+ * elements, not a winner-take-all pick.
52
+ *
53
+ * The case {@link resolveByTimestamp} and {@link resolveByModifiedAt} do not
54
+ * cover: a list of favorited item ids, a set of dismissed-tip ids, anything
55
+ * where two devices adding *different* elements between syncs should both
56
+ * survive rather than one write clobbering the other.
57
+ *
58
+ * Deletions do not propagate. Removing an element on one device and syncing
59
+ * does not remove it from the merged result, because nothing in a plain union
60
+ * distinguishes "never added" from "removed" - a plain array carries no
61
+ * tombstones. Track removals yourself (a separate `removedIds` set, synced the
62
+ * same way) if that matters for your data.
63
+ *
64
+ * A candidate that is not valid JSON, or whose JSON is not an array, is
65
+ * dropped rather than treated as empty - a value you cannot read as a list is
66
+ * not evidence that the list is empty. If every candidate drops out this way,
67
+ * falls back to preference order rather than returning `[]` and reading as
68
+ * "nothing favorited" on every device.
69
+ *
70
+ * Elements are deduplicated and ordered by first appearance across candidates
71
+ * in provider order, so the merged result is stable rather than reshuffling on
72
+ * every resolve. When two candidates hold the "same" element with different
73
+ * fields (an object whose non-key fields differ), the first-seen copy wins -
74
+ * this merges *membership*, not per-element conflicts. Primitives are
75
+ * deduplicated by value; supply `key` for arrays of objects.
76
+ *
77
+ * ```ts
78
+ * createCloudStore({
79
+ * providers: ['icloudKV', 'googleDrive'],
80
+ * writeMode: 'mirror',
81
+ * resolve: resolveByUnion({ key: item => item.id }),
82
+ * })
83
+ * ```
84
+ */
85
+ export declare function resolveByUnion<T = unknown>(options?: {
86
+ key?: (item: T) => string | number;
87
+ }): ResolveFn;
49
88
  /**
50
89
  * Tries each resolver in turn and takes the first non-null answer.
51
90
  *
@@ -1 +1 @@
1
- {"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../../src/resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,SAAS,EAAE,MAAM,YAAS,CAAA;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAc,GAAG,SAAS,CAqBjE;AAoBD,+EAA+E;AAC/E,eAAO,MAAM,wBAAwB,EAAE,SACa,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAO,GACrC,SAAS,CAsBX;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,SAAS,EAAE,SAAS,EAAE,GAAG,SAAS,CAQnE"}
1
+ {"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../../src/resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,SAAS,EAAE,MAAM,YAAS,CAAA;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAc,GAAG,SAAS,CAqBjE;AAoBD,+EAA+E;AAC/E,eAAO,MAAM,wBAAwB,EAAE,SACa,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAO,GACrC,SAAS,CAsBX;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,cAAc,CAAC,CAAC,GAAG,OAAO,EACxC,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,CAAA;CAAO,GACnD,SAAS,CAyBX;AAYD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,SAAS,EAAE,SAAS,EAAE,GAAG,SAAS,CAQnE"}
package/package.json CHANGED
@@ -1,28 +1,40 @@
1
1
  {
2
2
  "name": "react-native-cloud-sync",
3
- "version": "0.1.0",
4
- "description": "iCloud key-value store, CloudKit and Google Drive under one API - iOS, Android and Web, old and new architecture",
3
+ "version": "0.2.0",
4
+ "description": "Sync React Native app data across iCloud, CloudKit and Google Drive behind one API - offline-first with automatic retry, real-time updates and optional end-to-end encryption. iOS, Android, Web, old and new architecture",
5
5
  "workspaces": [
6
6
  "example"
7
7
  ],
8
8
  "keywords": [
9
9
  "react-native",
10
10
  "expo",
11
+ "expo-module",
11
12
  "icloud",
12
13
  "cloudkit",
13
14
  "ckrecord",
14
15
  "nsubiquitouskeyvaluestore",
16
+ "icloud-drive",
15
17
  "google-drive",
18
+ "google-drive-sync",
16
19
  "cloud",
17
20
  "cloud-storage",
21
+ "cloud-sync",
22
+ "data-sync",
23
+ "file-sync",
18
24
  "sync",
25
+ "offline",
26
+ "offline-first",
19
27
  "backup",
20
28
  "restore",
21
29
  "key-value",
22
- "offline",
30
+ "async-storage",
31
+ "encryption",
32
+ "end-to-end-encryption",
33
+ "react-hooks",
23
34
  "new-architecture",
24
35
  "turbomodule",
25
36
  "typescript",
37
+ "cross-platform",
26
38
  "ios",
27
39
  "android",
28
40
  "web",
package/src/index.ts CHANGED
@@ -37,6 +37,7 @@ export {
37
37
  resolveByTimestamp,
38
38
  resolveByModifiedAt,
39
39
  resolveByPreferenceOrder,
40
+ resolveByUnion,
40
41
  resolveFirstOf
41
42
  } from './resolvers'
42
43
 
package/src/resolvers.ts CHANGED
@@ -112,6 +112,81 @@ export function resolveByModifiedAt(
112
112
  }
113
113
  }
114
114
 
115
+ /**
116
+ * Resolves by merging JSON arrays across every candidate - the union of
117
+ * elements, not a winner-take-all pick.
118
+ *
119
+ * The case {@link resolveByTimestamp} and {@link resolveByModifiedAt} do not
120
+ * cover: a list of favorited item ids, a set of dismissed-tip ids, anything
121
+ * where two devices adding *different* elements between syncs should both
122
+ * survive rather than one write clobbering the other.
123
+ *
124
+ * Deletions do not propagate. Removing an element on one device and syncing
125
+ * does not remove it from the merged result, because nothing in a plain union
126
+ * distinguishes "never added" from "removed" - a plain array carries no
127
+ * tombstones. Track removals yourself (a separate `removedIds` set, synced the
128
+ * same way) if that matters for your data.
129
+ *
130
+ * A candidate that is not valid JSON, or whose JSON is not an array, is
131
+ * dropped rather than treated as empty - a value you cannot read as a list is
132
+ * not evidence that the list is empty. If every candidate drops out this way,
133
+ * falls back to preference order rather than returning `[]` and reading as
134
+ * "nothing favorited" on every device.
135
+ *
136
+ * Elements are deduplicated and ordered by first appearance across candidates
137
+ * in provider order, so the merged result is stable rather than reshuffling on
138
+ * every resolve. When two candidates hold the "same" element with different
139
+ * fields (an object whose non-key fields differ), the first-seen copy wins -
140
+ * this merges *membership*, not per-element conflicts. Primitives are
141
+ * deduplicated by value; supply `key` for arrays of objects.
142
+ *
143
+ * ```ts
144
+ * createCloudStore({
145
+ * providers: ['icloudKV', 'googleDrive'],
146
+ * writeMode: 'mirror',
147
+ * resolve: resolveByUnion({ key: item => item.id }),
148
+ * })
149
+ * ```
150
+ */
151
+ export function resolveByUnion<T = unknown>(
152
+ options: { key?: (item: T) => string | number } = {}
153
+ ): ResolveFn {
154
+ const keyOf = options.key ?? ((item: T) => JSON.stringify(item))
155
+
156
+ return (candidates: ResolveCandidate[]): string | null => {
157
+ if (candidates.length === 0) return null
158
+
159
+ const merged = new Map<string | number, T>()
160
+ let sawArray = false
161
+
162
+ for (const candidate of candidates) {
163
+ const items = arrayOf<T>(candidate.value)
164
+ if (items == null) continue
165
+ sawArray = true
166
+ for (const item of items) {
167
+ const key = keyOf(item)
168
+ if (!merged.has(key)) merged.set(key, item)
169
+ }
170
+ }
171
+
172
+ // Nothing was a readable array: defer to preference order rather than
173
+ // manufacturing an empty list.
174
+ if (!sawArray) return candidates[0].value
175
+
176
+ return JSON.stringify([...merged.values()])
177
+ }
178
+ }
179
+
180
+ function arrayOf<T>(value: string): T[] | null {
181
+ try {
182
+ const parsed: unknown = JSON.parse(value)
183
+ return Array.isArray(parsed) ? (parsed as T[]) : null
184
+ }
185
+ catch {
186
+ return null
187
+ }
188
+ }
189
+
115
190
  /**
116
191
  * Tries each resolver in turn and takes the first non-null answer.
117
192
  *