@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.
Files changed (3) hide show
  1. package/README.md +41 -45
  2. package/dist/index.js +21 -21
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # PANGjs (0.0.4)
1
+ # PANGjs (0.0.6)
2
2
 
3
3
  ![alt text](https://github.com/fedeghe/pangjs/blob/main/pangjs.png?raw=true "Pang js")
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
- All we need to do now is to:
15
- - define an asynchronous reducer as:
16
- ``` js
17
- const reducer = async (oldState, action, payload) => {
18
- const res = await fetch(targetUrl);
19
- //
20
- // your updates
21
- //
22
- return newState
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
- // commit, change it only internally
34
- store.commit({
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
43
- store.push()
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 (semantically is 'autocommit'):
43
+ alternatively one single call just adding `true` as second parameter also dispatch:
48
44
  ``` js
49
- store.commit({
45
+ store.stage({
50
46
  type: 'ADD',
51
47
  payload: { number: 4 }
52
- }, true /* autocommit */)
53
- .then(console.log); // here we get autopushed state
48
+ }, true /* autoDispatch */)
49
+ .then(console.log); // here we get the state
54
50
  ```
55
51
 
56
- alternatively it is possible to directly push the action:
52
+ alternatively it is possible to directly dispatch the action:
57
53
  ``` js
58
- store.push({
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 last pushed state
131
+ returns the state
136
132
 
137
- ### `storeInstance.commit(action, autoPush) -> Promise`
133
+ ### `storeInstance.stage(action, autoDispatch) -> Promise`
138
134
 
139
- commit or commit&push returning a promise resolving with new state (pushed or not);
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
- - autoPush `<Boolean>` default `false`
145
+ - autoDispatch `<Boolean>` default `false`
150
146
 
151
147
 
152
148
 
153
- ### `storeInstance.push() -> Promise`
154
- push all the committed but unpushed changes.
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 commit and push:
158
- `s.commit(action).then(() => s.push())`
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.commit(action, true)`
156
+ `s.stage(action, true)`
161
157
  and as
162
- `s.push(action))`
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 (pushed)
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
4
+ v. 0.0.6
5
5
 
6
- Size: ~3.82KB
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 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={
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?"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))})}}}}()
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedeghe/pangjs",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "dist/index.js",
5
5
  "author": "fedeghe <fedeghe@gmail.com>",
6
6
  "description": "Lightweight asynchronous state manager",