( options: CreateOfflineActionOptions<T>, mutationFn: OfflineMutationFn, persistTransaction: (tx: OfflineTransactionType) => Promise<void>, executor: any, )
| 16 | } |
| 17 | |
| 18 | export function createOfflineAction<T>( |
| 19 | options: CreateOfflineActionOptions<T>, |
| 20 | mutationFn: OfflineMutationFn, |
| 21 | persistTransaction: (tx: OfflineTransactionType) => Promise<void>, |
| 22 | executor: any, |
| 23 | ): (variables: T) => Transaction { |
| 24 | const { mutationFnName, onMutate } = options |
| 25 | console.log(`createOfflineAction 2`, options) |
| 26 | |
| 27 | return (variables: T): Transaction => { |
| 28 | const offlineTransaction = new OfflineTransaction( |
| 29 | { |
| 30 | mutationFnName, |
| 31 | autoCommit: false, |
| 32 | }, |
| 33 | mutationFn, |
| 34 | persistTransaction, |
| 35 | executor, |
| 36 | ) |
| 37 | |
| 38 | const transaction = offlineTransaction.mutate(() => { |
| 39 | console.log(`mutate`) |
| 40 | const maybePromise = onMutate(variables) as unknown |
| 41 | |
| 42 | if (isPromiseLike(maybePromise)) { |
| 43 | throw new OnMutateMustBeSynchronousError() |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | // Immediately commit |
| 48 | const commitPromise = (async () => { |
| 49 | try { |
| 50 | await transaction.commit() |
| 51 | console.log(`offlineAction committed - success`) |
| 52 | } catch { |
| 53 | console.log(`offlineAction commit failed - error`) |
| 54 | } |
| 55 | })() |
| 56 | |
| 57 | // Don't await - this is fire-and-forget for optimistic actions |
| 58 | // But catch to prevent unhandled rejection |
| 59 | commitPromise.catch(() => { |
| 60 | // Already handled in try/catch above |
| 61 | }) |
| 62 | |
| 63 | return transaction |
| 64 | } |
| 65 | } |
no test coverage detected