UNPKG

@vaadin/hilla-react-signals

Version:

Signals for Hilla React

135 lines 3.65 kB
import { randomId } from './utils'; export function createValueCondition(targetNodeId, expectedValue) { return { commandId: randomId(), targetNodeId, '@type': 'value', expectedValue, }; } export function createSetCommand(targetNodeId, value) { return { commandId: randomId(), targetNodeId, '@type': 'set', value, }; } export function createIncrementCommand(targetNodeId, delta) { return { commandId: randomId(), targetNodeId, '@type': 'inc', delta, }; } export function createTransactionCommand(commands) { return { commandId: randomId(), targetNodeId: '', '@type': 'tx', commands, }; } export const ZERO = ''; export function createInsertCommand(targetNodeId, value, position) { return { commandId: randomId(), targetNodeId, '@type': 'insert', value, position, }; } export const EDGE = ''; function idOf(signal) { return signal?.id ?? EDGE; } export const ListPosition = { first() { return { after: EDGE, before: null }; }, last() { return { after: null, before: EDGE }; }, after(signal) { return { after: idOf(signal), before: null }; }, before(signal) { return { after: null, before: idOf(signal) }; }, between(after, before) { return { after: idOf(after), before: idOf(before) }; }, }; export function createAdoptAtCommand(targetNodeId, childId, position) { return { commandId: randomId(), targetNodeId, '@type': 'at', childId, position, }; } export function createPositionCondition(targetNodeId, childId, expectedPosition) { return { commandId: randomId(), targetNodeId, '@type': 'pos', childId, expectedPosition, }; } export function createRemoveCommand(targetNodeId, expectedParentId) { return { commandId: randomId(), targetNodeId, '@type': 'remove', expectedParentId, }; } export function createSnapshotCommand(nodes) { return { commandId: randomId(), targetNodeId: '', '@type': 'snapshot', nodes, }; } function isSignalCommand(command) { return (typeof command === 'object' && command !== null && typeof command.commandId === 'string' && typeof command['@type'] === 'string'); } export function isSetCommand(command) { return isSignalCommand(command) && command['@type'] === 'set'; } export function isValueCondition(command) { return isSignalCommand(command) && command['@type'] === 'value'; } export function isIncrementCommand(command) { return isSignalCommand(command) && command['@type'] === 'inc'; } export function isTransactionCommand(command) { return (isSignalCommand(command) && command['@type'] === 'tx' && command.targetNodeId === '' && Array.isArray(command.commands)); } export function isInsertCommand(command) { return isSignalCommand(command) && command['@type'] === 'insert'; } export function isAdoptAtCommand(command) { return isSignalCommand(command) && command['@type'] === 'at'; } export function isPositionCondition(command) { return isSignalCommand(command) && command['@type'] === 'pos'; } export function isRemoveCommand(command) { return isSignalCommand(command) && command['@type'] === 'remove'; } export function isSnapshotCommand(command) { return isSignalCommand(command) && command['@type'] === 'snapshot'; } //# sourceMappingURL=commands.js.map