@fedeghe/pangjs 0.0.1 → 0.0.3
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/.coveralls.yml +2 -0
- package/.travis.yml +7 -0
- package/README.md +74 -35
- package/dist/index.js +25 -24
- package/package.json +12 -3
package/.coveralls.yml
ADDED
package/.travis.yml
ADDED
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
# PANGjs (0.0.
|
|
1
|
+
# PANGjs (0.0.3)
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
|
+
[](https://coveralls.io/github/fedeghe/pangjs?branch=main)
|
|
5
6
|
|
|
6
7
|
# Simple asynchronous state manager
|
|
7
8
|
|
|
@@ -14,11 +15,10 @@ All we need to do now is to:
|
|
|
14
15
|
- define an asynchronous reducer as:
|
|
15
16
|
``` js
|
|
16
17
|
const reducer = async (oldState, action, payload) => {
|
|
17
|
-
const res = await fetch(targetUrl)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
18
|
+
const res = await fetch(targetUrl);
|
|
19
|
+
//
|
|
20
|
+
// your updates
|
|
21
|
+
//
|
|
22
22
|
return newState
|
|
23
23
|
}
|
|
24
24
|
```
|
|
@@ -27,22 +27,19 @@ All we need to do now is to:
|
|
|
27
27
|
now it's time to get a store and use it:
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
``` js
|
|
31
|
-
const store = PANGjs.getStore(
|
|
32
|
-
reducer,
|
|
33
|
-
initState
|
|
34
|
-
);
|
|
30
|
+
``` js
|
|
31
|
+
const store = PANGjs.getStore( reducer, initState );
|
|
35
32
|
|
|
36
33
|
// commit, change it only internally
|
|
37
34
|
store.commit({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
35
|
+
type: 'ADD',
|
|
36
|
+
payload: { number: 4 }
|
|
37
|
+
})
|
|
38
|
+
.then(s => console.log(
|
|
39
|
+
'here we get the unpushed state:', s
|
|
40
|
+
));
|
|
41
|
+
|
|
42
|
+
// for the changes to be effective we have to push
|
|
46
43
|
store.push()
|
|
47
44
|
// here we get the pushed state
|
|
48
45
|
.then(console.log);
|
|
@@ -50,12 +47,19 @@ store.push()
|
|
|
50
47
|
alternatively one single call just adding `true` as second parameter (semantically is 'autocommit'):
|
|
51
48
|
``` js
|
|
52
49
|
store.commit({
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
type: 'ADD',
|
|
51
|
+
payload: { number: 4 }
|
|
52
|
+
}, true /* autocommit */)
|
|
53
|
+
.then(console.log); // here we get autopushed state
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
alternatively it is possible to directly push the action:
|
|
57
|
+
``` js
|
|
58
|
+
store.push({
|
|
59
|
+
type: 'ADD',
|
|
60
|
+
payload: { number: 4 }},
|
|
61
|
+
)
|
|
62
|
+
.then(console.log); // here we get the state
|
|
59
63
|
```
|
|
60
64
|
|
|
61
65
|
---
|
|
@@ -116,27 +120,62 @@ the resulting combined reducer
|
|
|
116
120
|
|
|
117
121
|
### `PANGjs.isStore(toBeChecked)`
|
|
118
122
|
|
|
119
|
-
Parameters
|
|
123
|
+
**Parameters**:
|
|
120
124
|
- **toBeChecked**:
|
|
121
125
|
what needs to be checked if it is a PANGjs store or not
|
|
122
126
|
|
|
123
127
|
---
|
|
124
128
|
|
|
125
|
-
# store
|
|
129
|
+
# store
|
|
126
130
|
|
|
127
|
-
Every store obtained invoking successfully `PANGjs.getStore` exposes the following
|
|
131
|
+
Every store obtained invoking successfully `PANGjs.getStore` exposes the following:
|
|
128
132
|
|
|
129
|
-
### `storeInstance.getState() -> state
|
|
133
|
+
### `storeInstance.getState() -> state`
|
|
130
134
|
|
|
131
|
-
|
|
135
|
+
returns the last pushed state
|
|
132
136
|
|
|
133
|
-
### `storeInstance.
|
|
134
|
-
|
|
137
|
+
### `storeInstance.commit(action, autoPush) -> Promise`
|
|
138
|
+
|
|
139
|
+
commit or commit&push returning a promise resolving with new state (pushed or not);
|
|
140
|
+
|
|
141
|
+
**Parameters**:
|
|
142
|
+
- **action**:
|
|
143
|
+
```js
|
|
144
|
+
{
|
|
145
|
+
type: <String>,
|
|
146
|
+
payload: <Object>
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
- autoPush `<Boolean>` default `false`
|
|
135
150
|
|
|
136
|
-
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
### `storeInstance.push() -> Promise`
|
|
154
|
+
push all the committed but unpushed changes.
|
|
155
|
+
Only this operations calls subscribers.
|
|
156
|
+
|
|
157
|
+
Optionally it can recieve an action and that will be equivalent to commit and push:
|
|
158
|
+
`s.commit(action).then(() => s.push())`
|
|
159
|
+
act as
|
|
160
|
+
`s.commit(action, true)`
|
|
161
|
+
and as
|
|
162
|
+
`s.push(action))`
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
### `storeInstance.subscribe(fn) -> unsubscribing function`
|
|
166
|
+
|
|
167
|
+
allows to register a subscribing function that will be invoked everytime the state changes (pushed)
|
|
168
|
+
|
|
169
|
+
**returns**: the unsubscribing function
|
|
137
170
|
|
|
138
171
|
### `storeInstance.replaceReducer(fn) -> store instance`
|
|
139
172
|
|
|
173
|
+
replace the store reducer
|
|
174
|
+
|
|
140
175
|
### `storeInstance.move(int) -> store instance`
|
|
141
176
|
|
|
142
|
-
|
|
177
|
+
in case the history is active allows to move in the states
|
|
178
|
+
|
|
179
|
+
### `storeInstance.reset() -> store instance`
|
|
180
|
+
|
|
181
|
+
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.3
|
|
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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedeghe/pangjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"author": "fedeghe <fedeghe@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"dev": "malta source/buildev.json",
|
|
10
10
|
"test": "jest --coverage --rootDir=test",
|
|
11
11
|
"watch": "jest --watch --coverage --rootDir=test",
|
|
12
|
-
"cover": "jest --coverage --rootDir=test",
|
|
13
|
-
"coveralls": "jest --coverage --rootDir=test | coveralls"
|
|
12
|
+
"cover": "jest --coverage --rootDir=test --coverageReporters=text",
|
|
13
|
+
"coveralls": "jest --coverage --rootDir=test --coverageReporters=text && cat ./test/coverage/lcov.info | coveralls"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"coveralls": "^3.1.1",
|
|
@@ -24,6 +24,15 @@
|
|
|
24
24
|
"malta": "^4.1.41",
|
|
25
25
|
"malta-header-comment": "^1.0.12",
|
|
26
26
|
"malta-js-uglify": "^1.0.13",
|
|
27
|
+
"mocha-lcov-reporter": "^1.3.0",
|
|
27
28
|
"nyc": "^17.1.0"
|
|
29
|
+
},
|
|
30
|
+
"jest": {
|
|
31
|
+
"collectCoverage": true,
|
|
32
|
+
"coverageReporters": [
|
|
33
|
+
"json",
|
|
34
|
+
"lcov",
|
|
35
|
+
"html"
|
|
36
|
+
]
|
|
28
37
|
}
|
|
29
38
|
}
|