| 193 | ) |
| 194 | |
| 195 | async function syncLists({ |
| 196 | transaction, |
| 197 | }: { |
| 198 | transaction: { mutations: Array<PendingMutation> } |
| 199 | idempotencyKey: string |
| 200 | }) { |
| 201 | for (const mutation of transaction.mutations) { |
| 202 | const data = mutation.modified as ShoppingList |
| 203 | switch (mutation.type) { |
| 204 | case `insert`: { |
| 205 | const created = await listsApi.create({ |
| 206 | id: data.id, |
| 207 | name: data.name, |
| 208 | createdAt: data.createdAt, |
| 209 | }) |
| 210 | await listsCollection.utils.awaitTxId(created.txid) |
| 211 | break |
| 212 | } |
| 213 | case `update`: { |
| 214 | const updated = await listsApi.update(data.id, { name: data.name }) |
| 215 | if (updated) { |
| 216 | await listsCollection.utils.awaitTxId(updated.txid) |
| 217 | } |
| 218 | break |
| 219 | } |
| 220 | case `delete`: { |
| 221 | const deleted = await listsApi.delete( |
| 222 | (mutation.original as ShoppingList).id, |
| 223 | ) |
| 224 | if (deleted) { |
| 225 | await listsCollection.utils.awaitTxId(deleted.txid) |
| 226 | } |
| 227 | break |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | async function syncItems({ |
| 234 | transaction, |