react-native-cloud-sync 0.1.0 → 0.3.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.
Files changed (57) hide show
  1. package/README.md +58 -53
  2. package/ios/CloudSyncImpl.swift +25 -2
  3. package/lib/commonjs/config.js +11 -1
  4. package/lib/commonjs/config.js.map +1 -1
  5. package/lib/commonjs/index.js +6 -0
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/internal/cloudKitRest.js +205 -21
  8. package/lib/commonjs/internal/cloudKitRest.js.map +1 -1
  9. package/lib/commonjs/internal/googleDriveRest.js +173 -38
  10. package/lib/commonjs/internal/googleDriveRest.js.map +1 -1
  11. package/lib/commonjs/providers/cloudKit.js +89 -19
  12. package/lib/commonjs/providers/cloudKit.js.map +1 -1
  13. package/lib/commonjs/resolvers.js +68 -0
  14. package/lib/commonjs/resolvers.js.map +1 -1
  15. package/lib/module/config.js +10 -1
  16. package/lib/module/config.js.map +1 -1
  17. package/lib/module/index.js +1 -1
  18. package/lib/module/index.js.map +1 -1
  19. package/lib/module/internal/cloudKitRest.js +205 -21
  20. package/lib/module/internal/cloudKitRest.js.map +1 -1
  21. package/lib/module/internal/googleDriveRest.js +174 -39
  22. package/lib/module/internal/googleDriveRest.js.map +1 -1
  23. package/lib/module/providers/cloudKit.js +92 -22
  24. package/lib/module/providers/cloudKit.js.map +1 -1
  25. package/lib/module/resolvers.js +67 -0
  26. package/lib/module/resolvers.js.map +1 -1
  27. package/lib/typescript/commonjs/config.d.ts +8 -0
  28. package/lib/typescript/commonjs/config.d.ts.map +1 -1
  29. package/lib/typescript/commonjs/index.d.ts +2 -2
  30. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  31. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts +90 -0
  32. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts +73 -8
  34. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts.map +1 -1
  35. package/lib/typescript/commonjs/providers/cloudKit.d.ts +32 -13
  36. package/lib/typescript/commonjs/providers/cloudKit.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/resolvers.d.ts +39 -0
  38. package/lib/typescript/commonjs/resolvers.d.ts.map +1 -1
  39. package/lib/typescript/module/config.d.ts +8 -0
  40. package/lib/typescript/module/config.d.ts.map +1 -1
  41. package/lib/typescript/module/index.d.ts +2 -2
  42. package/lib/typescript/module/index.d.ts.map +1 -1
  43. package/lib/typescript/module/internal/cloudKitRest.d.ts +90 -0
  44. package/lib/typescript/module/internal/cloudKitRest.d.ts.map +1 -1
  45. package/lib/typescript/module/internal/googleDriveRest.d.ts +73 -8
  46. package/lib/typescript/module/internal/googleDriveRest.d.ts.map +1 -1
  47. package/lib/typescript/module/providers/cloudKit.d.ts +32 -13
  48. package/lib/typescript/module/providers/cloudKit.d.ts.map +1 -1
  49. package/lib/typescript/module/resolvers.d.ts +39 -0
  50. package/lib/typescript/module/resolvers.d.ts.map +1 -1
  51. package/package.json +15 -3
  52. package/src/config.ts +12 -2
  53. package/src/index.ts +4 -1
  54. package/src/internal/cloudKitRest.ts +272 -29
  55. package/src/internal/googleDriveRest.ts +196 -42
  56. package/src/providers/cloudKit.ts +138 -41
  57. package/src/resolvers.ts +75 -0
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
  *