From 7d74d1870fd9436812f632acf770e8ed92cf635a Mon Sep 17 00:00:00 2001 From: Logan Allen Date: Fri, 12 Jun 2020 15:07:47 -0400 Subject: [PATCH] graph-js: added demo fetch api / subscription objs --- pkg/interface/src/api/graph.js | 21 ++++++++++++++++---- pkg/interface/src/subscription/graph.js | 26 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 pkg/interface/src/subscription/graph.js diff --git a/pkg/interface/src/api/graph.js b/pkg/interface/src/api/graph.js index 86e28af8c..04eb5c05a 100644 --- a/pkg/interface/src/api/graph.js +++ b/pkg/interface/src/api/graph.js @@ -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); } } diff --git a/pkg/interface/src/subscription/graph.js b/pkg/interface/src/subscription/graph.js new file mode 100644 index 000000000..2d242ac6b --- /dev/null +++ b/pkg/interface/src/subscription/graph.js @@ -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); + } + } +} +