Commit d060d41
authored
fix(runner): ensure IDs are added to array elements and default values (commontoolsinc#1908)
* fix(runner): ensure IDs are added to array elements and default values
This change ensures that when writing arrays to cells or pushing to array
cells, all objects are given IDs so they can be stored as separate documents
with links. This applies to both explicitly written arrays and default values
from schemas.
Changes to cell.ts:
1. Modified the `set` method to call `addIDIfNeeded` on each element when
writing an array, ensuring all objects get IDs before being processed by
`diffAndUpdate`.
2. Enhanced the `push` method to:
- Process default values from schemas using `processDefaultValue` to ensure
they're properly materialized with IDs
- Apply `addIDIfNeeded` to all elements (both existing defaults and newly
pushed values) to ensure consistent ID assignment
3. Improved the `update` method's schema validation to:
- Use `resolveSchema` to properly handle schema references
- Check for undefined schemas (which allow objects)
- Consolidate the schema validation logic to determine if objects are
allowed
4. Added new `addIDIfNeeded` helper function that:
- Checks if a value is an object without an ID
- Generates a new ID from the frame's counter if needed
- Preserves existing IDs when present
New tests in cell.test.ts:
- "set operations with arrays" suite:
- Tests that objects written as arrays each get their own document ID
- Verifies that existing IDs are preserved during array writes
- Uses `asSchema` with `asCell: true` to read back as cells and verify
each element has a distinct document ID
- "push operations with default values" suite:
- Tests that default values from schemas are properly materialized with IDs
- Verifies all objects (defaults + pushed) get unique IDs
- Tests push operations both with and without schema defaults
These changes ensure that array operations consistently create separate
documents for each object, maintaining proper referential structure in the
storage layer.
* make adding IDs recursive, since the calendar use-case sets objects with arrays
* make toCell non-enumerable so that {...obj} doesn't copy them and accidentally points at the old object
* fix test that was previously broken, but with the bug hidden by our bug
* feat(runner): implement Symbol.iterator for array query result proxies
Add custom Symbol.iterator implementation for array query result proxies
to support spreading and for...of iteration. When iterating over an array
proxy, each element is now wrapped in its own query result proxy,
maintaining reactivity and cell references throughout the iteration.
This enables patterns like:
- const spread = [...proxy.items]
- for (const item of proxy.items) { ... }
Each iterated element maintains its query result proxy wrapper, allowing
access to toCell() and preserving the link to the underlying cell data.
* test(runner): add tests for Symbol.iterator in array query result proxies
Add comprehensive tests verifying Symbol.iterator behavior for array
query result proxies:
- for...of iteration with object elements
- Spread operator with object elements
- Nested array spreading with proper proxy preservation
- Arrays containing cell references (links to other cells)
Tests confirm that iteration and spreading maintain query result proxy
wrappers for each element, allowing access to toCell() and preserving
reactivity throughout iteration. The cell references test specifically
validates that link resolution works correctly when iterating over arrays
of cell links.
* log .length read in tx for Symbol.iterator
* outliner: clone tree before applying edit
otherwise Cell.set turns this back into cell references and this can create loops
(fwiw, we might want to turn this into a structure where each node is its own document with its own id, in which case we'd actually want this behavior)1 parent c396155 commit d060d41
File tree
8 files changed
+592
-56
lines changed- packages
- generated-patterns/integration/patterns
- runner
- src
- builder
- test
- ui/src/v2/components/ct-outliner
8 files changed
+592
-56
lines changedLines changed: 2 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
576 | 576 | | |
577 | 577 | | |
578 | 578 | | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
| 579 | + | |
| 580 | + | |
585 | 581 | | |
586 | 582 | | |
587 | 583 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
| |||
448 | 453 | | |
449 | 454 | | |
450 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
451 | 459 | | |
452 | 460 | | |
453 | 461 | | |
454 | 462 | | |
455 | 463 | | |
456 | | - | |
| 464 | + | |
457 | 465 | | |
458 | 466 | | |
459 | 467 | | |
| |||
486 | 494 | | |
487 | 495 | | |
488 | 496 | | |
489 | | - | |
| 497 | + | |
| 498 | + | |
490 | 499 | | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
508 | 516 | | |
509 | 517 | | |
510 | 518 | | |
511 | 519 | | |
| 520 | + | |
512 | 521 | | |
513 | 522 | | |
514 | 523 | | |
515 | 524 | | |
516 | 525 | | |
517 | | - | |
518 | | - | |
| 526 | + | |
519 | 527 | | |
520 | 528 | | |
521 | 529 | | |
| |||
546 | 554 | | |
547 | 555 | | |
548 | 556 | | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
| |||
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
568 | | - | |
569 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
570 | 576 | | |
571 | 577 | | |
572 | 578 | | |
| |||
575 | 581 | | |
576 | 582 | | |
577 | 583 | | |
578 | | - | |
| 584 | + | |
579 | 585 | | |
580 | 586 | | |
581 | 587 | | |
| |||
877 | 883 | | |
878 | 884 | | |
879 | 885 | | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
880 | 958 | | |
881 | 959 | | |
882 | 960 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
126 | 153 | | |
127 | 154 | | |
128 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| |||
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
313 | | - | |
314 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
315 | 322 | | |
316 | 323 | | |
317 | 324 | | |
| |||
0 commit comments