Skip to content

Commit 54ae891

Browse files
feat(proxy-state-tree): expose option to wrap functions in the tree to inject custom stuff
1 parent 30e9937 commit 54ae891

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

packages/node_modules/proxy-state-tree/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,18 @@ const tree = new ProxyStateTree({
138138
})
139139
```
140140

141-
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).
141+
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).
142+
143+
You can inject a wrapper around this function by:
144+
145+
```js
146+
import ProxyStateTree from 'proxy-state-tree'
147+
148+
const tree = new ProxyStateTree({
149+
foo: (foo, proxyStateTree, path) => {}
150+
}, {
151+
dynamicWrapper: (proxyStateTree, path, func) => func('foo', proxyStateTree, path)
152+
})
153+
```
154+
155+
This helps you expose library entities to these functions.

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,23 @@ describe('FUNCTIONS', () => {
369369
}
370370
const tree = new ProxyStateTree(state)
371371

372+
expect(tree.get().foo).toBe('bar')
373+
})
374+
test('should be able to inject a wrapper around functions', () => {
375+
const state = {
376+
foo: (foo, proxyStateTree, path) => {
377+
expect(foo).toBe('foo')
378+
expect(proxyStateTree).toBe(tree)
379+
expect(path).toEqual('foo')
380+
381+
return 'bar'
382+
},
383+
}
384+
const tree = new ProxyStateTree(state, {
385+
dynamicWrapper: (proxyStateTree, path, func) =>
386+
func('foo', proxyStateTree, path),
387+
})
388+
372389
expect(tree.get().foo).toBe('bar')
373390
})
374391
})

packages/node_modules/proxy-state-tree/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import proxify, { IS_PROXY, STATUS } from './proxify'
22
const isPlainObject = require('is-plain-object')
33

44
export type Options = {
5-
devmode: boolean
5+
devmode?: boolean
6+
dynamicWrapper?: Function
67
}
78

89
type Mutation = {
@@ -21,11 +22,15 @@ class ProxyStateTree {
2122
paths: Set<string>[]
2223
status: STATUS
2324
proxy: any
24-
constructor(state: object, options: Options = { devmode: true }) {
25+
constructor(state: object, options: Options = {}) {
2526
if (!isPlainObject(state)) {
2627
throw new Error('You have to pass a plain object to the Proxy State Tree')
2728
}
2829

30+
if (typeof options.devmode === 'undefined') {
31+
options.devmode = true
32+
}
33+
2934
this.state = state
3035
this.options = options
3136
this.pathDependencies = {}

packages/node_modules/proxy-state-tree/src/proxify.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ function createObjectProxy(tree, value, path) {
112112
}
113113

114114
if (typeof targetValue === 'function') {
115-
return targetValue(tree, nestedPath)
115+
return tree.options.dynamicWrapper
116+
? tree.options.dynamicWrapper(tree, nestedPath, targetValue)
117+
: targetValue(tree, nestedPath)
116118
}
117119

118120
if (targetValue === undefined) {

0 commit comments

Comments
 (0)