Skip to content

Commit

Permalink
cerebral-module now includes cerebral-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tomohiroarakawa committed Apr 15, 2020
1 parent 813ac1d commit 0500bd4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@oada/cerebral-module",
"version": "3.0.6",
"description": "oada cerebral module",
"main": "./index.js",
"version": "4.0.0",
"description": "OADA cerebral module",
"main": "./build/index.js",
"scripts": {
"test": "mocha -w --require @babel/register test/**/*.test.js",
"build": "babel src --out-dir ./",
"build": "babel src --out-dir build",
"prepare": "npm run build",
"prettier": "prettier --write 'src/**/*.js'",
"build-watch": "babel --watch src --out-dir ./",
Expand All @@ -14,21 +14,21 @@
"author": "oada",
"license": "Apache-2.0",
"dependencies": {
"@oada/cerebral-provider": "^3.0.6",
"debug": "^4.1.0",
"fs": "^0.0.1-security",
"lodash": "^4.17.15",
"url": "^0.11.0"
"url": "^0.11.0",
"uuid": "^7.0.3"
},
"peerDependencies": {
"cerebral": "^5.1.1"
"cerebral": "^5.2.1"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/register": "^7.9.0",
"cerebral": "^5.1.1",
"cerebral": "^5.2.1",
"mocha": "^7.1.1",
"prettier": "2.0.2",
"chai": "^4.1.2",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sequences from "./sequences";
import oada from "@oada/cerebral-provider";
import state from "./state"
import oada from "./provider";
import state from "./state";

export default {
state,

providers: { oada },

sequences,
sequences
};
73 changes: 73 additions & 0 deletions src/provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Provider } from 'cerebral'
import oada from '@oada/oada-cache';
import uuid from 'uuid';
var connections = {};

const connect = function connect(args) {
if (!args.connection_id) throw 'connection_id not supplied'
if (args.connection_id && connections[args.connection_id]) return Promise.resolve(connections[args.connection_id]);
return oada.connect(args).then((conn) => {
conn.cache = {};
connections[args.connection_id] = conn;
return conn;
})
}

const get = function get(args) {
if (!args.connection_id) throw 'connection_id not supplied'
if (args.watch && args.watch.signals) {
let sigs = args.watch.signals.map((signal) => {
if (this.context) {
return this.context.controller.hasOwnProperty("getSignal") ?
this.context.controller.getSignal(signal) :
this.context.controller.getSequence(signal);
}
})
args.watch.func = (payload) => {
sigs.forEach((signal) => {
signal(payload)
})
}
}
return connections[args.connection_id].get(args);
}

const put = function put(args) {
if (!args.connection_id) throw 'connection_id not supplied'
return connections[args.connection_id].put(args);
}

const post = function post(args) {
if (!args.connection_id) throw 'connection_id not supplied'
return connections[args.connection_id].post(args);
}


const _delete = function _delete(args) {
if (!args.connection_id) throw 'connection_id not supplied'
return connections[args.connection_id].delete(args);
}


const disconnect = function _disconnect(args) {
if (!args.connection_id) throw 'connection_id not supplied'
var connection = connections[args.connection_id];
connections[args.connection_id] = undefined;
return connection.disconnect();
}

const resetCache = function resetCache(args) {
if (!args.connection_id) throw 'connection_id not supplied'
if (!connections[args.connection_id]) return; // reseting a non-existent connection
return connections[args.connection_id].resetCache(args);
}

export default {
connect,
get,
put,
post,
delete: _delete,
resetCache,
disconnect
};

0 comments on commit 0500bd4

Please sign in to comment.