-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* v1 * v2 * common cache class * change how cache is saved so it doesn't grow data infinitely * Update packages/dev/core/src/FlowGraph/flowGraphContext.ts Co-authored-by: Popov72 <github@evpopov.com> * Update packages/dev/core/src/FlowGraph/flowGraphContext.ts Co-authored-by: Popov72 <github@evpopov.com> * review changes --------- Co-authored-by: Popov72 <github@evpopov.com>
- Loading branch information
Showing
8 changed files
with
126 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/dev/core/src/FlowGraph/Blocks/Data/flowGraphCachedOperationBlock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |
14 changes: 5 additions & 9 deletions
14
packages/dev/core/src/FlowGraph/Blocks/Data/flowGraphConstantOperationBlock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters