@fedeghe/pangjs 0.0.4 → 0.0.6
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 +41 -45
- 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.6)
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
@@ -10,52 +10,48 @@ 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.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
// here we get the pushed state
|
|
30
|
+
// stage the results only internally
|
|
31
|
+
store.stage({
|
|
32
|
+
type: 'ADD',
|
|
33
|
+
payload: { number: 4 }
|
|
34
|
+
})
|
|
35
|
+
// here we get staged state
|
|
36
|
+
.then(console.log);
|
|
37
|
+
|
|
38
|
+
// make all staged changes effective
|
|
39
|
+
store.dispatch()
|
|
40
|
+
// here we get the state
|
|
45
41
|
.then(console.log);
|
|
46
42
|
```
|
|
47
|
-
alternatively one single call just adding `true` as second parameter
|
|
43
|
+
alternatively one single call just adding `true` as second parameter also dispatch:
|
|
48
44
|
``` js
|
|
49
|
-
store.
|
|
45
|
+
store.stage({
|
|
50
46
|
type: 'ADD',
|
|
51
47
|
payload: { number: 4 }
|
|
52
|
-
}, true /*
|
|
53
|
-
.then(console.log); // here we get
|
|
48
|
+
}, true /* autoDispatch */)
|
|
49
|
+
.then(console.log); // here we get the state
|
|
54
50
|
```
|
|
55
51
|
|
|
56
|
-
alternatively it is possible to directly
|
|
52
|
+
alternatively it is possible to directly dispatch the action:
|
|
57
53
|
``` js
|
|
58
|
-
store.
|
|
54
|
+
store.dispatch({
|
|
59
55
|
type: 'ADD',
|
|
60
56
|
payload: { number: 4 }},
|
|
61
57
|
)
|
|
@@ -132,11 +128,11 @@ Every store obtained invoking successfully `PANGjs.getStore` exposes the followi
|
|
|
132
128
|
|
|
133
129
|
### `storeInstance.getState() -> state`
|
|
134
130
|
|
|
135
|
-
returns the
|
|
131
|
+
returns the state
|
|
136
132
|
|
|
137
|
-
### `storeInstance.
|
|
133
|
+
### `storeInstance.stage(action, autoDispatch) -> Promise`
|
|
138
134
|
|
|
139
|
-
|
|
135
|
+
stage or stage&dispatch returning a promise resolving with new state (staged or not);
|
|
140
136
|
|
|
141
137
|
**Parameters**:
|
|
142
138
|
- **action**:
|
|
@@ -146,25 +142,25 @@ commit or commit&push returning a promise resolving with new state (pushed or no
|
|
|
146
142
|
payload: <Object>
|
|
147
143
|
}
|
|
148
144
|
```
|
|
149
|
-
-
|
|
145
|
+
- autoDispatch `<Boolean>` default `false`
|
|
150
146
|
|
|
151
147
|
|
|
152
148
|
|
|
153
|
-
### `storeInstance.
|
|
154
|
-
|
|
149
|
+
### `storeInstance.dispatch() -> Promise`
|
|
150
|
+
dispatch all the staged changes.
|
|
155
151
|
Only this operations calls subscribers.
|
|
156
152
|
|
|
157
|
-
Optionally it can recieve an action and that will be equivalent to
|
|
158
|
-
`s.
|
|
153
|
+
Optionally it can recieve an action and that will be equivalent to stage and dispatch:
|
|
154
|
+
`s.stage(action).then(() => s.dispatch())`
|
|
159
155
|
act as
|
|
160
|
-
`s.
|
|
156
|
+
`s.stage(action, true)`
|
|
161
157
|
and as
|
|
162
|
-
`s.
|
|
158
|
+
`s.dispatch(action))`
|
|
163
159
|
|
|
164
160
|
|
|
165
161
|
### `storeInstance.subscribe(fn) -> unsubscribing function`
|
|
166
162
|
|
|
167
|
-
allows to register a subscribing function that will be invoked everytime the state changes (
|
|
163
|
+
allows to register a subscribing function that will be invoked everytime the state changes (dispatched)
|
|
168
164
|
|
|
169
165
|
**returns**: the unsubscribing function
|
|
170
166
|
|
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.6
|
|
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);
|