-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfluxify.js
72 lines (60 loc) · 1.75 KB
/
fluxify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
var XDispatcher = require( './src/xDispatcher' ),
XStore = require( './src/xStore' )
;
//#build
/**
* Fluxify class that will be used as a singleton.
* Initializes the dispatcher and the store.
* Also set the Promise object if it is globally available.
*/
var Fluxify = function(){
Object.defineProperty( this, 'dispatcher', {
value: new XDispatcher()
});
this.stores = {};
if( typeof Promise != 'undefined' ){
this.promisify( Promise );
}
};
Fluxify.prototype = {
/**
* Create a new store. If an id is passed in the options,
* the store will be registered in the dispatcher and saved
* in fluxify.stores[id].
*
* @param {Object} options {id, initialState, actionCallback}
* @return {XStore}
*/
createStore: function( options ){
var store = new XStore( options );
// If the store has an id, register it in Fluxify and in the dispatcher
if( store._id ){
this.stores[ store._id ] = store;
this.dispatcher.registerStore( store._id, store );
}
return store;
},
/**
* Executes an action. The arguments of this function will be available
* for the action callbacks registered in the dispatcher.
* @return { Promise } A promise that is resolved when all the action callbacks
* have finished.
*/
doAction: function() {
return this.dispatcher.dispatch.apply( this.dispatcher, arguments );
},
/**
* If ES6 Promise object is not defined globally or polyfilled, a Promise object
* can be given to fluxify in order to make it work, using this method.
*
* @param { Promise } Promise ES6 Promise compatible object
* @return { undefined }
*/
promisify: function( Promise ){
this._Promise = Promise;
this.dispatcher._Promise = Promise;
}
};
//#build
module.exports = new Fluxify();