graph-js: added demo fetch api / subscription objs

This commit is contained in:
Logan Allen 2020-06-12 15:07:47 -04:00
parent 8d5fb16685
commit 7d74d1870f
2 changed files with 43 additions and 4 deletions

View File

@ -5,7 +5,7 @@ class PrivateHelper extends BaseApi {
this.action('graph-store', 'graph-action', data);
}
addGraph(resource, graph) {
addGraph(resource = { ship: '~zod', name: 'asdf' }, graph = {}) {
this.graphAction({
'add-graph': {
resource,
@ -14,7 +14,7 @@ class PrivateHelper extends BaseApi {
});
}
removeGraph(resource) {
removeGraph(resource = { ship: '~zod', name: 'asdf' }) {
this.graphAction({
'remove-graph': {
resource
@ -22,7 +22,7 @@ class PrivateHelper extends BaseApi {
});
}
addNodes(resource, nodes) {
addNodes(resource = { ship : '~zod', name: 'asdf' }, nodes = {}) {
this.graphAction({
'add-nodes': {
resource,
@ -31,7 +31,7 @@ class PrivateHelper extends BaseApi {
});
}
removeNodes(resource, indices) {
removeNodes(resource = { ship: '~zod', name: 'asdf' }, indices = []) {
this.graphAction({
'remove-nodes': {
resource,
@ -55,6 +55,15 @@ class PrivateHelper extends BaseApi {
removeTag() {
this.graphAction();
}
fetch(connection = 0) {
this.action('graph-view', 'graph-view-action', {
fetch: {
connection,
type: { all: null }
}
});
}
}
export default class GraphApi {
@ -64,6 +73,7 @@ export default class GraphApi {
this.ship = ship;
this.subscribe = helper.subscribe.bind(helper);
// store
this.addGraph = helper.addGraph.bind(helper);
this.removeGraph = helper.removeGraph.bind(helper);
@ -75,6 +85,9 @@ export default class GraphApi {
this.addTag = helper.addTag.bind(helper);
this.removeTag = helper.removeTag.bind(helper);
// view
this.fetch = helper.fetch.bind(helper);
}
}

View File

@ -0,0 +1,26 @@
import BaseSubscription from './base';
let getRandomInt = (max) => {
return Math.floor(Math.random() * Math.floor(max));
}
export default class GraphSubscription extends BaseSubscription {
constructor(store, api, channel) {
super(store, api, channel);
this.connectionNumber = getRandomInt(999);
}
start() {
this.subscribe('/updates/' + this.connectionNumber, 'graph-view');
}
handleEvent(diff) {
if ('graph-view' in diff) {
this.api.fetch(connectionNumber);
} else {
// extend
this.store.handleEvent(diff);
}
}
}