Comparing version 0.0.1 to 0.1.0
@@ -43,3 +43,6 @@ 'use strict'; | ||
var create = function create(initialObject) { | ||
var globalVersion = 0; | ||
var snapshotCache = new WeakMap(); | ||
var createProxy = function createProxy(initialObject) { | ||
if (initialObject === void 0) { | ||
@@ -49,20 +52,24 @@ initialObject = {}; | ||
var version = 0; | ||
var snapshotVersion = -1; | ||
var savedSnapshot; | ||
var version = globalVersion; | ||
var mutableSource; | ||
var listners = new Set(); | ||
var incrementVersion = function incrementVersion() { | ||
++version; | ||
listners.forEach(function (listener) { | ||
return listener(); | ||
}); | ||
var notifyUpdate = function notifyUpdate(nextVersion) { | ||
if (!nextVersion) { | ||
nextVersion = ++globalVersion; | ||
} | ||
if (version !== nextVersion) { | ||
version = nextVersion; | ||
listners.forEach(function (listener) { | ||
return listener(nextVersion); | ||
}); | ||
} | ||
}; | ||
var proxy = new Proxy(Object.create(initialObject.constructor.prototype), { | ||
get: function get(target, prop) { | ||
get: function get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(proxy); | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
@@ -78,7 +85,13 @@ | ||
if (prop === SNAPSHOT) { | ||
if (version === snapshotVersion) { | ||
return savedSnapshot; | ||
var cache = snapshotCache.get(receiver); | ||
if (cache && cache.version === version) { | ||
return cache.snapshot; | ||
} | ||
var snapshot = Object.create(target.constructor.prototype); | ||
snapshotCache.set(receiver, { | ||
version: version, | ||
snapshot: snapshot | ||
}); | ||
Reflect.ownKeys(target).forEach(function (key) { | ||
@@ -93,4 +106,2 @@ var value = target[key]; | ||
}); | ||
savedSnapshot = snapshot; | ||
snapshotVersion = version; | ||
return snapshot; | ||
@@ -105,7 +116,7 @@ } | ||
if (isObject(value)) { | ||
value[LISTNERS].delete(incrementVersion); | ||
value[LISTNERS].delete(notifyUpdate); | ||
} | ||
delete target[prop]; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -115,3 +126,3 @@ }, | ||
if (isObject(target[prop])) { | ||
target[prop][LISTNERS].delete(incrementVersion); | ||
target[prop][LISTNERS].delete(notifyUpdate); | ||
} | ||
@@ -127,6 +138,6 @@ | ||
} else { | ||
target[prop] = create(value); | ||
target[prop] = createProxy(value); | ||
} | ||
target[prop][LISTNERS].add(incrementVersion); | ||
target[prop][LISTNERS].add(notifyUpdate); | ||
} else { | ||
@@ -136,3 +147,3 @@ target[prop] = value; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -183,3 +194,3 @@ } | ||
exports.create = create; | ||
exports.proxy = createProxy; | ||
exports.useProxy = useProxy; |
@@ -1,2 +0,3 @@ | ||
export declare const create: <T extends object>(initialObject?: T) => T; | ||
export declare const useProxy: <T extends object>(proxy: T) => T; | ||
declare const createProxy: <T extends object>(initialObject?: T) => T; | ||
declare const useProxy: <T extends object>(proxy: T) => T; | ||
export { createProxy as proxy, useProxy }; |
@@ -38,3 +38,6 @@ var valtio = (function (exports, react, proxyCompare, useSubscription) { | ||
var create = function create(initialObject) { | ||
var globalVersion = 0; | ||
var snapshotCache = new WeakMap(); | ||
var createProxy = function createProxy(initialObject) { | ||
if (initialObject === void 0) { | ||
@@ -44,20 +47,24 @@ initialObject = {}; | ||
var version = 0; | ||
var snapshotVersion = -1; | ||
var savedSnapshot; | ||
var version = globalVersion; | ||
var mutableSource; | ||
var listners = new Set(); | ||
var incrementVersion = function incrementVersion() { | ||
++version; | ||
listners.forEach(function (listener) { | ||
return listener(); | ||
}); | ||
var notifyUpdate = function notifyUpdate(nextVersion) { | ||
if (!nextVersion) { | ||
nextVersion = ++globalVersion; | ||
} | ||
if (version !== nextVersion) { | ||
version = nextVersion; | ||
listners.forEach(function (listener) { | ||
return listener(nextVersion); | ||
}); | ||
} | ||
}; | ||
var proxy = new Proxy(Object.create(initialObject.constructor.prototype), { | ||
get: function get(target, prop) { | ||
get: function get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(proxy); | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
@@ -73,7 +80,13 @@ | ||
if (prop === SNAPSHOT) { | ||
if (version === snapshotVersion) { | ||
return savedSnapshot; | ||
var cache = snapshotCache.get(receiver); | ||
if (cache && cache.version === version) { | ||
return cache.snapshot; | ||
} | ||
var snapshot = Object.create(target.constructor.prototype); | ||
snapshotCache.set(receiver, { | ||
version: version, | ||
snapshot: snapshot | ||
}); | ||
Reflect.ownKeys(target).forEach(function (key) { | ||
@@ -88,4 +101,2 @@ var value = target[key]; | ||
}); | ||
savedSnapshot = snapshot; | ||
snapshotVersion = version; | ||
return snapshot; | ||
@@ -100,7 +111,7 @@ } | ||
if (isObject(value)) { | ||
value[LISTNERS].delete(incrementVersion); | ||
value[LISTNERS].delete(notifyUpdate); | ||
} | ||
delete target[prop]; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -110,3 +121,3 @@ }, | ||
if (isObject(target[prop])) { | ||
target[prop][LISTNERS].delete(incrementVersion); | ||
target[prop][LISTNERS].delete(notifyUpdate); | ||
} | ||
@@ -122,6 +133,6 @@ | ||
} else { | ||
target[prop] = create(value); | ||
target[prop] = createProxy(value); | ||
} | ||
target[prop][LISTNERS].add(incrementVersion); | ||
target[prop][LISTNERS].add(notifyUpdate); | ||
} else { | ||
@@ -131,3 +142,3 @@ target[prop] = value; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -178,3 +189,3 @@ } | ||
exports.create = create; | ||
exports.proxy = createProxy; | ||
exports.useProxy = useProxy; | ||
@@ -181,0 +192,0 @@ |
51
index.js
@@ -29,19 +29,26 @@ import { useMemo, useRef, useEffect } from 'react'; | ||
const create = (initialObject = {}) => { | ||
let version = 0; | ||
let snapshotVersion = -1; | ||
let savedSnapshot; | ||
let globalVersion = 0; | ||
const snapshotCache = new WeakMap(); | ||
const createProxy = (initialObject = {}) => { | ||
let version = globalVersion; | ||
let mutableSource; | ||
const listners = new Set(); | ||
const incrementVersion = () => { | ||
++version; | ||
listners.forEach(listener => listener()); | ||
const notifyUpdate = nextVersion => { | ||
if (!nextVersion) { | ||
nextVersion = ++globalVersion; | ||
} | ||
if (version !== nextVersion) { | ||
version = nextVersion; | ||
listners.forEach(listener => listener(nextVersion)); | ||
} | ||
}; | ||
const proxy = new Proxy(Object.create(initialObject.constructor.prototype), { | ||
get(target, prop) { | ||
get(target, prop, receiver) { | ||
if (prop === MUTABLE_SOURCE) { | ||
if (!mutableSource) { | ||
mutableSource = createMutableSource(proxy); | ||
mutableSource = createMutableSource(receiver); | ||
} | ||
@@ -57,7 +64,13 @@ | ||
if (prop === SNAPSHOT) { | ||
if (version === snapshotVersion) { | ||
return savedSnapshot; | ||
const cache = snapshotCache.get(receiver); | ||
if (cache && cache.version === version) { | ||
return cache.snapshot; | ||
} | ||
const snapshot = Object.create(target.constructor.prototype); | ||
snapshotCache.set(receiver, { | ||
version, | ||
snapshot | ||
}); | ||
Reflect.ownKeys(target).forEach(key => { | ||
@@ -72,4 +85,2 @@ const value = target[key]; | ||
}); | ||
savedSnapshot = snapshot; | ||
snapshotVersion = version; | ||
return snapshot; | ||
@@ -85,7 +96,7 @@ } | ||
if (isObject(value)) { | ||
value[LISTNERS].delete(incrementVersion); | ||
value[LISTNERS].delete(notifyUpdate); | ||
} | ||
delete target[prop]; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -96,3 +107,3 @@ }, | ||
if (isObject(target[prop])) { | ||
target[prop][LISTNERS].delete(incrementVersion); | ||
target[prop][LISTNERS].delete(notifyUpdate); | ||
} | ||
@@ -108,6 +119,6 @@ | ||
} else { | ||
target[prop] = create(value); | ||
target[prop] = createProxy(value); | ||
} | ||
target[prop][LISTNERS].add(incrementVersion); | ||
target[prop][LISTNERS].add(notifyUpdate); | ||
} else { | ||
@@ -117,3 +128,3 @@ target[prop] = value; | ||
incrementVersion(); | ||
notifyUpdate(); | ||
return true; | ||
@@ -163,2 +174,2 @@ } | ||
export { create, useProxy }; | ||
export { createProxy as proxy, useProxy }; |
{ | ||
"name": "valtio", | ||
"private": false, | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -6,0 +6,0 @@ "main": "index.cjs.js", |
@@ -1,30 +0,1 @@ | ||
# Valtio | ||
[![Build Status](https://img.shields.io/github/workflow/status/pmndrs/valtio/Lint?style=flat&colorA=000000&colorB=000000)](https://github.com/pmndrs/valtio/actions?query=workflow%3ALint) | ||
[![Build Size](https://img.shields.io/bundlephobia/min/valtio?label=bundle%20size&style=flat&colorA=000000&colorB=000000)](https://bundlephobia.com/result?p=valtio) | ||
[![Version](https://img.shields.io/npm/v/valtio?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/valtio) | ||
[![Downloads](https://img.shields.io/npm/dt/valtio.svg?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/valtio) | ||
[![Discord Shield](https://img.shields.io/discord/740090768164651008?style=flat&colorA=000000&colorB=000000&label=discord&logo=discord&logoColor=ffffff)](https://discord.gg/ZZjjNvJ) | ||
## Quick Start | ||
```jsx | ||
import { create, useProxy } from 'valtio' | ||
const globalState = create({ count: 0, text: 'hello' }) | ||
setInterval(() => { | ||
++globalState.count | ||
}, 1000) | ||
const Counter = () => { | ||
const snapshot = useProxy(globalState); | ||
return ( | ||
<div> | ||
{snapshot.count} | ||
<button onClick={() => ++globalState.count}>+1</button> | ||
{Math.random()} | ||
</div> | ||
); | ||
}; | ||
``` | ||
![](/readme.svg) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
455
0
16550
2