Skip to content

Commit

Permalink
Simplify computer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajthinking committed Aug 2, 2023
1 parent c5d48f1 commit ebbe6c5
Show file tree
Hide file tree
Showing 41 changed files with 123 additions and 153 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/core",
"version": "0.0.65",
"version": "0.0.66",
"main": "dist/index.js",
"type": "commonjs",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('descriptions', () => {
const app = new Application()

const computer1 = ComputerFactory.get(
Signal()
Signal
)

app.addComputers(
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/ComputerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComputerConfig } from './types/ComputerConfig';
import { DefaultParams } from './Param';
import { AbstractPort, Port, PortName } from './types/Port';
import { AbstractPort, PortName } from './types/Port';
import { Computer } from './types/Computer';

/**
Expand Down Expand Up @@ -33,11 +33,4 @@ export const ComputerFactory = {
canRun: config.canRun,
};
},
}


/*
ComputerConfigFactory: Creating a unique ComputerConfig
ComputerConfig: Serializeable
Computer: The Instance
*/
}
6 changes: 3 additions & 3 deletions packages/core/src/DiagramBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Computer, ComputerConfigFactory } from './types/Computer';
import { Computer } from './types/Computer';
import { ComputerFactory } from './ComputerFactory';
import { Diagram } from './Diagram';
import { Node } from './types/Node';
import { Link } from './types/Link';
import { PositionGuesser } from './PositionGuesser';
import { Port, PortName } from './types/Port';
import { ComputerConfig } from './types/ComputerConfig';

