Comparing version 0.2.3 to 0.3.0
@@ -35,3 +35,3 @@ 'use strict'; | ||
var MUTABLE_SOURCE = Symbol(); | ||
var VERSION = Symbol(); | ||
var LISTENERS = Symbol(); | ||
@@ -46,4 +46,3 @@ var SNAPSHOT = Symbol(); | ||
var snapshotCache = new WeakMap(); | ||
var createProxy = function createProxy(initialObject) { | ||
var proxy = function proxy(initialObject) { | ||
if (initialObject === void 0) { | ||
@@ -54,3 +53,2 @@ initialObject = {}; | ||
var version = globalVersion; | ||
var mutableSource; | ||
var listeners = new Set(); | ||
@@ -72,10 +70,6 @@ | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
var proxy = new Proxy(emptyCopy, { | ||
var p = new Proxy(emptyCopy, { | ||
get: function get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
return mutableSource; | ||
if (prop === VERSION) { | ||
return version; | ||
} | ||
@@ -94,6 +88,7 @@ | ||
var snapshot = Object.create(target.constructor.prototype); | ||
var _snapshot = Object.create(target.constructor.prototype); | ||
snapshotCache.set(receiver, { | ||
version: version, | ||
snapshot: snapshot | ||
snapshot: _snapshot | ||
}); | ||
@@ -104,5 +99,5 @@ Reflect.ownKeys(target).forEach(function (key) { | ||
if (!isObject(value)) { | ||
snapshot[key] = value; | ||
_snapshot[key] = value; | ||
} else if (value instanceof Promise) { | ||
Object.defineProperty(snapshot, key, { | ||
Object.defineProperty(_snapshot, key, { | ||
get: function get() { | ||
@@ -113,6 +108,6 @@ throw value; | ||
} else { | ||
snapshot[key] = value[SNAPSHOT]; | ||
_snapshot[key] = value[SNAPSHOT]; | ||
} | ||
}); | ||
return snapshot; | ||
return _snapshot; | ||
} | ||
@@ -155,3 +150,3 @@ | ||
} else { | ||
target[prop] = createProxy(value); | ||
target[prop] = proxy(value); | ||
} | ||
@@ -167,15 +162,27 @@ | ||
Reflect.ownKeys(initialObject).forEach(function (key) { | ||
proxy[key] = initialObject[key]; | ||
p[key] = initialObject[key]; | ||
}); | ||
return proxy; | ||
return p; | ||
}; | ||
var subscribe = function subscribe(proxy, callback) { | ||
proxy[LISTENERS].add(callback); | ||
var subscribe = function subscribe(p, callback) { | ||
p[LISTENERS].add(callback); | ||
return function () { | ||
proxy[LISTENERS].delete(callback); | ||
p[LISTENERS].delete(callback); | ||
}; | ||
}; | ||
var snapshot = function snapshot(p) { | ||
return p[SNAPSHOT]; | ||
}; | ||
var useProxy = function useProxy(proxy) { | ||
var mutableSourceCache = new WeakMap(); | ||
var getMutableSource = function getMutableSource(p) { | ||
if (!mutableSourceCache.has(p)) { | ||
mutableSourceCache.set(p, createMutableSource(p)); | ||
} | ||
return mutableSourceCache.get(p); | ||
}; | ||
var useProxy = function useProxy(p) { | ||
var affected = new WeakMap(); | ||
@@ -186,20 +193,20 @@ var lastAffected = react.useRef(); | ||
}); | ||
var getSnapshot = react.useMemo(function () { | ||
var getChangedSnapshot = react.useMemo(function () { | ||
var prevSnapshot = null; | ||
var deepChangedCache = new WeakMap(); | ||
return function (proxy) { | ||
var snapshot = proxy[SNAPSHOT]; | ||
return function (p) { | ||
var nextSnapshot = snapshot(p); | ||
try { | ||
if (prevSnapshot !== null && lastAffected.current && !proxyCompare.isDeepChanged(prevSnapshot, snapshot, lastAffected.current, deepChangedCache)) { | ||
if (prevSnapshot !== null && lastAffected.current && !proxyCompare.isDeepChanged(prevSnapshot, nextSnapshot, lastAffected.current, deepChangedCache)) { | ||
// not changed | ||
return prevSnapshot; | ||
} | ||
} catch (e) {// ignore and return new snapshot | ||
} catch (e) {// ignore and return new nextSnapshot | ||
} | ||
return prevSnapshot = snapshot; | ||
return prevSnapshot = nextSnapshot; | ||
}; | ||
}, []); | ||
var snapshot = useMutableSource(proxy[MUTABLE_SOURCE], getSnapshot, subscribe); | ||
var currSnapshot = useMutableSource(getMutableSource(p), getChangedSnapshot, subscribe); | ||
var proxyCache = react.useMemo(function () { | ||
@@ -209,7 +216,8 @@ return new WeakMap(); | ||
return proxyCompare.createDeepProxy(snapshot, affected, proxyCache); | ||
return proxyCompare.createDeepProxy(currSnapshot, affected, proxyCache); | ||
}; | ||
exports.proxy = createProxy; | ||
exports.proxy = proxy; | ||
exports.snapshot = snapshot; | ||
exports.subscribe = subscribe; | ||
exports.useProxy = useProxy; |
@@ -1,4 +0,3 @@ | ||
declare const createProxy: <T extends object>(initialObject?: T) => T; | ||
declare const subscribe: (proxy: any, callback: () => void) => () => void; | ||
declare const useProxy: <T extends object>(proxy: T) => T; | ||
export { createProxy as proxy, useProxy, subscribe }; | ||
import { proxy, subscribe, snapshot } from './vanilla'; | ||
declare const useProxy: <T extends object>(p: T) => T; | ||
export { proxy, subscribe, snapshot, useProxy }; |
@@ -30,3 +30,3 @@ var valtio = (function (exports, react, proxyCompare, useSubscription) { | ||
var MUTABLE_SOURCE = Symbol(); | ||
var VERSION = Symbol(); | ||
var LISTENERS = Symbol(); | ||
@@ -41,4 +41,3 @@ var SNAPSHOT = Symbol(); | ||
var snapshotCache = new WeakMap(); | ||
var createProxy = function createProxy(initialObject) { | ||
var proxy = function proxy(initialObject) { | ||
if (initialObject === void 0) { | ||
@@ -49,3 +48,2 @@ initialObject = {}; | ||
var version = globalVersion; | ||
var mutableSource; | ||
var listeners = new Set(); | ||
@@ -67,10 +65,6 @@ | ||
var emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
var proxy = new Proxy(emptyCopy, { | ||
var p = new Proxy(emptyCopy, { | ||
get: function get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
return mutableSource; | ||
if (prop === VERSION) { | ||
return version; | ||
} | ||
@@ -89,6 +83,7 @@ | ||
var snapshot = Object.create(target.constructor.prototype); | ||
var _snapshot = Object.create(target.constructor.prototype); | ||
snapshotCache.set(receiver, { | ||
version: version, | ||
snapshot: snapshot | ||
snapshot: _snapshot | ||
}); | ||
@@ -99,5 +94,5 @@ Reflect.ownKeys(target).forEach(function (key) { | ||
if (!isObject(value)) { | ||
snapshot[key] = value; | ||
_snapshot[key] = value; | ||
} else if (value instanceof Promise) { | ||
Object.defineProperty(snapshot, key, { | ||
Object.defineProperty(_snapshot, key, { | ||
get: function get() { | ||
@@ -108,6 +103,6 @@ throw value; | ||
} else { | ||
snapshot[key] = value[SNAPSHOT]; | ||
_snapshot[key] = value[SNAPSHOT]; | ||
} | ||
}); | ||
return snapshot; | ||
return _snapshot; | ||
} | ||
@@ -150,3 +145,3 @@ | ||
} else { | ||
target[prop] = createProxy(value); | ||
target[prop] = proxy(value); | ||
} | ||
@@ -162,15 +157,27 @@ | ||
Reflect.ownKeys(initialObject).forEach(function (key) { | ||
proxy[key] = initialObject[key]; | ||
p[key] = initialObject[key]; | ||
}); | ||
return proxy; | ||
return p; | ||
}; | ||
var subscribe = function subscribe(proxy, callback) { | ||
proxy[LISTENERS].add(callback); | ||
var subscribe = function subscribe(p, callback) { | ||
p[LISTENERS].add(callback); | ||
return function () { | ||
proxy[LISTENERS].delete(callback); | ||
p[LISTENERS].delete(callback); | ||
}; | ||
}; | ||
var snapshot = function snapshot(p) { | ||
return p[SNAPSHOT]; | ||
}; | ||
var useProxy = function useProxy(proxy) { | ||
var mutableSourceCache = new WeakMap(); | ||
var getMutableSource = function getMutableSource(p) { | ||
if (!mutableSourceCache.has(p)) { | ||
mutableSourceCache.set(p, createMutableSource(p)); | ||
} | ||
return mutableSourceCache.get(p); | ||
}; | ||
var useProxy = function useProxy(p) { | ||
var affected = new WeakMap(); | ||
@@ -181,20 +188,20 @@ var lastAffected = react.useRef(); | ||
}); | ||
var getSnapshot = react.useMemo(function () { | ||
var getChangedSnapshot = react.useMemo(function () { | ||
var prevSnapshot = null; | ||
var deepChangedCache = new WeakMap(); | ||
return function (proxy) { | ||
var snapshot = proxy[SNAPSHOT]; | ||
return function (p) { | ||
var nextSnapshot = snapshot(p); | ||
try { | ||
if (prevSnapshot !== null && lastAffected.current && !proxyCompare.isDeepChanged(prevSnapshot, snapshot, lastAffected.current, deepChangedCache)) { | ||
if (prevSnapshot !== null && lastAffected.current && !proxyCompare.isDeepChanged(prevSnapshot, nextSnapshot, lastAffected.current, deepChangedCache)) { | ||
// not changed | ||
return prevSnapshot; | ||
} | ||
} catch (e) {// ignore and return new snapshot | ||
} catch (e) {// ignore and return new nextSnapshot | ||
} | ||
return prevSnapshot = snapshot; | ||
return prevSnapshot = nextSnapshot; | ||
}; | ||
}, []); | ||
var snapshot = useMutableSource(proxy[MUTABLE_SOURCE], getSnapshot, subscribe); | ||
var currSnapshot = useMutableSource(getMutableSource(p), getChangedSnapshot, subscribe); | ||
var proxyCache = react.useMemo(function () { | ||
@@ -204,6 +211,7 @@ return new WeakMap(); | ||
return proxyCompare.createDeepProxy(snapshot, affected, proxyCache); | ||
return proxyCompare.createDeepProxy(currSnapshot, affected, proxyCache); | ||
}; | ||
exports.proxy = createProxy; | ||
exports.proxy = proxy; | ||
exports.snapshot = snapshot; | ||
exports.subscribe = subscribe; | ||
@@ -210,0 +218,0 @@ exports.useProxy = useProxy; |
60
index.js
@@ -23,3 +23,3 @@ import { useMemo, useRef, useEffect } from 'react'; | ||
const MUTABLE_SOURCE = Symbol(); | ||
const VERSION = Symbol(); | ||
const LISTENERS = Symbol(); | ||
@@ -32,6 +32,4 @@ const SNAPSHOT = Symbol(); | ||
const snapshotCache = new WeakMap(); | ||
const createProxy = (initialObject = {}) => { | ||
const proxy = (initialObject = {}) => { | ||
let version = globalVersion; | ||
let mutableSource; | ||
const listeners = new Set(); | ||
@@ -51,10 +49,6 @@ | ||
const emptyCopy = Array.isArray(initialObject) ? [] : Object.create(initialObject.constructor.prototype); | ||
const proxy = new Proxy(emptyCopy, { | ||
const p = new Proxy(emptyCopy, { | ||
get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
return mutableSource; | ||
if (prop === VERSION) { | ||
return version; | ||
} | ||
@@ -134,3 +128,3 @@ | ||
} else { | ||
target[prop] = createProxy(value); | ||
target[prop] = proxy(value); | ||
} | ||
@@ -147,15 +141,25 @@ | ||
Reflect.ownKeys(initialObject).forEach(key => { | ||
proxy[key] = initialObject[key]; | ||
p[key] = initialObject[key]; | ||
}); | ||
return proxy; | ||
return p; | ||
}; | ||
const subscribe = (proxy, callback) => { | ||
proxy[LISTENERS].add(callback); | ||
const subscribe = (p, callback) => { | ||
p[LISTENERS].add(callback); | ||
return () => { | ||
proxy[LISTENERS].delete(callback); | ||
p[LISTENERS].delete(callback); | ||
}; | ||
}; | ||
const snapshot = p => p[SNAPSHOT]; | ||
const useProxy = proxy => { | ||
const mutableSourceCache = new WeakMap(); | ||
const getMutableSource = p => { | ||
if (!mutableSourceCache.has(p)) { | ||
mutableSourceCache.set(p, createMutableSource(p)); | ||
} | ||
return mutableSourceCache.get(p); | ||
}; | ||
const useProxy = p => { | ||
const affected = new WeakMap(); | ||
@@ -166,25 +170,25 @@ const lastAffected = useRef(); | ||
}); | ||
const getSnapshot = useMemo(() => { | ||
const getChangedSnapshot = useMemo(() => { | ||
let prevSnapshot = null; | ||
const deepChangedCache = new WeakMap(); | ||
return proxy => { | ||
const snapshot = proxy[SNAPSHOT]; | ||
return p => { | ||
const nextSnapshot = snapshot(p); | ||
try { | ||
if (prevSnapshot !== null && lastAffected.current && !isDeepChanged(prevSnapshot, snapshot, lastAffected.current, deepChangedCache)) { | ||
if (prevSnapshot !== null && lastAffected.current && !isDeepChanged(prevSnapshot, nextSnapshot, lastAffected.current, deepChangedCache)) { | ||
// not changed | ||
return prevSnapshot; | ||
} | ||
} catch (e) {// ignore and return new snapshot | ||
} catch (e) {// ignore and return new nextSnapshot | ||
} | ||
return prevSnapshot = snapshot; | ||
return prevSnapshot = nextSnapshot; | ||
}; | ||
}, []); | ||
const snapshot = useMutableSource(proxy[MUTABLE_SOURCE], getSnapshot, subscribe); | ||
const currSnapshot = useMutableSource(getMutableSource(p), getChangedSnapshot, subscribe); | ||
const proxyCache = useMemo(() => new WeakMap(), []); // per-hook proxyCache | ||
return createDeepProxy(snapshot, affected, proxyCache); | ||
return createDeepProxy(currSnapshot, affected, proxyCache); | ||
}; | ||
export { createProxy as proxy, subscribe, useProxy }; | ||
export { proxy, snapshot, subscribe, useProxy }; |
{ | ||
"name": "valtio", | ||
"private": false, | ||
"version": "0.2.3", | ||
"description": "💊 Valtio makes proxy-state simple ", | ||
"version": "0.3.0", | ||
"description": "💊 Valtio makes proxy-state simple for React and Vanilla", | ||
"main": "index.cjs.js", | ||
@@ -34,3 +34,3 @@ "module": "index.js", | ||
"dependencies": { | ||
"proxy-compare": "1.0.3", | ||
"proxy-compare": "1.1.0", | ||
"use-subscription": "1.5.1" | ||
@@ -37,0 +37,0 @@ }, |
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
27171
11
750
+ Addedproxy-compare@1.1.0(transitive)
- Removedproxy-compare@1.0.3(transitive)
Updatedproxy-compare@1.1.0