aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/stores/flowstore.js
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2014-09-17 17:30:19 +0200
committerMaximilian Hils <git@maximilianhils.com>2014-09-17 17:30:19 +0200
commit102bd075689892b06765fb857c89604fe9cf33e5 (patch)
tree6782d4ee795337204d29a873e7854d6e426574df /web/src/js/stores/flowstore.js
parent8245dd19f4f2f4cdd74a6fdf9b5e051c2cd2fac6 (diff)
downloadmitmproxy-102bd075689892b06765fb857c89604fe9cf33e5.tar.gz
mitmproxy-102bd075689892b06765fb857c89604fe9cf33e5.tar.bz2
mitmproxy-102bd075689892b06765fb857c89604fe9cf33e5.zip
implement FlowStore basics
Diffstat (limited to 'web/src/js/stores/flowstore.js')
-rw-r--r--web/src/js/stores/flowstore.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/web/src/js/stores/flowstore.js b/web/src/js/stores/flowstore.js
new file mode 100644
index 00000000..006eeb24
--- /dev/null
+++ b/web/src/js/stores/flowstore.js
@@ -0,0 +1,74 @@
+function FlowView(store, live) {
+ EventEmitter.call(this);
+ this._store = store;
+ this.live = live;
+ this.flows = [];
+
+ this.add = this.add.bind(this);
+ this.update = this.update.bind(this);
+
+ if (live) {
+ this._store.addListener(ActionTypes.ADD_FLOW, this.add);
+ this._store.addListener(ActionTypes.UPDATE_FLOW, this.update);
+ }
+}
+
+_.extend(FlowView.prototype, EventEmitter.prototype, {
+ close: function () {
+ this._store.removeListener(ActionTypes.ADD_FLOW, this.add);
+ this._store.removeListener(ActionTypes.UPDATE_FLOW, this.update);
+ },
+ getAll: function () {
+ return this.flows;
+ },
+ add: function (flow) {
+ return this.update(flow);
+ },
+ add_bulk: function (flows) {
+ //Treat all previously received updates as newer than the bulk update.
+ //If they weren't newer, we're about to receive an update for them very soon.
+ var updates = this.flows;
+ this.flows = flows;
+ updates.forEach(function(flow){
+ this.update(flow);
+ }.bind(this));
+ },
+ update: function(flow){
+ console.debug("FIXME: Use UUID");
+ var idx = _.findIndex(this.flows, function(f){
+ return flow.request.timestamp_start == f.request.timestamp_start
+ });
+
+ if(idx < 0){
+ this.flows.push(flow);
+ } else {
+ this.flows[idx] = flow;
+ }
+ this.emit("change");
+ },
+});
+
+
+function _FlowStore() {
+ EventEmitter.call(this);
+}
+_.extend(_FlowStore.prototype, EventEmitter.prototype, {
+ getView: function (since) {
+ var view = new FlowView(this, !since);
+ return view;
+ },
+ handle: function (action) {
+ switch (action.type) {
+ case ActionTypes.ADD_FLOW:
+ case ActionTypes.UPDATE_FLOW:
+ this.emit(action.type, action.data);
+ break;
+ default:
+ return;
+ }
+ }
+});
+
+
+var FlowStore = new _FlowStore();
+AppDispatcher.register(FlowStore.handle.bind(FlowStore));