From 94a0b82ceedf1847a9bc67a6338369583f8410f9 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Mon, 7 Aug 2017 10:57:12 +0800 Subject: [web] Add static backend. --- web/src/js/backends/static.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 web/src/js/backends/static.js (limited to 'web/src/js/backends/static.js') diff --git a/web/src/js/backends/static.js b/web/src/js/backends/static.js new file mode 100644 index 00000000..676468ce --- /dev/null +++ b/web/src/js/backends/static.js @@ -0,0 +1,54 @@ +/* + * This backend uses the REST API only to host static instances, + * without any Websocket connection. + */ +import { fetchApi } from "../utils" + +const CMD_RESET = 'reset' + +export default class StaticBackend { + constructor(store) { + this.activeFetches = {} + this.store = store + this.onOpen() + } + + onOpen() { + this.fetchData("settings") + this.fetchData("flows") + this.fetchData("events") + this.fetchData("options") + } + + fetchData(resource) { + let queue = [] + this.activeFetches[resource] = queue + fetchApi(`/${resource}`) + .then(res => res.json()) + .then(json => { + if (this.activeFetches[resource] === queue) + this.receive(resource, json) + }) + } + + onMessage(msg) { + if (msg.cmd === CMD_RESET) { + return this.fetchData(msg.resource) + } + if (msg.resource in this.activeFetches) { + this.activeFetches[msg.resource].push(msg) + } else { + let type = `${msg.resource}_${msg.cmd}`.toUpperCase() + this.store.dispatch({ type, ...msg}) + } + } + + receive(resource, data) { + let type = `${resource}_RECEIVE`.toUpperCase() + this.store.dispatch({ type, cmd: "receive", resource, data }) + let queue = this.activeFetches[resource] + delete this.activeFetches[resource] + queue.forEach(msg => this.onMessage(msg)) + } + +} -- cgit v1.2.3 From 32957976f512ba0af75bae54670c0eb6b0e5456c Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Tue, 8 Aug 2017 22:33:41 +0800 Subject: [web] Remove activeFetches and onMessage in StaticBackend. --- web/src/js/backends/static.js | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'web/src/js/backends/static.js') diff --git a/web/src/js/backends/static.js b/web/src/js/backends/static.js index 676468ce..1de6b294 100644 --- a/web/src/js/backends/static.js +++ b/web/src/js/backends/static.js @@ -21,34 +21,16 @@ export default class StaticBackend { } fetchData(resource) { - let queue = [] - this.activeFetches[resource] = queue fetchApi(`/${resource}`) .then(res => res.json()) .then(json => { - if (this.activeFetches[resource] === queue) - this.receive(resource, json) + this.receive(resource, json) }) } - onMessage(msg) { - if (msg.cmd === CMD_RESET) { - return this.fetchData(msg.resource) - } - if (msg.resource in this.activeFetches) { - this.activeFetches[msg.resource].push(msg) - } else { - let type = `${msg.resource}_${msg.cmd}`.toUpperCase() - this.store.dispatch({ type, ...msg}) - } - } - receive(resource, data) { let type = `${resource}_RECEIVE`.toUpperCase() this.store.dispatch({ type, cmd: "receive", resource, data }) - let queue = this.activeFetches[resource] - delete this.activeFetches[resource] - queue.forEach(msg => this.onMessage(msg)) } } -- cgit v1.2.3 From 0ad552ead46ae501b8af0a28820aad40b927cba7 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Tue, 8 Aug 2017 22:46:47 +0800 Subject: [web] Minor fixes. --- web/src/js/backends/static.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'web/src/js/backends/static.js') diff --git a/web/src/js/backends/static.js b/web/src/js/backends/static.js index 1de6b294..6657fecf 100644 --- a/web/src/js/backends/static.js +++ b/web/src/js/backends/static.js @@ -4,11 +4,8 @@ */ import { fetchApi } from "../utils" -const CMD_RESET = 'reset' - export default class StaticBackend { constructor(store) { - this.activeFetches = {} this.store = store this.onOpen() } -- cgit v1.2.3