Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

valtio

Package Overview
Dependencies
Maintainers
2
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

valtio - npm Package Compare versions

Comparing version 1.11.3 to 1.12.0

2

esm/vanilla/utils.d.ts
export { subscribeKey } from './utils/subscribeKey';
export { watch } from './utils/watch';
export { devtools } from './utils/devtools';
export { derive, underive, unstable_deriveSubscriptions, } from './utils/derive';
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio';
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed';

@@ -6,0 +6,0 @@ export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed';

@@ -1,2 +0,4 @@

import { subscribe, snapshot, proxy, getVersion, ref, unstable_buildProxyFunction } from 'valtio/vanilla';
import { subscribe, snapshot, proxy, ref, unstable_buildProxyFunction } from 'valtio/vanilla';
import { derive } from 'derive-valtio';
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio';

@@ -155,206 +157,2 @@ function subscribeKey(proxyObject, key, callback, notifyInSync) {

const sourceObjectMap = /* @__PURE__ */ new WeakMap();
const derivedObjectMap = /* @__PURE__ */ new WeakMap();
const markPending = (sourceObject, callback) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
sourceObjectEntry[0].forEach((subscription) => {
const { d: derivedObject } = subscription;
if (sourceObject !== derivedObject) {
markPending(derivedObject);
}
});
++sourceObjectEntry[2];
if (callback) {
sourceObjectEntry[3].add(callback);
}
}
};
const checkPending = (sourceObject, callback) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry == null ? void 0 : sourceObjectEntry[2]) {
sourceObjectEntry[3].add(callback);
return true;
}
return false;
};
const unmarkPending = (sourceObject) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
--sourceObjectEntry[2];
if (!sourceObjectEntry[2]) {
sourceObjectEntry[3].forEach((callback) => callback());
sourceObjectEntry[3].clear();
}
sourceObjectEntry[0].forEach((subscription) => {
const { d: derivedObject } = subscription;
if (sourceObject !== derivedObject) {
unmarkPending(derivedObject);
}
});
}
};
const addSubscription = (subscription) => {
const { s: sourceObject, d: derivedObject } = subscription;
let derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (!derivedObjectEntry) {
derivedObjectEntry = [/* @__PURE__ */ new Set()];
derivedObjectMap.set(subscription.d, derivedObjectEntry);
}
derivedObjectEntry[0].add(subscription);
let sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (!sourceObjectEntry) {
const subscriptions = /* @__PURE__ */ new Set();
const unsubscribe = subscribe(
sourceObject,
(ops) => {
subscriptions.forEach((subscription2) => {
const {
d: derivedObject2,
c: callback,
n: notifyInSync,
i: ignoreKeys
} = subscription2;
if (sourceObject === derivedObject2 && ops.every(
(op) => op[1].length === 1 && ignoreKeys.includes(op[1][0])
)) {
return;
}
if (subscription2.p) {
return;
}
markPending(sourceObject, callback);
if (notifyInSync) {
unmarkPending(sourceObject);
} else {
subscription2.p = Promise.resolve().then(() => {
delete subscription2.p;
unmarkPending(sourceObject);
});
}
});
},
true
);
sourceObjectEntry = [subscriptions, unsubscribe, 0, /* @__PURE__ */ new Set()];
sourceObjectMap.set(sourceObject, sourceObjectEntry);
}
sourceObjectEntry[0].add(subscription);
};
const removeSubscription = (subscription) => {
const { s: sourceObject, d: derivedObject } = subscription;
const derivedObjectEntry = derivedObjectMap.get(derivedObject);
derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].delete(subscription);
if ((derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].size) === 0) {
derivedObjectMap.delete(derivedObject);
}
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
const [subscriptions, unsubscribe] = sourceObjectEntry;
subscriptions.delete(subscription);
if (!subscriptions.size) {
unsubscribe();
sourceObjectMap.delete(sourceObject);
}
}
};
const listSubscriptions = (derivedObject) => {
const derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (derivedObjectEntry) {
return Array.from(derivedObjectEntry[0]);
}
return [];
};
const unstable_deriveSubscriptions = {
add: addSubscription,
remove: removeSubscription,
list: listSubscriptions
};
function derive(derivedFns, options) {
const proxyObject = (options == null ? void 0 : options.proxy) || proxy({});
const notifyInSync = !!(options == null ? void 0 : options.sync);
const derivedKeys = Object.keys(derivedFns);
derivedKeys.forEach((key) => {
if (Object.getOwnPropertyDescriptor(proxyObject, key)) {
throw new Error("object property already defined");
}
const fn = derivedFns[key];
let lastDependencies = null;
const evaluate = () => {
if (lastDependencies) {
if (Array.from(lastDependencies).map(([p]) => checkPending(p, evaluate)).some((isPending) => isPending)) {
return;
}
if (Array.from(lastDependencies).every(
([p, entry]) => getVersion(p) === entry.v
)) {
return;
}
}
const dependencies = /* @__PURE__ */ new Map();
const get = (p) => {
dependencies.set(p, { v: getVersion(p) });
return p;
};
const value = fn(get);
const subscribeToDependencies = () => {
dependencies.forEach((entry, p) => {
var _a;
const lastSubscription = (_a = lastDependencies == null ? void 0 : lastDependencies.get(p)) == null ? void 0 : _a.s;
if (lastSubscription) {
entry.s = lastSubscription;
} else {
const subscription = {
s: p,
// sourceObject
d: proxyObject,
// derivedObject
k: key,
// derived key
c: evaluate,
// callback
n: notifyInSync,
i: derivedKeys
// ignoringKeys
};
addSubscription(subscription);
entry.s = subscription;
}
});
lastDependencies == null ? void 0 : lastDependencies.forEach((entry, p) => {
if (!dependencies.has(p) && entry.s) {
removeSubscription(entry.s);
}
});
lastDependencies = dependencies;
};
if (value instanceof Promise) {
value.finally(subscribeToDependencies);
} else {
subscribeToDependencies();
}
proxyObject[key] = value;
};
evaluate();
});
return proxyObject;
}
function underive(proxyObject, options) {
const keysToDelete = (options == null ? void 0 : options.delete) ? /* @__PURE__ */ new Set() : null;
listSubscriptions(proxyObject).forEach((subscription) => {
const { k: key } = subscription;
if (!(options == null ? void 0 : options.keys) || options.keys.includes(key)) {
removeSubscription(subscription);
if (keysToDelete) {
keysToDelete.add(key);
}
}
});
if (keysToDelete) {
keysToDelete.forEach((key) => {
delete proxyObject[key];
});
}
}
function addComputed_DEPRECATED(proxyObject, computedFns_FAKE, targetObject = proxyObject) {

@@ -598,2 +396,2 @@ if (process.env.NODE_ENV !== "production") {

export { addComputed_DEPRECATED as addComputed, derive, devtools, proxyMap, proxySet, proxyWithComputed_DEPRECATED as proxyWithComputed, proxyWithHistory, subscribeKey, underive, unstable_deriveSubscriptions, watch };
export { addComputed_DEPRECATED as addComputed, devtools, proxyMap, proxySet, proxyWithComputed_DEPRECATED as proxyWithComputed, proxyWithHistory, subscribeKey, watch };
{
"name": "valtio",
"private": false,
"version": "1.11.3",
"version": "1.12.0",
"description": "💊 Valtio makes proxy-state simple for React and Vanilla",

@@ -21,3 +21,2 @@ "main": "./index.js",

".": {
"types": "./index.d.ts",
"import": {

@@ -27,7 +26,12 @@ "types": "./esm/index.d.mts",

},
"module": "./esm/index.js",
"default": "./index.js"
"module": {
"types": "./esm/index.d.ts",
"default": "./esm/index.js"
},
"default": {
"types": "./index.d.ts",
"default": "./index.js"
}
},
"./*": {
"types": "./*.d.ts",
"import": {

@@ -37,4 +41,10 @@ "types": "./esm/*.d.mts",

},
"module": "./esm/*.js",
"default": "./*.js"
"module": {
"types": "./esm/*.d.ts",
"default": "./esm/*.js"
},
"default": {
"types": "./*.d.ts",
"default": "./*.js"
}
}

@@ -71,2 +81,3 @@ },

"proxy-compare": "2.5.1",
"derive-valtio": "0.1.0",
"use-sync-external-store": "1.2.0"

@@ -73,0 +84,0 @@ },

@@ -1,4 +0,4 @@

System.register(['valtio/vanilla'], (function (exports) {
System.register(['valtio/vanilla', 'derive-valtio'], (function (exports) {
'use strict';
var subscribe, snapshot, proxy, getVersion, ref, unstable_buildProxyFunction;
var subscribe, snapshot, proxy, ref, unstable_buildProxyFunction, derive;
return {

@@ -9,5 +9,7 @@ setters: [function (module) {

proxy = module.proxy;
getVersion = module.getVersion;
ref = module.ref;
unstable_buildProxyFunction = module.unstable_buildProxyFunction;
}, function (module) {
derive = module.derive;
exports({ derive: module.derive, underive: module.underive, unstable_deriveSubscriptions: module.unstable_deriveSubscriptions });
}],

@@ -18,3 +20,2 @@ execute: (function () {

addComputed: addComputed_DEPRECATED,
derive: derive,
devtools: devtools,

@@ -26,3 +27,2 @@ proxyMap: proxyMap,

subscribeKey: subscribeKey,
underive: underive,
watch: watch

@@ -183,206 +183,2 @@ });

const sourceObjectMap = /* @__PURE__ */ new WeakMap();
const derivedObjectMap = /* @__PURE__ */ new WeakMap();
const markPending = (sourceObject, callback) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
sourceObjectEntry[0].forEach((subscription) => {
const { d: derivedObject } = subscription;
if (sourceObject !== derivedObject) {
markPending(derivedObject);
}
});
++sourceObjectEntry[2];
if (callback) {
sourceObjectEntry[3].add(callback);
}
}
};
const checkPending = (sourceObject, callback) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry == null ? void 0 : sourceObjectEntry[2]) {
sourceObjectEntry[3].add(callback);
return true;
}
return false;
};
const unmarkPending = (sourceObject) => {
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
--sourceObjectEntry[2];
if (!sourceObjectEntry[2]) {
sourceObjectEntry[3].forEach((callback) => callback());
sourceObjectEntry[3].clear();
}
sourceObjectEntry[0].forEach((subscription) => {
const { d: derivedObject } = subscription;
if (sourceObject !== derivedObject) {
unmarkPending(derivedObject);
}
});
}
};
const addSubscription = (subscription) => {
const { s: sourceObject, d: derivedObject } = subscription;
let derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (!derivedObjectEntry) {
derivedObjectEntry = [/* @__PURE__ */ new Set()];
derivedObjectMap.set(subscription.d, derivedObjectEntry);
}
derivedObjectEntry[0].add(subscription);
let sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (!sourceObjectEntry) {
const subscriptions = /* @__PURE__ */ new Set();
const unsubscribe = subscribe(
sourceObject,
(ops) => {
subscriptions.forEach((subscription2) => {
const {
d: derivedObject2,
c: callback,
n: notifyInSync,
i: ignoreKeys
} = subscription2;
if (sourceObject === derivedObject2 && ops.every(
(op) => op[1].length === 1 && ignoreKeys.includes(op[1][0])
)) {
return;
}
if (subscription2.p) {
return;
}
markPending(sourceObject, callback);
if (notifyInSync) {
unmarkPending(sourceObject);
} else {
subscription2.p = Promise.resolve().then(() => {
delete subscription2.p;
unmarkPending(sourceObject);
});
}
});
},
true
);
sourceObjectEntry = [subscriptions, unsubscribe, 0, /* @__PURE__ */ new Set()];
sourceObjectMap.set(sourceObject, sourceObjectEntry);
}
sourceObjectEntry[0].add(subscription);
};
const removeSubscription = (subscription) => {
const { s: sourceObject, d: derivedObject } = subscription;
const derivedObjectEntry = derivedObjectMap.get(derivedObject);
derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].delete(subscription);
if ((derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].size) === 0) {
derivedObjectMap.delete(derivedObject);
}
const sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
const [subscriptions, unsubscribe] = sourceObjectEntry;
subscriptions.delete(subscription);
if (!subscriptions.size) {
unsubscribe();
sourceObjectMap.delete(sourceObject);
}
}
};
const listSubscriptions = (derivedObject) => {
const derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (derivedObjectEntry) {
return Array.from(derivedObjectEntry[0]);
}
return [];
};
const unstable_deriveSubscriptions = exports('unstable_deriveSubscriptions', {
add: addSubscription,
remove: removeSubscription,
list: listSubscriptions
});
function derive(derivedFns, options) {
const proxyObject = (options == null ? void 0 : options.proxy) || proxy({});
const notifyInSync = !!(options == null ? void 0 : options.sync);
const derivedKeys = Object.keys(derivedFns);
derivedKeys.forEach((key) => {
if (Object.getOwnPropertyDescriptor(proxyObject, key)) {
throw new Error("object property already defined");
}
const fn = derivedFns[key];
let lastDependencies = null;
const evaluate = () => {
if (lastDependencies) {
if (Array.from(lastDependencies).map(([p]) => checkPending(p, evaluate)).some((isPending) => isPending)) {
return;
}
if (Array.from(lastDependencies).every(
([p, entry]) => getVersion(p) === entry.v
)) {
return;
}
}
const dependencies = /* @__PURE__ */ new Map();
const get = (p) => {
dependencies.set(p, { v: getVersion(p) });
return p;
};
const value = fn(get);
const subscribeToDependencies = () => {
dependencies.forEach((entry, p) => {
var _a;
const lastSubscription = (_a = lastDependencies == null ? void 0 : lastDependencies.get(p)) == null ? void 0 : _a.s;
if (lastSubscription) {
entry.s = lastSubscription;
} else {
const subscription = {
s: p,
// sourceObject
d: proxyObject,
// derivedObject
k: key,
// derived key
c: evaluate,
// callback
n: notifyInSync,
i: derivedKeys
// ignoringKeys
};
addSubscription(subscription);
entry.s = subscription;
}
});
lastDependencies == null ? void 0 : lastDependencies.forEach((entry, p) => {
if (!dependencies.has(p) && entry.s) {
removeSubscription(entry.s);
}
});
lastDependencies = dependencies;
};
if (value instanceof Promise) {
value.finally(subscribeToDependencies);
} else {
subscribeToDependencies();
}
proxyObject[key] = value;
};
evaluate();
});
return proxyObject;
}
function underive(proxyObject, options) {
const keysToDelete = (options == null ? void 0 : options.delete) ? /* @__PURE__ */ new Set() : null;
listSubscriptions(proxyObject).forEach((subscription) => {
const { k: key } = subscription;
if (!(options == null ? void 0 : options.keys) || options.keys.includes(key)) {
removeSubscription(subscription);
if (keysToDelete) {
keysToDelete.add(key);
}
}
});
if (keysToDelete) {
keysToDelete.forEach((key) => {
delete proxyObject[key];
});
}
}
function addComputed_DEPRECATED(proxyObject, computedFns_FAKE, targetObject = proxyObject) {

@@ -389,0 +185,0 @@ {

@@ -1,2 +0,2 @@

System.register(["valtio/vanilla"],function(M){"use strict";var O,y,b,j,N,k;return{setters:[function(f){O=f.subscribe,y=f.snapshot,b=f.proxy,j=f.getVersion,N=f.ref,k=f.unstable_buildProxyFunction}],execute:function(){M({addComputed:K,derive:J,devtools:W,proxyMap:B,proxySet:$,proxyWithComputed:V,proxyWithHistory:X,subscribeKey:f,underive:L,watch:R});function f(s,t,e,r){let n=s[t];return O(s,()=>{const i=s[t];Object.is(n,i)||e(n=i)},r)}let S;function R(s,t){let e=!0;const r=new Set,n=new Map,i=()=>{e&&(e=!1,r.forEach(o=>o()),r.clear(),n.forEach(o=>o()),n.clear())},d=()=>{if(!e)return;r.forEach(l=>l()),r.clear();const o=new Set,h=S;S=r;try{const l=s(a=>(o.add(a),a));l&&r.add(l)}finally{S=h}n.forEach((l,a)=>{o.has(a)?o.delete(a):(n.delete(a),l())}),o.forEach(l=>{const a=O(l,d,t==null?void 0:t.sync);n.set(l,a)})};return S&&S.add(i),d(),i}const T=Symbol();function W(s,t){typeof t=="string"&&(console.warn("string name option is deprecated, use { name }. https://github.com/pmndrs/valtio/pull/400"),t={name:t});const{enabled:e,name:r="",...n}=t||{};let i;try{i=(e!=null?e:!1)&&window.__REDUX_DEVTOOLS_EXTENSION__}catch{}if(!i)return;let d=!1;const o=i.connect({name:r,...n}),h=O(s,a=>{const p=a.filter(([c,u])=>u[0]!==T).map(([c,u])=>`${c}:${u.map(String).join(".")}`).join(", ");if(p)if(d)d=!1;else{const c=Object.assign({},y(s));delete c[T],o.send({type:p,updatedAt:new Date().toLocaleString()},c)}}),l=o.subscribe(a=>{var p,c,u,E,m,w;if(a.type==="ACTION"&&a.payload)try{Object.assign(s,JSON.parse(a.payload))}catch(x){console.error(`please dispatch a serializable value that JSON.parse() and proxy() support
`,x)}if(a.type==="DISPATCH"&&a.state){if(((p=a.payload)==null?void 0:p.type)==="JUMP_TO_ACTION"||((c=a.payload)==null?void 0:c.type)==="JUMP_TO_STATE"){d=!0;const x=JSON.parse(a.state);Object.assign(s,x)}s[T]=a}else if(a.type==="DISPATCH"&&((u=a.payload)==null?void 0:u.type)==="COMMIT")o.init(y(s));else if(a.type==="DISPATCH"&&((E=a.payload)==null?void 0:E.type)==="IMPORT_STATE"){const x=(m=a.payload.nextLiftedState)==null?void 0:m.actionsById,q=((w=a.payload.nextLiftedState)==null?void 0:w.computedStates)||[];d=!0,q.forEach(({state:G},z)=>{const Q=x[z]||"No action found";Object.assign(s,G),z===0?o.init(y(s)):o.send(Q,y(s))})}});return o.init(y(s)),()=>{h(),l==null||l()}}const v=new WeakMap,g=new WeakMap,C=(s,t)=>{const e=v.get(s);e&&(e[0].forEach(r=>{const{d:n}=r;s!==n&&C(n)}),++e[2],t&&e[3].add(t))},U=(s,t)=>{const e=v.get(s);return e!=null&&e[2]?(e[3].add(t),!0):!1},P=s=>{const t=v.get(s);t&&(--t[2],t[2]||(t[3].forEach(e=>e()),t[3].clear()),t[0].forEach(e=>{const{d:r}=e;s!==r&&P(r)}))},D=s=>{const{s:t,d:e}=s;let r=g.get(e);r||(r=[new Set],g.set(s.d,r)),r[0].add(s);let n=v.get(t);if(!n){const i=new Set,d=O(t,o=>{i.forEach(h=>{const{d:l,c:a,n:p,i:c}=h;t===l&&o.every(u=>u[1].length===1&&c.includes(u[1][0]))||h.p||(C(t,a),p?P(t):h.p=Promise.resolve().then(()=>{delete h.p,P(t)}))})},!0);n=[i,d,0,new Set],v.set(t,n)}n[0].add(s)},_=s=>{const{s:t,d:e}=s,r=g.get(e);r==null||r[0].delete(s),(r==null?void 0:r[0].size)===0&&g.delete(e);const n=v.get(t);if(n){const[i,d]=n;i.delete(s),i.size||(d(),v.delete(t))}},I=s=>{const t=g.get(s);return t?Array.from(t[0]):[]},Y=M("unstable_deriveSubscriptions",{add:D,remove:_,list:I});function J(s,t){const e=(t==null?void 0:t.proxy)||b({}),r=!!(t!=null&&t.sync),n=Object.keys(s);return n.forEach(i=>{if(Object.getOwnPropertyDescriptor(e,i))throw new Error("object property already defined");const d=s[i];let o=null;const h=()=>{if(o&&(Array.from(o).map(([c])=>U(c,h)).some(c=>c)||Array.from(o).every(([c,u])=>j(c)===u.v)))return;const l=new Map,a=d(c=>(l.set(c,{v:j(c)}),c)),p=()=>{l.forEach((c,u)=>{var E;const m=(E=o==null?void 0:o.get(u))==null?void 0:E.s;if(m)c.s=m;else{const w={s:u,d:e,k:i,c:h,n:r,i:n};D(w),c.s=w}}),o==null||o.forEach((c,u)=>{!l.has(u)&&c.s&&_(c.s)}),o=l};a instanceof Promise?a.finally(p):p(),e[i]=a};h()}),e}function L(s,t){const e=t!=null&&t.delete?new Set:null;I(s).forEach(r=>{const{k:n}=r;(!(t!=null&&t.keys)||t.keys.includes(n))&&(_(r),e&&e.add(n))}),e&&e.forEach(r=>{delete s[r]})}function K(s,t,e=s){const r={};return Object.keys(t).forEach(n=>{r[n]=i=>t[n](i(s))}),J(r,{proxy:e})}function V(s,t){Object.keys(t).forEach(r=>{if(Object.getOwnPropertyDescriptor(s,r))throw new Error("object property already defined");const n=t[r],{get:i,set:d}=typeof n=="function"?{get:n}:n,o={};o.get=()=>i(y(e)),d&&(o.set=h=>d(e,h)),Object.defineProperty(s,r,o)});const e=b(s);return e}const F=s=>typeof s=="object"&&s!==null;let A;const H=s=>{if(A||(A=k()[2]),!F(s)||A.has(s))return s;const t=Array.isArray(s)?[]:Object.create(Object.getPrototypeOf(s));return Reflect.ownKeys(s).forEach(e=>{t[e]=H(s[e])}),t};function X(s,t=!1){const e=b({value:s,history:N({wip:void 0,snapshots:[],index:-1}),clone:H,canUndo:()=>e.history.index>0,undo:()=>{e.canUndo()&&(e.value=e.history.wip=e.clone(e.history.snapshots[--e.history.index]))},canRedo:()=>e.history.index<e.history.snapshots.length-1,redo:()=>{e.canRedo()&&(e.value=e.history.wip=e.clone(e.history.snapshots[++e.history.index]))},saveHistory:()=>{e.history.snapshots.splice(e.history.index+1),e.history.snapshots.push(y(e).value),++e.history.index},subscribe:()=>O(e,r=>{r.every(n=>n[1][0]==="value"&&(n[0]!=="set"||n[2]!==e.history.wip))&&e.saveHistory()})});return e.saveHistory(),t||e.subscribe(),e}function $(s){const t=b({data:Array.from(new Set(s)),has(e){return this.data.indexOf(e)!==-1},add(e){let r=!1;return typeof e=="object"&&e!==null&&(r=this.data.indexOf(b(e))!==-1),this.data.indexOf(e)===-1&&!r&&this.data.push(e),this},delete(e){const r=this.data.indexOf(e);return r===-1?!1:(this.data.splice(r,1),!0)},clear(){this.data.splice(0)},get size(){return this.data.length},forEach(e){this.data.forEach(r=>{e(r,r,this)})},get[Symbol.toStringTag](){return"Set"},toJSON(){return new Set(this.data)},[Symbol.iterator](){return this.data[Symbol.iterator]()},values(){return this.data.values()},keys(){return this.data.values()},entries(){return new Set(this.data).entries()}});return Object.defineProperties(t,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(t),t}function B(s){const t=b({data:Array.from(s||[]),has(e){return this.data.some(r=>r[0]===e)},set(e,r){const n=this.data.find(i=>i[0]===e);return n?n[1]=r:this.data.push([e,r]),this},get(e){var r;return(r=this.data.find(n=>n[0]===e))==null?void 0:r[1]},delete(e){const r=this.data.findIndex(n=>n[0]===e);return r===-1?!1:(this.data.splice(r,1),!0)},clear(){this.data.splice(0)},get size(){return this.data.length},toJSON(){return new Map(this.data)},forEach(e){this.data.forEach(r=>{e(r[1],r[0],this)})},keys(){return this.data.map(e=>e[0]).values()},values(){return this.data.map(e=>e[1]).values()},entries(){return new Map(this.data).entries()},get[Symbol.toStringTag](){return"Map"},[Symbol.iterator](){return this.entries()}});return Object.defineProperties(t,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(t),t}}}});
System.register(["valtio/vanilla","derive-valtio"],function(E){"use strict";var f,u,p,m,w,j;return{setters:[function(c){f=c.subscribe,u=c.snapshot,p=c.proxy,m=c.ref,w=c.unstable_buildProxyFunction},function(c){j=c.derive,E({derive:c.derive,underive:c.underive,unstable_deriveSubscriptions:c.unstable_deriveSubscriptions})}],execute:function(){E({addComputed:M,devtools:D,proxyMap:z,proxySet:R,proxyWithComputed:I,proxyWithHistory:H,subscribeKey:c,watch:C});function c(a,r,t,e){let s=a[r];return f(a,()=>{const o=a[r];Object.is(s,o)||t(s=o)},e)}let b;function C(a,r){let t=!0;const e=new Set,s=new Map,o=()=>{t&&(t=!1,e.forEach(i=>i()),e.clear(),s.forEach(i=>i()),s.clear())},l=()=>{if(!t)return;e.forEach(d=>d()),e.clear();const i=new Set,v=b;b=e;try{const d=a(n=>(i.add(n),n));d&&e.add(d)}finally{b=v}s.forEach((d,n)=>{i.has(n)?i.delete(n):(s.delete(n),d())}),i.forEach(d=>{const n=f(d,l,r==null?void 0:r.sync);s.set(d,n)})};return b&&b.add(o),l(),o}const x=Symbol();function D(a,r){typeof r=="string"&&(console.warn("string name option is deprecated, use { name }. https://github.com/pmndrs/valtio/pull/400"),r={name:r});const{enabled:t,name:e="",...s}=r||{};let o;try{o=(t!=null?t:!1)&&window.__REDUX_DEVTOOLS_EXTENSION__}catch{}if(!o)return;let l=!1;const i=o.connect({name:e,...s}),v=f(a,n=>{const O=n.filter(([h,y])=>y[0]!==x).map(([h,y])=>`${h}:${y.map(String).join(".")}`).join(", ");if(O)if(l)l=!1;else{const h=Object.assign({},u(a));delete h[x],i.send({type:O,updatedAt:new Date().toLocaleString()},h)}}),d=i.subscribe(n=>{var O,h,y,_,P,A;if(n.type==="ACTION"&&n.payload)try{Object.assign(a,JSON.parse(n.payload))}catch(S){console.error(`please dispatch a serializable value that JSON.parse() and proxy() support
`,S)}if(n.type==="DISPATCH"&&n.state){if(((O=n.payload)==null?void 0:O.type)==="JUMP_TO_ACTION"||((h=n.payload)==null?void 0:h.type)==="JUMP_TO_STATE"){l=!0;const S=JSON.parse(n.state);Object.assign(a,S)}a[x]=n}else if(n.type==="DISPATCH"&&((y=n.payload)==null?void 0:y.type)==="COMMIT")i.init(u(a));else if(n.type==="DISPATCH"&&((_=n.payload)==null?void 0:_.type)==="IMPORT_STATE"){const S=(P=n.payload.nextLiftedState)==null?void 0:P.actionsById,U=((A=n.payload.nextLiftedState)==null?void 0:A.computedStates)||[];l=!0,U.forEach(({state:k},N)=>{const L=S[N]||"No action found";Object.assign(a,k),N===0?i.init(u(a)):i.send(L,u(a))})}});return i.init(u(a)),()=>{v(),d==null||d()}}function M(a,r,t=a){const e={};return Object.keys(r).forEach(s=>{e[s]=o=>r[s](o(a))}),j(e,{proxy:t})}function I(a,r){Object.keys(r).forEach(e=>{if(Object.getOwnPropertyDescriptor(a,e))throw new Error("object property already defined");const s=r[e],{get:o,set:l}=typeof s=="function"?{get:s}:s,i={};i.get=()=>o(u(t)),l&&(i.set=v=>l(t,v)),Object.defineProperty(a,e,i)});const t=p(a);return t}const J=a=>typeof a=="object"&&a!==null;let g;const T=a=>{if(g||(g=w()[2]),!J(a)||g.has(a))return a;const r=Array.isArray(a)?[]:Object.create(Object.getPrototypeOf(a));return Reflect.ownKeys(a).forEach(t=>{r[t]=T(a[t])}),r};function H(a,r=!1){const t=p({value:a,history:m({wip:void 0,snapshots:[],index:-1}),clone:T,canUndo:()=>t.history.index>0,undo:()=>{t.canUndo()&&(t.value=t.history.wip=t.clone(t.history.snapshots[--t.history.index]))},canRedo:()=>t.history.index<t.history.snapshots.length-1,redo:()=>{t.canRedo()&&(t.value=t.history.wip=t.clone(t.history.snapshots[++t.history.index]))},saveHistory:()=>{t.history.snapshots.splice(t.history.index+1),t.history.snapshots.push(u(t).value),++t.history.index},subscribe:()=>f(t,e=>{e.every(s=>s[1][0]==="value"&&(s[0]!=="set"||s[2]!==t.history.wip))&&t.saveHistory()})});return t.saveHistory(),r||t.subscribe(),t}function R(a){const r=p({data:Array.from(new Set(a)),has(t){return this.data.indexOf(t)!==-1},add(t){let e=!1;return typeof t=="object"&&t!==null&&(e=this.data.indexOf(p(t))!==-1),this.data.indexOf(t)===-1&&!e&&this.data.push(t),this},delete(t){const e=this.data.indexOf(t);return e===-1?!1:(this.data.splice(e,1),!0)},clear(){this.data.splice(0)},get size(){return this.data.length},forEach(t){this.data.forEach(e=>{t(e,e,this)})},get[Symbol.toStringTag](){return"Set"},toJSON(){return new Set(this.data)},[Symbol.iterator](){return this.data[Symbol.iterator]()},values(){return this.data.values()},keys(){return this.data.values()},entries(){return new Set(this.data).entries()}});return Object.defineProperties(r,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(r),r}function z(a){const r=p({data:Array.from(a||[]),has(t){return this.data.some(e=>e[0]===t)},set(t,e){const s=this.data.find(o=>o[0]===t);return s?s[1]=e:this.data.push([t,e]),this},get(t){var e;return(e=this.data.find(s=>s[0]===t))==null?void 0:e[1]},delete(t){const e=this.data.findIndex(s=>s[0]===t);return e===-1?!1:(this.data.splice(e,1),!0)},clear(){this.data.splice(0)},get size(){return this.data.length},toJSON(){return new Map(this.data)},forEach(t){this.data.forEach(e=>{t(e[1],e[0],this)})},keys(){return this.data.map(t=>t[0]).values()},values(){return this.data.map(t=>t[1]).values()},entries(){return new Map(this.data).entries()},get[Symbol.toStringTag](){return"Map"},[Symbol.iterator](){return this.entries()}});return Object.defineProperties(r,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(r),r}}}});
export { subscribeKey } from './utils/subscribeKey';
export { watch } from './utils/watch';
export { devtools } from './utils/devtools';
export { derive, underive, unstable_deriveSubscriptions, } from './utils/derive';
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio';
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed';

@@ -6,0 +6,0 @@ export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed';

export { subscribeKey } from './utils/subscribeKey';
export { watch } from './utils/watch';
export { devtools } from './utils/devtools';
export { derive, underive, unstable_deriveSubscriptions, } from './utils/derive';
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio';
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed';

@@ -6,0 +6,0 @@ export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed';

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('valtio/vanilla')) :
typeof define === 'function' && define.amd ? define(['exports', 'valtio/vanilla'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.valtioVanillaUtils = {}, global.valtioVanilla));
})(this, (function (exports, vanilla) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('valtio/vanilla'), require('derive-valtio')) :
typeof define === 'function' && define.amd ? define(['exports', 'valtio/vanilla', 'derive-valtio'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.valtioVanillaUtils = {}, global.valtioVanilla, global.deriveValtio));
})(this, (function (exports, vanilla, deriveValtio) { 'use strict';

@@ -206,210 +206,2 @@ function subscribeKey(proxyObject, key, callback, notifyInSync) {

var sourceObjectMap = new WeakMap();
var derivedObjectMap = new WeakMap();
var markPending = function markPending(sourceObject, callback) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
sourceObjectEntry[0].forEach(function (subscription) {
var derivedObject = subscription.d;
if (sourceObject !== derivedObject) {
markPending(derivedObject);
}
});
++sourceObjectEntry[2];
if (callback) {
sourceObjectEntry[3].add(callback);
}
}
};
var checkPending = function checkPending(sourceObject, callback) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry != null && sourceObjectEntry[2]) {
sourceObjectEntry[3].add(callback);
return true;
}
return false;
};
var unmarkPending = function unmarkPending(sourceObject) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
--sourceObjectEntry[2];
if (!sourceObjectEntry[2]) {
sourceObjectEntry[3].forEach(function (callback) {
return callback();
});
sourceObjectEntry[3].clear();
}
sourceObjectEntry[0].forEach(function (subscription) {
var derivedObject = subscription.d;
if (sourceObject !== derivedObject) {
unmarkPending(derivedObject);
}
});
}
};
var addSubscription = function addSubscription(subscription) {
var sourceObject = subscription.s,
derivedObject = subscription.d;
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (!derivedObjectEntry) {
derivedObjectEntry = [new Set()];
derivedObjectMap.set(subscription.d, derivedObjectEntry);
}
derivedObjectEntry[0].add(subscription);
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (!sourceObjectEntry) {
var _subscriptions = new Set();
var _unsubscribe = vanilla.subscribe(sourceObject, function (ops) {
_subscriptions.forEach(function (subscription) {
var derivedObject = subscription.d,
callback = subscription.c,
notifyInSync = subscription.n,
ignoreKeys = subscription.i;
if (sourceObject === derivedObject && ops.every(function (op) {
return op[1].length === 1 && ignoreKeys.includes(op[1][0]);
})) {
return;
}
if (subscription.p) {
return;
}
markPending(sourceObject, callback);
if (notifyInSync) {
unmarkPending(sourceObject);
} else {
subscription.p = Promise.resolve().then(function () {
delete subscription.p;
unmarkPending(sourceObject);
});
}
});
}, true);
sourceObjectEntry = [_subscriptions, _unsubscribe, 0, new Set()];
sourceObjectMap.set(sourceObject, sourceObjectEntry);
}
sourceObjectEntry[0].add(subscription);
};
var removeSubscription = function removeSubscription(subscription) {
var sourceObject = subscription.s,
derivedObject = subscription.d;
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
derivedObjectEntry == null || derivedObjectEntry[0].delete(subscription);
if ((derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].size) === 0) {
derivedObjectMap.delete(derivedObject);
}
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
var _subscriptions2 = sourceObjectEntry[0],
_unsubscribe2 = sourceObjectEntry[1];
_subscriptions2.delete(subscription);
if (!_subscriptions2.size) {
_unsubscribe2();
sourceObjectMap.delete(sourceObject);
}
}
};
var listSubscriptions = function listSubscriptions(derivedObject) {
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (derivedObjectEntry) {
return Array.from(derivedObjectEntry[0]);
}
return [];
};
var unstable_deriveSubscriptions = {
add: addSubscription,
remove: removeSubscription,
list: listSubscriptions
};
function derive(derivedFns, options) {
var proxyObject = (options == null ? void 0 : options.proxy) || vanilla.proxy({});
var notifyInSync = !!(options != null && options.sync);
var derivedKeys = Object.keys(derivedFns);
derivedKeys.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).map(function (_ref) {
var p = _ref[0];
return checkPending(p, evaluate);
}).some(function (isPending) {
return isPending;
})) {
return;
}
if (Array.from(lastDependencies).every(function (_ref2) {
var p = _ref2[0],
entry = _ref2[1];
return vanilla.getVersion(p) === entry.v;
})) {
return;
}
}
var dependencies = new Map();
var get = function get(p) {
dependencies.set(p, {
v: vanilla.getVersion(p)
});
return p;
};
var value = fn(get);
var subscribeToDependencies = function subscribeToDependencies() {
var _lastDependencies2;
dependencies.forEach(function (entry, p) {
var _lastDependencies;
var lastSubscription = (_lastDependencies = lastDependencies) == null || (_lastDependencies = _lastDependencies.get(p)) == null ? void 0 : _lastDependencies.s;
if (lastSubscription) {
entry.s = lastSubscription;
} else {
var subscription = {
s: p,
d: proxyObject,
k: key,
c: evaluate,
n: notifyInSync,
i: derivedKeys
};
addSubscription(subscription);
entry.s = subscription;
}
});
(_lastDependencies2 = lastDependencies) == null || _lastDependencies2.forEach(function (entry, p) {
if (!dependencies.has(p) && entry.s) {
removeSubscription(entry.s);
}
});
lastDependencies = dependencies;
};
if (value instanceof Promise) {
value.finally(subscribeToDependencies);
} else {
subscribeToDependencies();
}
proxyObject[key] = value;
};
evaluate();
});
return proxyObject;
}
function underive(proxyObject, options) {
var keysToDelete = options != null && options.delete ? new Set() : null;
listSubscriptions(proxyObject).forEach(function (subscription) {
var key = subscription.k;
if (!(options != null && options.keys) || options.keys.includes(key)) {
removeSubscription(subscription);
if (keysToDelete) {
keysToDelete.add(key);
}
}
});
if (keysToDelete) {
keysToDelete.forEach(function (key) {
delete proxyObject[key];
});
}
}
function addComputed_DEPRECATED(proxyObject, computedFns_FAKE, targetObject) {

@@ -428,3 +220,3 @@ if (targetObject === void 0) {

});
return derive(derivedFns, {
return deriveValtio.derive(derivedFns, {
proxy: targetObject

@@ -679,4 +471,15 @@ });

Object.defineProperty(exports, 'derive', {
enumerable: true,
get: function () { return deriveValtio.derive; }
});
Object.defineProperty(exports, 'underive', {
enumerable: true,
get: function () { return deriveValtio.underive; }
});
Object.defineProperty(exports, 'unstable_deriveSubscriptions', {
enumerable: true,
get: function () { return deriveValtio.unstable_deriveSubscriptions; }
});
exports.addComputed = addComputed_DEPRECATED;
exports.derive = derive;
exports.devtools = devtools;

@@ -688,6 +491,4 @@ exports.proxyMap = proxyMap;

exports.subscribeKey = subscribeKey;
exports.underive = underive;
exports.unstable_deriveSubscriptions = unstable_deriveSubscriptions;
exports.watch = watch;
}));

