@fedeghe/pangjs 0.0.4 → 0.0.5

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 +29 -32
  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.5)
2
2
 
3
3
  ![alt text](https://github.com/fedeghe/pangjs/blob/main/pangjs.png?raw=true "Pang js")
4
4
 
@@ -10,28 +10,25 @@ 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({
30
+ // stage the results only internally
31
+ store.stage({
35
32
  type: 'ADD',
36
33
  payload: { number: 4 }
37
34
  })
@@ -39,23 +36,23 @@ store.commit({
39
36
  'here we get the unpushed state:', s
40
37
  ));
41
38
 
42
- // for the changes to be effective we have to push
43
- store.push()
39
+ // make all staged changes effective
40
+ store.dispatch()
44
41
  // here we get the pushed state
45
42
  .then(console.log);
46
43
  ```
47
- alternatively one single call just adding `true` as second parameter (semantically is 'autocommit'):
44
+ alternatively one single call just adding `true` as second parameter also dispatch:
48
45
  ``` js
49
- store.commit({
46
+ store.stage({
50
47
  type: 'ADD',
51
48
  payload: { number: 4 }
52
- }, true /* autocommit */)
49
+ }, true /* autoDispatch */)
53
50
  .then(console.log); // here we get autopushed state
54
51
  ```
55
52
 
56
53
  alternatively it is possible to directly push the action:
57
54
  ``` js
58
- store.push({
55
+ store.dispatch({
59
56
  type: 'ADD',
60
57
  payload: { number: 4 }},
61
58
  )
@@ -134,9 +131,9 @@ Every store obtained invoking successfully `PANGjs.getStore` exposes the followi
134
131
 
135
132
  returns the last pushed state
136
133
 
137
- ### `storeInstance.commit(action, autoPush) -> Promise`
134
+ ### `storeInstance.stage(action, autoPush) -> Promise`
138
135
 
139
- commit or commit&push returning a promise resolving with new state (pushed or not);
136
+ stage or stage&dispatch returning a promise resolving with new state (staged or not);
140
137
 
141
138
  **Parameters**:
142
139
  - **action**:
@@ -150,16 +147,16 @@ commit or commit&push returning a promise resolving with new state (pushed or no
150
147
 
151
148
 
152
149
 
153
- ### `storeInstance.push() -> Promise`
154
- push all the committed but unpushed changes.
150
+ ### `storeInstance.dispatch() -> Promise`
151
+ dispatch all the staged changes.
155
152
  Only this operations calls subscribers.
156
153
 
157
- Optionally it can recieve an action and that will be equivalent to commit and push:
158
- `s.commit(action).then(() => s.push())`
154
+ Optionally it can recieve an action and that will be equivalent to stage and dispatch:
155
+ `s.stage(action).then(() => s.dispatch())`
159
156
  act as
160
- `s.commit(action, true)`
157
+ `s.stage(action, true)`
161
158
  and as
162
- `s.push(action))`
159
+ `s.dispatch(action))`
163
160
 
164
161
 
165
162
  ### `storeInstance.subscribe(fn) -> unsubscribing function`
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.5
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.5",
4
4
  "main": "dist/index.js",
5
5
  "author": "fedeghe <fedeghe@gmail.com>",
6
6
  "description": "Lightweight asynchronous state manager",