@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.
Files changed (3) hide show
  1. package/README.md +73 -35
  2. package/dist/index.js +25 -24
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # PANGjs (0.0.1)
1
+ # PANGjs (0.0.2)
2
2
 
3
- ![alt text](./pangjs.png "Pang js")
3
+ ![alt text](https://github.com/fedeghe/pangjs/blob/main/pangjs.png?raw=true "Pang js")
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
- newstate = {
19
- ...oldState,
20
- // your updates
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
- 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
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
- type: 'ADD',
54
- payload: { number: 4 }},
55
- true // autoCommit changes
56
- )
57
- // here we get autopushed state
58
- .then(console.log);
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 API
128
+ # store
126
129
 
127
- Every store obtained invoking successfully `PANGjs.getStore` exposes the following (more details will come):
130
+ Every store obtained invoking successfully `PANGjs.getStore` exposes the following:
128
131
 
129
- ### `storeInstance.getState() -> state (last pushed)`
132
+ ### `storeInstance.getState() -> state`
130
133
 
131
- ### `storeInstance.commit(action, autoPush) -> Promise resolved with new state (pushed or not)`
134
+ returns the last pushed state
132
135
 
133
- ### `storeInstance.push() -> Promise resolved with new pushed state`
134
- - calls subscribers
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
- ### `storeInstance.suubscribe(fn) -> unsubscribing function`
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
- ### `storeInstance.reset() -> store instance`
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.1
5
- 8:3:34
6
- Size: ~3.63KB
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 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);
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.1",
3
+ "version": "0.0.2",
4
4
  "main": "dist/index.js",
5
5
  "author": "fedeghe <fedeghe@gmail.com>",
6
6
  "license": "MIT",