web: add flow stub

This commit is contained in:
Maximilian Hils 2014-09-14 02:04:48 +02:00
parent 48211a2069
commit abc91d6658
3 changed files with 2066 additions and 26 deletions

2012
web/src/flows.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,26 @@
class EventEmitter { class EventEmitter {
constructor(){ constructor(){
this._listeners = {}; this.listeners = {};
} }
emit(event){ emit(event){
if(!(event in this._listeners)){ if(!(event in this.listeners)){
return; return;
} }
this._listeners[event].forEach(function (listener) { this.listeners[event].forEach(function (listener) {
listener(event, this); listener(event, this);
}.bind(this)); }.bind(this));
} }
addListener(event, f){ addListener(event, f){
this._listeners[event] = this._listeners[event] || []; this.listeners[event] = this.listeners[event] || [];
this._listeners[event].push(f); this.listeners[event].push(f);
} }
removeListener(event, f){ removeListener(event, f){
if(!(event in this._listeners)){ if(!(event in this.listeners)){
return false; return false;
} }
var index = this._listeners.indexOf(f); var index = this.listeners.indexOf(f);
if (index >= 0) { if (index >= 0) {
this._listeners.splice(this._listeners.indexOf(f), 1); this.listeners.splice(this.listeners.indexOf(f), 1);
} }
} }
} }
@ -31,13 +31,17 @@ class FlowStore extends EventEmitter{
constructor() { constructor() {
super(); super();
this.flows = []; this.flows = [];
this._listeners = [];
} }
getAll() { getAll() {
return this.flows; return this.flows;
} }
close(){
console.log("FlowStore.close()");
this.listeners = [];
}
emitChange() { emitChange() {
return this.emit(FLOW_CHANGED); return this.emit(FLOW_CHANGED);
} }
@ -57,14 +61,14 @@ class DummyFlowStore extends FlowStore {
this.flows = flows; this.flows = flows;
} }
addFlow(f) { addFlow(flow) {
this.flows.push(f); this.flows.push(flow);
this.emitChange(); this.emitChange();
} }
} }
var SETTINGS_CHANGED = "settings.change"; var SETTINGS_CHANGED = "settings.changed";
class Settings extends EventEmitter { class Settings extends EventEmitter {
constructor(){ constructor(){

View File

@ -9,16 +9,13 @@ var App = React.createClass({
}, },
componentDidMount: function () { componentDidMount: function () {
//TODO: Replace DummyStore with real settings over WS (https://facebook.github.io/react/tips/initial-ajax.html) //TODO: Replace DummyStore with real settings over WS (https://facebook.github.io/react/tips/initial-ajax.html)
//TODO: Is there a sensible place where we can store this? var settingsStore = new DummySettings({
var settings = new DummySettings({
version: "0.12" version: "0.12"
}); });
settings.addChangeListener(this._onSettingsChange); this.setState({settingsStore: settingsStore});
settingsStore.addChangeListener(this.onSettingsChange);
//This would be async in some way or another.
this._onSettingsChange(null, settings);
}, },
_onSettingsChange: function(event, settings){ onSettingsChange: function(event, settings){
this.setState({settings: settings.getAll()}); this.setState({settings: settings.getAll()});
}, },
render: function () { render: function () {
@ -34,12 +31,39 @@ var App = React.createClass({
} }
}); });
var Traffic = React.createClass({ var TrafficTable = React.createClass({
render: function(){ getInitialState: function(){
var json = JSON.stringify(this.props, null, 4); return {
var i = 5; flows: []
while(i--) json += json; };
return (<pre>{json}</pre>); },
componentDidMount: function () {
var flowStore = new DummyFlowStore([]);
this.setState({flowStore: flowStore});
flowStore.addChangeListener(this.onFlowsChange);
$.getJSON("/flows.json").success(function (flows) {
flows.forEach(function (flow, i) {
window.setTimeout(function () {
flowStore.addFlow(flow);
}, _.random(i*400,i*400+1000));
});
}.bind(this));
},
componentWillUnmount: function(){
this.state.flowStore.close();
},
onFlowsChange: function(event, flows){
this.setState({flows: flows.getAll()});
},
render: function () {
var flows = this.state.flows.map(function(flow){
return <div>{flow.request.method} {flow.request.scheme}://{flow.request.host}{flow.request.path}</div>;
});
return <pre>{flows}</pre>;
} }
}); });
@ -52,7 +76,7 @@ var Reports = React.createClass({
var routes = ( var routes = (
<ReactRouter.Routes location="hash"> <ReactRouter.Routes location="hash">
<ReactRouter.Route name="app" path="/" handler={App}> <ReactRouter.Route name="app" path="/" handler={App}>
<ReactRouter.Route name="main" handler={Traffic}/> <ReactRouter.Route name="main" handler={TrafficTable}/>
<ReactRouter.Route name="reports" handler={Reports}/> <ReactRouter.Route name="reports" handler={Reports}/>
<ReactRouter.Redirect to="main"/> <ReactRouter.Redirect to="main"/>
</ReactRouter.Route> </ReactRouter.Route>