UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

66 lines (65 loc) 2.31 kB
"use strict"; import { EventConnectionPoint, EventConnectionPointType } from "../utils/io/connections/Event"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { TypedEventNode } from "./_Base"; import debounce from "lodash-es/debounce"; const callbackOption = { callback: (node) => { DebounceEventNode.PARAM_CALLBACK_resetDebouncedFunc(node); } }; class DebounceEventParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param max wait time between each event, in milliseconds */ this.maxwait = ParamConfig.INTEGER(100, { range: [0, 1e3], rangeLocked: [true, false], ...callbackOption }); /** @param defines if event is dispatched on the leading edge of the timeout */ this.leading = ParamConfig.BOOLEAN(false, callbackOption); /** @param defines if event is trailing on the leading edge of the timeout */ this.trailing = ParamConfig.BOOLEAN(true, callbackOption); } } const ParamsConfig = new DebounceEventParamsConfig(); const _DebounceEventNode = class extends TypedEventNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "debounce"; } initializeNode() { this.io.inputs.setNamedInputConnectionPoints([ new EventConnectionPoint("trigger", EventConnectionPointType.BASE) ]); this.io.outputs.setNamedOutputConnectionPoints([ new EventConnectionPoint(_DebounceEventNode.OUTPUT_NAME, EventConnectionPointType.BASE) ]); } processEvent(eventContext) { this._lastEventContextReceived = eventContext; this._debouncedFunc = this._debouncedFunc || this._buildDebouncedFunc(); this._debouncedFunc(); } _buildDebouncedFunc() { const { leading, trailing, maxwait } = this.pv; const func = () => { if (this._lastEventContextReceived) { this.dispatchEventToOutput(_DebounceEventNode.OUTPUT_NAME, this._lastEventContextReceived); } }; return debounce(func, maxwait, { leading, trailing }); } _resetDebouncedFunc() { this._debouncedFunc = void 0; } static PARAM_CALLBACK_resetDebouncedFunc(node) { node._resetDebouncedFunc(); } }; export let DebounceEventNode = _DebounceEventNode; DebounceEventNode.OUTPUT_NAME = "output";