From d810b928cd69d8cc1a357182df457d993ef3a858 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Sat, 2 Nov 2019 17:48:39 +1100 Subject: [PATCH] handle objects with empty prototype, fixes #23 --- _tests/smoke.spec.js | 8 ++++++++ src/shouldInstrument.js | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/_tests/smoke.spec.js b/_tests/smoke.spec.js index 29e6a4a..ca8d056 100644 --- a/_tests/smoke.spec.js +++ b/_tests/smoke.spec.js @@ -332,6 +332,14 @@ describe('proxy', () => { expect(proxyEqual(o1, o2, trapped.affected)).to.be.equal(false); }); + it('handles orphan objects', () => { + const obj = Object.create(null); + obj.field=42; + const trapped = proxyState(obj); + expect(trapped.state.field).to.be.equal(42); + expect(trapped.affected).to.be.deep.equal(['.field']); + }); + it('detect self', () => { const A = {a: 1}; const B = proxyState(A).state; diff --git a/src/shouldInstrument.js b/src/shouldInstrument.js index 8bc690d..f44cb41 100644 --- a/src/shouldInstrument.js +++ b/src/shouldInstrument.js @@ -36,6 +36,9 @@ const handlers = { const globalObj = typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}; export function shouldInstrument({constructor}) { + if(!constructor) { + return true; + } const name = constructor.name; const isBuiltIn = ( typeof constructor === 'function' && @@ -45,4 +48,4 @@ export function shouldInstrument({constructor}) { return !isBuiltIn || handlers.hasOwnProperty(name); } -export const getCollectionHandlers = ({constructor}) => handlers[constructor.name]; +export const getCollectionHandlers = ({constructor}) => constructor && handlers[constructor.name];