@fedeghe/pangjs 0.0.1 → 0.0.2
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 +73 -35
- package/dist/index.js +25 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# PANGjs (0.0.
|
|
1
|
+
# PANGjs (0.0.2)
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
# Simple asynchronous state manager
|
|
@@ -14,11 +14,10 @@ All we need to do now is to:
|
|
|
14
14
|
- define an asynchronous reducer as:
|
|
15
15
|
``` js
|
|
16
16
|
const reducer = async (oldState, action, payload) => {
|
|
17
|
-
const res = await fetch(targetUrl)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
17
|
+
const res = await fetch(targetUrl);
|
|
18
|
+
//
|
|
19
|
+
// your updates
|
|
20
|
+
//
|
|
22
21
|
return newState
|
|
23
22
|
}
|
|
24
23
|
```
|
|
@@ -27,22 +26,19 @@ All we need to do now is to:
|
|
|
27
26
|
now it's time to get a store and use it:
|
|
28
27
|
|
|
29
28
|
|
|
30
|
-
``` js
|
|
31
|
-
const store = PANGjs.getStore(
|
|
32
|
-
reducer,
|
|
33
|
-
initState
|
|
34
|
-
);
|
|
29
|
+
``` js
|
|
30
|
+
const store = PANGjs.getStore( reducer, initState );
|
|
35
31
|
|
|
36
32
|
// commit, change it only internally
|
|
37
33
|
store.commit({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
34
|
+
type: 'ADD',
|
|
35
|
+
payload: { number: 4 }
|
|
36
|
+
})
|
|
37
|
+
.then(s => console.log(
|
|
38
|
+
'here we get the unpushed state:', s
|
|
39
|
+
));
|
|
40
|
+
|
|
41
|
+
// for the changes to be effective we have to push
|
|
46
42
|
store.push()
|
|
47
43
|
// here we get the pushed state
|
|
48
44
|
.then(console.log);
|
|
@@ -50,12 +46,19 @@ store.push()
|
|
|
50
46
|
alternatively one single call just adding `true` as second parameter (semantically is 'autocommit'):
|
|
51
47
|
``` js
|
|
52
48
|
store.commit({
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
49
|
+
type: 'ADD',
|
|
50
|
+
payload: { number: 4 }
|
|
51
|
+
}, true /* autocommit */)
|
|
52
|
+
.then(console.log); // here we get autopushed state
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
alternatively it is possible to directly push the action:
|
|
56
|
+
``` js
|
|
57
|
+
store.push({
|
|
58
|
+
type: 'ADD',
|
|
59
|
+
payload: { number: 4 }},
|
|
60
|
+
)
|
|
61
|
+
.then(console.log); // here we get the state
|
|
59
62
|
```
|
|
60
63
|
|
|
61
64
|
---
|
|
@@ -116,27 +119,62 @@ the resulting combined reducer
|
|
|
116
119
|
|
|
117
120
|
### `PANGjs.isStore(toBeChecked)`
|
|
118
121
|
|
|
119
|
-
Parameters
|
|
122
|
+
**Parameters**:
|
|
120
123
|
- **toBeChecked**:
|
|
121
124
|
what needs to be checked if it is a PANGjs store or not
|
|
122
125
|
|
|
123
126
|
---
|
|
124
127
|
|
|
125
|
-
# store
|
|
128
|
+
# store
|
|
126
129
|
|
|
127
|
-
Every store obtained invoking successfully `PANGjs.getStore` exposes the following
|
|
130
|
+
Every store obtained invoking successfully `PANGjs.getStore` exposes the following:
|
|
128
131
|
|
|
129
|
-
### `storeInstance.getState() -> state
|
|
132
|
+
### `storeInstance.getState() -> state`
|
|
130
133
|
|
|
131
|
-
|
|
134
|
+
returns the last pushed state
|
|
132
135
|
|
|
133
|
-
### `storeInstance.
|
|
134
|
-
|
|
136
|
+
### `storeInstance.commit(action, autoPush) -> Promise`
|
|
137
|
+
|
|
138
|
+
commit or commit&push returning a promise resolving with new state (pushed or not);
|
|
139
|
+
|
|
140
|
+
**Parameters**:
|
|
141
|
+
- **action**:
|
|
142
|
+
```js
|
|
143
|
+
{
|
|
144
|
+
type: <String>,
|
|
145
|
+
payload: <Object>
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
- autoPush `<Boolean>` default `false`
|
|
135
149
|
|
|
136
|
-
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
### `storeInstance.push() -> Promise`
|
|
153
|
+
push all the committed but unpushed changes.
|
|
154
|
+
Only this operations calls subscribers.
|
|
155
|
+
|
|
156
|
+
Optionally it can recieve an action and that will be equivalent to commit and push:
|
|
157
|
+
`s.commit(action).then(() => s.push())`
|
|
158
|
+
act as
|
|
159
|
+
`s.commit(action, true)`
|
|
160
|
+
and as
|
|
161
|
+
`s.push(action))`
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
### `storeInstance.subscribe(fn) -> unsubscribing function`
|
|
165
|
+
|
|
166
|
+
allows to register a subscribing function that will be invoked everytime the state changes (pushed)
|
|
167
|
+
|
|
168
|
+
**returns**: the unsubscribing function
|
|
137
169
|
|
|
138
170
|
### `storeInstance.replaceReducer(fn) -> store instance`
|
|
139
171
|
|
|
172
|
+
replace the store reducer
|
|
173
|
+
|
|
140
174
|
### `storeInstance.move(int) -> store instance`
|
|
141
175
|
|
|
142
|
-
|
|
176
|
+
in case the history is active allows to move in the states
|
|
177
|
+
|
|
178
|
+
### `storeInstance.reset() -> store instance`
|
|
179
|
+
|
|
180
|
+
reset the store to its initial state
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
/*
|
|
3
3
|
PANGjs
|
|
4
|
-
v. 0.0.
|
|
5
|
-
|
|
6
|
-
Size: ~3.
|
|
4
|
+
v. 0.0.2
|
|
5
|
+
|
|
6
|
+
Size: ~3.82KB
|
|
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.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.
|
|
18
|
-
|
|
19
|
-
this.HistoryManager.unpuhedStates=this.HistoryManager.states},
|
|
20
|
-
;var
|
|
21
|
-
;return e(h,r.REDUCERS_RETURN),this.previousAction=
|
|
22
|
-
|
|
23
|
-
;return this.subscribers.push(e),
|
|
24
|
-
this.HistoryManager.index!==this.HistoryManager.unpushedIndex||void 0===t||0===t)return this
|
|
25
|
-
;
|
|
26
|
-
|
|
27
|
-
function(t,
|
|
28
|
-
},Promise.
|
|
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 n(t,e){
|
|
9
|
+
if("number"!=typeof t)throw new Error(e)}function i(t,e){this.initState=t,this.states=[t],this.unpushedStates=[t],this.config=e,this.maxElements=Math.max(1,parseInt(this.config.maxElements,10))||1,
|
|
10
|
+
this.index=0,this.unpushedIndex=0}function s(e,n,s){this.reducer=e||o,t(this.reducer,r.REDUCERS_FUNCTION),this.initState=n||{},this.config=s||{},this.config.check=this.config.check||function(){
|
|
11
|
+
return!0},t(this.config.check,r.REDUCERS_FUNCTION),this.subscribers=[],this.previousAction="ORIGIN",this.HistoryManager=new i(this.initState,this.config)}var r={
|
|
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
|
+
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?"unpushedStates":"states"][this[t?"unpushedIndex":"index"]]},i.prototype.commit=function(t,e){
|
|
15
|
+
var n=this.unpushedStates.slice(0,this.unpushedIndex+1);return n.push(t),this.maxElements&&n.length>this.maxElements?n.shift():this.unpushedIndex++,this.unpushedStates=n,e&&this.push(),this},
|
|
16
|
+
i.prototype.push=function(){this.states=this.unpushedStates,this.index=this.unpushedIndex},i.prototype.reset=function(){this.index=0,this.states=[this.initState],this.unpushedIndex=0,
|
|
17
|
+
this.unpushedStates=this.unpushedStates.slice(0,1)};var o=function(){return Promise.resolve({})};return s.prototype.getState=function(t){return this.HistoryManager.top(t)},
|
|
18
|
+
s.prototype.uncommit=function(){if(1===this.HistoryManager.maxElements)return this;this.HistoryManager.unpushedIndex=this.HistoryManager.index,
|
|
19
|
+
this.HistoryManager.unpuhedStates=this.HistoryManager.states},s.prototype.commit=function(t,n){if(!("type"in t))return Promise.reject(new Error(r.ACTION_TYPE))
|
|
20
|
+
;var i=this,s=t.type,o=t.payload||{},u=this.getState(!0);if(!i.config.check(u,i.previousAction,s,o))return Promise.reject(new Error(r.UNAUTHORIZED_STATECHANGE));var h=this.reducer(u,s,o)
|
|
21
|
+
;return e(h,r.REDUCERS_RETURN),this.previousAction=s,h.then(function(t){return i.HistoryManager.commit(t,n),n&&i.emit(t),t})},s.prototype.emit=function(t){this.subscribers.filter(function(t){
|
|
22
|
+
return Boolean(t)}).forEach(function(e){e(t)})},s.prototype.push=function(t){if(t)return this.commit(t,!0);this.HistoryManager.push();var e=this.HistoryManager.top();return this.emit(e),
|
|
23
|
+
Promise.resolve(e)},s.prototype.subscribe=function(e){t(e,r.SUBSCRIBERS_FUNCTION);var n,i=this;return this.subscribers.push(e),n=this.subscribers.length-1,function(){i.subscribers[n]=null}},
|
|
24
|
+
s.prototype.move=function(t){if(n(t,r.MOVE_TO_NUMBER),1===this.HistoryManager.maxElements||this.HistoryManager.index!==this.HistoryManager.unpushedIndex||void 0===t||0===t)return this
|
|
25
|
+
;var e=this.HistoryManager.index+t,i=e>-1&&e<this.HistoryManager.states.length,s=i?e:this.HistoryManager.index;return this.HistoryManager.index=s,this.HistoryManager.unpushedIndex=s,this},
|
|
26
|
+
s.prototype.replaceReducer=function(e){return t(e,r.SUBSCRIBERS_FUNCTION),this.reducer=e,this},s.prototype.reset=function(){return this.HistoryManager.reset(),this.subscribers=[],this},{ERRORS:r,
|
|
27
|
+
getStore:function(t,e,n){return new s(t,e,n)},isStore:function(t){return t instanceof s},combine:function(e){return e.forEach(function(e){t(e,r.REDUCERS_FUNCTION)}),function(t,n,i){
|
|
28
|
+
var s=Object.assign({},t),r=e.length;return new Promise(function(t){return e.reduce(function(e,s,o){return e.then(function(e){return r-1===o?t(s(e,n,i)):s(e,n,i)})},Promise.resolve(s))})}}}}()
|
|
29
|
+
;"object"==typeof exports&&(module.exports=PANGjs);
|