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

proxy-state-tree

Package Overview
Dependencies
Maintainers
1
Versions
865
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proxy-state-tree - npm Package Compare versions

Comparing version 1.0.0-alpha4 to 1.0.0-alpha5

dist/proxy-statet-ree.cjs.js

197

dist/proxy-state-tree.cjs.js

@@ -9,106 +9,119 @@ 'use strict';

const IS_PROXY = Symbol('IS_PROXY');
const IS_PROXY = Symbol("IS_PROXY");
function concat(path, prop) {
return path === undefined ? prop : path + '.' + prop;
return path === undefined ? prop : path + "." + prop;
}
const arrayMutations = new Set([ 'push', 'shift', 'pop', 'unshift', 'splice' ]);
const arrayMutations = new Set([
"push",
"shift",
"pop",
"unshift",
"splice",
"reverse",
"sort"
]);
function createArrayProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
if (prop === 'length' || (typeof target[prop] === 'function' && !arrayMutations.has(prop))) {
return target[prop];
}
if (
prop === "length" ||
(typeof target[prop] === "function" && !arrayMutations.has(prop))
) {
return target[prop];
}
const nestedPath = concat(path, prop);
const nestedPath = concat(path, prop);
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (arrayMutations.has(prop)) {
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
return (...args) => {
tree.mutations.push({
method: prop,
path: path,
args: args
});
if (arrayMutations.has(prop)) {
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
return (...args) => {
tree.mutations.push({
method: prop,
path: path,
args: args
});
return target[prop](...args);
};
}
return target[prop](...args);
};
}
return (target[prop] = proxify(tree, target[prop], nestedPath));
}
});
return (target[prop] = proxify(tree, target[prop], nestedPath));
}
});
}
function createObjectProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
const value = target[prop];
const nestedPath = concat(path, prop);
const value = target[prop];
const nestedPath = concat(path, prop);
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (typeof value === 'function') {
return value(tree, nestedPath);
}
if (typeof value === "function") {
return value(tree, nestedPath);
}
return (target[prop] = proxify(tree, value, nestedPath));
},
set(target, prop, value) {
const nestedPath = concat(path, prop);
return (target[prop] = proxify(tree, value, nestedPath));
},
set(target, prop, value) {
const nestedPath = concat(path, prop);
if (!tree.isTrackingMutations) {
throw new Error(`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`);
}
tree.mutations.push({
method: 'set',
path: nestedPath,
args: [ value ]
});
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
tree.mutations.push({
method: "set",
path: nestedPath,
args: [value]
});
return (target[prop] = value);
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop);
return (target[prop] = value);
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop);
tree.mutations.push({
method: 'unset',
path: nestedPath,
args: []
});
tree.mutations.push({
method: "unset",
path: nestedPath,
args: []
});
delete target[prop];
delete target[prop];
return true;
}
});
return true;
}
});
}
function proxify(tree, value, path) {
if (value) {
if (value[IS_PROXY]) {
return value;
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path);
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path);
}
}
return value;
if (value) {
if (value[IS_PROXY]) {
return value;
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path);
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path);
}
}
return value;
}

