| 231 | } |
| 232 | |
| 233 | async function syncItems({ |
| 234 | transaction, |
| 235 | }: { |
| 236 | transaction: { mutations: Array<PendingMutation> } |
| 237 | idempotencyKey: string |
| 238 | }) { |
| 239 | for (const mutation of transaction.mutations) { |
| 240 | const data = mutation.modified as ShoppingItem |
| 241 | switch (mutation.type) { |
| 242 | case `insert`: { |
| 243 | const created = await itemsApi.create({ |
| 244 | id: data.id, |
| 245 | listId: data.listId, |
| 246 | text: data.text, |
| 247 | checked: data.checked, |
| 248 | createdAt: data.createdAt, |
| 249 | }) |
| 250 | await itemsCollection.utils.awaitTxId(created.txid) |
| 251 | break |
| 252 | } |
| 253 | case `update`: { |
| 254 | const updated = await itemsApi.update(data.id, { |
| 255 | text: data.text, |
| 256 | checked: data.checked, |
| 257 | }) |
| 258 | if (updated) { |
| 259 | await itemsCollection.utils.awaitTxId(updated.txid) |
| 260 | } |
| 261 | break |
| 262 | } |
| 263 | case `delete`: { |
| 264 | const deleted = await itemsApi.delete( |
| 265 | (mutation.original as ShoppingItem).id, |
| 266 | ) |
| 267 | if (deleted) { |
| 268 | await itemsCollection.utils.awaitTxId(deleted.txid) |
| 269 | } |
| 270 | break |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | export function createOfflineExecutor() { |
| 277 | return startOfflineExecutor({ |