@fedeghe/pangjs 0.0.1
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 +142 -0
- package/dist/index.js +28 -0
- package/package.json +29 -0
- package/pangjs.png +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# PANGjs (0.0.1)
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Simple asynchronous state manager
|
|
7
|
+
|
|
8
|
+
install it
|
|
9
|
+
``` sh
|
|
10
|
+
> npm install @fedeghe/pangjs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
All we need to do now is to:
|
|
14
|
+
- define an asynchronous reducer as:
|
|
15
|
+
``` js
|
|
16
|
+
const reducer = async (oldState, action, payload) => {
|
|
17
|
+
const res = await fetch(targetUrl),
|
|
18
|
+
newstate = {
|
|
19
|
+
...oldState,
|
|
20
|
+
// your updates
|
|
21
|
+
}
|
|
22
|
+
return newState
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
now it's time to get a store and use it:
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
``` js
|
|
31
|
+
const store = PANGjs.getStore(
|
|
32
|
+
reducer,
|
|
33
|
+
initState
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// commit, change it only internally
|
|
37
|
+
store.commit({
|
|
38
|
+
type: 'ADD',
|
|
39
|
+
payload: { number: 4 }
|
|
40
|
+
})
|
|
41
|
+
.then(s => console.log(
|
|
42
|
+
'here we get the unpushed state:', s
|
|
43
|
+
));
|
|
44
|
+
|
|
45
|
+
// to push the changes to be effective we have to push
|
|
46
|
+
store.push()
|
|
47
|
+
// here we get the pushed state
|
|
48
|
+
.then(console.log);
|
|
49
|
+
```
|
|
50
|
+
alternatively one single call just adding `true` as second parameter (semantically is 'autocommit'):
|
|
51
|
+
``` js
|
|
52
|
+
store.commit({
|
|
53
|
+
type: 'ADD',
|
|
54
|
+
payload: { number: 4 }},
|
|
55
|
+
true // autoCommit changes
|
|
56
|
+
)
|
|
57
|
+
// here we get autopushed state
|
|
58
|
+
.then(console.log);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
# API
|
|
64
|
+
|
|
65
|
+
### `PANGjs.getStore(reducer, initialState, config) -> store`
|
|
66
|
+
|
|
67
|
+
**Parameters**:
|
|
68
|
+
- `reducer`:
|
|
69
|
+
the `reducer` function expected to have the following signature:
|
|
70
|
+
|
|
71
|
+
`(oldState, actionType, payload) => Promise`
|
|
72
|
+
|
|
73
|
+
where the returning promise resolves with the updated state.
|
|
74
|
+
|
|
75
|
+
- `initialState`:
|
|
76
|
+
optional object representing the initial state needed; when not provided it will be just an empty object.
|
|
77
|
+
|
|
78
|
+
- `config`: this is an optional object allowing to change some default behaviors:
|
|
79
|
+
- maxElements (default 1):
|
|
80
|
+
by default no history is available, but if here we pass a number bigger than one, for example 5 then we can navigate the state back up to 5 steps using the `move` function to the store.
|
|
81
|
+
- check (default no check):
|
|
82
|
+
here we can pass a synchrhonous function expected to have to following signature:
|
|
83
|
+
```
|
|
84
|
+
(
|
|
85
|
+
state,
|
|
86
|
+
currentAction,
|
|
87
|
+
previousAction,
|
|
88
|
+
payload
|
|
89
|
+
) -> <Boolean>
|
|
90
|
+
```
|
|
91
|
+
allowing to prevent a state change under some circumstances; that decision can be made based on the `state`, the ongoing `currentAction` which we might block, the `previousAction` and the current `payload`. Only if we return `true`, the reducer action will be run.
|
|
92
|
+
|
|
93
|
+
**Returns**:
|
|
94
|
+
the `store` instance
|
|
95
|
+
|
|
96
|
+
**Throws**:
|
|
97
|
+
`[ERROR] Reducer should return something!` in case the passed reducer is not a function
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
### `PANGjs.combine([reducer, ...])`
|
|
102
|
+
|
|
103
|
+
Synchronous function to combine 2 or more reducers.
|
|
104
|
+
|
|
105
|
+
**Parameters**:
|
|
106
|
+
- **[reducer, ...]**:
|
|
107
|
+
two or more reducers to be combined
|
|
108
|
+
|
|
109
|
+
**Returns**:
|
|
110
|
+
the resulting combined reducer
|
|
111
|
+
|
|
112
|
+
**Throws**:
|
|
113
|
+
`[ERROR] Reducer must be a function!` in case one of the parameters is not a function
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### `PANGjs.isStore(toBeChecked)`
|
|
118
|
+
|
|
119
|
+
Parameters:
|
|
120
|
+
- **toBeChecked**:
|
|
121
|
+
what needs to be checked if it is a PANGjs store or not
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
# store API
|
|
126
|
+
|
|
127
|
+
Every store obtained invoking successfully `PANGjs.getStore` exposes the following (more details will come):
|
|
128
|
+
|
|
129
|
+
### `storeInstance.getState() -> state (last pushed)`
|
|
130
|
+
|
|
131
|
+
### `storeInstance.commit(action, autoPush) -> Promise resolved with new state (pushed or not)`
|
|
132
|
+
|
|
133
|
+
### `storeInstance.push() -> Promise resolved with new pushed state`
|
|
134
|
+
- calls subscribers
|
|
135
|
+
|
|
136
|
+
### `storeInstance.suubscribe(fn) -> unsubscribing function`
|
|
137
|
+
|
|
138
|
+
### `storeInstance.replaceReducer(fn) -> store instance`
|
|
139
|
+
|
|
140
|
+
### `storeInstance.move(int) -> store instance`
|
|
141
|
+
|
|
142
|
+
### `storeInstance.reset() -> store instance`
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
PANGjs
|
|
4
|
+
v. 0.0.1
|
|
5
|
+
8:3:34
|
|
6
|
+
Size: ~3.63KB
|
|
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 s(t,e){
|
|
9
|
+
if("number"!=typeof t)throw new Error(e)}function n(t,e){this.states=[t||{}],this.unpushedStates=[t||{}],this.config=e||{},this.maxElements=this.config.maxElements||!1,
|
|
10
|
+
this.maxElements=Math.max(0,parseInt(this.config.maxElements,0))||!1,this.index=0,this.unpushedIndex=0}function i(e,s,i){this.reducer=e||o,t(this.reducer,r.REDUCERS_FUNCTION),this.initState=s||{},
|
|
11
|
+
this.config=i||{},this.config.check=this.config.check||function(){return!0},t(this.config.check,r.REDUCERS_FUNCTION),this.subscribers=[],this.previousAction="ORIGIN",
|
|
12
|
+
this.HistoryManager=new n(this.initState,this.config)}var r={REDUCERS_FUNCTION:"[ERROR] Reducer must be a function!",REDUCERS_RETURN:"[ERROR] Reducer should return a promise!",
|
|
13
|
+
REDUCERS_ASYNC:"[ERROR] Reducer should be asynchronous!",SUBSCRIBERS_FUNCTION:"[ERROR] Subscribers must be a functions!",ACTION_TYPE:"[ERROR] Actions needs a type",
|
|
14
|
+
UNAUTHORIZED_STATECHANGE:"[ERROR] State transition not allowed",MOVE_TO_NUMBER:"[ERROR] Move requires a number"};n.prototype.top=function(t){
|
|
15
|
+
return this[t?"unpushedStates":"states"][this[t?"unpushedIndex":"index"]]},n.prototype.commit=function(t,e){var s=this.unpushedStates.slice(0,this.unpushedIndex+1);return s.push(t),
|
|
16
|
+
this.maxElements&&s.length>this.maxElements?s.shift():this.unpushedIndex++,this.unpushedStates=s,e&&this.push(),this},n.prototype.push=function(){this.states=this.unpushedStates,
|
|
17
|
+
this.index=this.unpushedIndex},n.prototype.reset=function(){this.index=0,this.states=this.states.slice(0,1),this.unpushedIndex=0,this.unpushedStates=this.unpushedStates.slice(0,1)};var o=function(){
|
|
18
|
+
return Promise.resolve({})};return i.prototype.getState=function(t){return this.HistoryManager.top(t)},i.prototype.uncommit=function(){this.HistoryManager.unpushedIndex=this.HistoryManager.index,
|
|
19
|
+
this.HistoryManager.unpuhedStates=this.HistoryManager.states},i.prototype.commit=function(t,s){if(!("type"in t))return Promise.reject(r.ACTION_TYPE)
|
|
20
|
+
;var n=this,i=t.type,o=t.payload||{},u=this.getState(!0);if(!n.config.check(u,n.previousAction,i,o))return Promise.reject(r.UNAUTHORIZED_STATECHANGE);var h=this.reducer(u,i,o)
|
|
21
|
+
;return e(h,r.REDUCERS_RETURN),this.previousAction=i,h.then(function(t){return n.HistoryManager.commit(t,s),t})},i.prototype.push=function(){this.HistoryManager.push();var t=this.HistoryManager.top()
|
|
22
|
+
;return this.subscribers.filter(function(t){return Boolean(t)}).forEach(function(e){e(t)}),Promise.resolve(t)},i.prototype.subscribe=function(e){t(e,r.SUBSCRIBERS_FUNCTION);var s,n=this
|
|
23
|
+
;return this.subscribers.push(e),s=this.subscribers.length-1,function(){n.subscribers[s]=null}},i.prototype.move=function(t){if(s(t,r.MOVE_TO_NUMBER),
|
|
24
|
+
this.HistoryManager.index!==this.HistoryManager.unpushedIndex||void 0===t||0===t)return this;var e=this.HistoryManager.index+t,n=e>-1&&e<this.HistoryManager.states.length,i=n?e:this.currentIndex
|
|
25
|
+
;return this.HistoryManager.index=i,this.HistoryManager.unpushedIndex=i,this},i.prototype.replaceReducer=function(t){this.reducer=t||o},i.prototype.reset=function(){this.HistoryManager.reset(),
|
|
26
|
+
this.subscribers=[]},{ERRORS:r,getStore:function(t,e,s){return new i(t,e,s)},isStore:function(t){return t instanceof i},combine:function(e){return e.forEach(function(e){t(e,r.REDUCERS_FUNCTION)}),
|
|
27
|
+
function(t,s,n){t=t||initState;var i=Object.assign({},t),r=e.length;return new Promise(function(t){return e.reduce(function(e,i,o){return e.then(function(e){return r-1===o?t(i(e,s,n)):i(e,s,n)})
|
|
28
|
+
},Promise.resolve(i))})}}}}();"object"==typeof exports&&(module.exports=PANGjs);
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedeghe/pangjs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"author": "fedeghe <fedeghe@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "malta source/build.json",
|
|
9
|
+
"dev": "malta source/buildev.json",
|
|
10
|
+
"test": "jest --coverage --rootDir=test",
|
|
11
|
+
"watch": "jest --watch --coverage --rootDir=test",
|
|
12
|
+
"cover": "jest --coverage --rootDir=test",
|
|
13
|
+
"coveralls": "jest --coverage --rootDir=test | coveralls"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"coveralls": "^3.1.1",
|
|
17
|
+
"eslint": "^9.17.0",
|
|
18
|
+
"eslint-config-standard": "^17.1.0",
|
|
19
|
+
"eslint-plugin-import": "^2.31.0",
|
|
20
|
+
"eslint-plugin-node": "^11.1.0",
|
|
21
|
+
"eslint-plugin-promise": "^7.2.1",
|
|
22
|
+
"eslint-plugin-standard": "^5.0.0",
|
|
23
|
+
"jest": "^29.7.0",
|
|
24
|
+
"malta": "^4.1.41",
|
|
25
|
+
"malta-header-comment": "^1.0.12",
|
|
26
|
+
"malta-js-uglify": "^1.0.13",
|
|
27
|
+
"nyc": "^17.1.0"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/pangjs.png
ADDED
|
Binary file
|