@@ -129,2 +142,21 @@

}
flush() {
const pathCallbacksCalled = [];
for (let mutation in this.mutations) {
const path = this.mutations[mutation].path;
if (this.pathDependencies[path]) {
for (let pathCallback in this.pathDependencies[path]) {
const callback = this.pathDependencies[path][pathCallback];
if (pathCallbacksCalled.indexOf(callback) === -1) {
pathCallbacksCalled.push(callback);
callback();
}
}
}
}
pathCallbacksCalled.length = 0;
}
startMutationTracking() {

@@ -139,13 +171,2 @@ const currentMutations = this.mutations.slice();

stopMutationTracking() {
for (let callback in this.mutationCallbacks) {
this.mutationCallbacks[callback](this.mutations);
}
for (let mutation in this.mutations) {
const path = this.mutations[mutation].path;
if (this.pathDependencies[path]) {
for (let pathCallback in this.pathDependencies[path]) {
this.pathDependencies[path][pathCallback]();
}
}
}
this.isTrackingMutations = false;

@@ -152,0 +173,0 @@

import isPlainObject from 'is-plain-object';
const IS_PROXY = Symbol('IS_PROXY');
const IS_PROXY = Symbol("IS_PROXY");
function concat(path, prop) {
return path === undefined ? prop : path + '.' + prop;
return path === undefined ? prop : path + "." + prop;
}
const arrayMutations = new Set([ 'push', 'shift', 'pop', 'unshift', 'splice' ]);
const arrayMutations = new Set([
"push",
"shift",
"pop",
"unshift",
"splice",
"reverse",
"sort"
]);
function createArrayProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
if (prop === 'length' || (typeof target[prop] === 'function' && !arrayMutations.has(prop))) {
return target[prop];
}
if (
prop === "length" ||
(typeof target[prop] === "function" && !arrayMutations.has(prop))
) {
return target[prop];
}
const nestedPath = concat(path, prop);
const nestedPath = concat(path, prop);
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (arrayMutations.has(prop)) {
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
return (...args) => {
tree.mutations.push({
method: prop,
path: path,
args: args
});
if (arrayMutations.has(prop)) {
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
return (...args) => {
tree.mutations.push({
method: prop,
path: path,
args: args
});
return target[prop](...args);
};
}
return target[prop](...args);
};
}
return (target[prop] = proxify(tree, target[prop], nestedPath));
}
});
return (target[prop] = proxify(tree, target[prop], nestedPath));
}
});
}
function createObjectProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
const value = target[prop];
const nestedPath = concat(path, prop);
const value = target[prop];
const nestedPath = concat(path, prop);
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (tree.isTrackingPaths) {
tree.paths.add(nestedPath);
}
if (typeof value === 'function') {
return value(tree, nestedPath);
}
if (typeof value === "function") {
return value(tree, nestedPath);
}
return (target[prop] = proxify(tree, value, nestedPath));
},
set(target, prop, value) {
const nestedPath = concat(path, prop);
return (target[prop] = proxify(tree, value, nestedPath));
},
set(target, prop, value) {
const nestedPath = concat(path, prop);
if (!tree.isTrackingMutations) {
throw new Error(`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`);
}
tree.mutations.push({
method: 'set',
path: nestedPath,
args: [ value ]
});
if (!tree.isTrackingMutations) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
tree.mutations.push({
method: "set",
path: nestedPath,
args: [value]
});
return (target[prop] = value);
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop);
return (target[prop] = value);
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop);
tree.mutations.push({
method: 'unset',
path: nestedPath,
args: []
});
tree.mutations.push({
method: "unset",
path: nestedPath,
args: []
});
delete target[prop];
delete target[prop];
return true;
}
});
return true;
}
});
}
function proxify(tree, value, path) {
if (value) {
if (value[IS_PROXY]) {
return value;
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path);
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path);
}
}
return value;
if (value) {
if (value[IS_PROXY]) {
return value;
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path);
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path);
}
}
return value;
}

@@ -122,2 +135,21 @@

}
flush() {
const pathCallbacksCalled = [];
for (let mutation in this.mutations) {
const path = this.mutations[mutation].path;
if (this.pathDependencies[path]) {
for (let pathCallback in this.pathDependencies[path]) {
const callback = this.pathDependencies[path][pathCallback];
if (pathCallbacksCalled.indexOf(callback) === -1) {
pathCallbacksCalled.push(callback);
callback();
}
}
}
}
pathCallbacksCalled.length = 0;
}
startMutationTracking() {

@@ -132,13 +164,2 @@ const currentMutations = this.mutations.slice();

stopMutationTracking() {
for (let callback in this.mutationCallbacks) {
this.mutationCallbacks[callback](this.mutations);
}
for (let mutation in this.mutations) {
const path = this.mutations[mutation].path;
if (this.pathDependencies[path]) {
for (let pathCallback in this.pathDependencies[path]) {
this.pathDependencies[path][pathCallback]();
}
}
}
this.isTrackingMutations = false;

@@ -145,0 +166,0 @@

{
"name": "proxy-state-tree",
"version": "1.0.0-alpha4",
"version": "1.0.0-alpha5",
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors",

@@ -5,0 +5,0 @@ "main": "dist/proxy-state-tree.cjs.js",

@@ -49,6 +49,6 @@ # proxy-state-tree

console.log(paths) // [['foo'], ['bar']]
console.log(paths) // ['foo', 'bar']
```
You would typically use this mechanism to track usage of state. For example rendering a component, calculating a a computed value etc. The returned paths array is stored for later usage. The paths structure is used internally by proxy-state-tree, but you can also consume it as a library author to for example showing components and what paths they depend on in a devtool.
You would typically use this mechanism to track usage of state. For example rendering a component, calculating a a computed value etc. The returned paths array is stored for later usage. The paths structure is used internally by proxy-state-tree, but you can also consume it as a library author to for example showing components and what paths they depend on in a devtool. Nested paths uses dot notation, for example `['foo.bar']`.

@@ -85,3 +85,3 @@ ## Track mutations

You would use **startMutationTracking** and **stopMutationTracking** around logic that is allowed to do mutations, for example actions or similar. Internally **proxy-state-tree** will notify all mutation listeners about updated state, but you can also use this structure in combination with a devtool. Show a list of mutations that occurs in your app, and what action performed the mutation even.
You would use **startMutationTracking** and **stopMutationTracking** around logic that is allowed to do mutations, for example actions or similar. Trying to mutate without this tracking active results in an error. The returned array can be used in combination with a devtool.

@@ -114,5 +114,2 @@ ## Check need to update

listener.update(render())
// Remove listener
listener.dispose()
})

@@ -124,2 +121,9 @@

tree.stopMutationTracking()
// This command flushes out the current mutations and
// notifies any listeners
tree.flush()
// Remove listener
listener.dispose()
```

@@ -126,0 +130,0 @@

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