Comparing version 1.1.3 to 1.2.0
@@ -6,4 +6,4 @@ import { useRef, useState, useEffect, useReducer, useCallback, useMemo, useLayoutEffect, useDebugValue } from 'react'; | ||
const TARGET = Symbol(); | ||
const GET_VERSION = Symbol(); | ||
const TARGET = "_uMS_T"; | ||
const GET_VERSION = "_uMS_V"; | ||
const createMutableSource = (target, getVersion) => ({ | ||
@@ -14,3 +14,3 @@ [TARGET]: target, | ||
const useMutableSource = (source, getSnapshot, subscribe) => { | ||
const lastVersion = useRef(0); | ||
const lastVersion = useRef(); | ||
const currentVersion = source[GET_VERSION](source[TARGET]); | ||
@@ -17,0 +17,0 @@ const [state, setState] = useState(() => [ |
@@ -1,2 +0,9 @@ | ||
export declare const createMutableSource: (target: any, getVersion: any) => any; | ||
export declare const useMutableSource: (source: any, getSnapshot: any, subscribe: any) => any; | ||
declare const TARGET = "_uMS_T"; | ||
declare const GET_VERSION = "_uMS_V"; | ||
declare type MutableSource<T, V> = { | ||
[TARGET]: T; | ||
[GET_VERSION]: (target: T) => V; | ||
}; | ||
export declare const createMutableSource: <T, V>(target: T, getVersion: (target: T) => V) => MutableSource<T, V>; | ||
export declare const useMutableSource: <T, V, S>(source: MutableSource<T, V>, getSnapshot: (target: T) => S, subscribe: (target: T, callback: () => void) => () => void) => S; | ||
export {}; |
export { subscribeKey } from './utils/subscribeKey'; | ||
export { watch } from './utils/watch'; | ||
export { devtools } from './utils/devtools'; | ||
export { addComputed } from './utils/addComputed'; | ||
export { derive, underive, unstable_getDeriveSubscriptions, } from './utils/derive'; | ||
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed'; | ||
export { proxyWithComputed } from './utils/proxyWithComputed'; | ||
export { proxyWithHistory } from './utils/proxyWithHistory'; |
154
esm/utils.js
@@ -1,2 +0,2 @@ | ||
import { subscribe, snapshot, proxy, ref } from 'valtio/vanilla'; | ||
import { subscribe, snapshot, proxy, getVersion, ref } from 'valtio/vanilla'; | ||
import { createProxy, isChanged } from 'proxy-compare'; | ||
@@ -115,39 +115,131 @@ | ||
const addComputed = (proxyObject, computedFns, targetObject = proxyObject) => { | ||
Object.keys(computedFns).forEach((key) => { | ||
if (Object.getOwnPropertyDescriptor(targetObject, key)) { | ||
const subscriptionsCache = new WeakMap(); | ||
const getSubscriptions = (proxyObject) => { | ||
let subscriptions = subscriptionsCache.get(proxyObject); | ||
if (!subscriptions) { | ||
subscriptions = new Map(); | ||
subscriptionsCache.set(proxyObject, subscriptions); | ||
} | ||
return subscriptions; | ||
}; | ||
const unstable_getDeriveSubscriptions = getSubscriptions; | ||
const derive = (derivedFns, options) => { | ||
const proxyObject = (options == null ? void 0 : options.proxy) || proxy({}); | ||
const notifyInSync = options == null ? void 0 : options.sync; | ||
const subscriptions = getSubscriptions(proxyObject); | ||
const addSubscription = (p, key, callback) => { | ||
const subscription = subscriptions.get(p); | ||
if (subscription) { | ||
subscription[0].set(key, callback); | ||
} else { | ||
const unsubscribe = subscribe(p, (ops) => { | ||
var _a; | ||
if (p === proxyObject && ops.every((op) => op[1].length === 1 && op[1][0] in derivedFns)) { | ||
return; | ||
} | ||
(_a = subscriptions.get(p)) == null ? void 0 : _a[0].forEach((cb) => { | ||
cb(); | ||
}); | ||
}, notifyInSync); | ||
subscriptions.set(p, [new Map([[key, callback]]), unsubscribe]); | ||
} | ||
}; | ||
const removeSubscription = (p, key) => { | ||
const subscription = subscriptions.get(p); | ||
if (subscription) { | ||
const [callbackMap, unsubscribe] = subscription; | ||
callbackMap.delete(key); | ||
if (!callbackMap.size) { | ||
unsubscribe(); | ||
subscriptions.delete(p); | ||
} | ||
} | ||
}; | ||
Object.keys(derivedFns).forEach((key) => { | ||
if (Object.getOwnPropertyDescriptor(proxyObject, key)) { | ||
throw new Error("object property already defined"); | ||
} | ||
const get = computedFns[key]; | ||
let prevSnapshot; | ||
let affected = new WeakMap(); | ||
let pending = false; | ||
const callback = () => { | ||
const nextSnapshot = snapshot(proxyObject); | ||
if (!pending && (!prevSnapshot || isChanged(prevSnapshot, nextSnapshot, affected))) { | ||
affected = new WeakMap(); | ||
const value = get(createProxy(nextSnapshot, affected)); | ||
prevSnapshot = nextSnapshot; | ||
if (value instanceof Promise) { | ||
pending = true; | ||
value.then((v) => { | ||
targetObject[key] = v; | ||
}).catch((e) => { | ||
targetObject[key] = new Proxy({}, { | ||
get() { | ||
throw e; | ||
} | ||
}); | ||
}).finally(() => { | ||
pending = false; | ||
}); | ||
const fn = derivedFns[key]; | ||
let lastDependencies = null; | ||
const evaluate = () => { | ||
if (lastDependencies) { | ||
if (Array.from(lastDependencies).every(([p, n]) => getVersion(p) === n)) { | ||
return; | ||
} | ||
targetObject[key] = value; | ||
} | ||
const dependencies = new Map(); | ||
const get = (p) => { | ||
dependencies.set(p, getVersion(p)); | ||
return p; | ||
}; | ||
const value = fn(get); | ||
const subscribe2 = () => { | ||
dependencies.forEach((_, p) => { | ||
if (!(lastDependencies == null ? void 0 : lastDependencies.has(p))) { | ||
addSubscription(p, key, evaluate); | ||
} | ||
}); | ||
lastDependencies == null ? void 0 : lastDependencies.forEach((_, p) => { | ||
if (!dependencies.has(p)) { | ||
removeSubscription(p, key); | ||
} | ||
}); | ||
lastDependencies = dependencies; | ||
}; | ||
if (value instanceof Promise) { | ||
value.then(() => { | ||
subscribe2(); | ||
evaluate(); | ||
}); | ||
} else { | ||
subscribe2(); | ||
} | ||
proxyObject[key] = value; | ||
}; | ||
subscribe(proxyObject, callback); | ||
callback(); | ||
evaluate(); | ||
}); | ||
return proxyObject; | ||
}; | ||
const underive = (proxyObject, options) => { | ||
const subscriptions = getSubscriptions(proxyObject); | ||
const keysToDelete = (options == null ? void 0 : options.delete) ? new Set() : null; | ||
subscriptions.forEach(([callbackMap, unsubscribe], p) => { | ||
if (options == null ? void 0 : options.keys) { | ||
options.keys.forEach((key) => { | ||
if (callbackMap.has(key)) { | ||
callbackMap.delete(key); | ||
if (keysToDelete) { | ||
keysToDelete.add(key); | ||
} | ||
} | ||
}); | ||
} else { | ||
if (keysToDelete) { | ||
Array.from(callbackMap.keys()).forEach((key) => { | ||
keysToDelete.add(key); | ||
}); | ||
} | ||
callbackMap.clear(); | ||
} | ||
if (!callbackMap.size) { | ||
unsubscribe(); | ||
subscriptions.delete(p); | ||
} | ||
}); | ||
if (keysToDelete) { | ||
keysToDelete.forEach((key) => { | ||
delete proxyObject[key]; | ||
}); | ||
} | ||
}; | ||
const addComputed_DEPRECATED = (proxyObject, computedFns_FAKE, targetObject = proxyObject) => { | ||
console.warn("addComputed is deprecated. Please consider using `derive` or `proxyWithComputed` instead. Falling back to emulation with derive."); | ||
const derivedFns = {}; | ||
Object.keys(computedFns_FAKE).forEach((key) => { | ||
derivedFns[key] = (get) => computedFns_FAKE[key](get(proxyObject)); | ||
}); | ||
return derive(derivedFns, { proxy: targetObject }); | ||
}; | ||
const proxyWithComputed = (initialObject, computedFns) => { | ||
@@ -219,2 +311,2 @@ Object.keys(computedFns).forEach((key) => { | ||
export { addComputed, devtools, proxyWithComputed, proxyWithHistory, subscribeKey, watch }; | ||
export { addComputed_DEPRECATED as addComputed, derive, devtools, proxyWithComputed, proxyWithHistory, subscribeKey, underive, unstable_getDeriveSubscriptions, watch }; |
@@ -1,22 +0,6 @@ | ||
import type { DeepResolveType } from '../vanilla'; | ||
/** | ||
* addComputed | ||
* addComputed (DEPRECATED) | ||
* | ||
* This adds computed values to an existing proxy object. | ||
* | ||
* [Notes] | ||
* This comes with a cost and overlaps with useSnapshot. | ||
* Do not try to optimize too early. It can worsen the performance. | ||
* Measurement and comparison will be very important. | ||
* | ||
* @example | ||
* import { proxy } from 'valtio' | ||
* import { addComputed } from 'valtio/utils' | ||
* const state = proxy({ | ||
* count: 1, | ||
* }) | ||
* addComputed(state, { | ||
* doubled: snap => snap.count * 2, | ||
* }) | ||
* @deprecated Please consider using `derive` or `proxyWithComputed` instead. | ||
*/ | ||
export declare const addComputed: <T extends object, U extends object>(proxyObject: T, computedFns: { [K in keyof U]: (snap: DeepResolveType<T>) => U[K]; }, targetObject?: any) => void; | ||
export declare const addComputed_DEPRECATED: <T extends object, U extends object>(proxyObject: T, computedFns_FAKE: { [K in keyof U]: (snap_FAKE: T) => U[K]; }, targetObject?: any) => any; |
@@ -9,4 +9,4 @@ 'use strict'; | ||
var TARGET = Symbol(); | ||
var GET_VERSION = Symbol(); | ||
var TARGET = '_uMS_T'; | ||
var GET_VERSION = '_uMS_V'; | ||
var createMutableSource = function createMutableSource(target, getVersion) { | ||
@@ -18,3 +18,3 @@ var _ref; | ||
var useMutableSource = function useMutableSource(source, getSnapshot, subscribe) { | ||
var lastVersion = react.useRef(0); | ||
var lastVersion = react.useRef(); | ||
var currentVersion = source[GET_VERSION](source[TARGET]); | ||
@@ -21,0 +21,0 @@ |
{ | ||
"name": "valtio", | ||
"private": false, | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "💊 Valtio makes proxy-state simple for React and Vanilla", | ||
@@ -6,0 +6,0 @@ "main": "./index.js", |
@@ -246,17 +246,8 @@ <img src="logo.svg" alt="valtio"> | ||
#### Computed values | ||
#### `derive` util | ||
You can have computed values with dependency tracking. | ||
Dependency tracking in valtio conflicts with the work in useSnapshot. | ||
React users should consider using render functions (optionally useMemo) | ||
as a primary mean. | ||
Computed works well for some edge cases and for vanilla-js users. | ||
You can subscribe to some proxies and create a derived proxy. | ||
##### `addComputed` | ||
This is to add new computed to an existing proxy state. | ||
It can add computed to different proxy state. | ||
```js | ||
import { addComputed } from 'valtio/utils' | ||
import { derive } from 'valtio/utils' | ||
@@ -268,22 +259,21 @@ // create a base proxy | ||
// add computed to state | ||
addComputed(state, { | ||
doubled: snap => snap.count * 2, | ||
// create a derived proxy | ||
const derived = derive({ | ||
doubled: (get) => get(state).count * 2, | ||
}) | ||
// create another proxy | ||
const state2 = proxy({ | ||
text: 'hello', | ||
// alternatively, attach derived properties to an existing proxy | ||
derive({ | ||
tripled: (get) => get(state).count * 3, | ||
}, { | ||
proxy: state, | ||
}) | ||
// add computed from state to state2 | ||
addComputed(state, { | ||
doubled: snap => snap.count * 2, | ||
}, state2) | ||
``` | ||
##### `proxyWithComputed` | ||
#### `proxyWithComputed` | ||
This is to create a proxy state with computed at the same time. | ||
It can define setters optionally. | ||
You can have computed values with dependency tracking with property access. | ||
Dependency tracking in `proxyWithComputed` conflicts with the work in `useSnapshot`. | ||
React users should prefer using `derive`. | ||
`proxyWithComputed` works well for some edge cases and for vanilla-js users. | ||
@@ -290,0 +280,0 @@ ```js |
@@ -1,2 +0,9 @@ | ||
export declare const createMutableSource: (target: any, getVersion: any) => any; | ||
export declare const useMutableSource: (source: any, getSnapshot: any, subscribe: any) => any; | ||
declare const TARGET = "_uMS_T"; | ||
declare const GET_VERSION = "_uMS_V"; | ||
declare type MutableSource<T, V> = { | ||
[TARGET]: T; | ||
[GET_VERSION]: (target: T) => V; | ||
}; | ||
export declare const createMutableSource: <T, V>(target: T, getVersion: (target: T) => V) => MutableSource<T, V>; | ||
export declare const useMutableSource: <T, V, S>(source: MutableSource<T, V>, getSnapshot: (target: T) => S, subscribe: (target: T, callback: () => void) => () => void) => S; | ||
export {}; |
export { subscribeKey } from './utils/subscribeKey'; | ||
export { watch } from './utils/watch'; | ||
export { devtools } from './utils/devtools'; | ||
export { addComputed } from './utils/addComputed'; | ||
export { derive, underive, unstable_getDeriveSubscriptions, } from './utils/derive'; | ||
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed'; | ||
export { proxyWithComputed } from './utils/proxyWithComputed'; | ||
export { proxyWithHistory } from './utils/proxyWithHistory'; |
@@ -1,24 +0,8 @@ | ||
import { DeepResolveType } from '../vanilla'; | ||
/** | ||
* addComputed | ||
* addComputed (DEPRECATED) | ||
* | ||
* This adds computed values to an existing proxy object. | ||
* | ||
* [Notes] | ||
* This comes with a cost and overlaps with useSnapshot. | ||
* Do not try to optimize too early. It can worsen the performance. | ||
* Measurement and comparison will be very important. | ||
* | ||
* @example | ||
* import { proxy } from 'valtio' | ||
* import { addComputed } from 'valtio/utils' | ||
* const state = proxy({ | ||
* count: 1, | ||
* }) | ||
* addComputed(state, { | ||
* doubled: snap => snap.count * 2, | ||
* }) | ||
* @deprecated Please consider using `derive` or `proxyWithComputed` instead. | ||
*/ | ||
export declare const addComputed: <T extends object, U extends object>(proxyObject: T, computedFns: { | ||
[K in keyof U]: (snap: DeepResolveType<T>) => U[K]; | ||
}, targetObject?: any) => void; | ||
export declare const addComputed_DEPRECATED: <T extends object, U extends object>(proxyObject: T, computedFns_FAKE: { | ||
[K in keyof U]: (snap_FAKE: T) => U[K]; | ||
}, targetObject?: any) => any; |
@@ -1,2 +0,9 @@ | ||
export declare const createMutableSource: (target: any, getVersion: any) => any; | ||
export declare const useMutableSource: (source: any, getSnapshot: any, subscribe: any) => any; | ||
declare const TARGET = "_uMS_T"; | ||
declare const GET_VERSION = "_uMS_V"; | ||
declare type MutableSource<T, V> = { | ||
[TARGET]: T; | ||
[GET_VERSION]: (target: T) => V; | ||
}; | ||
export declare const createMutableSource: <T, V>(target: T, getVersion: (target: T) => V) => MutableSource<T, V>; | ||
export declare const useMutableSource: <T, V, S>(source: MutableSource<T, V>, getSnapshot: (target: T) => S, subscribe: (target: T, callback: () => void) => () => void) => S; | ||
export {}; |
export { subscribeKey } from './utils/subscribeKey'; | ||
export { watch } from './utils/watch'; | ||
export { devtools } from './utils/devtools'; | ||
export { addComputed } from './utils/addComputed'; | ||
export { derive, underive, unstable_getDeriveSubscriptions, } from './utils/derive'; | ||
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed'; | ||
export { proxyWithComputed } from './utils/proxyWithComputed'; | ||
export { proxyWithHistory } from './utils/proxyWithHistory'; |
@@ -1,24 +0,8 @@ | ||
import { DeepResolveType } from '../vanilla'; | ||
/** | ||
* addComputed | ||
* addComputed (DEPRECATED) | ||
* | ||
* This adds computed values to an existing proxy object. | ||
* | ||
* [Notes] | ||
* This comes with a cost and overlaps with useSnapshot. | ||
* Do not try to optimize too early. It can worsen the performance. | ||
* Measurement and comparison will be very important. | ||
* | ||
* @example | ||
* import { proxy } from 'valtio' | ||
* import { addComputed } from 'valtio/utils' | ||
* const state = proxy({ | ||
* count: 1, | ||
* }) | ||
* addComputed(state, { | ||
* doubled: snap => snap.count * 2, | ||
* }) | ||
* @deprecated Please consider using `derive` or `proxyWithComputed` instead. | ||
*/ | ||
export declare const addComputed: <T extends object, U extends object>(proxyObject: T, computedFns: { | ||
[K in keyof U]: (snap: DeepResolveType<T>) => U[K]; | ||
}, targetObject?: any) => void; | ||
export declare const addComputed_DEPRECATED: <T extends object, U extends object>(proxyObject: T, computedFns_FAKE: { | ||
[K in keyof U]: (snap_FAKE: T) => U[K]; | ||
}, targetObject?: any) => any; |
@@ -1,2 +0,9 @@ | ||
export declare const createMutableSource: (target: any, getVersion: any) => any; | ||
export declare const useMutableSource: (source: any, getSnapshot: any, subscribe: any) => any; | ||
declare const TARGET = "_uMS_T"; | ||
declare const GET_VERSION = "_uMS_V"; | ||
declare type MutableSource<T, V> = { | ||
[TARGET]: T; | ||
[GET_VERSION]: (target: T) => V; | ||
}; | ||
export declare const createMutableSource: <T, V>(target: T, getVersion: (target: T) => V) => MutableSource<T, V>; | ||
export declare const useMutableSource: <T, V, S>(source: MutableSource<T, V>, getSnapshot: (target: T) => S, subscribe: (target: T, callback: () => void) => () => void) => S; | ||
export {}; |
export { subscribeKey } from './utils/subscribeKey'; | ||
export { watch } from './utils/watch'; | ||
export { devtools } from './utils/devtools'; | ||
export { addComputed } from './utils/addComputed'; | ||
export { derive, underive, unstable_getDeriveSubscriptions, } from './utils/derive'; | ||
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed'; | ||
export { proxyWithComputed } from './utils/proxyWithComputed'; | ||
export { proxyWithHistory } from './utils/proxyWithHistory'; |
199
utils.js
@@ -145,48 +145,178 @@ 'use strict'; | ||
var addComputed = function addComputed(proxyObject, computedFns, targetObject) { | ||
if (targetObject === void 0) { | ||
targetObject = proxyObject; | ||
var subscriptionsCache = new WeakMap(); | ||
var getSubscriptions = function getSubscriptions(proxyObject) { | ||
var subscriptions = subscriptionsCache.get(proxyObject); | ||
if (!subscriptions) { | ||
subscriptions = new Map(); | ||
subscriptionsCache.set(proxyObject, subscriptions); | ||
} | ||
Object.keys(computedFns).forEach(function (key) { | ||
if (Object.getOwnPropertyDescriptor(targetObject, key)) { | ||
throw new Error('object property already defined'); | ||
return subscriptions; | ||
}; | ||
var unstable_getDeriveSubscriptions = getSubscriptions; | ||
var derive = function derive(derivedFns, options) { | ||
var proxyObject = (options == null ? void 0 : options.proxy) || vanilla.proxy({}); | ||
var notifyInSync = options == null ? void 0 : options.sync; | ||
var subscriptions = getSubscriptions(proxyObject); | ||
var addSubscription = function addSubscription(p, key, callback) { | ||
var subscription = subscriptions.get(p); | ||
if (subscription) { | ||
subscription[0].set(key, callback); | ||
} else { | ||
var unsubscribe = vanilla.subscribe(p, function (ops) { | ||
var _subscriptions$get; | ||
if (p === proxyObject && ops.every(function (op) { | ||
return op[1].length === 1 && op[1][0] in derivedFns; | ||
})) { | ||
return; | ||
} | ||
(_subscriptions$get = subscriptions.get(p)) == null ? void 0 : _subscriptions$get[0].forEach(function (cb) { | ||
cb(); | ||
}); | ||
}, notifyInSync); | ||
subscriptions.set(p, [new Map([[key, callback]]), unsubscribe]); | ||
} | ||
}; | ||
var get = computedFns[key]; | ||
var prevSnapshot; | ||
var affected = new WeakMap(); | ||
var pending = false; | ||
var removeSubscription = function removeSubscription(p, key) { | ||
var subscription = subscriptions.get(p); | ||
var callback = function callback() { | ||
var nextSnapshot = vanilla.snapshot(proxyObject); | ||
if (subscription) { | ||
var callbackMap = subscription[0], | ||
unsubscribe = subscription[1]; | ||
callbackMap.delete(key); | ||
if (!pending && (!prevSnapshot || proxyCompare.isChanged(prevSnapshot, nextSnapshot, affected))) { | ||
affected = new WeakMap(); | ||
var value = get(proxyCompare.createProxy(nextSnapshot, affected)); | ||
prevSnapshot = nextSnapshot; | ||
if (!callbackMap.size) { | ||
unsubscribe(); | ||
subscriptions.delete(p); | ||
} | ||
} | ||
}; | ||
if (value instanceof Promise) { | ||
pending = true; | ||
value.then(function (v) { | ||
targetObject[key] = v; | ||
}).catch(function (e) { | ||
targetObject[key] = new Proxy({}, { | ||
get: function get() { | ||
throw e; | ||
} | ||
}); | ||
}).finally(function () { | ||
pending = false; | ||
}); | ||
Object.keys(derivedFns).forEach(function (key) { | ||
if (Object.getOwnPropertyDescriptor(proxyObject, key)) { | ||
throw new Error('object property already defined'); | ||
} | ||
var fn = derivedFns[key]; | ||
var lastDependencies = null; | ||
var evaluate = function evaluate() { | ||
if (lastDependencies) { | ||
if (Array.from(lastDependencies).every(function (_ref) { | ||
var p = _ref[0], | ||
n = _ref[1]; | ||
return vanilla.getVersion(p) === n; | ||
})) { | ||
return; | ||
} | ||
} | ||
targetObject[key] = value; | ||
var dependencies = new Map(); | ||
var get = function get(p) { | ||
dependencies.set(p, vanilla.getVersion(p)); | ||
return p; | ||
}; | ||
var value = fn(get); | ||
var subscribe = function subscribe() { | ||
var _lastDependencies2; | ||
dependencies.forEach(function (_, p) { | ||
var _lastDependencies; | ||
if (!((_lastDependencies = lastDependencies) != null && _lastDependencies.has(p))) { | ||
addSubscription(p, key, evaluate); | ||
} | ||
}); | ||
(_lastDependencies2 = lastDependencies) == null ? void 0 : _lastDependencies2.forEach(function (_, p) { | ||
if (!dependencies.has(p)) { | ||
removeSubscription(p, key); | ||
} | ||
}); | ||
lastDependencies = dependencies; | ||
}; | ||
if (value instanceof Promise) { | ||
value.then(function () { | ||
subscribe(); | ||
evaluate(); | ||
}); | ||
} else { | ||
subscribe(); | ||
} | ||
proxyObject[key] = value; | ||
}; | ||
vanilla.subscribe(proxyObject, callback); | ||
callback(); | ||
evaluate(); | ||
}); | ||
return proxyObject; | ||
}; | ||
var underive = function underive(proxyObject, options) { | ||
var subscriptions = getSubscriptions(proxyObject); | ||
var keysToDelete = options != null && options.delete ? new Set() : null; | ||
subscriptions.forEach(function (_ref2, p) { | ||
var callbackMap = _ref2[0], | ||
unsubscribe = _ref2[1]; | ||
if (options != null && options.keys) { | ||
options.keys.forEach(function (key) { | ||
if (callbackMap.has(key)) { | ||
callbackMap.delete(key); | ||
if (keysToDelete) { | ||
keysToDelete.add(key); | ||
} | ||
} | ||
}); | ||
} else { | ||
if (keysToDelete) { | ||
Array.from(callbackMap.keys()).forEach(function (key) { | ||
keysToDelete.add(key); | ||
}); | ||
} | ||
callbackMap.clear(); | ||
} | ||
if (!callbackMap.size) { | ||
unsubscribe(); | ||
subscriptions.delete(p); | ||
} | ||
}); | ||
if (keysToDelete) { | ||
keysToDelete.forEach(function (key) { | ||
delete proxyObject[key]; | ||
}); | ||
} | ||
}; | ||
var addComputed_DEPRECATED = function addComputed_DEPRECATED(proxyObject, computedFns_FAKE, targetObject) { | ||
if (targetObject === void 0) { | ||
targetObject = proxyObject; | ||
} | ||
console.warn('addComputed is deprecated. Please consider using `derive` or `proxyWithComputed` instead. Falling back to emulation with derive.'); | ||
var derivedFns = {}; | ||
Object.keys(computedFns_FAKE).forEach(function (key) { | ||
derivedFns[key] = function (get) { | ||
return computedFns_FAKE[key](get(proxyObject)); | ||
}; | ||
}); | ||
return derive(derivedFns, { | ||
proxy: targetObject | ||
}); | ||
}; | ||
var proxyWithComputed = function proxyWithComputed(initialObject, computedFns) { | ||
@@ -278,3 +408,4 @@ Object.keys(computedFns).forEach(function (key) { | ||
exports.addComputed = addComputed; | ||
exports.addComputed = addComputed_DEPRECATED; | ||
exports.derive = derive; | ||
exports.devtools = devtools; | ||
@@ -284,2 +415,4 @@ exports.proxyWithComputed = proxyWithComputed; | ||
exports.subscribeKey = subscribeKey; | ||
exports.underive = underive; | ||
exports.unstable_getDeriveSubscriptions = unstable_getDeriveSubscriptions; | ||
exports.watch = watch; |
@@ -1,22 +0,6 @@ | ||
import type { DeepResolveType } from '../vanilla'; | ||
/** | ||
* addComputed | ||
* addComputed (DEPRECATED) | ||
* | ||
* This adds computed values to an existing proxy object. | ||
* | ||
* [Notes] | ||
* This comes with a cost and overlaps with useSnapshot. | ||
* Do not try to optimize too early. It can worsen the performance. | ||
* Measurement and comparison will be very important. | ||
* | ||
* @example | ||
* import { proxy } from 'valtio' | ||
* import { addComputed } from 'valtio/utils' | ||
* const state = proxy({ | ||
* count: 1, | ||
* }) | ||
* addComputed(state, { | ||
* doubled: snap => snap.count * 2, | ||
* }) | ||
* @deprecated Please consider using `derive` or `proxyWithComputed` instead. | ||
*/ | ||
export declare const addComputed: <T extends object, U extends object>(proxyObject: T, computedFns: { [K in keyof U]: (snap: DeepResolveType<T>) => U[K]; }, targetObject?: any) => void; | ||
export declare const addComputed_DEPRECATED: <T extends object, U extends object>(proxyObject: T, computedFns_FAKE: { [K in keyof U]: (snap_FAKE: T) => U[K]; }, targetObject?: any) => any; |
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
106215
64
2665
329