react-wire-persisted 1.0.0
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/LICENSE +21 -0
- package/README.md +111 -0
- package/lib/react-wire-persisted.js +1 -0
- package/lib/utils/index.js +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Wesley Bliss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Persisted React Wire
|
|
2
|
+
|
|
3
|
+
> Persisted storage for [React Wire](https://github.com/forminator/react-wire)
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
$ yarn add -D @forminator/react-wire react-wire-persisted
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
// constants.js
|
|
15
|
+
import { key, getPrefixedKeys } from 'react-wire-persisted/lib/utils'
|
|
16
|
+
|
|
17
|
+
export const NS = 'myapp'
|
|
18
|
+
|
|
19
|
+
key('foo')
|
|
20
|
+
|
|
21
|
+
const prefixedKeys = getPrefixedKeys(NS)
|
|
22
|
+
|
|
23
|
+
export { prefixedKeys as keys }
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
// index.js
|
|
28
|
+
import { NS } from './constants'
|
|
29
|
+
import * as reactPersistedWire from 'react-wire-persisted'
|
|
30
|
+
|
|
31
|
+
reactPersistedWire.setNamespace(NS)
|
|
32
|
+
|
|
33
|
+
// Normal React init code
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
// store.js
|
|
38
|
+
import { createPersistedWire } from 'react-wire-persisted'
|
|
39
|
+
import { keys } from './constants'
|
|
40
|
+
|
|
41
|
+
export const foo = createPersistedWire(keys.foo, null)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
// SomeComponent.js
|
|
46
|
+
import { useWireState } from '@forminator/react-wire'
|
|
47
|
+
import * as store from './store'
|
|
48
|
+
|
|
49
|
+
const SomeComponent = () => {
|
|
50
|
+
const [foo, setFoo] = useWireState(store.foo)
|
|
51
|
+
return (
|
|
52
|
+
<div>
|
|
53
|
+
<p>Foo: {foo}</p>
|
|
54
|
+
<button onClick={() => setFoo('bar')}>
|
|
55
|
+
CHANGE FOO
|
|
56
|
+
</button>
|
|
57
|
+
</div>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default SomeComponent
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
See the [demo](demo) folder for a more complete example.
|
|
65
|
+
|
|
66
|
+
## Storage Providers
|
|
67
|
+
|
|
68
|
+
This library uses `localStorage`, and will only work in browser environments.
|
|
69
|
+
|
|
70
|
+
See [LocalStorageProvider](src/LocalStorageProvider.js) for implementation.
|
|
71
|
+
|
|
72
|
+
## Miscellaneous
|
|
73
|
+
|
|
74
|
+
For generating some sample data:
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import nickGenerator from 'nick-generator'
|
|
78
|
+
|
|
79
|
+
export const categories = [
|
|
80
|
+
'Entrepeneurs',
|
|
81
|
+
'Investors',
|
|
82
|
+
'Superheroes',
|
|
83
|
+
'Engineers',
|
|
84
|
+
'Chefs',
|
|
85
|
+
'Performers',
|
|
86
|
+
'Musicians',
|
|
87
|
+
].reduce((acc, it, i) => [
|
|
88
|
+
...acc,
|
|
89
|
+
{
|
|
90
|
+
id: (i + 1),
|
|
91
|
+
name: it,
|
|
92
|
+
}
|
|
93
|
+
], [])
|
|
94
|
+
|
|
95
|
+
export const people = Object.values(categories).map(category => {
|
|
96
|
+
|
|
97
|
+
return Array(8).fill(null).map((_, i) => {
|
|
98
|
+
|
|
99
|
+
const [firstName, lastName] = nickGenerator().split(' ')
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
id: (i + 1),
|
|
103
|
+
categoryId: category.id,
|
|
104
|
+
firstName,
|
|
105
|
+
lastName,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
}).flat()
|
|
111
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,(function(){return(()=>{"use strict";var e={449:(e,t,r)=>{r.r(t),r.d(t,{addKey:()=>s,getKeys:()=>v,getPrefixedKeys:()=>p,isPrimitive:()=>y,key:()=>f});const n=require("@babel/runtime/helpers/typeof");var o=r.n(n),i=r(779),a=r.n(i);function c(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function l(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?c(Object(r),!0).forEach((function(t){a()(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):c(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var u={},s=function(e){u[e]=e},f=function(e){return s(e)},v=function(){return u},p=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,r=t||u;return e?Object.keys(r).reduce((function(t,n){return l(l({},t),{},a()({},n,"".concat(e,".").concat(r[n])))}),{}):r},y=function(e){var t=o()(e);return!Array.isArray(e)&&("object"===t?null===e:"function"!==t)}},779:e=>{e.exports=require("@babel/runtime/helpers/defineProperty")}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,r),i.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};return(()=>{r.r(n),r.d(n,{createPersistedWire:()=>A,getNamespace:()=>k,getStorage:()=>w,setNamespace:()=>I,utils:()=>e});var e=r(449),t=r(779),o=r.n(t);const i=require("@forminator/react-wire"),a=require("@babel/runtime/helpers/slicedToArray");var c=r.n(a);const l=require("@babel/runtime/helpers/classCallCheck");var u=r.n(l);const s=require("@babel/runtime/helpers/createClass");var f=r.n(s);const v=require("@babel/runtime/helpers/inherits");var p=r.n(v);const y=require("@babel/runtime/helpers/possibleConstructorReturn");var g=r.n(y);const h=require("@babel/runtime/helpers/getPrototypeOf");var b=r.n(h);function m(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var r,n=b()(e);if(t){var o=b()(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return g()(this,r)}}var d=function(t){p()(n,t);var r=m(n);function n(){var e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(u()(this,n),e=r.call(this,t,o),"undefined"==typeof localStorage)throw new Error("LocalStorageProvider: localStorage not supported");return e}return f()(n,[{key:"setNamespace",value:function(e){if(this.namespace){if(this.namespace!==e){var t=JSON.parse(JSON.stringify(this.getAll()));this.removeAll();for(var r=0,n=Object.entries(t);r<n.length;r++){var o=c()(n[r],2),i=o[0],a=o[1],l=i.replace(this.namespace,e);this.setItem(l,a)}this.namespace=e}}else this.namespace=e}},{key:"getItem",value:function(e){var t=localStorage.getItem(e);if(null==t)return null;try{return JSON.parse(t)}catch(e){return t}}},{key:"setItem",value:function(t,r){var n=r;return null!=n&&(n=(0,e.isPrimitive)(r)?r:JSON.stringify(r)),localStorage.setItem(t,n)}},{key:"removeItem",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return t&&delete this.registry[e],localStorage.removeItem(e)}},{key:"getAll",value:function(){var e=this,t="".concat(this.namespace,".");return Object.keys(localStorage).reduce((function(r,n){return e.namespace&&!n.startsWith(t)||(r[n]=localStorage.getItem(n)),r}),{})}},{key:"_resetAll",value:function(){var e=this,t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],o="".concat(this.namespace,".");Object.keys(localStorage).forEach((function(i){var a=!e.namespace||i.startsWith(o),c=(null==r?void 0:r.includes(i))||!1;a&&!c&&(t?Object.prototype.hasOwnProperty.call(e.registry,i)?localStorage.setItem(i,e.registry[i]):localStorage.removeItem(i):(localStorage.removeItem(i),n&&delete e.registry[i]))}))}},{key:"resetAll",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this._resetAll(!0,e||[],t)}},{key:"removeAll",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this._resetAll(!1,e||[],t)}}]),n}(function(){function e(t,r){if(u()(this,e),(this instanceof e?this.constructor:void 0)===e)throw TypeError("StorageProvider is abstract. Extend this class to implement it");this.namespace=t||null,this.registry=r||{}}return f()(e,[{key:"setNamespace",value:function(e){}},{key:"register",value:function(e,t){this.registry[e]=t}},{key:"getItem",value:function(e){}},{key:"setItem",value:function(e,t){}},{key:"removeItem",value:function(e){}},{key:"getAll",value:function(){}},{key:"_resetAll",value:function(){}},{key:"resetAll",value:function(){}},{key:"removeAll",value:function(){}}]),e}());function O(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function j(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?O(Object(r),!0).forEach((function(t){o()(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):O(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var P=d,S=new P,k=function(){var e;return(null===(e=S)||void 0===e?void 0:e.namespace)||null},w=function(){return S},I=function(e){S.setNamespace(e),S=new P(e||k())},A=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(!e&&"number"!=typeof e)throw new Error("createPersistedWire: Key cannot be a falsey value (".concat(e,"}"));S.register(e,t);var r=(0,i.createWire)(t),n=function(){return r.getValue()},o=function(t){return S.setItem(e,t),r.setValue(t)},a=function(e){r.subscribe(e)},c=S.getItem(e),l=!1!==c&&(c||t);return l!==t&&o(l),j(j({},r),{},{getValue:n,setValue:o,subscribe:a})}})(),n})()}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var t=r();for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,(function(){return(()=>{"use strict";var e={779:e=>{e.exports=require("@babel/runtime/helpers/defineProperty")}},r={};function t(n){var o=r[n];if(void 0!==o)return o.exports;var i=r[n]={exports:{}};return e[n](i,i.exports,t),i.exports}t.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},t.d=(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},t.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};return(()=>{t.r(n),t.d(n,{addKey:()=>p,getKeys:()=>s,getPrefixedKeys:()=>l,isPrimitive:()=>y,key:()=>a});const e=require("@babel/runtime/helpers/typeof");var r=t.n(e),o=t(779),i=t.n(o);function c(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function u(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?c(Object(t),!0).forEach((function(r){i()(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):c(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var f={},p=function(e){f[e]=e},a=function(e){return p(e)},s=function(){return f},l=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=r||f;return e?Object.keys(t).reduce((function(r,n){return u(u({},r),{},i()({},n,"".concat(e,".").concat(t[n])))}),{}):t},y=function(e){var t=r()(e);return!Array.isArray(e)&&("object"===t?null===e:"function"!==t)}})(),n})()}));
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-wire-persisted",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Wesley Bliss <wesley.bliss@gmail.com>",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "lib/react-wire-persisted.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "NODE_ENV=development webpack serve --config config/webpack.config.js --progress",
|
|
12
|
+
"build": "NODE_ENV=production webpack --config config/webpack.config.js --progress",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"test:watch": "jest --watch",
|
|
15
|
+
"test:watch:all": "jest --watchAll",
|
|
16
|
+
"test:watch:coverage": "jest --watch --collectCoverage",
|
|
17
|
+
"test:snapshots": "jest --updateSnapshot",
|
|
18
|
+
"test:coverage": "jest --collectCoverage",
|
|
19
|
+
"test:ci": "jest --runInBand"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/core": "^7.17.5",
|
|
24
|
+
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
25
|
+
"@babel/preset-env": "^7.16.11",
|
|
26
|
+
"@babel/preset-react": "^7.16.7",
|
|
27
|
+
"@forminator/react-wire": "^0.5.0-alpha.1",
|
|
28
|
+
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.4",
|
|
29
|
+
"@testing-library/jest-dom": "^5.16.2",
|
|
30
|
+
"@testing-library/react": "^12.1.3",
|
|
31
|
+
"babel-jest": "^27.5.1",
|
|
32
|
+
"babel-loader": "^8.2.3",
|
|
33
|
+
"browserslist": "^4.19.3",
|
|
34
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
35
|
+
"css-loader": "^6.6.0",
|
|
36
|
+
"dotenv-webpack": "^7.1.0",
|
|
37
|
+
"esbuild": "^0.14.23",
|
|
38
|
+
"html-webpack-plugin": "^5.5.0",
|
|
39
|
+
"interpolate-html-plugin": "^4.0.0",
|
|
40
|
+
"jest": "^27.5.1",
|
|
41
|
+
"react": "^17.0.2",
|
|
42
|
+
"react-dom": "^17.0.2",
|
|
43
|
+
"snapshot-diff": "^0.9.0",
|
|
44
|
+
"style-loader": "^3.3.1",
|
|
45
|
+
"webpack": "^5.69.1",
|
|
46
|
+
"webpack-bundle-analyzer": "^4.5.0",
|
|
47
|
+
"webpack-cli": "^4.9.2",
|
|
48
|
+
"webpack-dev-server": "^4.7.4",
|
|
49
|
+
"webpack-node-externals": "^3.0.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@forminator/react-wire": "^0.5.0-alpha.1",
|
|
53
|
+
"react": "^17.0.2",
|
|
54
|
+
"react-dom": "^17.0.2"
|
|
55
|
+
},
|
|
56
|
+
"browserslist": {
|
|
57
|
+
"production": [
|
|
58
|
+
">0.2%",
|
|
59
|
+
"not dead",
|
|
60
|
+
"not op_mini all"
|
|
61
|
+
],
|
|
62
|
+
"development": [
|
|
63
|
+
"last 1 chrome version",
|
|
64
|
+
"last 1 firefox version",
|
|
65
|
+
"last 1 safari version"
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
}
|