UNPKG

@vaadin/hilla-react-signals

Version:

Signals for Hilla React

31 lines 1.3 kB
import { createSetCommand, isSetCommand, isSnapshotCommand } from './commands.js'; import { $createOperation, $processServerResponse, $resolveOperation, $setValueQuietly, $update, FullStackSignal, } from './FullStackSignal.js'; export class ValueSignal extends FullStackSignal { #pendingRequests = new Map(); set(value) { const command = createSetCommand('', value); const promise = this[$update](command); this[$setValueQuietly](value); return this[$createOperation]({ id: command.commandId, promise }); } [$processServerResponse](command) { const record = this.#pendingRequests.get(command.commandId); if (record) { this.#pendingRequests.delete(command.commandId); } this.#recalculateState(command); [record?.id, command.commandId].filter(Boolean).forEach((id) => this[$resolveOperation](id, undefined)); } #recalculateState(command) { if (isSetCommand(command)) { this[$setValueQuietly](command.value); } else if (isSnapshotCommand(command)) { const node = command.nodes['']; if (node && 'value' in node) { this[$setValueQuietly](node.value); } } } } //# sourceMappingURL=ValueSignal.js.map