Skip to content

Commit

Permalink
feat(node): Allow metaConfig to be passed to nodes in WF chain
Browse files Browse the repository at this point in the history
Instead of mixing meta config with regular config, just have them passed in as separate parameters

closes #26
  • Loading branch information
SomeKittens committed Jan 6, 2016
1 parent da4b875 commit c9a6bd9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
32 changes: 16 additions & 16 deletions Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import {GustavGraph} from './GustavGraph';
import {gustav} from './index';
import {Observable} from '@reactivex/rxjs';
import * as uuid from 'node-uuid';
import {INodeDef, ISourceNode, ITransfNode, ISinkNode} from './defs';
import {INodeDef, ISourceNode, ITransfNode, ISinkNode, IMetaConfig} from './defs';

export interface IStrongNodeDef extends INodeDef {
dataFrom: number[];
}

export interface IWorkflowChain {
prevNode: symbol;
transf(name: string | ITransfNode, config?): IWorkflowChain;
sink(name: string | ISinkNode, config?): Workflow;
transf(name: string | ITransfNode, config?: any, metaConfig?: IMetaConfig): IWorkflowChain;
sink(name: string | ISinkNode, config?: any, metaConfig?: IMetaConfig): Workflow;
merge(...nodes: IWorkflowChain[]): IWorkflowChain;
tap(name: string | ISinkNode, config?): IWorkflowChain;
tap(name: string | ISinkNode, config?: any, metaConfig?: IMetaConfig): IWorkflowChain;
clone(): IWorkflowChain;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export class Workflow {
throw new Error('Attempted to add a listener to an ongoing Workflow');
}

let listenerNode = gustav.makeNode(def.name, this.ggraph, def.config);
let listenerNode = gustav.makeNode(def.name, this.ggraph, def.config, def.metaConfig);

this.ggraph.sinkEdges
.map(edge => edge.to)
Expand All @@ -128,7 +128,7 @@ export class Workflow {
this.metadataFuncs.push(func);
return this;
}
source (sourceName: string | ISourceNode, SourceConfig?: Object): IWorkflowChain {
source (sourceName: string | ISourceNode, SourceConfig?: Object, metaConfig?: IMetaConfig): IWorkflowChain {
let registerTmpNode = (type, factory): string => {
let name = uuid.v4();
gustav[type](name, factory);
Expand All @@ -137,7 +137,7 @@ export class Workflow {
if (typeof sourceName !== 'string') {
sourceName = registerTmpNode('source', sourceName);
}
let prevNode = gustav.makeNode(<string>sourceName, this.ggraph, SourceConfig);
let prevNode = gustav.makeNode(<string>sourceName, this.ggraph, SourceConfig, metaConfig);
return new WorkflowChain(this, prevNode);
}
/**
Expand Down Expand Up @@ -222,7 +222,7 @@ export class Workflow {

// Create a new node for each def
this.nodeDefs.forEach(def => {
let sym = gustav.makeNode(def.name, this.ggraph, def.config);
let sym = gustav.makeNode(def.name, this.ggraph, def.config, def.metaConfig);
idSymbolMap[def.id] = sym;
});

Expand All @@ -242,13 +242,13 @@ export class Workflow {
class WorkflowChain {
// prevNode tracks whatever node we've touched last
constructor(public workflow: Workflow, public prevNode: symbol) {}
transf (name: string | ITransfNode, config?): IWorkflowChain {
this.addNodeToGraph(name, 'transformer', config, true);
transf (name: string | ITransfNode, config?: any, metaConfig?: any): IWorkflowChain {
this.addNodeToGraph(name, 'transformer', config, metaConfig, true);
return new WorkflowChain(this.workflow, this.prevNode);
}
sink (name: string | ISinkNode, config?): Workflow {
sink (name: string | ISinkNode, config?: any, metaConfig?: IMetaConfig): Workflow {
// Add the sink to the graph
this.addNodeToGraph(name, 'sink', config);
this.addNodeToGraph(name, 'sink', config, metaConfig);

// return the workflow
return this.workflow;
Expand All @@ -263,8 +263,8 @@ class WorkflowChain {
this.prevNode = mergeNode;
return new WorkflowChain(this.workflow, this.prevNode);
}
tap (name: string | ISinkNode, config?): IWorkflowChain {
this.addNodeToGraph(name, 'sink', config);
tap (name: string | ISinkNode, config?, metaConfig?: IMetaConfig): IWorkflowChain {
this.addNodeToGraph(name, 'sink', config, metaConfig);
return new WorkflowChain(this.workflow, this.prevNode);
}
clone(): IWorkflowChain {
Expand All @@ -278,11 +278,11 @@ class WorkflowChain {
* @config {Configuration for the node}
* @replaceOld {Should we overwrite prevNode?}
*/
private addNodeToGraph(name, type: string, config?, replaceOld?): void {
private addNodeToGraph(name, type: string, config?: any, metaConfig?: IMetaConfig, replaceOld?: boolean): void {
if (typeof name !== 'string') {
name = this.registerTmpNode(type, name);
}
let currentNode = gustav.makeNode(name, this.workflow.ggraph, config);
let currentNode = gustav.makeNode(name, this.workflow.ggraph, config, metaConfig);

this.workflow.ggraph.addEdge(currentNode, this.prevNode);

Expand Down
5 changes: 5 additions & 0 deletions defs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {Observable, Subscription} from '@reactivex/rxjs';

export interface IMetaConfig {
gid: string | number;
}

export interface INodeDef {
name: string;
type?: string;
config?: any;
metaConfig?: IMetaConfig;
dataFrom?: number[] | number; // Only on non-source nodes
id: number;
}
Expand Down
7 changes: 4 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {GustavGraph} from './GustavGraph';
import {Workflow} from './Workflow';
import {IMetaConfig} from './defs';

export interface INodeFactory {
(...config: any[]): symbol;
Expand All @@ -24,7 +25,7 @@ let workflows = {};
let register;

export let gustav = {
makeNode: (nodeName: string, graph: GustavGraph, config: any): symbol => {
makeNode: (nodeName: string, graph: GustavGraph, config: any, metaConfig?: IMetaConfig): symbol => {
let node = registeredNodes.filter((regNode) => regNode.name === nodeName)[0];

if (!node) {
Expand All @@ -33,8 +34,8 @@ export let gustav = {

// Attempt to detect config to make symbol tag more descriptive
let symbolTag = node.name;
if (config && config.__gid) {
symbolTag += '-' + config.__gid;
if (metaConfig && metaConfig.gid) {
symbolTag += '-' + metaConfig.gid;
}
let sym = Symbol(symbolTag);
graph.nodes[sym] = {
Expand Down
20 changes: 20 additions & 0 deletions test/gid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

import {gustav} from '../index';
import {expect} from 'chai';
import {addCommonNodes} from './testNodes';

addCommonNodes(gustav);

describe('Adding a custom id to a node', () => {
it('should allow the user to add a custom id', () => {
let wf = gustav.createWorkflow('gid test')
.source('intSource', null, {gid: 'hello'})
.sink('fromIntSource', () => {}, {gid: 'bye'});


// Simple way to get the nodes themselves
expect(wf.ggraph.sinkEdges[0].to.toString()).to.equal('Symbol(intSource-hello)');
expect(wf.ggraph.sinkEdges[0].from.toString()).to.equal('Symbol(fromIntSource-bye)');
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test/factory-direct.ts",
"test/meta.ts",
"test/register.ts",
"test/gid.ts",

"Symbol.d.ts",
"Promise.d.ts",
Expand Down

0 comments on commit c9a6bd9

Please sign in to comment.