* Start the sync process for this collection * This is called when the collection is first accessed or preloaded
()
| 76 | * This is called when the collection is first accessed or preloaded |
| 77 | */ |
| 78 | public startSync(): void { |
| 79 | if ( |
| 80 | this.lifecycle.status !== `idle` && |
| 81 | this.lifecycle.status !== `cleaned-up` |
| 82 | ) { |
| 83 | return // Already started or in progress |
| 84 | } |
| 85 | |
| 86 | this.lifecycle.setStatus(`loading`) |
| 87 | |
| 88 | try { |
| 89 | const syncRes = normalizeSyncFnResult( |
| 90 | this.config.sync.sync({ |
| 91 | collection: this.collection, |
| 92 | begin: (options?: { immediate?: boolean }) => { |
| 93 | this.state.pendingSyncedTransactions.push({ |
| 94 | committed: false, |
| 95 | operations: [], |
| 96 | deletedKeys: new Set(), |
| 97 | rowMetadataWrites: new Map(), |
| 98 | collectionMetadataWrites: new Map(), |
| 99 | immediate: options?.immediate, |
| 100 | }) |
| 101 | }, |
| 102 | write: ( |
| 103 | messageWithOptionalKey: ChangeMessageOrDeleteKeyMessage< |
| 104 | TOutput, |
| 105 | TKey |
| 106 | >, |
| 107 | ) => { |
| 108 | const pendingTransaction = |
| 109 | this.state.pendingSyncedTransactions[ |
| 110 | this.state.pendingSyncedTransactions.length - 1 |
| 111 | ] |
| 112 | if (!pendingTransaction) { |
| 113 | throw new NoPendingSyncTransactionWriteError() |
| 114 | } |
| 115 | if (pendingTransaction.committed) { |
| 116 | throw new SyncTransactionAlreadyCommittedWriteError() |
| 117 | } |
| 118 | |
| 119 | let key: TKey | undefined = undefined |
| 120 | if (`key` in messageWithOptionalKey) { |
| 121 | key = messageWithOptionalKey.key |
| 122 | } else { |
| 123 | key = this.config.getKey(messageWithOptionalKey.value) |
| 124 | } |
| 125 | |
| 126 | if (this.state.pendingLocalChanges.has(key)) { |
| 127 | this.state.pendingLocalOrigins.add(key) |
| 128 | } |
| 129 | |
| 130 | let messageType = messageWithOptionalKey.type |
| 131 | |
| 132 | // Check if an item with this key already exists when inserting |
| 133 | if (messageWithOptionalKey.type === `insert`) { |
| 134 | const insertingIntoExistingSynced = this.state.syncedData.has(key) |
| 135 | const hasPendingDeleteForKey = |
no test coverage detected