(params: InsertMutationFnParams<any>)
| 437 | * Wraps the user's onInsert handler to also save changes to localStorage |
| 438 | */ |
| 439 | const wrappedOnInsert = async (params: InsertMutationFnParams<any>) => { |
| 440 | // Validate that all values in the transaction can be JSON serialized |
| 441 | params.transaction.mutations.forEach((mutation) => { |
| 442 | validateJsonSerializable(parser, mutation.modified, `insert`) |
| 443 | }) |
| 444 | |
| 445 | // Call the user handler BEFORE persisting changes (if provided) |
| 446 | let handlerResult: any = {} |
| 447 | if (config.onInsert) { |
| 448 | handlerResult = (await config.onInsert(params)) ?? {} |
| 449 | } |
| 450 | |
| 451 | // Always persist to storage |
| 452 | // Use lastKnownData (in-memory cache) instead of reading from storage |
| 453 | // Add new items with version keys |
| 454 | params.transaction.mutations.forEach((mutation) => { |
| 455 | // Use the engine's pre-computed key for consistency |
| 456 | const storedItem: StoredItem<any> = { |
| 457 | versionKey: generateUuid(), |
| 458 | data: mutation.modified, |
| 459 | } |
| 460 | lastKnownData.set(mutation.key, storedItem) |
| 461 | }) |
| 462 | |
| 463 | // Save to storage |
| 464 | saveToStorage(lastKnownData) |
| 465 | |
| 466 | // Confirm mutations through sync interface (moves from optimistic to synced state) |
| 467 | // without reloading from storage |
| 468 | sync.confirmOperationsSync(params.transaction.mutations) |
| 469 | |
| 470 | return handlerResult |
| 471 | } |
| 472 | |
| 473 | const wrappedOnUpdate = async (params: UpdateMutationFnParams<any>) => { |
| 474 | // Validate that all values in the transaction can be JSON serialized |
nothing calls this directly
no test coverage detected