()
| 481 | |
| 482 | /** Create a new tree state store instance. */ |
| 483 | export function createTreeStateStore(): TreeStateStore { |
| 484 | const table = new Map<string, TreeLocalState>(); |
| 485 | |
| 486 | const store: TreeStateStore = Object.freeze({ |
| 487 | get: (id) => table.get(id) ?? DEFAULT_TREE_STATE, |
| 488 | set: (id, patch) => { |
| 489 | const prev = table.get(id) ?? DEFAULT_TREE_STATE; |
| 490 | const next: TreeLocalState = Object.freeze({ |
| 491 | focusedKey: patch.focusedKey !== undefined ? patch.focusedKey : prev.focusedKey, |
| 492 | loadingKeys: |
| 493 | patch.loadingKeys !== undefined ? cloneReadonlySet(patch.loadingKeys) : prev.loadingKeys, |
| 494 | scrollTop: patch.scrollTop !== undefined ? patch.scrollTop : prev.scrollTop, |
| 495 | viewportHeight: |
| 496 | patch.viewportHeight !== undefined ? patch.viewportHeight : prev.viewportHeight, |
| 497 | flatCache: patch.flatCache !== undefined ? patch.flatCache : prev.flatCache, |
| 498 | expandedSetRef: |
| 499 | patch.expandedSetRef !== undefined ? patch.expandedSetRef : prev.expandedSetRef, |
| 500 | expandedSet: |
| 501 | patch.expandedSet !== undefined ? cloneReadonlySet(patch.expandedSet) : prev.expandedSet, |
| 502 | prefixCache: patch.prefixCache !== undefined ? patch.prefixCache : prev.prefixCache, |
| 503 | }); |
| 504 | table.set(id, next); |
| 505 | return next; |
| 506 | }, |
| 507 | delete: (id) => { |
| 508 | table.delete(id); |
| 509 | }, |
| 510 | keys: () => table.keys(), |
| 511 | startLoading: (id, nodeKey) => { |
| 512 | const prev = table.get(id) ?? DEFAULT_TREE_STATE; |
| 513 | const newLoading = new Set(prev.loadingKeys); |
| 514 | newLoading.add(nodeKey); |
| 515 | const next: TreeLocalState = Object.freeze({ |
| 516 | ...prev, |
| 517 | loadingKeys: cloneReadonlySet(newLoading), |
| 518 | }); |
| 519 | table.set(id, next); |
| 520 | return next; |
| 521 | }, |
| 522 | finishLoading: (id, nodeKey) => { |
| 523 | const prev = table.get(id) ?? DEFAULT_TREE_STATE; |
| 524 | if (!prev.loadingKeys.has(nodeKey)) { |
| 525 | return prev; |
| 526 | } |
| 527 | const newLoading = new Set(prev.loadingKeys); |
| 528 | newLoading.delete(nodeKey); |
| 529 | const next: TreeLocalState = Object.freeze({ |
| 530 | ...prev, |
| 531 | loadingKeys: cloneReadonlySet(newLoading), |
| 532 | }); |
| 533 | table.set(id, next); |
| 534 | return next; |
| 535 | }, |
| 536 | }); |
| 537 | |
| 538 | return store; |
| 539 | } |
no test coverage detected