UNPKG

@vaadin/hilla-react-signals

Version:

Signals for Hilla React

108 lines 5.23 kB
import { CollectionSignal } from './CollectionSignal.js'; import { createInsertCommand, createRemoveCommand, isAdoptAtCommand, isInsertCommand, isPositionCondition, isRemoveCommand, isSetCommand, isSnapshotCommand, ListPosition, ZERO, } from './commands.js'; import { $createOperation, $processServerResponse, $resolveOperation, $setValueQuietly, $update, } from './FullStackSignal.js'; import { ValueSignal } from './ValueSignal.js'; export class ListSignal extends CollectionSignal { constructor(config, id) { super([], config, id); } insertFirst(value) { return this.insertAt(value, ListPosition.first()); } insertLast(value) { return this.insertAt(value, ListPosition.last()); } insertAt(value, at) { const command = createInsertCommand(ZERO, value, at); const promise = this[$update](command); return this[$createOperation]({ id: command.commandId, promise }); } remove(child) { const command = createRemoveCommand(child.id, ZERO); const promise = this[$update](command); return this[$createOperation]({ id: command.commandId, promise }); } [$processServerResponse](command) { if ((isSnapshotCommand(command) || isSetCommand(command)) && command.targetNodeId) { const targetChild = this.value.find((child) => child.id === command.targetNodeId); if (targetChild) { targetChild[$processServerResponse](command); return; } } if (isInsertCommand(command)) { const valueSignal = new ValueSignal(command.value, this.server.config, command.commandId, this); let insertIndex = this.value.length; const pos = command.position; if (pos.after === '' && pos.before == null) { insertIndex = 0; } else if (pos.after == null && pos.before === '') { insertIndex = this.value.length; } else if (typeof pos.after === 'string' && pos.after !== '') { const idx = this.value.findIndex((v) => v.id === pos.after); insertIndex = idx !== -1 ? idx + 1 : this.value.length; } else if (typeof pos.before === 'string' && pos.before !== '') { const idx = this.value.findIndex((v) => v.id === pos.before); insertIndex = idx !== -1 ? idx : this.value.length; } const newList = [...this.value.slice(0, insertIndex), valueSignal, ...this.value.slice(insertIndex)]; this[$setValueQuietly](newList); this[$resolveOperation](command.commandId, undefined); } else if (isRemoveCommand(command)) { const removeIndex = this.value.findIndex((child) => child.id === command.targetNodeId); if (removeIndex !== -1) { const newList = [...this.value.slice(0, removeIndex), ...this.value.slice(removeIndex + 1)]; this[$setValueQuietly](newList); } this[$resolveOperation](command.commandId, undefined); } else if (isAdoptAtCommand(command)) { const moveIndex = this.value.findIndex((child) => child.id === command.childId); if (moveIndex !== -1) { const [movedChild] = this.value.splice(moveIndex, 1); let newIndex = this.value.length; const pos = command.position; if (pos.after === '' && pos.before == null) { newIndex = 0; } else if (pos.after == null && pos.before === '') { newIndex = this.value.length; } else if (typeof pos.after === 'string' && pos.after !== '') { const idx = this.value.findIndex((v) => v.id === pos.after); newIndex = idx !== -1 ? idx + 1 : this.value.length; } else if (typeof pos.before === 'string' && pos.before !== '') { const idx = this.value.findIndex((v) => v.id === pos.before); newIndex = idx !== -1 ? idx : this.value.length; } this.value.splice(newIndex, 0, movedChild); } this[$resolveOperation](command.commandId, undefined); } else if (isPositionCondition(command)) { this[$resolveOperation](command.commandId, undefined); } else if (isSnapshotCommand(command)) { const { nodes } = command; const listNode = nodes['']; const childrenIds = listNode.listChildren; const valueSignals = childrenIds .map((childId) => { const childNode = nodes[childId]; if ('value' in childNode) { return new ValueSignal(childNode.value, this.server.config, childId, this); } return null; }) .filter(Boolean); this[$setValueQuietly](valueSignals); this[$resolveOperation](command.commandId, undefined); } } } //# sourceMappingURL=ListSignal.js.map