@fedeghe/pangjs 0.0.4 → 0.0.5
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 +29 -32
- package/dist/index.js +21 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# PANGjs (0.0.
|
|
1
|
+
# PANGjs (0.0.5)
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
@@ -10,28 +10,25 @@ install it
|
|
|
10
10
|
``` sh
|
|
11
11
|
> npm install @fedeghe/pangjs
|
|
12
12
|
```
|
|
13
|
+
define an asynchronous reducer as:
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
now it's time to get a store and use it:
|
|
28
|
-
|
|
15
|
+
``` js
|
|
16
|
+
const reducer = async (oldState, action, payload) => {
|
|
17
|
+
const res = await fetch(targetUrl);
|
|
18
|
+
//
|
|
19
|
+
// your updates
|
|
20
|
+
//
|
|
21
|
+
return newState
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
get a store and use it:
|
|
29
25
|
|
|
30
26
|
``` js
|
|
27
|
+
const PANGjs = require('@fedeghe/pangjs')
|
|
31
28
|
const store = PANGjs.getStore( reducer, initState );
|
|
32
29
|
|
|
33
|
-
//
|
|
34
|
-
store.
|
|
30
|
+
// stage the results only internally
|
|
31
|
+
store.stage({
|
|
35
32
|
type: 'ADD',
|
|
36
33
|
payload: { number: 4 }
|
|
37
34
|
})
|
|
@@ -39,23 +36,23 @@ store.commit({
|
|
|
39
36
|
'here we get the unpushed state:', s
|
|
40
37
|
));
|
|
41
38
|
|
|
42
|
-
//
|
|
43
|
-
store.
|
|
39
|
+
// make all staged changes effective
|
|
40
|
+
store.dispatch()
|
|
44
41
|
// here we get the pushed state
|
|
45
42
|
.then(console.log);
|
|
46
43
|
```
|
|
47
|
-
alternatively one single call just adding `true` as second parameter
|
|
44
|
+
alternatively one single call just adding `true` as second parameter also dispatch:
|
|
48
45
|
``` js
|
|
49
|
-
store.
|
|
46
|
+
store.stage({
|
|
50
47
|
type: 'ADD',
|
|
51
48
|
payload: { number: 4 }
|
|
52
|
-
}, true /*
|
|
49
|
+
}, true /* autoDispatch */)
|
|
53
50
|
.then(console.log); // here we get autopushed state
|
|
54
51
|
```
|
|
55
52
|
|
|
56
53
|
alternatively it is possible to directly push the action:
|
|
57
54
|
``` js
|
|
58
|
-
store.
|
|
55
|
+
store.dispatch({
|
|
59
56
|
type: 'ADD',
|
|
60
57
|
payload: { number: 4 }},
|
|
61
58
|
)
|
|
@@ -134,9 +131,9 @@ Every store obtained invoking successfully `PANGjs.getStore` exposes the followi
|
|
|
134
131
|
|
|
135
132
|
returns the last pushed state
|
|
136
133
|
|
|
137
|
-
### `storeInstance.
|
|
134
|
+
### `storeInstance.stage(action, autoPush) -> Promise`
|
|
138
135
|
|
|
139
|
-
|
|
136
|
+
stage or stage&dispatch returning a promise resolving with new state (staged or not);
|
|
140
137
|
|
|
141
138
|
**Parameters**:
|
|
142
139
|
- **action**:
|
|
@@ -150,16 +147,16 @@ commit or commit&push returning a promise resolving with new state (pushed or no
|
|
|
150
147
|
|
|
151
148
|
|
|
152
149
|
|
|
153
|
-
### `storeInstance.
|
|
154
|
-
|
|
150
|
+
### `storeInstance.dispatch() -> Promise`
|
|
151
|
+
dispatch all the staged changes.
|
|
155
152
|
Only this operations calls subscribers.
|
|
156
153
|
|
|
157
|
-
Optionally it can recieve an action and that will be equivalent to
|
|
158
|
-
`s.
|
|
154
|
+
Optionally it can recieve an action and that will be equivalent to stage and dispatch:
|
|
155
|
+
`s.stage(action).then(() => s.dispatch())`
|
|
159
156
|
act as
|
|
160
|
-
`s.
|
|
157
|
+
`s.stage(action, true)`
|
|
161
158
|
and as
|
|
162
|
-
`s.
|
|
159
|
+
`s.dispatch(action))`
|
|
163
160
|
|
|
164
161
|
|
|
165
162
|
### `storeInstance.subscribe(fn) -> unsubscribing function`
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
/*
|
|
3
3
|
PANGjs
|
|
4
|
-
v. 0.0.
|
|
4
|
+
v. 0.0.5
|
|
5
5
|
|
|
6
|
-
Size: ~3.
|
|
6
|
+
Size: ~3.79KB
|
|
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 i(t,e){this.initState=t,this.states=[t],this.
|
|
10
|
-
this.index=0,this.
|
|
11
|
-
|
|
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 r(e,s,r){this.reducer=e||o,t(this.reducer,n.REDUCERS_FUNCTION),this.initState=s||{},this.config=r||{},this.config.check=this.config.check||function(){return!0
|
|
11
|
+
},t(this.config.check,n.REDUCERS_FUNCTION),this.subscribers=[],this.previousAction="ORIGIN",this.HistoryManager=new i(this.initState,this.config)}var n={
|
|
12
12
|
REDUCERS_FUNCTION:"[ERROR] Reducer must be a function!",REDUCERS_RETURN:"[ERROR] Reducer should return a promise!",REDUCERS_ASYNC:"[ERROR] Reducer should be asynchronous!",
|
|
13
13
|
SUBSCRIBERS_FUNCTION:"[ERROR] Subscribers must be a functions!",ACTION_TYPE:"[ERROR] Actions needs a type!",UNAUTHORIZED_STATECHANGE:"[ERROR] State transition not allowed!",
|
|
14
|
-
MOVE_TO_NUMBER:"[ERROR] Move requires a number!"};i.prototype.top=function(t){return this[t?"
|
|
15
|
-
var
|
|
16
|
-
i.prototype.
|
|
17
|
-
this.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
;var e=this.HistoryManager.index+t,i=e>-1&&e<this.HistoryManager.states.length,
|
|
26
|
-
|
|
27
|
-
getStore:function(t,e,
|
|
28
|
-
var
|
|
14
|
+
MOVE_TO_NUMBER:"[ERROR] Move requires a number!"};i.prototype.top=function(t){return this[t?"stagedStates":"states"][this[t?"stagedIndex":"index"]]},i.prototype.stage=function(t,e){
|
|
15
|
+
var s=this.stagedStates.slice(0,this.stagedIndex+1);return s.push(t),this.maxElements&&s.length>this.maxElements?s.shift():this.stagedIndex++,this.stagedStates=s,e&&this.sync(),this},
|
|
16
|
+
i.prototype.sync=function(){this.states=this.stagedStates,this.index=this.stagedIndex},i.prototype.reset=function(){this.index=0,this.states=[this.initState],this.stagedIndex=0,
|
|
17
|
+
this.stagedStates=this.stagedStates.slice(0,1)};var o=function(){return Promise.resolve({})};return r.prototype.getState=function(t){return this.HistoryManager.top(t)},r.prototype.unstage=function(){
|
|
18
|
+
if(1===this.HistoryManager.maxElements)return this;this.HistoryManager.stagedIndex=this.HistoryManager.index,this.HistoryManager.stagedStates=this.HistoryManager.states},
|
|
19
|
+
r.prototype.stage=function(t,s){if(!("type"in t))return Promise.reject(new Error(n.ACTION_TYPE));var i=this,r=t.type,o=t.payload||{},a=this.getState(!0)
|
|
20
|
+
;if(!i.config.check(a,i.previousAction,r,o))return Promise.reject(new Error(n.UNAUTHORIZED_STATECHANGE));var u=this.reducer(a,r,o);return e(u,n.REDUCERS_RETURN),this.previousAction=r,
|
|
21
|
+
u.then(function(t){return i.HistoryManager.stage(t,s),s&&i.emit(t),t})},r.prototype.emit=function(t){this.subscribers.filter(function(t){return Boolean(t)}).forEach(function(e){e(t)})},
|
|
22
|
+
r.prototype.dispatch=function(t){if(t)return this.stage(t,!0);this.HistoryManager.sync();var e=this.HistoryManager.top();return this.emit(e),Promise.resolve(e)},r.prototype.subscribe=function(e){
|
|
23
|
+
t(e,n.SUBSCRIBERS_FUNCTION);var s,i=this;return this.subscribers.push(e),s=this.subscribers.length-1,function(){i.subscribers[s]=null}},r.prototype.move=function(t){if(s(t,n.MOVE_TO_NUMBER),
|
|
24
|
+
1===this.HistoryManager.maxElements||this.HistoryManager.index!==this.HistoryManager.stagedIndex||void 0===t||0===t)return this
|
|
25
|
+
;var e=this.HistoryManager.index+t,i=e>-1&&e<this.HistoryManager.states.length,r=i?e:this.HistoryManager.index;return this.HistoryManager.index=r,this.HistoryManager.stagedIndex=r,this},
|
|
26
|
+
r.prototype.replaceReducer=function(e){return t(e,n.SUBSCRIBERS_FUNCTION),this.reducer=e,this},r.prototype.reset=function(){return this.HistoryManager.reset(),this.subscribers=[],this},{ERRORS:n,
|
|
27
|
+
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,n.REDUCERS_FUNCTION)}),function(t,s,i){
|
|
28
|
+
var r=Object.assign({},t),n=e.length;return new Promise(function(t){return e.reduce(function(e,r,o){return e.then(function(e){return n-1===o?t(r(e,s,i)):r(e,s,i)})},Promise.resolve(r))})}}}}()
|
|
29
29
|
;"object"==typeof exports&&(module.exports=PANGjs);
|