Skip to content

Instantly share code, notes, and snippets.

@SH4DY
Created November 6, 2015 08:21
Show Gist options
  • Save SH4DY/7219385a528a1480aa69 to your computer and use it in GitHub Desktop.
Save SH4DY/7219385a528a1480aa69 to your computer and use it in GitHub Desktop.
import AppDispatcherInstance = require('../dispatcher/AppDispatcher');
import Events = require('events');
import React = require('react');
import AppDispatcher = require("../dispatcher/AppDispatcher");
//This is just an alias for the interface AppDispatcher.AppPayload
import Payload = AppDispatcher.AppPayload
import ActionIdentifier = AppDispatcher.ActionIdentifier
import Interfaces = require('../Tree/Interfaces')
var CHANGE_EVENT='change';
/**
* This is the store of the app. It holds a '''up-to-date''' representation of the data
*/
//From the outside inaccessible representation of the data
var _nodes:Interfaces.NodeProps={key:0, node:undefined};
//We use EventEmitter of node.js
var emitter = new Events.EventEmitter();
export function emitChange() {
emitter.emit(CHANGE_EVENT);
}
/** This is the call back which gets registered at the the AppDispatcher
* @param {function} callback
*/
export function addChangeListener(callback:() => any) {
emitter.on(CHANGE_EVENT, callback);
}
//
// export function getNode(id:any) {
// return _nodes[id];
// }
export function getAllNodes() {
return _nodes;
}
var dispatchToken=AppDispatcherInstance.instance.register((payload:Payload)=> {
switch(payload.action) {
case ActionIdentifier.REFRESH_FROM_SERVER:
_nodes=payload.content;
emitChange();
break;
default: //do nothing
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment