@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
82 lines • 3.63 kB
JavaScript
import { FlowGraphAsyncExecutionBlock } from "../../../flowGraphAsyncExecutionBlock.js";
import { RichTypeNumber } from "../../../flowGraphRichTypes.js";
import { AdvancedTimer } from "../../../../Misc/timer.js";
import { Logger } from "../../../../Misc/logger.js";
import { RegisterClass } from "../../../../Misc/typeStore.js";
/**
* Block that sets a delay in seconds before activating the output signal.
*/
export class FlowGraphSetDelayBlock extends FlowGraphAsyncExecutionBlock {
constructor(config) {
super(config);
this.cancel = this._registerSignalInput("cancel");
this.duration = this.registerDataInput("duration", RichTypeNumber);
this.lastDelayIndex = this.registerDataOutput("lastDelayIndex", RichTypeNumber, -1);
}
_preparePendingTasks(context) {
const duration = this.duration.getValue(context);
if (duration < 0 || isNaN(duration) || !isFinite(duration)) {
return this._reportError(context, "Invalid duration in SetDelay block");
}
// active delays are global to the context
const activeDelays = context._getGlobalContextVariable("activeDelays", 0);
if (activeDelays >= FlowGraphSetDelayBlock.MaxParallelDelayCount) {
return this._reportError(context, "Max parallel delays reached");
}
// get the last global delay index
const lastDelayIndex = context._getGlobalContextVariable("lastDelayIndex", -1);
// these are block-specific and not global
const timers = context._getExecutionVariable(this, "pendingDelays", []);
const scene = context.configuration.scene;
const timer = new AdvancedTimer({
timeout: duration * 1000, // duration is in seconds
contextObservable: scene.onBeforeRenderObservable,
onEnded: () => this._onEnded(timer, context),
});
timer.start();
const newIndex = lastDelayIndex + 1;
this.lastDelayIndex.setValue(newIndex, context);
context._setGlobalContextVariable("lastDelayIndex", newIndex);
timers[newIndex] = timer;
context._setExecutionVariable(this, "pendingDelays", timers);
}
_cancelPendingTasks(context) {
const timers = context._getExecutionVariable(this, "pendingDelays", []);
for (const timer of timers) {
timer?.dispose();
}
context._deleteExecutionVariable(this, "pendingDelays");
this.lastDelayIndex.setValue(-1, context);
}
_execute(context, callingSignal) {
if (callingSignal === this.cancel) {
this._cancelPendingTasks(context);
return;
}
else {
this._preparePendingTasks(context);
this.out._activateSignal(context);
}
}
getClassName() {
return "FlowGraphSetDelayBlock" /* FlowGraphBlockNames.SetDelay */;
}
_onEnded(timer, context) {
const timers = context._getExecutionVariable(this, "pendingDelays", []);
const index = timers.indexOf(timer);
if (index !== -1) {
timers.splice(index, 1);
}
else {
Logger.Warn("FlowGraphTimerBlock: Timer ended but was not found in the running timers list");
}
context._removePendingBlock(this);
this.done._activateSignal(context);
}
}
/**
* The maximum number of parallel delays that can be set per node.
*/
FlowGraphSetDelayBlock.MaxParallelDelayCount = 100;
RegisterClass("FlowGraphSetDelayBlock" /* FlowGraphBlockNames.SetDelay */, FlowGraphSetDelayBlock);
//# sourceMappingURL=flowGraphSetDelayBlock.js.map