* Internal function to create localStorage sync configuration * Creates a sync configuration that handles localStorage persistence and cross-tab synchronization * @param storageKey - The key used for storing data in localStorage * @param storage - The storage API to use (localStorage, sessionStor
( storageKey: string, storage: StorageApi, storageEventApi: StorageEventApi, parser: Parser, _getKey: (item: T) => string | number, lastKnownData: Map<string | number, StoredItem<T>>, )
| 689 | * @returns Sync configuration with manual trigger capability |
| 690 | */ |
| 691 | function createLocalStorageSync<T extends object>( |
| 692 | storageKey: string, |
| 693 | storage: StorageApi, |
| 694 | storageEventApi: StorageEventApi, |
| 695 | parser: Parser, |
| 696 | _getKey: (item: T) => string | number, |
| 697 | lastKnownData: Map<string | number, StoredItem<T>>, |
| 698 | ): SyncConfig<T> & { |
| 699 | manualTrigger?: () => void |
| 700 | collection: any |
| 701 | confirmOperationsSync: (mutations: Array<any>) => void |
| 702 | } { |
| 703 | let syncParams: Parameters<SyncConfig<T>[`sync`]>[0] | null = null |
| 704 | let collection: any = null |
| 705 | |
| 706 | /** |
| 707 | * Compare two Maps to find differences using version keys |
| 708 | * @param oldData - The previous state of stored items |
| 709 | * @param newData - The current state of stored items |
| 710 | * @returns Array of changes with type, key, and value information |
| 711 | */ |
| 712 | const findChanges = ( |
| 713 | oldData: Map<string | number, StoredItem<T>>, |
| 714 | newData: Map<string | number, StoredItem<T>>, |
| 715 | ): Array<{ |
| 716 | type: `insert` | `update` | `delete` |
| 717 | key: string | number |
| 718 | value?: T |
| 719 | }> => { |
| 720 | const changes: Array<{ |
| 721 | type: `insert` | `update` | `delete` |
| 722 | key: string | number |
| 723 | value?: T |
| 724 | }> = [] |
| 725 | |
| 726 | // Check for deletions and updates |
| 727 | oldData.forEach((oldStoredItem, key) => { |
| 728 | const newStoredItem = newData.get(key) |
| 729 | if (!newStoredItem) { |
| 730 | changes.push({ type: `delete`, key, value: oldStoredItem.data }) |
| 731 | } else if (oldStoredItem.versionKey !== newStoredItem.versionKey) { |
| 732 | changes.push({ type: `update`, key, value: newStoredItem.data }) |
| 733 | } |
| 734 | }) |
| 735 | |
| 736 | // Check for insertions |
| 737 | newData.forEach((newStoredItem, key) => { |
| 738 | if (!oldData.has(key)) { |
| 739 | changes.push({ type: `insert`, key, value: newStoredItem.data }) |
| 740 | } |
| 741 | }) |
| 742 | |
| 743 | return changes |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Process storage changes and update collection |
| 748 | * Loads new data from storage, compares with last known state, and applies changes |
no test coverage detected