From bd453b61e9a23f6db4ca560fe29d30a32c7793ef Mon Sep 17 00:00:00 2001 From: Bill Christo Date: Wed, 9 Nov 2016 13:31:44 -0500 Subject: [PATCH] Bug Fixes --- app.js | 34 ++++++++++++++++++---------------- package.json | 4 ++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 8436df8..d3bf270 100644 --- a/app.js +++ b/app.js @@ -23,24 +23,30 @@ function Filejson(cfg) { var handler = { get: function(target, key, receiver) { - // return target[key]; - // return new Proxy(target[key], this); - return Reflect.get(target, key, receiver); + if (key === "__isProxy") { + // Implementing a virtual __isProxy key allows us to check whether an object is already a Proxy. + return true; + } + + return Reflect.get(target, key, receiver); }, set: function(target, key, value, receiver) { var check = function(value, tree) { var t = typeof value; if(!(t === "string" || t === "number" || t === "object" || t === "boolean" || t === "undefined")) { - throw new Error("NON-JSON COMPATIBLE TYPE FOUND. " + t + " found within: " + tree); + throw new Error("NON-JSON COMPATIBLE TYPE FOUND. " + t + " found at: " + tree); } }; - var loopAll = function(obj, tree) { - for(var key in obj){ - tree += "." + key; - if(typeof obj[key] !== "object"){ + var loopAll = function(obj, parent) { + var tree = parent || ""; + + for(var key in obj) { + if(typeof obj[key] !== "object") { check(obj[key], tree); } else { + tree = parent + "." + key; + obj[key] = new Proxy(obj[key], handler); loopAll(obj[key], tree); } } @@ -55,17 +61,14 @@ function Filejson(cfg) { throw new Error("You must specify a filename"); } - if( value instanceof Object ) { - value = new Proxy(value, this); + if( value instanceof Object && value.__isProxy === undefined) { + value = new Proxy(value, handler); + loopAll(value, "..."); } // The default behavior to store the value Reflect.set(target, key, value, receiver); - if( !self.cfg.speed ) { - loopAll(self.contents, "file.contents"); - } - if( !self.paused ) { self.save(function(error) { if(error) { @@ -87,8 +90,7 @@ function Filejson(cfg) { this.cfg = { filename: cfg.filename || "", space: cfg.space || 2, - verbose: cfg.verbose || false, - speed: cfg.speed || false + verbose: cfg.verbose || false }; // Boolean - pauses any future changes to this.contents from auto triggering a save to disk diff --git a/package.json b/package.json index 4d1c310..600ec2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "filejson", - "version": "1.0.11", + "version": "1.0.12", "description": "Seamlessly syncs a JavaScript value to a JSON file in the background whenever that value changes.", "main": "app.js", "scripts": { @@ -9,7 +9,7 @@ "author": "Bill Christo", "license": "MIT", "devDependencies": { - "eslint": "^3.8.1" + "eslint": "^3.9.1" }, "dependencies": {}, "bugs": {