From aeee8d65a53ae2c1af57012dffe21a260e77d88e Mon Sep 17 00:00:00 2001 From: christellevs Date: Tue, 14 Jan 2025 15:24:38 +0000 Subject: [PATCH 1/3] wip: rename subgraph scope --- src/main/runtime/GraphEvalContext.ts | 11 +++++++++++ src/main/runtime/ModuleLoader.ts | 1 + src/main/runtime/NodeView.ts | 9 ++++++++- src/main/system/Scope.ts | 24 ++++++++++++++++++++++++ src/main/system/index.ts | 1 + src/main/types/ctx.ts | 1 + 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/system/Scope.ts diff --git a/src/main/runtime/GraphEvalContext.ts b/src/main/runtime/GraphEvalContext.ts index 2bf94e2..c7bda48 100644 --- a/src/main/runtime/GraphEvalContext.ts +++ b/src/main/runtime/GraphEvalContext.ts @@ -24,6 +24,7 @@ export class GraphEvalContext implements t.GraphEvalContext { cache = new Map(); // Locals are stored per-context. Lookups delegate up the hierarchy. locals = new Map(); + _currentScope: any = null; constructor( readonly parent: GraphEvalContext | null = null, @@ -115,6 +116,16 @@ export class GraphEvalContext implements t.GraphEvalContext { */ span() {} + get currentScope(): any { + return this._currentScope ?? this.parent?.currentScope; + } + + withScope(scope: any): t.GraphEvalContext { + const ctx = new GraphEvalContext(this); + ctx._currentScope = scope; + return ctx; + } + } export class NodePendingError extends Error { diff --git a/src/main/runtime/ModuleLoader.ts b/src/main/runtime/ModuleLoader.ts index 3b0d4d5..323c4de 100644 --- a/src/main/runtime/ModuleLoader.ts +++ b/src/main/runtime/ModuleLoader.ts @@ -25,6 +25,7 @@ export abstract class GenericModuleLoader implements ModuleLoader { this.addModule('@system/Output', systemModules.Output); this.addModule('@system/Param', systemModules.Param); this.addModule('@system/Result', systemModules.Output); + this.addModule('@system/Scope', systemModules.Scope); } abstract resolveComputeUrl(ref: string): string; diff --git a/src/main/runtime/NodeView.ts b/src/main/runtime/NodeView.ts index 4abe915..55f7cfb 100644 --- a/src/main/runtime/NodeView.ts +++ b/src/main/runtime/NodeView.ts @@ -95,12 +95,15 @@ export class NodeView { supportsSubgraph() { const { subgraph } = this.getModuleSpec(); + if (this.isScopeNode() && this.graph.isSubgraph()) { + return false; + } return !!subgraph; } getSubgraph(): GraphView | null { const { subgraph } = this.getModuleSpec(); - if (!subgraph) { + if (!subgraph || (this.isScopeNode() && this.graph.isSubgraph())) { return null; } const { nodes = {}, rootNodeId = '', metadata = {} } = this.nodeSpec.subgraph ?? {}; @@ -303,6 +306,10 @@ export class NodeView { return this.canDock() && !!this.metadata.docked; } + isScopeNode() { + return this.ref === '@system/Scope'; + } + } export interface NodeLink { diff --git a/src/main/system/Scope.ts b/src/main/system/Scope.ts new file mode 100644 index 0000000..fc7807e --- /dev/null +++ b/src/main/system/Scope.ts @@ -0,0 +1,24 @@ +import { ModuleSpecSchema } from '../schema/ModuleSpec.js'; + +export const Scope = ModuleSpecSchema.create({ + moduleName: 'Scope', + version: '1.0.0', + keywords: ['system'], + description: 'Returns the current scope or an empty object', + deprecated: '', + params: {}, + result: { + schema: { type: 'object' }, + async: false, + }, + newScope: false, + cacheMode: 'auto', + evalMode: 'auto', + attributes: { + customImportUrl: 'data:text/javascript;base64,' + (` + export function compute(params, ctx) { + return ctx.currentScope ?? {}; + } + `) + }, +}); \ No newline at end of file diff --git a/src/main/system/index.ts b/src/main/system/index.ts index c3a7a4e..0ec507e 100644 --- a/src/main/system/index.ts +++ b/src/main/system/index.ts @@ -9,3 +9,4 @@ export * from './Frame.js'; export * from './Input.js'; export * from './Output.js'; export * from './Param.js'; +export * from './Scope.js'; diff --git a/src/main/types/ctx.ts b/src/main/types/ctx.ts index 9a7dfe3..6eccb6c 100644 --- a/src/main/types/ctx.ts +++ b/src/main/types/ctx.ts @@ -15,6 +15,7 @@ export interface GraphEvalContext { locals: Map; nodeEvaluated: Event; scopeCaptured: Event; + _currentScope: any; clear(): void; finalize(): Promise; From d761a5caa722ca217d6f6d15bf7a271110610069 Mon Sep 17 00:00:00 2001 From: christellevs Date: Tue, 14 Jan 2025 15:27:17 +0000 Subject: [PATCH 2/3] chore: tidy --- src/main/system/Scope.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/system/Scope.ts b/src/main/system/Scope.ts index fc7807e..991ffa3 100644 --- a/src/main/system/Scope.ts +++ b/src/main/system/Scope.ts @@ -13,12 +13,5 @@ export const Scope = ModuleSpecSchema.create({ }, newScope: false, cacheMode: 'auto', - evalMode: 'auto', - attributes: { - customImportUrl: 'data:text/javascript;base64,' + (` - export function compute(params, ctx) { - return ctx.currentScope ?? {}; - } - `) - }, -}); \ No newline at end of file + evalMode: 'auto' +}); From f6fc4ec39ae453996ef6f7f523b49ae7def304db Mon Sep 17 00:00:00 2001 From: christellevs Date: Tue, 14 Jan 2025 15:28:52 +0000 Subject: [PATCH 3/3] fix: eslint --- src/main/system/Scope.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/system/Scope.ts b/src/main/system/Scope.ts index 991ffa3..7ab938a 100644 --- a/src/main/system/Scope.ts +++ b/src/main/system/Scope.ts @@ -14,4 +14,4 @@ export const Scope = ModuleSpecSchema.create({ newScope: false, cacheMode: 'auto', evalMode: 'auto' -}); +});