@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
66 lines (65 loc) • 2.28 kB
JavaScript
;
import { EventConnectionPoint, EventConnectionPointType } from "../utils/io/connections/Event";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { TypedEventNode } from "./_Base";
import throttle from "lodash-es/throttle";
const callbackOption = {
callback: (node) => {
ThrottleEventNode.PARAM_CALLBACK_resetDebouncedFunc(node);
}
};
class ThrottleEventParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param max time between each event */
this.time = 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(true, callbackOption);
/** @param defines if event is trailing on the leading edge of the timeout */
this.trailing = ParamConfig.BOOLEAN(true, callbackOption);
}
}
const ParamsConfig = new ThrottleEventParamsConfig();
const _ThrottleEventNode = class extends TypedEventNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "throttle";
}
initializeNode() {
this.io.inputs.setNamedInputConnectionPoints([
new EventConnectionPoint("trigger", EventConnectionPointType.BASE)
]);
this.io.outputs.setNamedOutputConnectionPoints([
new EventConnectionPoint(_ThrottleEventNode.OUTPUT_NAME, EventConnectionPointType.BASE)
]);
}
processEvent(eventContext) {
this._lastEventContextReceived = eventContext;
this._debouncedFunc = this._debouncedFunc || this._buildDebouncedFunc();
this._debouncedFunc();
}
_buildDebouncedFunc() {
const { leading, trailing, time } = this.pv;
const func = () => {
if (this._lastEventContextReceived) {
this.dispatchEventToOutput(_ThrottleEventNode.OUTPUT_NAME, this._lastEventContextReceived);
}
};
return throttle(func, time, { leading, trailing });
}
_resetDebouncedFunc() {
this._debouncedFunc = void 0;
}
static PARAM_CALLBACK_resetDebouncedFunc(node) {
node._resetDebouncedFunc();
}
};
export let ThrottleEventNode = _ThrottleEventNode;
ThrottleEventNode.OUTPUT_NAME = "output";