@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 ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis
2
+ repo_token: s70zTJdyYdnFJD63dVSg9iCovgQ80azIz
package/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ install: yarn
2
+ language: node_js
3
+ script: yarn test
4
+ after_success:
5
+ - yarn coveralls
6
+ node_js:
7
+ - "10.12.0"
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
- # PANGjs (0.0.1)
1
+ # PANGjs (0.0.3)
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
+ [![Coverage Status](https://coveralls.io/repos/github/fedeghe/pangjs/badge.svg?branch=main)](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
- newstate = {
19
- ...oldState,
20
- // your updates
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
- 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
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
- type: 'ADD',
54
- payload: { number: 4 }},
55
- true // autoCommit changes
56
- )
57
- // here we get autopushed state
58
- .then(console.log);
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 API
129
+ # store
126
130
 
127
- Every store obtained invoking successfully `PANGjs.getStore` exposes the following (more details will come):
131
+ Every store obtained invoking successfully `PANGjs.getStore` exposes the following:
128
132
 
129
- ### `storeInstance.getState() -> state (last pushed)`
133
+ ### `storeInstance.getState() -> state`
130
134
 
131
- ### `storeInstance.commit(action, autoPush) -> Promise resolved with new state (pushed or not)`
135
+ returns the last pushed state
132
136
 
133
- ### `storeInstance.push() -> Promise resolved with new pushed state`
134
- - calls subscribers
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
- ### `storeInstance.suubscribe(fn) -> unsubscribing function`
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
- ### `storeInstance.reset() -> store instance`
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.1
5
- 8:3:34
6
- Size: ~3.63KB
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 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.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
  }