Skip to content

Commit

Permalink
Improve BOS API
Browse files Browse the repository at this point in the history
- `domain.runtime` with autoloader
- `api.bos.listFlows()`
- `api.bos.getFlow({ name })`
- `api.bos.saveFlow({ name, source })`
  • Loading branch information
tshemsedinov committed Apr 26, 2022
1 parent 7991e58 commit 8df3558
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion application/api/bos.1/getFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

async method({ name }) {
const filePath = `./application/flow/${name}.md`;
const source = await node.fs.promises.readFile(filePath, 'utf8');
const source = await node.fsp.readFile(filePath, 'utf8');
return { name, source };
},
});
10 changes: 10 additions & 0 deletions application/api/bos.1/listFlows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
access: 'public',

async method({}) {
const dirPath = `./application/flow/`;
const files = await node.fsp.readdir(dirPath);
const names = files.map((file) => node.path.basename(file, '.md'));
return { names };
},
});
10 changes: 10 additions & 0 deletions application/api/bos.1/saveFlow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
access: 'public',

async method({ name, source }) {
const filePath = `./application/flow/${name}.md`;
await node.fsp.writeFile(filePath, source);
await domain.runtime.load({ name: filePath });
return { success: true };
},
});
14 changes: 5 additions & 9 deletions application/api/bos.1/startFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
access: 'public',

async method({ name }) {
const { clients, processes } = domain.flow;

context.client.on('close', () => {
clients.delete(context.client);
});

const filePath = `./application/flow/${name}.md`;
const src = await node.fs.promises.readFile(filePath, 'utf8');
const domainProcess = npm.lowscript.parseProcess(src);
const instance = await npm.lowscript.startProcess(domainProcess);
const proc = domain.runtime.flows.get(name);
const flow = await npm.lowscript.startProcess(proc);

instance.on('step', (step) => {
flow.on('step', (step) => {
context.client.emit('demo/step', { step });
});

processes.set(context.client, instance);
clients.set(instance, context.client);
domain.flow.processes.set(context.client, instance);
domain.flow.clients.set(instance, context.client);

return { success: true };
},
Expand Down
4 changes: 0 additions & 4 deletions application/domain/runtime.js

This file was deleted.

8 changes: 8 additions & 0 deletions application/domain/runtime/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async ({ name }) => {
const filePath = `./application/flow/${name}.md`;
const src = await node.fsp.readFile(filePath, 'utf8');
const procedures = npm.lowscript.parseProcess(src);
for (const proc of procedures) {
domain.runtime.flows.set(proc.name, proc);
}
};
12 changes: 12 additions & 0 deletions application/domain/runtime/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
async () => {
domain.runtime.clients = new Map();
domain.runtime.processes = new Map();
domain.runtime.flows = new Map();

const dirPath = `./application/flow/`;
const files = await node.fsp.readdir(dirPath);
for (const file of files) {
const name = node.path.basename(file, '.md');
await domain.runtime.load({ name });
}
};
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"impress": "^2.6.8",
"lowscript": "0.0.2",
"metasql": "^2.0.2",
"pg": "^8.7.3",
"redis": "^4.0.4"
Expand Down

0 comments on commit 8df3558

Please sign in to comment.