forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowGraphCachedOperationBlock.ts
44 lines (38 loc) · 1.71 KB
/
flowGraphCachedOperationBlock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { IFlowGraphBlockConfiguration } from "../../flowGraphBlock";
import { FlowGraphBlock } from "../../flowGraphBlock";
import type { FlowGraphContext } from "../../flowGraphContext";
import type { FlowGraphDataConnection } from "../../flowGraphDataConnection";
import type { RichType } from "../../flowGraphRichTypes";
const CACHE_NAME = "cachedOperationValue";
const CACHE_EXEC_ID_NAME = "cachedExecutionId";
/**
* @experimental
*/
export abstract class FlowGraphCachedOperationBlock<OutputT> extends FlowGraphBlock {
/**
* The output of the operation
*/
public readonly output: FlowGraphDataConnection<OutputT>;
constructor(outputRichType: RichType<OutputT>, config?: IFlowGraphBlockConfiguration) {
super(config);
this.output = this._registerDataOutput("output", outputRichType);
}
/**
* @internal
* Operation to realize
* @param context the graph context
*/
public abstract _doOperation(context: FlowGraphContext): OutputT;
public _updateOutputs(context: FlowGraphContext) {
const cachedExecutionId = context._getExecutionVariable(this, CACHE_EXEC_ID_NAME);
const cachedValue = context._getExecutionVariable(this, CACHE_NAME);
if (cachedValue !== undefined && cachedExecutionId === context.executionId) {
this.output.setValue(cachedValue, context);
} else {
const calculatedValue = this._doOperation(context);
context._setExecutionVariable(this, CACHE_NAME, calculatedValue);
context._setExecutionVariable(this, CACHE_EXEC_ID_NAME, context.executionId);
this.output.setValue(calculatedValue, context);
}
}
}