@fedeghe/pangjs 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +24 -23
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
/*
|
|
3
3
|
PANGjs
|
|
4
|
-
v. 0.0.
|
|
4
|
+
v. 0.0.8
|
|
5
5
|
|
|
6
|
-
Size: ~3.
|
|
6
|
+
Size: ~3.77KB
|
|
7
7
|
*/
|
|
8
|
-
var PANGjs=function(){"use strict";function t(t,e){if("function"!=typeof t)throw new Error(e)}function e(t,e){if("Promise"!==t.constructor.name)throw new Error(e)}function
|
|
9
|
-
if("number"!=typeof t)throw new Error(e)}function
|
|
10
|
-
this.index=0,this.stagedIndex=0}function n(
|
|
11
|
-
|
|
12
|
-
REDUCERS_FUNCTION:"[ERROR] Reducer must be a function!",
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
s.
|
|
17
|
-
this.stagedStates=[this.initState]};var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
n.prototype.dispatch=function(t){if(t)return this.stage(t,!0);this.HistoryManager.sync();var e=this.HistoryManager.top()
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
var PANGjs=function(){"use strict";function t(t,e){if("function"!=typeof t)throw new Error(e)}function e(t,e){if("Promise"!==t.constructor.name)throw new Error(e)}function s(t,e){
|
|
9
|
+
if("number"!=typeof t)throw new Error(e)}function i(t,e){this.initState=t,this.states=[t],this.stagedStates=[t],this.config=e,this.maxElements=Math.max(1,parseInt(this.config.maxElements,10))||1,
|
|
10
|
+
this.index=0,this.stagedIndex=0}function n(t){this.subscribers.filter(function(t){return Boolean(t)}).forEach(function(e){e(t)})}function r(e,s,n){this.reducer=e||a,
|
|
11
|
+
t(this.reducer,o.REDUCERS_FUNCTION),this.initState=s||{},this.config=n||{},this.config.check=this.config.check||function(){return!0},t(this.config.check,o.REDUCERS_FUNCTION),this.subscribers=[],
|
|
12
|
+
this.previousAction="ORIGIN",this.HistoryManager=new i(this.initState,this.config)}var o={REDUCERS_FUNCTION:"[ERROR] Reducer must be a function!",
|
|
13
|
+
REDUCERS_RETURN:"[ERROR] Reducer should return a promise!",REDUCERS_ASYNC:"[ERROR] Reducer should be asynchronous!",SUBSCRIBERS_FUNCTION:"[ERROR] Subscribers must be a functions!",
|
|
14
|
+
ACTION_TYPE:"[ERROR] Actions needs a type!",UNAUTHORIZED_STATECHANGE:"[ERROR] State transition not allowed!",MOVE_TO_NUMBER:"[ERROR] Move requires a number!"};i.prototype.top=function(t){
|
|
15
|
+
return this[t?"stagedStates":"states"][this[t?"stagedIndex":"index"]]},i.prototype.stage=function(t,e){var s=this.stagedStates.slice(0,this.stagedIndex+1);return s.push(t),
|
|
16
|
+
this.maxElements&&s.length>this.maxElements?s.shift():this.stagedIndex++,this.stagedStates=s,e&&this.sync(),this},i.prototype.sync=function(){this.states=this.stagedStates,this.index=this.stagedIndex
|
|
17
|
+
},i.prototype.reset=function(){this.index=0,this.states=[this.initState],this.stagedIndex=0,this.stagedStates=[this.initState]};var a=function(){return Promise.resolve({})}
|
|
18
|
+
;return r.prototype.getState=function(t){return this.HistoryManager.top(t)},r.prototype.unstage=function(){if(1===this.HistoryManager.maxElements)return this
|
|
19
|
+
;this.HistoryManager.stagedIndex=this.HistoryManager.index,this.HistoryManager.stagedStates=this.HistoryManager.states},r.prototype.stage=function(t,s){
|
|
20
|
+
if(!("type"in t))return Promise.reject(new Error(o.ACTION_TYPE));var i=this,r=t.type,a=t.payload||{},h=this.getState(!0)
|
|
21
|
+
;if(!i.config.check(h,i.previousAction,r,a))return Promise.reject(new Error(o.UNAUTHORIZED_STATECHANGE));var u=this.reducer(h,r,a);return e(u,o.REDUCERS_RETURN),this.previousAction=r,
|
|
22
|
+
u.then(function(t){return i.HistoryManager.stage(t,s),s&&n.call(i,t),t})},r.prototype.dispatch=function(t){if(t)return this.stage(t,!0);this.HistoryManager.sync();var e=this.HistoryManager.top()
|
|
23
|
+
;return n.call(this,e),Promise.resolve(e)},r.prototype.subscribe=function(e){t(e,o.SUBSCRIBERS_FUNCTION);var s,i=this;return this.subscribers.push(e),s=this.subscribers.length-1,function(){
|
|
24
|
+
i.subscribers[s]=null}},r.prototype.move=function(t){
|
|
25
|
+
if(s(t,o.MOVE_TO_NUMBER),1===this.HistoryManager.maxElements||this.HistoryManager.index!==this.HistoryManager.stagedIndex||void 0===t||0===t)return this
|
|
26
|
+
;var e=this.HistoryManager.index+t,i=e>-1&&e<this.HistoryManager.states.length,n=i?e:this.HistoryManager.index;return this.HistoryManager.index=n,this.HistoryManager.stagedIndex=n,this},
|
|
27
|
+
r.prototype.replaceReducer=function(e){return t(e,o.SUBSCRIBERS_FUNCTION),this.reducer=e,this},r.prototype.reset=function(){return this.HistoryManager.reset(),n.call(this,this.initState),this},{
|
|
28
|
+
ERRORS:o,getStore:function(t,e,s){return new r(t,e,s)},isStore:function(t){return t instanceof r},combine:function(e){return e.forEach(function(e){t(e,o.REDUCERS_FUNCTION)}),function(t,s,i){
|
|
29
|
+
var n=Object.assign({},t),r=e.length;return new Promise(function(t){return e.reduce(function(e,n,o){return e.then(function(e){return r-1===o?t(n(e,s,i)):n(e,s,i)})},Promise.resolve(n))})}}}}()
|
|
29
30
|
;"object"==typeof exports&&(module.exports=PANGjs);
|