Skip to content

Commit

Permalink
reintroducing registry type instead of various destructured patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 22, 2023
1 parent db950f6 commit 4f4e1ce
Show file tree
Hide file tree
Showing 24 changed files with 102 additions and 165 deletions.
6 changes: 1 addition & 5 deletions apps/exec-graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ async function execGraph({
const textFile = await fs.readFile(graphJsonPath);
const graph = readGraphFromJSON({
graphJson: JSON.parse(textFile.toString('utf8')),
...registry,
dependencies: {
logger,
lifecycleEventEmitter
}
registry
});
graph.name = graphJsonPath;

Expand Down
1 change: 1 addition & 0 deletions apps/export-node-spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {},
"dependencies": {
"@behave-graph/core": "*",
"@behave-graph/scene": "*",
"csv-stringify": "^6.2.1"
}
}
21 changes: 14 additions & 7 deletions apps/export-node-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validateNodeRegistry,
writeNodeSpecsToJSON
} from '@behave-graph/core';
import { DummyScene, registerSceneProfile } from '@behave-graph/scene';
import { program } from 'commander';
import { stringify } from 'csv-stringify';
import { createRequire } from 'module';
Expand Down Expand Up @@ -37,12 +38,22 @@ export const main = async () => {
const lifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();

const registry = registerCoreProfile({
let registry = registerCoreProfile({
values: {},
nodes: {},
dependencies: {}
dependencies: {
ILogger: logger,
ILifecycleEventEmitter: lifecycleEventEmitter
}
});

const scene = new DummyScene();

registry = registerSceneProfile(registry);
registry.dependencies.IScene = scene;

console.log({ registry });

const errorList: string[] = [];
errorList.push(...validateNodeRegistry(registry));
if (errorList.length > 0) {
Expand All @@ -54,11 +65,7 @@ export const main = async () => {
}

const nodeSpecJson = writeNodeSpecsToJSON({
...registry,
dependencies: {
logger,
lifecycleEventEmitter
}
...registry
});
nodeSpecJson.sort((a, b) => a.type.localeCompare(b.type));
const jsonOutput = JSON.stringify(nodeSpecJson, null, ' ');
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions packages/core/src/Graphs/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,25 @@ export const createNode = ({
export const makeGraphApi = ({
variables = {},
customEvents = {},
valuesTypeRegistry,
values,
dependencies = {}
}: {
customEvents?: GraphCustomEvents;
variables?: GraphVariables;
valuesTypeRegistry: ValueTypeMap;
values: ValueTypeMap;
dependencies: Dependencies;
}): IGraphApi => ({
variables,
customEvents,
values: valuesTypeRegistry,
values,
getDependency: (id: string) => {
const result = dependencies[id];
if (!result)
console.error(`Dependency not found ${id}. Did you register it?`);
console.error(
`Dependency not found ${id}. Did you register it? Existing dependencies: ${Object.keys(
dependencies
)}`
);
return result;
}
});
26 changes: 9 additions & 17 deletions packages/core/src/Graphs/IO/readGraphFromJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Logger } from '../../Diagnostics/Logger.js';
import { CustomEvent } from '../../Events/CustomEvent.js';
import { Link } from '../../Nodes/Link.js';
import { NodeConfiguration } from '../../Nodes/Node.js';
import { Dependencies } from '../../Nodes/NodeDefinitions.js';
import { INode } from '../../Nodes/NodeInstance.js';
import { NodeDefinitionsMap } from '../../Nodes/Registry/NodeDefinitionsMap.js';
import { IRegistry } from '../../Registry.js';
import { Socket } from '../../Sockets/Socket.js';
import { ValueTypeMap } from '../../Values/ValueTypeMap.js';
import { Variable } from '../../Values/Variables/Variable.js';
Expand All @@ -30,14 +30,10 @@ import {
// - loads a node graph
export function readGraphFromJSON({
graphJson,
nodes: nodesTypeRegistry,
values: valuesTypeRegistry,
dependencies
registry
}: {
graphJson: GraphJSON;
nodes: NodeDefinitionsMap;
values: ValueTypeMap;
dependencies: Dependencies;
registry: IRegistry;
}): GraphInstance {
const graphName = graphJson?.name || '';
const graphMetadata = graphJson?.metadata || {};
Expand All @@ -46,14 +42,11 @@ export function readGraphFromJSON({
let customEvents: GraphCustomEvents = {};

if ('variables' in graphJson) {
variables = readVariablesJSON(
valuesTypeRegistry,
graphJson.variables ?? []
);
variables = readVariablesJSON(registry.values, graphJson.variables ?? []);
}
if ('customEvents' in graphJson) {
customEvents = readCustomEventsJSON(
valuesTypeRegistry,
registry.values,
graphJson.customEvents ?? []
);
}
Expand All @@ -65,10 +58,9 @@ export function readGraphFromJSON({
}

const graphApi = makeGraphApi({
valuesTypeRegistry,
...registry,
variables,
customEvents,
dependencies
customEvents
});

const nodes: GraphNodes = {};
Expand All @@ -77,8 +69,8 @@ export function readGraphFromJSON({
const nodeJson = nodesJson[i];
const node = readNodeJSON({
graph: graphApi,
nodes: nodesTypeRegistry,
values: valuesTypeRegistry,
nodes: registry.nodes,
values: registry.values,
nodeJson
});
const id = nodeJson.id;
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/Graphs/IO/writeNodeSpecsToJSON.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeCategory } from '../../Nodes/NodeDefinitions.js';
import { IRegistry } from '../../Registry.js';
import { Choices } from '../../Sockets/Socket.js';
import { createNode, IGraphApi } from '../Graph.js';
import { createNode, makeGraphApi } from '../Graph.js';
import {
ChoiceJSON,
InputSocketSpecJSON,
Expand All @@ -25,12 +25,12 @@ export function writeNodeSpecsToJSON({

// const graph = new Graph(registry);

const graph: IGraphApi = {
values: values,
const graph = makeGraphApi({
values,
customEvents: {},
getDependency: <T>(id: string) => dependencies[id] as T,
dependencies,
variables: {}
};
});

Object.keys(nodes).forEach((nodeTypeName) => {
const node = createNode({
Expand Down
15 changes: 6 additions & 9 deletions packages/core/src/Nodes/Validation/validateNodeRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { createNode, makeGraphApi } from '../../Graphs/Graph.js';
import { ValueTypeMap } from '../../Values/ValueTypeMap.js';
import { NodeDefinitionsMap } from '../Registry/NodeDefinitionsMap.js';
import { IRegistry } from '../../Registry.js';

const nodeTypeNameRegex = /^\w+(\/\w+)*$/;
const socketNameRegex = /^\w+$/;

export function validateNodeRegistry({
nodes,
values
}: {
nodes: NodeDefinitionsMap;
values: ValueTypeMap;
}): string[] {
values,
dependencies
}: IRegistry): string[] {
const errorList: string[] = [];
// const graph = new Graph(registry);
const graph = makeGraphApi({
valuesTypeRegistry: values,
dependencies: {}
values,
dependencies
});
Object.keys(nodes).forEach((nodeTypeName) => {
const node = createNode({ graph, nodes, values, nodeTypeName });
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Nodes/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NodeConfigurationDescription } from './Registry/NodeDescription.js';
const makeEmptyGraph = (): IGraphApi => {
return makeGraphApi({
dependencies: {},
valuesTypeRegistry: {}
values: {}
});
};

Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/Profiles/Core/Debug/DebugLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
} from '../../../Nodes/NodeDefinitions.js';
import { ILogger } from '../Abstractions/ILogger.js';

export const loggerDependencyKey = 'logger';

export const Log = makeFlowNodeDefinition({
typeName: 'debug/log',
category: NodeCategory.Action,
Expand All @@ -23,7 +21,7 @@ export const Log = makeFlowNodeDefinition({
out: { flow: 'flow' },
initialState: undefined,
triggered: ({ read, commit, graph: { getDependency } }) => {
const logger = getDependency<ILogger>(loggerDependencyKey);
const logger = getDependency<ILogger>('ILogger');

const text = read<string>('text');
switch (read<string>('severity')) {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/Profiles/Core/Lifecycle/LifecycleOnEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
NodeCategory
} from '../../../Nodes/NodeDefinitions.js';
import { ILifecycleEventEmitter } from '../Abstractions/ILifecycleEventEmitter.js';
import { lifecycleEventEmitterDependencyKey } from './LifecycleOnStart.js';

// inspired by: https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/Blueprints/UserGuide/Events/

Expand Down Expand Up @@ -32,7 +31,7 @@ export const LifecycleOnEnd = makeEventNodeDefinition({
};

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

lifecycleEventEmitter?.endEvent.addListener(onEndEvent);
Expand All @@ -45,7 +44,7 @@ export const LifecycleOnEnd = makeEventNodeDefinition({
Assert.mustBeTrue(onEndEvent !== undefined);

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

if (onEndEvent) lifecycleEventEmitter?.endEvent.removeListener(onEndEvent);
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/Profiles/Core/Lifecycle/LifecycleOnStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const makeInitialState = (): State => ({
onStartEvent: undefined
});

export const lifecycleEventEmitterDependencyKey = 'lifecycleEventEmitter';

export const LifecycleOnStart = makeEventNodeDefinition({
typeName: 'lifecycle/onStart',
label: 'On Start',
Expand All @@ -31,7 +29,7 @@ export const LifecycleOnStart = makeEventNodeDefinition({
};

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

lifecycleEventEmitter?.startEvent.addListener(onStartEvent);
Expand All @@ -44,7 +42,7 @@ export const LifecycleOnStart = makeEventNodeDefinition({
Assert.mustBeTrue(onStartEvent !== undefined);

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

if (onStartEvent)
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/Profiles/Core/Lifecycle/LifecycleOnTick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
NodeCategory
} from '../../../Nodes/NodeDefinitions.js';
import { ILifecycleEventEmitter } from '../Abstractions/ILifecycleEventEmitter.js';
import { lifecycleEventEmitterDependencyKey } from './LifecycleOnStart.js';

type State = {
onTickEvent?: (() => void) | undefined;
Expand Down Expand Up @@ -36,7 +35,7 @@ export const LifecycleOnTick = makeEventNodeDefinition({
};

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

lifecycleEventEmitter?.tickEvent.addListener(onTickEvent);
Expand All @@ -49,7 +48,7 @@ export const LifecycleOnTick = makeEventNodeDefinition({
Assert.mustBeTrue(onTickEvent !== undefined);

const lifecycleEventEmitter = getDependency<ILifecycleEventEmitter>(
lifecycleEventEmitterDependencyKey
'ILifecycleEventEmitter'
);

if (onTickEvent)
Expand Down
20 changes: 2 additions & 18 deletions packages/core/src/Profiles/Core/registerCoreProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { getNodeDescriptions } from '../../Nodes/Registry/NodeDescription.js';
import { IRegistry } from '../../Registry.js';
import { ValueTypeMap } from '../../Values/ValueTypeMap.js';
import { getStringConversionsForValueType } from '../registerSerializersForValueType.js';
import { ILifecycleEventEmitter } from './Abstractions/ILifecycleEventEmitter.js';
import { ILogger } from './Abstractions/ILogger.js';
import { OnCustomEvent } from './CustomEvents/OnCustomEvent.js';
import { TriggerCustomEvent } from './CustomEvents/TriggerCustomEvent.js';
import { ExpectTrue as AssertExpectTrue } from './Debug/AssertExpectTrue.js';
import { Log as DebugLog, loggerDependencyKey } from './Debug/DebugLog.js';
import { Log as DebugLog } from './Debug/DebugLog.js';
import { Branch } from './Flow/Branch.js';
import { Counter } from './Flow/Counter.js';
import { Debounce } from './Flow/Debounce.js';
Expand All @@ -26,10 +24,7 @@ import { SwitchOnString } from './Flow/SwitchOnString.js';
import { Throttle } from './Flow/Throttle.js';
import { WaitAll } from './Flow/WaitAll.js';
import { LifecycleOnEnd } from './Lifecycle/LifecycleOnEnd.js';
import {
lifecycleEventEmitterDependencyKey,
LifecycleOnStart
} from './Lifecycle/LifecycleOnStart.js';
import { LifecycleOnStart } from './Lifecycle/LifecycleOnStart.js';
import { LifecycleOnTick } from './Lifecycle/LifecycleOnTick.js';
import { Easing } from './Logic/Easing.js';
import { Delay } from './Time/Delay.js';
Expand All @@ -45,17 +40,6 @@ import { StringValue } from './Values/StringValue.js';
import { VariableGet } from './Variables/VariableGet.js';
import { VariableSet } from './Variables/VariableSet.js';

export const makeCoreDependencies = ({
lifecycleEmitter,
logger
}: {
lifecycleEmitter: ILifecycleEventEmitter;
logger: ILogger;
}) => ({
[lifecycleEventEmitterDependencyKey]: lifecycleEmitter,
[loggerDependencyKey]: logger
});

export const getCoreValuesMap = memo<ValueTypeMap>(() => {
const valueTypes = [BooleanValue, StringValue, IntegerValue, FloatValue];
return Object.fromEntries(
Expand Down

0 comments on commit 4f4e1ce

Please sign in to comment.