Skip to content

Commit

Permalink
add set trap
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 22, 2018
1 parent 9f9dc0d commit 8c6bfbd
Show file tree
Hide file tree
Showing 6 changed files with 1,441 additions and 84 deletions.
65 changes: 46 additions & 19 deletions _tests/smoke.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
deproxify,
isProxyfied,
getProxyKey,
spreadGuardsEnabled
} from '../src/index';
spreadGuardsEnabled,
sourceMutationsEnabled
} from '../src';

describe('proxy', () => {
it('arrays', () => {
Expand All @@ -30,16 +31,16 @@ describe('proxy', () => {
expect(proxyEqual(A1, A4, trapped.affected)).to.be.true;


trapped.state[0] += 0;
trapped.state[0] += 0;
trapped.state[0];// += 0;
trapped.state[0];// += 0;

expect(trapped.affected).to.be.deep.equal(['.0']);

expect(proxyEqual(A1, A2, trapped.affected)).to.be.true;
expect(proxyEqual(A1, A3, trapped.affected)).to.be.true;
expect(proxyEqual(A1, A4, trapped.affected)).to.be.true;

trapped.state[1] += 0;
trapped.state[1];// += 0;

expect(trapped.affected).to.be.deep.equal(['.0', '.1']);

Expand All @@ -50,11 +51,11 @@ describe('proxy', () => {
expect(drainDifference()).to.be.deep.equal([['.1', 'differs', 1, 2]]);

trapped.seal();
trapped.state[2] += 0;
trapped.state[2];// += 0;
expect(trapped.affected).to.be.deep.equal(['.0', '.1']);
trapped.unseal();

trapped.state[3] += 0;
trapped.state[3];// += 0;
expect(trapped.affected).to.be.deep.equal(['.0', '.1', '.3']);
});

Expand Down Expand Up @@ -86,8 +87,8 @@ describe('proxy', () => {
expect(proxyShallow(A1, A2, trapped.affected)).to.be.true;
expect(proxyShallow(A1, A3, trapped.affected)).to.be.true;

trapped.state.key1 += 0;
trapped.state.key1 += 0;
trapped.state.key1;// += 0;
trapped.state.key1;// += 0;
expect(trapped.affected).to.be.deep.equal(['.key1']);

expect(proxyEqual(A1, A2, trapped.affected)).to.be.true;
Expand All @@ -96,7 +97,7 @@ describe('proxy', () => {

trapped.reset();
expect(trapped.affected).to.be.deep.equal([]);
trapped.state.key2.array[0] += 0;
trapped.state.key2.array[0];// += 0;
expect(trapped.affected).to.be.deep.equal(['.key2', '.key2.array', '.key2.array.0']);

expect(proxyEqual(A1, A2, trapped.affected)).to.be.false;
Expand Down Expand Up @@ -144,8 +145,8 @@ describe('proxy', () => {
expect(proxyShallowEqual(A1, A2, trapped.affected)).to.be.true;
expect(proxyShallowEqual(A1, A3, trapped.affected)).to.be.true;

trapped.state.key1 += 0;
trapped.state.key1 += 0;
trapped.state.key1;// += 0;
trapped.state.key1;/// += 0;
expect(trapped.affected).to.be.deep.equal(['.key1']);

expect(proxyShallowEqual(A1, A2, trapped.affected)).to.be.true;
Expand All @@ -154,7 +155,7 @@ describe('proxy', () => {

trapped.reset();
expect(trapped.affected).to.be.deep.equal([]);
trapped.state.key2.array[0] += 0;
trapped.state.key2.array[0];// += 0;
expect(trapped.affected).to.be.deep.equal(['.key2', '.key2.array', '.key2.array.0']);

expect(proxyShallowEqual(A1, A2, trapped.affected)).to.be.false;
Expand All @@ -181,20 +182,20 @@ describe('proxy', () => {
const trapped1 = proxyState(A1);
const trapped2 = proxyState(trapped1.state);

trapped2.state.key1 += 0;
trapped2.state.key1;// += 0;
expect(trapped1.affected).to.be.deep.equal(['.key1']);
expect(trapped2.affected).to.be.deep.equal(['.key1']);

expect(proxyShallowEqual(A1, A2, trapped1.affected)).to.be.true;
trapped2.state.key1 += 1;
trapped2.state.key1;// += 1;
expect(proxyShallowEqual(trapped2.state, A2, trapped1.affected)).to.be.true;
A1.key1 += 1;
expect(proxyShallowEqual(trapped2.state, A2, trapped1.affected)).to.be.false;
expect(drainDifference()).to.be.deep.equal([['.key1', 'not equal']]);

trapped1.reset();
trapped2.reset();
trapped2.state.key2.array[0] += 0;
trapped2.state.key2.array[0];// += 0;
expect(trapped1.affected).to.be.deep.equal(['.key2', '.key2.array', '.key2.array.0']);

expect(proxyShallowEqual(trapped2.state, A2, trapped1.affected)).to.be.true;
Expand Down Expand Up @@ -233,11 +234,11 @@ describe('proxy', () => {
const p0 = proxyState(A);
const p1 = proxyState(p0.state.root);
const p2 = proxyState(p1.state.b);
p2.state.c.d++;
p2.state.c.d;//++;

expect(p1.affected).to.be.deep.equal(['.b', '.b.c', '.b.c.d']);
expect(p2.affected).to.be.deep.equal(['.c', '.c.d']);
p2.state.a++;
p2.state.a;//++;

expect(p1.affected).to.be.deep.equal(['.b', '.b.c', '.b.c.d', '.b.a']);
expect(p2.affected).to.be.deep.equal(['.c', '.c.d', '.a']);
Expand Down Expand Up @@ -413,8 +414,34 @@ describe('proxy', () => {
".d.entries.0.1.sub1",
]);


spreadGuardsEnabled(true);
})
});

describe('set', () => {
it('should not let you set value', () => {
const A = {a: 1, b: 2};
const p = proxyState(A);
const B = p.state;

expect(() => B.a = 2).to.throw();
expect(A.a).to.be.equal(1);
expect(B.a).to.be.equal(1);
});

it('should not let you set value', () => {
const A = {a: 1, b: 2};
const p = proxyState(A);
const B = p.state;

sourceMutationsEnabled(true);
expect(() => B.a = 2).not.to.throw();

expect(A.a).to.be.equal(2);
expect(B.a).to.be.equal(2);

sourceMutationsEnabled(false);
expect(() => B.a = 3).to.throw();
});
})
});
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ machine:
test:

override:
- npm run test
- npm run test:ci && codecov
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "src/proxy-polyfill.js"
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"test": "npm run test:pick -- '_tests/**/*spec.js'",
"test:pick": "BABEL_ENV=cjs mocha --compilers js:babel-core/register",
"test:cov": "BABEL_ENV=cjs nyc mocha --compilers js:babel-core/register --report lcovonly -- '_tests/**/*spec.js'",
"build": "babel src -d lib",
"prepublish": "npm run build",
"lint": "eslint src tests",
Expand All @@ -27,8 +28,10 @@
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"chai": "^4.1.2",
"codecov": "^3.0.4",
"eslint": "^4.18.0",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"nyc": "^12.0.2"
},
"repository": {
"type": "git",
Expand Down
28 changes: 24 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const ProxyConstructor = hasProxy ? Proxy : ProxyPolyfill();
const spreadMarker = '!SPREAD';
const __proxyequal_scanEnd = '__proxyequal_scanEnd';
const spreadActivation = '__proxyequal_spreadActivation';
const sourceModification ='__proxyequal_sourceObjectMutations';

let isSpreadGuardsEnabled = true;
let areSpreadGuardsEnabled = true;
let areSourceMutationsEnabled = false;

export const spreadGuardsEnabled = (flag) => (isSpreadGuardsEnabled=flag);
export const spreadGuardsEnabled = (flag) => (areSpreadGuardsEnabled=flag);
export const sourceMutationsEnabled = (flag) => (areSourceMutationsEnabled=flag);

const ProxyToState = new WeakMap();
const ProxyToFinderPrint = new WeakMap();
Expand Down Expand Up @@ -67,7 +70,7 @@ function proxyfy(state, report, suffix = '', fingerPrint, ProxyMap) {
}

const theBaseObject = alreadyProxy ? state : prepareObject(state);
const shouldHookOwnKeys = isSpreadGuardsEnabled && !isProxyfied(state);
const shouldHookOwnKeys = areSpreadGuardsEnabled && !isProxyfied(state);

const iterable = (key, iterator) => {
let index = 0;
Expand All @@ -84,7 +87,7 @@ function proxyfy(state, report, suffix = '', fingerPrint, ProxyMap) {
return proxyValue(subKey, nextItem.value)
}
};
}
};
return {
[Symbol.iterator]: () => ({
next
Expand Down Expand Up @@ -124,6 +127,23 @@ function proxyfy(state, report, suffix = '', fingerPrint, ProxyMap) {
};

const hooks = {
set(target, prop, value) {
const thisId = suffix + '.' + prop;
if(areSourceMutationsEnabled){
state[prop] = value;
report(thisId);
return true
} else {
console.error(
'Source object mutations are disabled, but you tried to change',
thisId,
'on',
state
);
return false;
}
},

get(target, prop) {
if (prop === __proxyequal_scanEnd) {
report(spreadMarker, suffix);
Expand Down
Loading

0 comments on commit 8c6bfbd

Please sign in to comment.