@@ -1,1 +0,1 @@

!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("valtio/vanilla")):"function"==typeof define&&define.amd?define(["exports","valtio/vanilla"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).valtioVanillaUtils={},t.valtioVanilla)}(this,(function(t,n){"use strict";var e;function r(t,n,e,r){var i={configurable:!0,enumerable:!0};return i[t]=r,Object.defineProperty(n,e,i)}function i(){return i=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},i.apply(this,arguments)}var a=["enabled","name"],o=Symbol();var u=new WeakMap,s=new WeakMap,c=function t(n,e){var r=u.get(n);r&&(r[0].forEach((function(e){var r=e.d;n!==r&&t(r)})),++r[2],e&&r[3].add(e))},f=function(t,n){var e=u.get(t);return!(null==e||!e[2])&&(e[3].add(n),!0)},l=function t(n){var e=u.get(n);e&&(--e[2],e[2]||(e[3].forEach((function(t){return t()})),e[3].clear()),e[0].forEach((function(e){var r=e.d;n!==r&&t(r)})))},d=function(t){var e=t.s,r=t.d,i=s.get(r);i||(i=[new Set],s.set(t.d,i)),i[0].add(t);var a=u.get(e);if(!a){var o=new Set,f=n.subscribe(e,(function(t){o.forEach((function(n){var r=n.d,i=n.c,a=n.n,o=n.i;e===r&&t.every((function(t){return 1===t[1].length&&o.includes(t[1][0])}))||n.p||(c(e,i),a?l(e):n.p=Promise.resolve().then((function(){delete n.p,l(e)})))}))}),!0);a=[o,f,0,new Set],u.set(e,a)}a[0].add(t)},p=function(t){var n=t.s,e=t.d,r=s.get(e);null==r||r[0].delete(t),0===(null==r?void 0:r[0].size)&&s.delete(e);var i=u.get(n);if(i){var a=i[0],o=i[1];a.delete(t),a.size||(o(),u.delete(n))}},h=function(t){var n=s.get(t);return n?Array.from(n[0]):[]},v={add:d,remove:p,list:h};function y(t,e){var r=(null==e?void 0:e.proxy)||n.proxy({}),i=!(null==e||!e.sync),a=Object.keys(t);return a.forEach((function(e){if(Object.getOwnPropertyDescriptor(r,e))throw new Error("object property already defined");var o=t[e],u=null;!function t(){if(u){if(Array.from(u).map((function(n){var e=n[0];return f(e,t)})).some((function(t){return t})))return;if(Array.from(u).every((function(t){var e=t[0],r=t[1];return n.getVersion(e)===r.v})))return}var s=new Map,c=o((function(t){return s.set(t,{v:n.getVersion(t)}),t})),l=function(){var n;s.forEach((function(n,o){var s,c=null==(s=u)||null==(s=s.get(o))?void 0:s.s;if(c)n.s=c;else{var f={s:o,d:r,k:e,c:t,n:i,i:a};d(f),n.s=f}})),null==(n=u)||n.forEach((function(t,n){!s.has(n)&&t.s&&p(t.s)})),u=s};c instanceof Promise?c.finally(l):l(),r[e]=c}()})),r}var b,O=function t(e){if(b||(b=n.unstable_buildProxyFunction()[2]),"object"!=typeof(r=e)||null===r||b.has(e))return e;var r,i=Array.isArray(e)?[]:Object.create(Object.getPrototypeOf(e));return Reflect.ownKeys(e).forEach((function(n){i[n]=t(e[n])})),i};t.addComputed=function(t,n,e){void 0===e&&(e=t);var r={};return Object.keys(n).forEach((function(e){r[e]=function(r){return n[e](r(t))}})),y(r,{proxy:e})},t.derive=y,t.devtools=function(t,e){"string"==typeof e&&(console.warn("string name option is deprecated, use { name }. https://github.com/pmndrs/valtio/pull/400"),e={name:e});var r,u=e||{},s=u.enabled,c=u.name,f=void 0===c?"":c,l=function(t,n){if(null==t)return{};var e,r,i={},a=Object.keys(t);for(r=0;r<a.length;r++)e=a[r],n.indexOf(e)>=0||(i[e]=t[e]);return i}(u,a);try{r=null!=s&&s&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(t){}if(r){var d=!1,p=r.connect(i({name:f},l)),h=n.subscribe(t,(function(e){var r=e.filter((function(t){return t[0],t[1][0]!==o})).map((function(t){return t[0]+":"+t[1].map(String).join(".")})).join(", ");if(r)if(d)d=!1;else{var i=Object.assign({},n.snapshot(t));delete i[o],p.send({type:r,updatedAt:(new Date).toLocaleString()},i)}})),v=p.subscribe((function(e){var r,i;if("ACTION"===e.type&&e.payload)try{Object.assign(t,JSON.parse(e.payload))}catch(t){console.error("please dispatch a serializable value that JSON.parse() and proxy() support\n",t)}if("DISPATCH"===e.type&&e.state){var a,u;if("JUMP_TO_ACTION"===(null==(a=e.payload)?void 0:a.type)||"JUMP_TO_STATE"===(null==(u=e.payload)?void 0:u.type)){d=!0;var s=JSON.parse(e.state);Object.assign(t,s)}t[o]=e}else if("DISPATCH"===e.type&&"COMMIT"===(null==(r=e.payload)?void 0:r.type))p.init(n.snapshot(t));else if("DISPATCH"===e.type&&"IMPORT_STATE"===(null==(i=e.payload)?void 0:i.type)){var c,f,l=null==(c=e.payload.nextLiftedState)?void 0:c.actionsById,h=(null==(f=e.payload.nextLiftedState)?void 0:f.computedStates)||[];d=!0,h.forEach((function(e,r){var i=e.state,a=l[r]||"No action found";Object.assign(t,i),0===r?p.init(n.snapshot(t)):p.send(a,n.snapshot(t))}))}}));return p.init(n.snapshot(t)),function(){h(),null==v||v()}}},t.proxyMap=function(t){var e,i=n.proxy((r("get",e={data:Array.from(t||[]),has:function(t){return this.data.some((function(n){return n[0]===t}))},set:function(t,n){var e=this.data.find((function(n){return n[0]===t}));return e?e[1]=n:this.data.push([t,n]),this},get:function(t){var n;return null==(n=this.data.find((function(n){return n[0]===t})))?void 0:n[1]},delete:function(t){var n=this.data.findIndex((function(n){return n[0]===t}));return-1!==n&&(this.data.splice(n,1),!0)},clear:function(){this.data.splice(0)},get size(){return this.data.length},toJSON:function(){return new Map(this.data)},forEach:function(t){var n=this;this.data.forEach((function(e){t(e[1],e[0],n)}))},keys:function(){return this.data.map((function(t){return t[0]})).values()},values:function(){return this.data.map((function(t){return t[1]})).values()},entries:function(){return new Map(this.data).entries()}},Symbol.toStringTag,(function(){return"Map"})),e[Symbol.iterator]=function(){return this.entries()},e));return Object.defineProperties(i,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(i),i},t.proxySet=function(t){var e,i=n.proxy((r("get",e={data:Array.from(new Set(t)),has:function(t){return-1!==this.data.indexOf(t)},add:function(t){var e=!1;return"object"==typeof t&&null!==t&&(e=-1!==this.data.indexOf(n.proxy(t))),-1!==this.data.indexOf(t)||e||this.data.push(t),this},delete:function(t){var n=this.data.indexOf(t);return-1!==n&&(this.data.splice(n,1),!0)},clear:function(){this.data.splice(0)},get size(){return this.data.length},forEach:function(t){var n=this;this.data.forEach((function(e){t(e,e,n)}))}},Symbol.toStringTag,(function(){return"Set"})),e.toJSON=function(){return new Set(this.data)},e[Symbol.iterator]=function(){return this.data[Symbol.iterator]()},e.values=function(){return this.data.values()},e.keys=function(){return this.data.values()},e.entries=function(){return new Set(this.data).entries()},e));return Object.defineProperties(i,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(i),i},t.proxyWithComputed=function(t,e){Object.keys(e).forEach((function(i){if(Object.getOwnPropertyDescriptor(t,i))throw new Error("object property already defined");var a=e[i],o="function"==typeof a?{get:a}:a,u=o.get,s=o.set,c={get:function(){return u(n.snapshot(r))}};s&&(c.set=function(t){return s(r,t)}),Object.defineProperty(t,i,c)}));var r=n.proxy(t);return r},t.proxyWithHistory=function(t,e){void 0===e&&(e=!1);var r=n.proxy({value:t,history:n.ref({wip:void 0,snapshots:[],index:-1}),clone:O,canUndo:function(){return r.history.index>0},undo:function(){r.canUndo()&&(r.value=r.history.wip=r.clone(r.history.snapshots[--r.history.index]))},canRedo:function(){return r.history.index<r.history.snapshots.length-1},redo:function(){r.canRedo()&&(r.value=r.history.wip=r.clone(r.history.snapshots[++r.history.index]))},saveHistory:function(){r.history.snapshots.splice(r.history.index+1),r.history.snapshots.push(n.snapshot(r).value),++r.history.index},subscribe:function(){return n.subscribe(r,(function(t){t.every((function(t){return"value"===t[1][0]&&("set"!==t[0]||t[2]!==r.history.wip)}))&&r.saveHistory()}))}});return r.saveHistory(),e||r.subscribe(),r},t.subscribeKey=function(t,e,r,i){var a=t[e];return n.subscribe(t,(function(){var n=t[e];Object.is(a,n)||r(a=n)}),i)},t.underive=function(t,n){var e=null!=n&&n.delete?new Set:null;h(t).forEach((function(t){var r=t.k;null!=n&&n.keys&&!n.keys.includes(r)||(p(t),e&&e.add(r))})),e&&e.forEach((function(n){delete t[n]}))},t.unstable_deriveSubscriptions=v,t.watch=function(t,r){var i=!0,a=new Set,o=new Map,u=function(){i&&(i=!1,a.forEach((function(t){return t()})),a.clear(),o.forEach((function(t){return t()})),o.clear())};return e&&e.add(u),function u(){if(i){a.forEach((function(t){return t()})),a.clear();var s=new Set,c=e;e=a;try{var f=t((function(t){return s.add(t),t}));f&&a.add(f)}finally{e=c}o.forEach((function(t,n){s.has(n)?s.delete(n):(o.delete(n),t())})),s.forEach((function(t){var e=n.subscribe(t,u,null==r?void 0:r.sync);o.set(t,e)}))}}(),u}}));
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("valtio/vanilla"),require("derive-valtio")):"function"==typeof define&&define.amd?define(["exports","valtio/vanilla","derive-valtio"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).valtioVanillaUtils={},t.valtioVanilla,t.deriveValtio)}(this,(function(t,e,n){"use strict";var r;function i(t,e,n,r){var i={configurable:!0,enumerable:!0};return i[t]=r,Object.defineProperty(e,n,i)}function a(){return a=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},a.apply(this,arguments)}var o=["enabled","name"],u=Symbol();var s,c=function t(n){if(s||(s=e.unstable_buildProxyFunction()[2]),"object"!=typeof(r=n)||null===r||s.has(n))return n;var r,i=Array.isArray(n)?[]:Object.create(Object.getPrototypeOf(n));return Reflect.ownKeys(n).forEach((function(e){i[e]=t(n[e])})),i};Object.defineProperty(t,"derive",{enumerable:!0,get:function(){return n.derive}}),Object.defineProperty(t,"underive",{enumerable:!0,get:function(){return n.underive}}),Object.defineProperty(t,"unstable_deriveSubscriptions",{enumerable:!0,get:function(){return n.unstable_deriveSubscriptions}}),t.addComputed=function(t,e,r){void 0===r&&(r=t);var i={};return Object.keys(e).forEach((function(n){i[n]=function(r){return e[n](r(t))}})),n.derive(i,{proxy:r})},t.devtools=function(t,n){"string"==typeof n&&(console.warn("string name option is deprecated, use { name }. https://github.com/pmndrs/valtio/pull/400"),n={name:n});var r,i=n||{},s=i.enabled,c=i.name,f=void 0===c?"":c,d=function(t,e){if(null==t)return{};var n,r,i={},a=Object.keys(t);for(r=0;r<a.length;r++)n=a[r],e.indexOf(n)>=0||(i[n]=t[n]);return i}(i,o);try{r=null!=s&&s&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(t){}if(r){var l=!1,p=r.connect(a({name:f},d)),h=e.subscribe(t,(function(n){var r=n.filter((function(t){return t[0],t[1][0]!==u})).map((function(t){return t[0]+":"+t[1].map(String).join(".")})).join(", ");if(r)if(l)l=!1;else{var i=Object.assign({},e.snapshot(t));delete i[u],p.send({type:r,updatedAt:(new Date).toLocaleString()},i)}})),y=p.subscribe((function(n){var r,i;if("ACTION"===n.type&&n.payload)try{Object.assign(t,JSON.parse(n.payload))}catch(t){console.error("please dispatch a serializable value that JSON.parse() and proxy() support\n",t)}if("DISPATCH"===n.type&&n.state){var a,o;if("JUMP_TO_ACTION"===(null==(a=n.payload)?void 0:a.type)||"JUMP_TO_STATE"===(null==(o=n.payload)?void 0:o.type)){l=!0;var s=JSON.parse(n.state);Object.assign(t,s)}t[u]=n}else if("DISPATCH"===n.type&&"COMMIT"===(null==(r=n.payload)?void 0:r.type))p.init(e.snapshot(t));else if("DISPATCH"===n.type&&"IMPORT_STATE"===(null==(i=n.payload)?void 0:i.type)){var c,f,d=null==(c=n.payload.nextLiftedState)?void 0:c.actionsById,h=(null==(f=n.payload.nextLiftedState)?void 0:f.computedStates)||[];l=!0,h.forEach((function(n,r){var i=n.state,a=d[r]||"No action found";Object.assign(t,i),0===r?p.init(e.snapshot(t)):p.send(a,e.snapshot(t))}))}}));return p.init(e.snapshot(t)),function(){h(),null==y||y()}}},t.proxyMap=function(t){var n,r=e.proxy((i("get",n={data:Array.from(t||[]),has:function(t){return this.data.some((function(e){return e[0]===t}))},set:function(t,e){var n=this.data.find((function(e){return e[0]===t}));return n?n[1]=e:this.data.push([t,e]),this},get:function(t){var e;return null==(e=this.data.find((function(e){return e[0]===t})))?void 0:e[1]},delete:function(t){var e=this.data.findIndex((function(e){return e[0]===t}));return-1!==e&&(this.data.splice(e,1),!0)},clear:function(){this.data.splice(0)},get size(){return this.data.length},toJSON:function(){return new Map(this.data)},forEach:function(t){var e=this;this.data.forEach((function(n){t(n[1],n[0],e)}))},keys:function(){return this.data.map((function(t){return t[0]})).values()},values:function(){return this.data.map((function(t){return t[1]})).values()},entries:function(){return new Map(this.data).entries()}},Symbol.toStringTag,(function(){return"Map"})),n[Symbol.iterator]=function(){return this.entries()},n));return Object.defineProperties(r,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(r),r},t.proxySet=function(t){var n,r=e.proxy((i("get",n={data:Array.from(new Set(t)),has:function(t){return-1!==this.data.indexOf(t)},add:function(t){var n=!1;return"object"==typeof t&&null!==t&&(n=-1!==this.data.indexOf(e.proxy(t))),-1!==this.data.indexOf(t)||n||this.data.push(t),this},delete:function(t){var e=this.data.indexOf(t);return-1!==e&&(this.data.splice(e,1),!0)},clear:function(){this.data.splice(0)},get size(){return this.data.length},forEach:function(t){var e=this;this.data.forEach((function(n){t(n,n,e)}))}},Symbol.toStringTag,(function(){return"Set"})),n.toJSON=function(){return new Set(this.data)},n[Symbol.iterator]=function(){return this.data[Symbol.iterator]()},n.values=function(){return this.data.values()},n.keys=function(){return this.data.values()},n.entries=function(){return new Set(this.data).entries()},n));return Object.defineProperties(r,{data:{enumerable:!1},size:{enumerable:!1},toJSON:{enumerable:!1}}),Object.seal(r),r},t.proxyWithComputed=function(t,n){Object.keys(n).forEach((function(i){if(Object.getOwnPropertyDescriptor(t,i))throw new Error("object property already defined");var a=n[i],o="function"==typeof a?{get:a}:a,u=o.get,s=o.set,c={get:function(){return u(e.snapshot(r))}};s&&(c.set=function(t){return s(r,t)}),Object.defineProperty(t,i,c)}));var r=e.proxy(t);return r},t.proxyWithHistory=function(t,n){void 0===n&&(n=!1);var r=e.proxy({value:t,history:e.ref({wip:void 0,snapshots:[],index:-1}),clone:c,canUndo:function(){return r.history.index>0},undo:function(){r.canUndo()&&(r.value=r.history.wip=r.clone(r.history.snapshots[--r.history.index]))},canRedo:function(){return r.history.index<r.history.snapshots.length-1},redo:function(){r.canRedo()&&(r.value=r.history.wip=r.clone(r.history.snapshots[++r.history.index]))},saveHistory:function(){r.history.snapshots.splice(r.history.index+1),r.history.snapshots.push(e.snapshot(r).value),++r.history.index},subscribe:function(){return e.subscribe(r,(function(t){t.every((function(t){return"value"===t[1][0]&&("set"!==t[0]||t[2]!==r.history.wip)}))&&r.saveHistory()}))}});return r.saveHistory(),n||r.subscribe(),r},t.subscribeKey=function(t,n,r,i){var a=t[n];return e.subscribe(t,(function(){var e=t[n];Object.is(a,e)||r(a=e)}),i)},t.watch=function(t,n){var i=!0,a=new Set,o=new Map,u=function(){i&&(i=!1,a.forEach((function(t){return t()})),a.clear(),o.forEach((function(t){return t()})),o.clear())};return r&&r.add(u),function u(){if(i){a.forEach((function(t){return t()})),a.clear();var s=new Set,c=r;r=a;try{var f=t((function(t){return s.add(t),t}));f&&a.add(f)}finally{r=c}o.forEach((function(t,e){s.has(e)?s.delete(e):(o.delete(e),t())})),s.forEach((function(t){var r=e.subscribe(t,u,null==n?void 0:n.sync);o.set(t,r)}))}}(),u}}));
export { subscribeKey } from './utils/subscribeKey';
export { watch } from './utils/watch';
export { devtools } from './utils/devtools';
export { derive, underive, unstable_deriveSubscriptions, } from './utils/derive';
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio';
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed';

@@ -6,0 +6,0 @@ export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed';

'use strict';
var vanilla = require('valtio/vanilla');
var deriveValtio = require('derive-valtio');

@@ -204,210 +205,2 @@ function subscribeKey(proxyObject, key, callback, notifyInSync) {

var sourceObjectMap = new WeakMap();
var derivedObjectMap = new WeakMap();
var markPending = function markPending(sourceObject, callback) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
sourceObjectEntry[0].forEach(function (subscription) {
var derivedObject = subscription.d;
if (sourceObject !== derivedObject) {
markPending(derivedObject);
}
});
++sourceObjectEntry[2];
if (callback) {
sourceObjectEntry[3].add(callback);
}
}
};
var checkPending = function checkPending(sourceObject, callback) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry != null && sourceObjectEntry[2]) {
sourceObjectEntry[3].add(callback);
return true;
}
return false;
};
var unmarkPending = function unmarkPending(sourceObject) {
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
--sourceObjectEntry[2];
if (!sourceObjectEntry[2]) {
sourceObjectEntry[3].forEach(function (callback) {
return callback();
});
sourceObjectEntry[3].clear();
}
sourceObjectEntry[0].forEach(function (subscription) {
var derivedObject = subscription.d;
if (sourceObject !== derivedObject) {
unmarkPending(derivedObject);
}
});
}
};
var addSubscription = function addSubscription(subscription) {
var sourceObject = subscription.s,
derivedObject = subscription.d;
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (!derivedObjectEntry) {
derivedObjectEntry = [new Set()];
derivedObjectMap.set(subscription.d, derivedObjectEntry);
}
derivedObjectEntry[0].add(subscription);
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (!sourceObjectEntry) {
var _subscriptions = new Set();
var _unsubscribe = vanilla.subscribe(sourceObject, function (ops) {
_subscriptions.forEach(function (subscription) {
var derivedObject = subscription.d,
callback = subscription.c,
notifyInSync = subscription.n,
ignoreKeys = subscription.i;
if (sourceObject === derivedObject && ops.every(function (op) {
return op[1].length === 1 && ignoreKeys.includes(op[1][0]);
})) {
return;
}
if (subscription.p) {
return;
}
markPending(sourceObject, callback);
if (notifyInSync) {
unmarkPending(sourceObject);
} else {
subscription.p = Promise.resolve().then(function () {
delete subscription.p;
unmarkPending(sourceObject);
});
}
});
}, true);
sourceObjectEntry = [_subscriptions, _unsubscribe, 0, new Set()];
sourceObjectMap.set(sourceObject, sourceObjectEntry);
}
sourceObjectEntry[0].add(subscription);
};
var removeSubscription = function removeSubscription(subscription) {
var sourceObject = subscription.s,
derivedObject = subscription.d;
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
derivedObjectEntry == null || derivedObjectEntry[0].delete(subscription);
if ((derivedObjectEntry == null ? void 0 : derivedObjectEntry[0].size) === 0) {
derivedObjectMap.delete(derivedObject);
}
var sourceObjectEntry = sourceObjectMap.get(sourceObject);
if (sourceObjectEntry) {
var _subscriptions2 = sourceObjectEntry[0],
_unsubscribe2 = sourceObjectEntry[1];
_subscriptions2.delete(subscription);
if (!_subscriptions2.size) {
_unsubscribe2();
sourceObjectMap.delete(sourceObject);
}
}
};
var listSubscriptions = function listSubscriptions(derivedObject) {
var derivedObjectEntry = derivedObjectMap.get(derivedObject);
if (derivedObjectEntry) {
return Array.from(derivedObjectEntry[0]);
}
return [];
};
var unstable_deriveSubscriptions = {
add: addSubscription,
remove: removeSubscription,
list: listSubscriptions
};
function derive(derivedFns, options) {
var proxyObject = (options == null ? void 0 : options.proxy) || vanilla.proxy({});
var notifyInSync = !!(options != null && options.sync);
var derivedKeys = Object.keys(derivedFns);
derivedKeys.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).map(function (_ref) {
var p = _ref[0];
return checkPending(p, evaluate);
}).some(function (isPending) {
return isPending;
})) {
return;
}
if (Array.from(lastDependencies).every(function (_ref2) {
var p = _ref2[0],
entry = _ref2[1];
return vanilla.getVersion(p) === entry.v;
})) {
return;
}
}
var dependencies = new Map();
var get = function get(p) {
dependencies.set(p, {
v: vanilla.getVersion(p)
});
return p;
};
var value = fn(get);
var subscribeToDependencies = function subscribeToDependencies() {
var _lastDependencies2;
dependencies.forEach(function (entry, p) {
var _lastDependencies;
var lastSubscription = (_lastDependencies = lastDependencies) == null || (_lastDependencies = _lastDependencies.get(p)) == null ? void 0 : _lastDependencies.s;
if (lastSubscription) {
entry.s = lastSubscription;
} else {
var subscription = {
s: p,
d: proxyObject,
k: key,
c: evaluate,
n: notifyInSync,
i: derivedKeys
};
addSubscription(subscription);
entry.s = subscription;
}
});
(_lastDependencies2 = lastDependencies) == null || _lastDependencies2.forEach(function (entry, p) {
if (!dependencies.has(p) && entry.s) {
removeSubscription(entry.s);
}
});
lastDependencies = dependencies;
};
if (value instanceof Promise) {
value.finally(subscribeToDependencies);
} else {
subscribeToDependencies();
}
proxyObject[key] = value;
};
evaluate();
});
return proxyObject;
}
function underive(proxyObject, options) {
var keysToDelete = options != null && options.delete ? new Set() : null;
listSubscriptions(proxyObject).forEach(function (subscription) {
var key = subscription.k;
if (!(options != null && options.keys) || options.keys.includes(key)) {
removeSubscription(subscription);
if (keysToDelete) {
keysToDelete.add(key);
}
}
});
if (keysToDelete) {
keysToDelete.forEach(function (key) {
delete proxyObject[key];
});
}
}
function addComputed_DEPRECATED(proxyObject, computedFns_FAKE, targetObject) {

@@ -426,3 +219,3 @@ if (targetObject === void 0) {

});
return derive(derivedFns, {
return deriveValtio.derive(derivedFns, {
proxy: targetObject

@@ -677,4 +470,15 @@ });

Object.defineProperty(exports, 'derive', {
enumerable: true,
get: function () { return deriveValtio.derive; }
});
Object.defineProperty(exports, 'underive', {
enumerable: true,
get: function () { return deriveValtio.underive; }
});
Object.defineProperty(exports, 'unstable_deriveSubscriptions', {
enumerable: true,
get: function () { return deriveValtio.unstable_deriveSubscriptions; }
});
exports.addComputed = addComputed_DEPRECATED;
exports.derive = derive;
exports.devtools = devtools;

@@ -686,4 +490,2 @@ exports.proxyMap = proxyMap;

exports.subscribeKey = subscribeKey;
exports.underive = underive;
exports.unstable_deriveSubscriptions = unstable_deriveSubscriptions;
exports.watch = watch;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc