aboutsummaryrefslogtreecommitdiffstats
path: root/web/src
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2014-12-09 18:55:16 +0100
committerMaximilian Hils <git@maximilianhils.com>2014-12-09 18:55:16 +0100
commit05bc7e8cd8382aabdd44f7bc569d2fd421c26f21 (patch)
tree754fc4ad2c21c833fdf06949a30a412161353ccb /web/src
parent14a8d2f5b83a1ea28abbb490f6c94c43b4e1f960 (diff)
downloadmitmproxy-05bc7e8cd8382aabdd44f7bc569d2fd421c26f21.tar.gz
mitmproxy-05bc7e8cd8382aabdd44f7bc569d2fd421c26f21.tar.bz2
mitmproxy-05bc7e8cd8382aabdd44f7bc569d2fd421c26f21.zip
generalize store
Diffstat (limited to 'web/src')
-rw-r--r--web/src/js/stores/base.js112
-rw-r--r--web/src/js/stores/flowstore.js112
2 files changed, 114 insertions, 110 deletions
diff --git a/web/src/js/stores/base.js b/web/src/js/stores/base.js
index cf9f015e..f9534bd2 100644
--- a/web/src/js/stores/base.js
+++ b/web/src/js/stores/base.js
@@ -27,3 +27,115 @@ EventEmitter.prototype.removeListener = function (events, f) {
}
}.bind(this));
};
+
+
+function Store() {
+ this._views = [];
+ this.reset();
+}
+_.extend(Store.prototype, {
+ add: function (elem) {
+ if (elem.id in this._pos_map) {
+ return;
+ }
+
+ this._pos_map[elem.id] = this._list.length;
+ this._list.push(elem);
+ for (var i = 0; i < this._views.length; i++) {
+ this._views[i].add(elem);
+ }
+ },
+ update: function (elem) {
+ if (!(elem.id in this._pos_map)) {
+ return;
+ }
+
+ this._list[this._pos_map[elem.id]] = elem;
+ for (var i = 0; i < this._views.length; i++) {
+ this._views[i].update(elem);
+ }
+ },
+ remove: function (elem_id) {
+ if (!(elem.id in this._pos_map)) {
+ return;
+ }
+
+ this._list.splice(this._pos_map[elem_id], 1);
+ this._build_map();
+ for (var i = 0; i < this._views.length; i++) {
+ this._views[i].remove(elem_id);
+ }
+ },
+ reset: function (elems) {
+ this._list = elems || [];
+ this._build_map();
+ for (var i = 0; i < this._views.length; i++) {
+ this._views[i].recalculate(this._list);
+ }
+ },
+ _build_map: function () {
+ this._pos_map = {};
+ for (var i = 0; i < this._list.length; i++) {
+ var elem = this._list[i];
+ this._pos_map[elem.id] = i;
+ }
+ },
+ get: function (elem_id) {
+ return this._list[this._pos_map[elem_id]];
+ }
+});
+
+
+function LiveStore(type) {
+ Store.call(this);
+ this.type = type;
+
+ this._updates_before_fetch = undefined;
+ this._fetchxhr = false;
+
+ this.handle = this.handle.bind(this);
+ AppDispatcher.register(this.handle);
+
+ // Avoid double-fetch on startup.
+ if (!(window.ws && window.ws.readyState === WebSocket.CONNECTING)) {
+ this.fetch();
+ }
+}
+_.extend(LiveStore.prototype, Store.prototype, {
+ handle: function (event) {
+ if (event.type === ActionTypes.CONNECTION_OPEN) {
+ return this.fetch();
+ }
+ if (event.type === this.type) {
+ if (event.cmd === "reset") {
+ this.fetch();
+ } else if (this._updates_before_fetch) {
+ console.log("defer update", event);
+ this._updates_before_fetch.push(event);
+ } else {
+ this[event.cmd](event.data);
+ }
+ }
+ },
+ close: function () {
+ AppDispatcher.unregister(this.handle);
+ },
+ fetch: function () {
+ console.log("fetch " + this.type);
+ if (this._fetchxhr) {
+ this._fetchxhr.abort();
+ }
+ this._fetchxhr = $.getJSON("/" + this.type, this.handle_fetch.bind(this));
+ this._updates_before_fetch = []; // (JS: empty array is true)
+ },
+ handle_fetch: function (data) {
+ this._fetchxhr = false;
+ console.log(this.type + " fetched.", this._updates_before_fetch);
+ this.reset(data.flows);
+ var updates = this._updates_before_fetch;
+ this._updates_before_fetch = false;
+ for (var i = 0; i < updates.length; i++) {
+ this.handle(updates[i]);
+ }
+ },
+}); \ No newline at end of file
diff --git a/web/src/js/stores/flowstore.js b/web/src/js/stores/flowstore.js
index 4110ea7f..8bffe5b2 100644
--- a/web/src/js/stores/flowstore.js
+++ b/web/src/js/stores/flowstore.js
@@ -1,114 +1,6 @@
-function FlowStore() {
- this._views = [];
- this.reset();
-}
-_.extend(FlowStore.prototype, {
- add: function (flow) {
- this._pos_map[flow.id] = this._flow_list.length;
- this._flow_list.push(flow);
- for (var i = 0; i < this._views.length; i++) {
- this._views[i].add(flow);
- }
- },
- update: function (flow) {
- this._flow_list[this._pos_map[flow.id]] = flow;
- for (var i = 0; i < this._views.length; i++) {
- this._views[i].update(flow);
- }
- },
- remove: function (flow_id) {
- this._flow_list.splice(this._pos_map[flow_id], 1);
- this._build_map();
- for (var i = 0; i < this._views.length; i++) {
- this._views[i].remove(flow_id);
- }
- },
- reset: function (flows) {
- this._flow_list = flows || [];
- this._build_map();
- for (var i = 0; i < this._views.length; i++) {
- this._views[i].recalculate(this._flow_list);
- }
- },
- _build_map: function () {
- this._pos_map = {};
- for (var i = 0; i < this._flow_list.length; i++) {
- var flow = this._flow_list[i];
- this._pos_map[flow.id] = i;
- }
- },
- get: function (flow_id) {
- return this._flow_list[this._pos_map[flow_id]];
- }
-});
-
-
function LiveFlowStore() {
- FlowStore.call(this);
- this.updates_before_fetch = undefined;
- this._fetchxhr = false;
-
- this.handle = this.handle.bind(this);
- AppDispatcher.register(this.handle);
-
- // Avoid double-fetch on startup.
- if(!(window.ws && window.ws.readyState === WebSocket.CONNECTING)) {
- this.fetch();
- }
+ return new LiveStore("flows");
}
-_.extend(LiveFlowStore.prototype, FlowStore.prototype, {
- handle: function (event) {
- switch (event.type) {
- case ActionTypes.CONNECTION_OPEN:
- case ActionTypes.RESET_FLOWS:
- this.fetch();
- break;
- case ActionTypes.ADD_FLOW:
- case ActionTypes.UPDATE_FLOW:
- case ActionTypes.REMOVE_FLOW:
- if (this.updates_before_fetch) {
- console.log("defer update", type, data);
- this.updates_before_fetch.push(event);
- } else {
- if(event.type === ActionTypes.ADD_FLOW){
- this.add(event.data);
- } else if (event.type === ActionTypes.UPDATE_FLOW){
- this.update(event.data);
- } else {
- this.remove(event.data);
- }
- }
- break;
- }
- },
- close: function () {
- AppDispatcher.unregister(this.handle);
- },
- add: function (flow) {
- // Make sure that deferred adds don't add an element twice.
- if (!(flow.id in this._pos_map)) {
- FlowStore.prototype.add.call(this, flow);
- }
- },
- fetch: function () {
- console.log("fetch");
- if (this._fetchxhr) {
- this._fetchxhr.abort();
- }
- this._fetchxhr = $.getJSON("/flows", this.handle_fetch.bind(this));
- this.updates_before_fetch = []; // (JS: empty array is true)
- },
- handle_fetch: function (data) {
- this._fetchxhr = false;
- console.log("Flows fetched.", this.updates_before_fetch);
- this.reset(data.flows);
- var updates = this.updates_before_fetch;
- this.updates_before_fetch = false;
- for (var i = 0; i < updates.length; i++) {
- this.handle(updates[i]);
- }
- },
-});
function SortByInsertionOrder() {
this.i = 0;
@@ -134,7 +26,7 @@ function FlowView(store, filt, sortfun) {
this.store = store;
this.store._views.push(this);
- this.recalculate(this.store._flow_list, filt, sortfun);
+ this.recalculate(this.store._list, filt, sortfun);
}
_.extend(FlowView.prototype, EventEmitter.prototype, {