proxy-state-tree
Advanced tools
Comparing version 1.0.0-1532018253864 to 1.0.0-1532031987960
import { IS_PROXY, STATUS } from './proxify'; | ||
export declare type Options = { | ||
devmode: boolean; | ||
devmode?: boolean; | ||
dynamicWrapper?: Function; | ||
}; | ||
@@ -5,0 +6,0 @@ declare type Mutation = { |
import proxify, { IS_PROXY, STATUS } from './proxify'; | ||
const isPlainObject = require('is-plain-object'); | ||
class ProxyStateTree { | ||
constructor(state, options = { devmode: true }) { | ||
constructor(state, options = {}) { | ||
if (!isPlainObject(state)) { | ||
throw new Error('You have to pass a plain object to the Proxy State Tree'); | ||
} | ||
if (typeof options.devmode === 'undefined') { | ||
options.devmode = true; | ||
} | ||
this.state = state; | ||
@@ -9,0 +12,0 @@ this.options = options; |
@@ -350,2 +350,16 @@ import ProxyStateTree, { IS_PROXY } from './'; | ||
}); | ||
test('should be able to inject a wrapper around functions', () => { | ||
const state = { | ||
foo: (foo, proxyStateTree, path) => { | ||
expect(foo).toBe('foo'); | ||
expect(proxyStateTree).toBe(tree); | ||
expect(path).toEqual('foo'); | ||
return 'bar'; | ||
}, | ||
}; | ||
const tree = new ProxyStateTree(state, { | ||
dynamicWrapper: (proxyStateTree, path, func) => func('foo', proxyStateTree, path), | ||
}); | ||
expect(tree.get().foo).toBe('bar'); | ||
}); | ||
}); | ||
@@ -352,0 +366,0 @@ describe('REACTIONS', () => { |
@@ -96,3 +96,5 @@ const isPlainObject = require('is-plain-object'); | ||
if (typeof targetValue === 'function') { | ||
return targetValue(tree, nestedPath); | ||
return tree.options.dynamicWrapper | ||
? tree.options.dynamicWrapper(tree, nestedPath, targetValue) | ||
: targetValue(tree, nestedPath); | ||
} | ||
@@ -99,0 +101,0 @@ if (targetValue === undefined) { |
import { IS_PROXY, STATUS } from './proxify'; | ||
export declare type Options = { | ||
devmode: boolean; | ||
devmode?: boolean; | ||
dynamicWrapper?: Function; | ||
}; | ||
@@ -5,0 +6,0 @@ declare type Mutation = { |
@@ -7,6 +7,9 @@ "use strict"; | ||
class ProxyStateTree { | ||
constructor(state, options = { devmode: true }) { | ||
constructor(state, options = {}) { | ||
if (!isPlainObject(state)) { | ||
throw new Error('You have to pass a plain object to the Proxy State Tree'); | ||
} | ||
if (typeof options.devmode === 'undefined') { | ||
options.devmode = true; | ||
} | ||
this.state = state; | ||
@@ -13,0 +16,0 @@ this.options = options; |
@@ -352,2 +352,16 @@ "use strict"; | ||
}); | ||
test('should be able to inject a wrapper around functions', () => { | ||
const state = { | ||
foo: (foo, proxyStateTree, path) => { | ||
expect(foo).toBe('foo'); | ||
expect(proxyStateTree).toBe(tree); | ||
expect(path).toEqual('foo'); | ||
return 'bar'; | ||
}, | ||
}; | ||
const tree = new _1.default(state, { | ||
dynamicWrapper: (proxyStateTree, path, func) => func('foo', proxyStateTree, path), | ||
}); | ||
expect(tree.get().foo).toBe('bar'); | ||
}); | ||
}); | ||
@@ -354,0 +368,0 @@ describe('REACTIONS', () => { |
@@ -98,3 +98,5 @@ "use strict"; | ||
if (typeof targetValue === 'function') { | ||
return targetValue(tree, nestedPath); | ||
return tree.options.dynamicWrapper | ||
? tree.options.dynamicWrapper(tree, nestedPath, targetValue) | ||
: targetValue(tree, nestedPath); | ||
} | ||
@@ -101,0 +103,0 @@ if (targetValue === undefined) { |
{ | ||
"name": "proxy-state-tree", | ||
"version": "1.0.0-1532018253864", | ||
"version": "1.0.0-1532031987960", | ||
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -141,2 +141,16 @@ # proxy-state-tree | ||
The allows you to easily extend functionality with for example a computed concept that lives in the tree, as you can see in this [codesandbox](https://codesandbox.io/s/xnv45zmkz). | ||
The allows you to easily extend functionality with for example a computed concept that lives in the tree, as you can see in this [codesandbox](https://codesandbox.io/s/xnv45zmkz). | ||
You can inject a wrapper around this function by: | ||
```js | ||
import ProxyStateTree from 'proxy-state-tree' | ||
const tree = new ProxyStateTree({ | ||
foo: (foo, proxyStateTree, path) => {} | ||
}, { | ||
dynamicWrapper: (proxyStateTree, path, func) => func('foo', proxyStateTree, path) | ||
}) | ||
``` | ||
This helps you expose library entities to these functions. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
205406
2062
155