Comparing version 0.3.0 to 0.3.1
@@ -43,5 +43,8 @@ 'use strict'; | ||
var proxyCache = new WeakMap(); | ||
var globalVersion = 0; | ||
var snapshotCache = new WeakMap(); | ||
var proxy = function proxy(initialObject) { | ||
var _initialObject$constr; | ||
if (initialObject === void 0) { | ||
@@ -67,3 +70,3 @@ initialObject = {}; | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(((_initialObject$constr = initialObject.constructor) == null ? void 0 : _initialObject$constr.prototype) || null); | ||
var p = new Proxy(emptyCopy, { | ||
@@ -138,8 +141,8 @@ get: function get(target, prop, receiver) { | ||
} else { | ||
var _getUntrackedObject; | ||
value = proxyCompare.getUntrackedObject(value) || value; | ||
value = (_getUntrackedObject = proxyCompare.getUntrackedObject(value)) != null ? _getUntrackedObject : value; | ||
if (value[LISTENERS]) { | ||
target[prop] = value; | ||
} else if (proxyCache.has(value)) { | ||
target[prop] = proxyCache.get(value); | ||
} else { | ||
@@ -156,2 +159,3 @@ target[prop] = proxy(value); | ||
}); | ||
proxyCache.set(initialObject, p); | ||
Reflect.ownKeys(initialObject).forEach(function (key) { | ||
@@ -158,0 +162,0 @@ p[key] = initialObject[key]; |
@@ -38,5 +38,8 @@ var valtio = (function (exports, react, proxyCompare, useSubscription) { | ||
var proxyCache = new WeakMap(); | ||
var globalVersion = 0; | ||
var snapshotCache = new WeakMap(); | ||
var proxy = function proxy(initialObject) { | ||
var _initialObject$constr; | ||
if (initialObject === void 0) { | ||
@@ -62,3 +65,3 @@ initialObject = {}; | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(((_initialObject$constr = initialObject.constructor) == null ? void 0 : _initialObject$constr.prototype) || null); | ||
var p = new Proxy(emptyCopy, { | ||
@@ -133,8 +136,8 @@ get: function get(target, prop, receiver) { | ||
} else { | ||
var _getUntrackedObject; | ||
value = proxyCompare.getUntrackedObject(value) || value; | ||
value = (_getUntrackedObject = proxyCompare.getUntrackedObject(value)) != null ? _getUntrackedObject : value; | ||
if (value[LISTENERS]) { | ||
target[prop] = value; | ||
} else if (proxyCache.has(value)) { | ||
target[prop] = proxyCache.get(value); | ||
} else { | ||
@@ -151,2 +154,3 @@ target[prop] = proxy(value); | ||
}); | ||
proxyCache.set(initialObject, p); | ||
Reflect.ownKeys(initialObject).forEach(function (key) { | ||
@@ -153,0 +157,0 @@ p[key] = initialObject[key]; |
12
index.js
@@ -29,5 +29,8 @@ import { useMemo, useRef, useEffect } from 'react'; | ||
const proxyCache = new WeakMap(); | ||
let globalVersion = 0; | ||
const snapshotCache = new WeakMap(); | ||
const proxy = (initialObject = {}) => { | ||
var _initialObject$constr; | ||
let version = globalVersion; | ||
@@ -47,3 +50,3 @@ const listeners = new Set(); | ||
const emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
const emptyCopy = Array.isArray(initialObject) ? [] : Object.create(((_initialObject$constr = initialObject.constructor) == null ? void 0 : _initialObject$constr.prototype) || null); | ||
const p = new Proxy(emptyCopy, { | ||
@@ -120,8 +123,8 @@ get(target, prop, receiver) { | ||
} else { | ||
var _getUntrackedObject; | ||
value = getUntrackedObject(value) || value; | ||
value = (_getUntrackedObject = getUntrackedObject(value)) != null ? _getUntrackedObject : value; | ||
if (value[LISTENERS]) { | ||
target[prop] = value; | ||
} else if (proxyCache.has(value)) { | ||
target[prop] = proxyCache.get(value); | ||
} else { | ||
@@ -139,2 +142,3 @@ target[prop] = proxy(value); | ||
}); | ||
proxyCache.set(initialObject, p); | ||
Reflect.ownKeys(initialObject).forEach(key => { | ||
@@ -141,0 +145,0 @@ p[key] = initialObject[key]; |
{ | ||
"name": "valtio", | ||
"private": false, | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "💊 Valtio makes proxy-state simple for React and Vanilla", | ||
@@ -6,0 +6,0 @@ "main": "index.cjs.js", |
@@ -51,3 +51,5 @@ ![](/readme.svg) | ||
// Suscribe to all state changes | ||
const unsubscribe = subscribe(state, () => console.log(`state has changed to ${state}`)) | ||
const unsubscribe = subscribe(state, () => | ||
console.log(`state has changed to ${state}`) | ||
) | ||
// Unsubscribe by calling the result | ||
@@ -59,4 +61,25 @@ unsubscribe() | ||
### Suspense out of the box | ||
```js | ||
const state = create({ post: fetch(url).then((res) => res.json()) }) | ||
function Post() { | ||
const snapshot = useProxy(state) | ||
// Valtio suspends promises, access async data directly | ||
return <div>{snapshot.post.title}</div> | ||
} | ||
function App() { | ||
return ( | ||
<Suspense fallback={<span>waiting...</span>}> | ||
<Post /> | ||
</Suspense> | ||
) | ||
} | ||
``` | ||
**And that's it!** | ||
</details> |
@@ -15,5 +15,8 @@ 'use strict'; | ||
var proxyCache = new WeakMap(); | ||
var globalVersion = 0; | ||
var snapshotCache = new WeakMap(); | ||
var proxy = function proxy(initialObject) { | ||
var _initialObject$constr; | ||
if (initialObject === void 0) { | ||
@@ -39,3 +42,3 @@ initialObject = {}; | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(((_initialObject$constr = initialObject.constructor) == null ? void 0 : _initialObject$constr.prototype) || null); | ||
var p = new Proxy(emptyCopy, { | ||
@@ -110,8 +113,8 @@ get: function get(target, prop, receiver) { | ||
} else { | ||
var _getUntrackedObject; | ||
value = proxyCompare.getUntrackedObject(value) || value; | ||
value = (_getUntrackedObject = proxyCompare.getUntrackedObject(value)) != null ? _getUntrackedObject : value; | ||
if (value[LISTENERS]) { | ||
target[prop] = value; | ||
} else if (proxyCache.has(value)) { | ||
target[prop] = proxyCache.get(value); | ||
} else { | ||
@@ -128,2 +131,3 @@ target[prop] = proxy(value); | ||
}); | ||
proxyCache.set(initialObject, p); | ||
Reflect.ownKeys(initialObject).forEach(function (key) { | ||
@@ -130,0 +134,0 @@ p[key] = initialObject[key]; |
@@ -9,5 +9,8 @@ import { getUntrackedObject } from 'proxy-compare'; | ||
const proxyCache = new WeakMap(); | ||
let globalVersion = 0; | ||
const snapshotCache = new WeakMap(); | ||
const proxy = (initialObject = {}) => { | ||
var _initialObject$constr; | ||
let version = globalVersion; | ||
@@ -27,3 +30,3 @@ const listeners = new Set(); | ||
const emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
const emptyCopy = Array.isArray(initialObject) ? [] : Object.create(((_initialObject$constr = initialObject.constructor) == null ? void 0 : _initialObject$constr.prototype) || null); | ||
const p = new Proxy(emptyCopy, { | ||
@@ -100,8 +103,8 @@ get(target, prop, receiver) { | ||
} else { | ||
var _getUntrackedObject; | ||
value = getUntrackedObject(value) || value; | ||
value = (_getUntrackedObject = getUntrackedObject(value)) != null ? _getUntrackedObject : value; | ||
if (value[LISTENERS]) { | ||
target[prop] = value; | ||
} else if (proxyCache.has(value)) { | ||
target[prop] = proxyCache.get(value); | ||
} else { | ||
@@ -119,2 +122,3 @@ target[prop] = proxy(value); | ||
}); | ||
proxyCache.set(initialObject, p); | ||
Reflect.ownKeys(initialObject).forEach(key => { | ||
@@ -121,0 +125,0 @@ p[key] = initialObject[key]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28468
770
84