Skip to content

Commit 2bf2e43

Browse files
State tree improvements (cerebral#306)
State tree improvements
2 parents 8aea60d + 752ae6d commit 2bf2e43

File tree

4 files changed

+130
-62
lines changed

4 files changed

+130
-62
lines changed
Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
{
2-
"name": "proxy-state-tree",
3-
"version": "1.0.0-beta4",
4-
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors",
5-
"main": "lib/index.js",
6-
"module": "es/index.js",
7-
"types": "lib/index.d.ts",
8-
"scripts": {
9-
"build": "npm run build:lib & npm run build:es",
10-
"build:lib": "tsc --outDir lib --module commonjs",
11-
"build:es": "tsc --outDir es --module es2015",
12-
"clean": "rimraf es lib coverage",
13-
"typecheck": "tsc --noEmit",
14-
"test": "jest --runInBand",
2+
"name": "proxy-state-tree",
3+
"version": "1.0.0-beta4",
4+
"description": "An implementation of the Mobx/Vue state tracking approach, for library authors",
5+
"main": "lib/index.js",
6+
"module": "es/index.js",
7+
"types": "lib/index.d.ts",
8+
"scripts": {
9+
"build": "(npm run build:lib & npm run build:es) && npm run build:dist",
10+
"build:lib": "tsc --outDir lib --module commonjs",
11+
"build:es": "tsc --outDir es --module es2015",
12+
"build:dist": "webpack --config webpack.config.js",
13+
"clean": "rimraf es lib coverage",
14+
"typecheck": "tsc --noEmit",
15+
"test": "jest --runInBand && npm run test:size",
1516
"test:watch": "jest --watch --updateSnapshot --coverage false",
16-
"prebuild": "npm run clean",
17-
"postbuild": "rimraf {lib,es}/**/__tests__",
17+
"test:size": "bundlesize",
18+
"prebuild": "npm run clean",
19+
"postbuild": "rimraf {lib,es}/**/__tests__",
1820
"posttest": "npm run typecheck"
19-
},
20-
"repository": {
21-
"type": "git",
22-
"url": "git+https://github.com/christianalfoni/proxy-state-tree.git"
23-
},
24-
"keywords": [
25-
"state",
26-
"proxy",
27-
"mobx",
28-
"vue",
29-
"store"
30-
],
31-
"author": "Christian Alfoni",
32-
"license": "MIT",
33-
"bugs": {
34-
"url": "https://github.com/christianalfoni/proxy-state-tree/issues"
35-
},
36-
"homepage": "https://github.com/christianalfoni/proxy-state-tree#readme",
37-
"dependencies": {
38-
"is-plain-obj": "^1.1.0"
39-
}
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/christianalfoni/proxy-state-tree.git"
25+
},
26+
"keywords": [
27+
"state",
28+
"proxy",
29+
"mobx",
30+
"vue",
31+
"store"
32+
],
33+
"author": "Christian Alfoni",
34+
"license": "MIT",
35+
"bugs": {
36+
"url": "https://github.com/christianalfoni/proxy-state-tree/issues"
37+
},
38+
"homepage": "https://github.com/christianalfoni/proxy-state-tree#readme",
39+
"dependencies": {
40+
"is-plain-obj": "^1.1.0"
41+
},
42+
"devDependencies": {
43+
"bundlesize": "^0.17.2",
44+
"terser-webpack-plugin": "^1.3.0",
45+
"webpack": "^4.35.0"
46+
},
47+
"bundlesize": [
48+
{
49+
"path": "./dist/proxy-state-tree.min.js",
50+
"maxSize": "3 kB"
51+
}
52+
]
4053
}

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const arrayMutations = new Set([
1616
'copyWithin',
1717
])
1818

19+
const getValue = (proxyOrValue) =>
20+
proxyOrValue && proxyOrValue[IS_PROXY] ? proxyOrValue[VALUE] : proxyOrValue
21+
1922
export class Proxifier {
2023
CACHED_PROXY = Symbol('CACHED_PROXY')
2124
constructor(private tree: TTree) {}
@@ -24,6 +27,8 @@ export class Proxifier {
2427
}
2528

2629
ensureMutationTrackingIsEnabled(path) {
30+
if (process.env.NODE_ENV === 'production') return
31+
2732
if (this.tree.master.options.devmode && !this.tree.canMutate()) {
2833
throw new Error(
2934
`proxy-state-tree - You are mutating the path "${path}", but it is not allowed. The following could have happened:
@@ -40,6 +45,8 @@ export class Proxifier {
4045
}
4146

4247
ensureValueDosntExistInStateTreeElsewhere(value) {
48+
if (process.env.NODE_ENV === 'production') return
49+
4350
if (value && value[IS_PROXY] === true) {
4451
throw new Error(
4552
`proxy-state-tree - You are trying to insert a value that already exists in the state tree on path "${
@@ -105,7 +112,8 @@ export class Proxifier {
105112
if (prop === PATH) return path
106113
if (prop === VALUE) return value
107114
if (prop === 'indexOf') {
108-
return (arg) => value.indexOf(arg && arg[IS_PROXY] ? arg[VALUE] : arg)
115+
return (searchTerm, offset) =>
116+
value.indexOf(getValue(searchTerm), getValue(offset))
109117
}
110118
if (
111119
prop === 'length' ||
@@ -126,7 +134,7 @@ export class Proxifier {
126134
const method = String(prop)
127135

128136
if (arrayMutations.has(method)) {
129-
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
137+
/* @__PURE__ */ proxifier.ensureMutationTrackingIsEnabled(nestedPath)
130138
return (...args) => {
131139
const mutationTree = proxifier.getMutationTree()
132140

@@ -137,11 +145,17 @@ export class Proxifier {
137145
hasChangedValue: true,
138146
})
139147

140-
return target[prop](
141-
...args.map((arg) =>
142-
proxifier.ensureValueDosntExistInStateTreeElsewhere(arg)
148+
if (process.env.NODE_ENV === 'production') {
149+
return target[prop](...args)
150+
} else {
151+
return target[prop](
152+
...args.map((arg) =>
153+
/* @__PURE__ */ proxifier.ensureValueDosntExistInStateTreeElsewhere(
154+
arg
155+
)
156+
)
143157
)
144-
)
158+
}
145159
}
146160
}
147161

@@ -154,8 +168,10 @@ export class Proxifier {
154168
set(target, prop, value) {
155169
const nestedPath = proxifier.concat(path, prop)
156170

157-
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
158-
proxifier.ensureValueDosntExistInStateTreeElsewhere(value)
171+
/* @__PURE__ */ proxifier.ensureMutationTrackingIsEnabled(nestedPath)
172+
/* @__PURE__ */ proxifier.ensureValueDosntExistInStateTreeElsewhere(
173+
value
174+
)
159175

160176
const mutationTree = proxifier.getMutationTree()
161177

@@ -239,8 +255,10 @@ export class Proxifier {
239255
set(target, prop, value) {
240256
const nestedPath = proxifier.concat(path, prop)
241257

242-
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
243-
proxifier.ensureValueDosntExistInStateTreeElsewhere(value)
258+
/* @__PURE__ */ proxifier.ensureMutationTrackingIsEnabled(nestedPath)
259+
/* @__PURE__ */ proxifier.ensureValueDosntExistInStateTreeElsewhere(
260+
value
261+
)
244262

245263
let objectChangePath
246264

@@ -269,7 +287,7 @@ export class Proxifier {
269287
deleteProperty(target, prop) {
270288
const nestedPath = proxifier.concat(path, prop)
271289

272-
proxifier.ensureMutationTrackingIsEnabled(nestedPath)
290+
/* @__PURE__ */ proxifier.ensureMutationTrackingIsEnabled(nestedPath)
273291

274292
let objectChangePath
275293
if (prop in target) {

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ describe('ARRAYS', () => {
369369
})
370370
})
371371

372+
test('should create proxies of arrays', () => {
373+
const state = {
374+
foo: [],
375+
}
376+
const tree = new ProxyStateTree(state)
377+
const trackStateTree = tree.getTrackStateTree().track(() => {})
378+
const foo = trackStateTree.state.foo
379+
380+
expect(foo[IS_PROXY]).toBeTruthy()
381+
})
382+
372383
describe('MUTATIONS', () => {
373384
test('should throw when mutating without tracking mutations', () => {
374385
const state = {
@@ -493,24 +504,15 @@ describe('ARRAYS', () => {
493504
})
494505
})
495506

496-
test('should allow CONCAT of existing array', () => {
507+
test('should allow indexOf using proxy values', () => {
497508
const state = {
498-
foo: ['foo'],
509+
things: [{ some: 'thing' }],
510+
ids: [2, 1, 1, 1, 2],
499511
}
500512
const tree = new ProxyStateTree(state)
501-
const mutationTree = tree.getMutationTree()
502-
mutationTree.state.foo = mutationTree.state.foo.concat('bar')
503-
504-
expect(mutationTree.mutations).toEqual([
505-
{
506-
method: 'set',
507-
path: 'foo',
508-
args: [['foo', 'bar']],
509-
hasChangedValue: true,
510-
},
511-
])
512-
513-
expect(state.foo).toEqual(['foo', 'bar'])
513+
expect(tree.state.things.indexOf(tree.state.things[0])).toEqual(0)
514+
expect(tree.state.ids.indexOf(2)).toEqual(0)
515+
expect(tree.state.ids.indexOf(2, 1)).toEqual(4)
514516
})
515517
})
516518

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path')
2+
const TerserPlugin = require('terser-webpack-plugin')
3+
4+
module.exports = {
5+
mode: 'production',
6+
entry: path.resolve('./es/index.js'),
7+
output: {
8+
path: path.resolve('dist'),
9+
filename: 'proxy-state-tree.min.js',
10+
},
11+
optimization: {
12+
minimizer: [
13+
new TerserPlugin({
14+
test: /\.js(\?.*)?$/i,
15+
parallel: true,
16+
sourceMap: true,
17+
terserOptions: {
18+
module: true,
19+
},
20+
}),
21+
],
22+
},
23+
module: {
24+
rules: [
25+
{
26+
test: /\.js$/,
27+
loader: 'babel-loader',
28+
include: path.resolve('src'),
29+
options: {
30+
presets: ['@babel/preset-env'],
31+
},
32+
},
33+
],
34+
},
35+
}

0 commit comments

Comments
 (0)