Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/nanoviews/src/flow/for.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,36 @@ describe('nanoviews', () => {
expect(runs).toEqual([0])
})

it('should render the placeholder for an absent array', () => {
const items = signal<Player[] | undefined>(undefined)
const { container } = render(() => ul()(
for_(items, trackById)(
item => li()(record(item).$name),
() => li()('nobody')
)
))

expect(container.innerHTML).toBe('<div><ul><li>nobody</li></ul></div>')

items([
{
id: 1,
name: 'Yatoro'
}
])

expect(container.innerHTML).toBe('<div><ul><li>Yatoro</li></ul></div>')

// no array is the same emptiness as an empty one
items(undefined)

expect(container.innerHTML).toBe('<div><ul><li>nobody</li></ul></div>')

items([])

expect(container.innerHTML).toBe('<div><ul><li>nobody</li></ul></div>')
})

describe('fuzz', () => {
// A named test pins a shape someone thought of; these walk sequences
// nobody did. Both invariants are checked after every step: the rows
Expand Down
13 changes: 9 additions & 4 deletions packages/nanoviews/src/flow/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type Accessor,
type ReadableSignal,
type WritableSignal,
type EmptyValue,
isAccessor
} from 'kida'
import {
Expand Down Expand Up @@ -51,24 +52,28 @@ type StaticEach<T> = (

type UnknownTrack = (item: unknown, index: number) => unknown

// A signal is invariant, so the nullable array is a separate arm rather than
// a widening of the first: `WritableSignal<T[]>` does not fit
// `WritableSignal<T[] | EmptyValue>`, and widening would take the rows of
// every existing caller down to read-only
export function for_<T>(
$items: WritableSignal<T[]>,
$items: WritableSignal<T[]> | WritableSignal<T[] | EmptyValue>,
track?: (item: T, index: number) => unknown
): (
each_: WritableEach<T>,
else_?: () => Child
) => Child

export function for_<T>(
$items: Accessor<T[]>,
$items: Accessor<T[] | EmptyValue>,
track?: (item: T, index: number) => unknown
): (
each_: ReadableEach<T>,
else_?: () => Child
) => Child

export function for_<T>(
$items: T[]
$items: T[] | EmptyValue
): (
each_: StaticEach<T>,
else_?: () => Child
Expand All @@ -81,7 +86,7 @@ export function for_<T>(
* @returns Function to receive each_ and else_ functions
*/
export function for_(
$items: unknown[] | Accessor<unknown[]>,
$items: unknown[] | EmptyValue | Accessor<unknown[] | EmptyValue>,
track?: UnknownTrack
) {
if (isAccessor($items)) {
Expand Down
17 changes: 11 additions & 6 deletions packages/nanoviews/src/internals/flow/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
nextValue,
assignIndex
} from 'kida'
import type { Child } from '../types/index.js'
import type {
Child,
EmptyValue
} from '../types/index.js'
import {
deferScopeBindContext,
effectScopeSwapper
Expand Down Expand Up @@ -341,7 +344,7 @@ function reconcile(
}

export function loop(
$items: Accessor<unknown[]>,
$items: Accessor<unknown[] | EmptyValue>,
each_: AnyEach,
else_?: () => Child,
track: UnknownTrack = (_, i) => i
Expand Down Expand Up @@ -389,9 +392,11 @@ export function loop(

effectScopeSwapper($items, (
destroyPrev: DeferredScope | undefined,
items: unknown[]
items: unknown[] | EmptyValue
) => {
const itemsCount = items.length
// No array at all is the same emptiness as an empty one: the rows go and
// the placeholder comes, and nothing below ever reaches into it
const itemsCount = items?.length

if (itemsCount && destroyPrev !== undefined && !isPlaceholder) {
// [...m] -> [...n]
Expand All @@ -405,7 +410,7 @@ export function loop(
reconcile,
itemsList,
blocksMap,
$items,
$items as Accessor<unknown[]>,
each_,
track,
end,
Expand Down Expand Up @@ -438,7 +443,7 @@ export function loop(
reconcile(
itemsList,
blocksMap,
$items,
$items as Accessor<unknown[]>,
each_,
track,
end,
Expand Down
2 changes: 0 additions & 2 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
- hooks/effects naming convention

- error boundaries: cleanup half-created scope on render throw (stopScope in catch) - rows/content are not parent-linked, teardown must be explicit
- for loop array or empty check for else
- for_ duplicate track keys: unsupported by design, must keep failing loudly - the crash lands a step away from the cause, so it needs a clear throw at the point of detection
- rework return slot$ to look like element/component? fn.prop is faster than {f,p}
- util to transform static props to signal props?
- typed effectattrs <T>
- additional arg for record in for$? as$
- controls addEventListener to events system
- static index for untracked loop?

for_($items)(
Expand Down