forked from luisvinicius167/mockstate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riotux.js
154 lines (152 loc) · 4.82 KB
/
riotux.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
!function(e,n){"function"==typeof define&&define.amd?define([],n):"undefined"!=typeof module?module.exports=n:e.riotux=n()}(this,
function ( ) {
/*!
--------------------------------
riotux.js
--------------------------------
+ https://luisvinicius167.github.io/riotux/
+ Copyright 2016 Luis Vinícius
+ Licensed under the MIT license
+ Documentation: https://github.com/luisvinicius167/riotux
*/
'use strict';
/**
* @name _currentState
* @description The current state for state that will be changed
*/
var _currentState;
/**
* @name the store state and mutations
* @type { Object }
*/
var _store = {
dispatch: function ( name ) {
var _slice = Array.prototype.slice.call(arguments, 1)
, state = [_store.state]
, args = state.concat(_slice)
;
return Promise
.resolve(_store.mutations[name].apply(null, args))
.then(function ( ) {
_store.update();
});
},
/**
* [tags contain all tags and states]
* @type {Array}
*/
tags: [],
/**
* @name subscribe
* @description Add the tag and the states for the tag
* update when the states changes
* @param { Component instance } component Your component
* @param { array } states Array that contain the states
*/
subscribe: function ( component, states, handler ) {
_store.tags.push({ component: component, states: states, handler:handler });
},
/**
* @name unsubscribe
* @description Unsubscribre the component for states changes
* @param { Component instance } tag Your component
*/
unsubscribe: function ( tag ) {
_store.tags.forEach(function( el, i ) {
if ( el.component === tag ) {
_store.tags.splice(i, 1);
}
});
},
/**
* @name update
* @description Execute the component handler, because the state changed
*/
update: function ( ) {
_store.tags.forEach(function ( el, index, arr ) {
if ( el.states.indexOf(_currentState) !== -1 ) {
if (typeof el.handler === "function") {
el.handler( _currentState, _store.state[_currentState] );
}
}
});
}
};
/**
* @desc Central State management inspired in Redux and Flux pattern
* @function riotux
*/
function riotux ( ) {
/**
* @name actions
* @description All actions for components call
* @type {Object}
*/
this.actions = {};
};
riotux.prototype = {
/**
* @name subscribe
* @description subscribe the tag to update when the states changes
* @param { String } [component] The Component instance
* @param { Array } states The states that your component listen
* @param { Function } handler The function that you use to update your component when the each state change
*/
subscribe: function ( component, states, handler ) {
_store.subscribe(component, states, handler);
},
/**
* @name unsubscribe
* @description unsubscribe component for states changes
* @param { string } component The Component instance
*/
unsubscribe: function ( component ) {
_store.unsubscribe(component);
},
/**
* @name Store
* @param { object } data The data that contain the store mutations and state
* @return { object } Return the store
*/
Store: function ( data ) {
_store = Object.assign(_store, data);
},
/**
* @name Actions
* @param { object } data The data that contain all actions
* @return { object } Return actions
*/
Actions: function ( data ) {
this.actions = data;
return this.actions;
},
/**
* @name action
* @description Emit an action for store dispatcher to change the state
* @return { void }
*/
action: function ( ) {
_currentState = arguments[0];
// pass just the method dispatch to action
var store_to_action = { dispatch: _store.dispatch }
, store = [store_to_action]
, args
;
if (_store.state[_currentState] !== undefined ) {
args = store.concat(Array.prototype.slice.call(arguments, 2));
this.actions[arguments[1]].apply(null, args);
} else {
args = store.concat(Array.prototype.slice.call(arguments, 1));
this.actions[arguments[0]].apply(null, args);
}
},
/**
* @name getter
* @param { string } name The name of state
*/
getter: function ( name ) {
return _store.state[name];
}
};
return new riotux;
});