import { never } from "./../never"; import { ElementId, IGlobalObject, EventType } from "../types"; import { Processor } from "../Processor"; import { TickState } from "../TickState"; import { disposeSubscriptions } from "./disposeSubscriptions"; import { dispose } from "./dispose"; export function tick(elementId: ElementId, globalObject: IGlobalObject, processor: Processor) { const eventsManager = processor.locator.eventsManager; const elementState = processor.getElementStateById(elementId); if (elementState.isDisposed) { return; } elementState.tickState = new TickState(); const oldCurrent = globalObject.current; globalObject.current = { elementId, locator: processor.locator, elementState, }; // run current call try { const result = elementState.func(...elementState.props); elementState.isFirstTick = false; elementState.nextValue = result; } catch (e) { elementState.isFirstTick = false; elementState.error = e; elementState.nextValue = never(); } if (typeof elementState.error !== "undefined") { eventsManager.triggerEvent({ elementId, params: {}, type: EventType.Error, value: elementState.error, }); return; } eventsManager.triggerEvent({ elementId, params: {}, type: EventType.Next, value: elementState.nextValue, }); if (elementState.isForceComplete) { elementState.effects.map((effect) => (effect.dispose ? effect.dispose() : null)); if (elementState.currentCall) { dispose(processor, elementState.currentCall.elementId); elementState.currentCall = undefined; } elementState.effects = []; elementState.stack = []; eventsManager.triggerEvent({ elementId, params: {}, type: EventType.Complete, value: undefined, }); } // dispose subscription disposeSubscriptions(processor, elementState); globalObject.current = oldCurrent; }