export class DiagramBuilder {
diagram: Diagram
Expand All @@ -31,10 +32,9 @@ export class DiagramBuilder {
}

add(
addable: ComputerConfigFactory | Computer,
config: ComputerConfig,
params: Record<string, any> = {}
) {
const config = typeof addable === 'function' ? addable() : addable
const computer = ComputerFactory.get(config)

const nodeId = `${computer.name}.${this.getScopedId(computer.name)}`
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/computerRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as computerConfigFactories from './computers'
import * as computerConfigs from './computers'
import { Computer } from './types/Computer';
import { ComputerFactory } from './ComputerFactory';
import { NodeDescriptionFactory } from './NodeDescriptionFactory';
Expand All @@ -9,8 +9,7 @@ import { NodeDescriptionFactory } from './NodeDescriptionFactory';
const computers = (() => {
const map = new Map<string, Computer>()

for(const configFactory of Object.values(computerConfigFactories)) {
const config = configFactory()
for(const config of Object.values(computerConfigs)) {
const computer = ComputerFactory.get(config)

map.set(computer.name, computer)
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/computers/AskChatGpt/AskChatGpt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Configuration, OpenAIApi } from 'openai';
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';
import { ComputerConfig } from '../../types/ComputerConfig';
import { string } from '../../ParamBuilder';

export const AskChatGpt: ComputerConfigFactory = (): ComputerConfig => ({
export const AskChatGpt: ComputerConfig = {
name: 'AskChatGpt',
inputs: ['input'],
outputs: ['completions'],
Expand Down Expand Up @@ -38,4 +37,4 @@ export const AskChatGpt: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Comment/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { json, string } from '../../ParamBuilder';
import { ComputerConfig } from '../../types/ComputerConfig';

export const Comment: ComputerConfigFactory = (): ComputerConfig => ({
export const Comment: ComputerConfig = {
name: 'Comment',
inputs: [],
outputs: [],
Expand All @@ -11,4 +11,4 @@ export const Comment: ComputerConfigFactory = (): ComputerConfig => ({
},

async *run({}) {},
});
};
5 changes: 2 additions & 3 deletions packages/core/src/computers/Concatenate/Concatenate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { string } from '../../ParamBuilder';
import { Computer, ComputerConfigFactory } from '../../types/Computer';
import { ComputerConfig } from '../../types/ComputerConfig';

export const Concatenate: ComputerConfigFactory = (): ComputerConfig => ({
export const Concatenate: ComputerConfig = {
name: 'Concatenate',
inputs: ['input'],
outputs: ['output'],
Expand All @@ -29,4 +28,4 @@ export const Concatenate: ComputerConfigFactory = (): ComputerConfig => ({
.join(params.delimiter),
}])
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/ConsoleLog/ConsoleLog.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ItemWithParams } from '../../ItemWithParams';
import { string, text } from '../../ParamBuilder';
import { ItemValue } from '../../types/ItemValue';
import { ComputerConfig } from '../../types/ComputerConfig';

export const ConsoleLog: ComputerConfigFactory = (): ComputerConfig => ({
export const ConsoleLog: ComputerConfig = {
name: 'ConsoleLog',
label: 'Console.log',
inputs: ['input'],
Expand All @@ -31,4 +31,4 @@ export const ConsoleLog: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/CreateJson/CreateJson.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { json } from '../../ParamBuilder';
import { ComputerConfig } from '../../types/ComputerConfig';

export const CreateJson: ComputerConfigFactory = (): ComputerConfig => ({
export const CreateJson: ComputerConfig = {
name: 'CreateJson',
outputs: ['output'],
params: {
Expand All @@ -22,4 +22,4 @@ export const CreateJson: ComputerConfigFactory = (): ComputerConfig => ({
throw error;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/CreateProperty/CreateProperty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ItemWithParams } from '../../ItemWithParams';
import { string, text } from '../../ParamBuilder';
import { ComputerConfig } from '../../types/ComputerConfig';

export const CreateProperty: ComputerConfigFactory = (): ComputerConfig => ({
export const CreateProperty: ComputerConfig = {
name: 'CreateProperty',
inputs: ['input'],
outputs: ['output'],
Expand All @@ -23,4 +23,4 @@ export const CreateProperty: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Dump/Dump.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ItemWithParams } from '../../ItemWithParams';
import { string, text } from '../../ParamBuilder';
import { ItemValue } from '../../types/ItemValue';
import { ComputerConfig } from '../../types/ComputerConfig';

export const Dump: ComputerConfigFactory = (): ComputerConfig => ({
export const Dump: ComputerConfig = {
name: 'Dump',
inputs: ['input'],
params: {
Expand All @@ -29,4 +29,4 @@ export const Dump: ComputerConfigFactory = (): ComputerConfig => ({
})

},
});
};
5 changes: 2 additions & 3 deletions packages/core/src/computers/Eval/Eval.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { json } from '../../ParamBuilder';
import { Computer, ComputerConfigFactory } from '../../types/Computer';
import { ComputerConfig } from '../../types/ComputerConfig';

export const Eval: ComputerConfigFactory = (): ComputerConfig => ({
export const Eval: ComputerConfig = {
name: 'Eval',
inputs: [{
name: 'input',
Expand Down Expand Up @@ -34,4 +33,4 @@ export const Eval: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Filter/Filter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ItemValue } from '../../types/ItemValue';
import { string } from '../../ParamBuilder';
import { ComputerConfig } from '../../types/ComputerConfig';

export const Filter: ComputerConfigFactory = (): ComputerConfig => ({
export const Filter: ComputerConfig = {
name: 'Filter',
inputs: ['input'],
outputs: ['passed', 'failed'],
Expand All @@ -29,4 +29,4 @@ export const Filter: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/HubSpot/GetAllEntities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ComputerConfig } from '../../types/ComputerConfig';
import { json, number, string } from '../../ParamBuilder';
import { hubspot } from './hubspot';
Expand All @@ -20,7 +20,7 @@ type EntityPage = {
}
}

export const GetAllEntities: ComputerConfigFactory = (): ComputerConfig => ({
export const GetAllEntities: ComputerConfig = {
name: 'GetAll',
outputs: ['all', 'errors'],
params: {
Expand Down Expand Up @@ -63,4 +63,4 @@ export const GetAllEntities: ComputerConfigFactory = (): ComputerConfig => ({

} while (nextPage && taken < params.limit);
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/HubSpot/UpdateEntity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ItemValue } from '../../types/ItemValue';
import { json, string } from '../../ParamBuilder';
import { hubspot } from './hubspot';
import { ComputerConfig } from '../../types/ComputerConfig';
import { CrmEntity } from './CrmEntity';

export const UpdateEntity: ComputerConfigFactory = (): ComputerConfig => ({
export const UpdateEntity: ComputerConfig = {
name: 'UpdateEntity',
inputs: ['input'],
outputs: ['updated', 'errors'],
Expand Down Expand Up @@ -35,4 +35,4 @@ export const UpdateEntity: ComputerConfigFactory = (): ComputerConfig => ({
}
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Ignore/Ignore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ComputerConfig } from '../../types/ComputerConfig';

export const Ignore: ComputerConfigFactory = (): ComputerConfig => ({
export const Ignore: ComputerConfig = {
name: 'Ignore',
inputs: ['input'],

Expand All @@ -11,4 +11,4 @@ export const Ignore: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Input/Input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ComputerConfig } from '../../types/ComputerConfig';

export const Input: ComputerConfigFactory = (): ComputerConfig => ({
export const Input: ComputerConfig = {
name: 'Input',
outputs: ['output'],

Expand All @@ -21,4 +21,4 @@ export const Input: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/InstantThrow/InstantThrow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { sleep } from '../../utils/sleep';
import { ComputerConfig } from '../../types/ComputerConfig';

export const InstantThrow: ComputerConfigFactory = (): ComputerConfig => ({
export const InstantThrow: ComputerConfig = {
name: 'InstantThrow',

async *run({}) {
throw Error("Instant Error!")
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/JsonFile/JsonFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { string } from '../../ParamBuilder';
import { promises as fs } from 'fs'
import { ComputerConfig } from '../../types/ComputerConfig';

export const JsonFile: ComputerConfigFactory = (): ComputerConfig => ({
export const JsonFile: ComputerConfig = {
name: 'JsonFile',
outputs: ['items', 'error'],
params: {
Expand All @@ -26,4 +26,4 @@ export const JsonFile: ComputerConfigFactory = (): ComputerConfig => ({

yield;
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/ListFiles/ListFiles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { string } from '../../ParamBuilder';
import { promises as fs } from 'fs'
import * as nodePath from 'path'
import { ComputerConfig } from '../../types/ComputerConfig';

export const ListFiles: ComputerConfigFactory = (): ComputerConfig => ({
export const ListFiles: ComputerConfig = {
name: 'ListFiles',
inputs: ['input'],
outputs: [{
Expand Down Expand Up @@ -37,4 +37,4 @@ export const ListFiles: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
6 changes: 3 additions & 3 deletions packages/core/src/computers/Log/Log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComputerConfigFactory, RunArgs } from '../../types/Computer';

import { ComputerConfig } from '../../types/ComputerConfig';

export const Log: ComputerConfigFactory = (): ComputerConfig => ({
export const Log: ComputerConfig = {
name: 'Log',
inputs: ['input'],

Expand All @@ -14,4 +14,4 @@ export const Log: ComputerConfigFactory = (): ComputerConfig => ({
yield;
}
},
});
};
Loading

0 comments on commit ebbe6c5

Please sign in to comment.