From 9d18276ede307d47ca1fb1391d1ef425f6ab47f3 Mon Sep 17 00:00:00 2001 From: Kasper Kuijpers Date: Mon, 2 Jan 2017 15:55:20 +0100 Subject: [PATCH 1/5] removing anti-patter --- index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 5f1902b..c3c9398 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ ContentfulPull.prototype.sync = function(options) { console.log("ContentfulPull | Syncing..."); - var d = Promise.defer(); + var d = new Promise; var client = contentful.createClient({ space: this.settings.space, @@ -56,7 +56,7 @@ ContentfulPull.prototype.sync = function(options) { d.resolve(_this.transformData(handledData, options)); }); - return d.promise; + return d; }; /* @@ -199,7 +199,7 @@ ContentfulPull.prototype.transformData = function(data, options) { */ ContentfulPull.prototype.getFromFile = function(options) { var _this = this; - var d = Promise.defer(); + var d = new Promise; fs.readFile(this.settings.path, "utf8", function(err, resp) { if (err) d.reject(err); @@ -210,7 +210,7 @@ ContentfulPull.prototype.getFromFile = function(options) { } }) - return d.promise; + return d; } /* @@ -219,7 +219,7 @@ ContentfulPull.prototype.getFromFile = function(options) { */ ContentfulPull.prototype.get = function(options) { var _this = this; - var d = Promise.defer(); + var d = new Promise; // If no data is in memory, get from file, if that's not existing, sync from contentful if (!this.data) { @@ -232,7 +232,7 @@ ContentfulPull.prototype.get = function(options) { }) } - return d.promise; + return d; } /* @@ -240,7 +240,7 @@ ContentfulPull.prototype.get = function(options) { Saves the data to a local file */ ContentfulPull.prototype.saveLocal = function(data) { - var d = Promise.defer(); + var d = new Promise; console.log("ContentfulPull | Saving to local file..."); @@ -257,7 +257,7 @@ ContentfulPull.prototype.saveLocal = function(data) { } }); - return d.promise; + return d; }; module.exports = ContentfulPull; From d5021461c182b9e1cc015714f73e1043fc997314 Mon Sep 17 00:00:00 2001 From: Kasper Kuijpers Date: Mon, 2 Jan 2017 16:03:23 +0100 Subject: [PATCH 2/5] removing anti pattern --- index.js | 107 ++++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 57 deletions(-) diff --git a/index.js b/index.js index c3c9398..29783fd 100644 --- a/index.js +++ b/index.js @@ -33,30 +33,28 @@ ContentfulPull.prototype.sync = function(options) { console.log("ContentfulPull | Syncing..."); - var d = new Promise; + return new Promise(function(resolve, reject){ + var client = contentful.createClient({ + space: this.settings.space, + accessToken: this.settings.accessToken + }) - var client = contentful.createClient({ - space: this.settings.space, - accessToken: this.settings.accessToken - }) + var isInitial = this.currentSyncToken ? false : true; - var isInitial = this.currentSyncToken ? false : true; + var spacePromise = client.getSpace(); + var contentTypesPromise = client.getContentTypes(); + var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: this.currentSyncToken}); - var spacePromise = client.getSpace(); - var contentTypesPromise = client.getContentTypes(); - var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: this.currentSyncToken}); + Promise.all([spacePromise, contentTypesPromise, syncPromise]).then(function(result) { + var handledData = _this.handleSyncResponse({ + space: result[0], + contentTypes: result[1], + sync: result[2] + }); - Promise.all([spacePromise, contentTypesPromise, syncPromise]).then(function(result) { - var handledData = _this.handleSyncResponse({ - space: result[0], - contentTypes: result[1], - sync: result[2] + resolve(_this.transformData(handledData, options)); }); - - d.resolve(_this.transformData(handledData, options)); }); - - return d; }; /* @@ -199,18 +197,16 @@ ContentfulPull.prototype.transformData = function(data, options) { */ ContentfulPull.prototype.getFromFile = function(options) { var _this = this; - var d = new Promise; - - fs.readFile(this.settings.path, "utf8", function(err, resp) { - if (err) d.reject(err); - if (!err) { - _this.data = JSON.parse(resp); - _this.currentSyncToken = _this.data.sync.currentSyncToken; - d.resolve(_this.transformData(_this.data, options)); - } - }) - - return d; + return new Promise(function(resolve, reject){ + fs.readFile(this.settings.path, "utf8", function(err, resp) { + if (err) reject(err); + if (!err) { + _this.data = JSON.parse(resp); + _this.currentSyncToken = _this.data.sync.currentSyncToken; + resolve(_this.transformData(_this.data, options)); + } + }) + }); } /* @@ -219,20 +215,19 @@ ContentfulPull.prototype.getFromFile = function(options) { */ ContentfulPull.prototype.get = function(options) { var _this = this; - var d = new Promise; - - // If no data is in memory, get from file, if that's not existing, sync from contentful - if (!this.data) { - this.getFromFile(options).then(d.resolve).catch(function() { - _this.sync(options).then(d.resolve); - }) - }else{ - return new Promise(function(resolve, reject) { - resolve(_this.transformData(_this.data, options)); - }) - } + return new Promise(function(resolve, reject){ - return d; + // If no data is in memory, get from file, if that's not existing, sync from contentful + if (!this.data) { + this.getFromFile(options).then(resolve).catch(function() { + _this.sync(options).then(resolve); + }) + }else{ + return new Promise(function(resolve, reject) { + resolve(_this.transformData(_this.data, options)); + }) + } + }); } /* @@ -240,24 +235,22 @@ ContentfulPull.prototype.get = function(options) { Saves the data to a local file */ ContentfulPull.prototype.saveLocal = function(data) { - var d = new Promise; - console.log("ContentfulPull | Saving to local file..."); // Write to local storage - fs.writeFile(this.settings.path, JSON.stringify(data), "utf8", function(err) { - if (err) { - console.log("ContentfulPull | An error occurred while saving local file."); - console.log(err.stack); - d.reject(); - } - if (!err) { - console.log("ContentfulPull | File saved successfully."); - d.resolve(); - } + return new Promise(function(resolve, reject){ + fs.writeFile(this.settings.path, JSON.stringify(data), "utf8", function(err) { + if (err) { + console.log("ContentfulPull | An error occurred while saving local file."); + console.log(err.stack); + reject(); + } + if (!err) { + console.log("ContentfulPull | File saved successfully."); + resolve(); + } + }); }); - - return d; }; module.exports = ContentfulPull; From d1f318914b6ef36d970fdf04f233ce91ac12662a Mon Sep 17 00:00:00 2001 From: Kasper Kuijpers Date: Mon, 2 Jan 2017 16:05:12 +0100 Subject: [PATCH 3/5] fixing reference error to _this --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 29783fd..272350b 100644 --- a/index.js +++ b/index.js @@ -218,8 +218,8 @@ ContentfulPull.prototype.get = function(options) { return new Promise(function(resolve, reject){ // If no data is in memory, get from file, if that's not existing, sync from contentful - if (!this.data) { - this.getFromFile(options).then(resolve).catch(function() { + if (!_this.data) { + _this.getFromFile(options).then(resolve).catch(function() { _this.sync(options).then(resolve); }) }else{ From e783d1a7522a0dadac722a42c2cfe577944e19ba Mon Sep 17 00:00:00 2001 From: Kasper Kuijpers Date: Mon, 2 Jan 2017 16:09:31 +0100 Subject: [PATCH 4/5] updated this references in sync --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 272350b..52e179a 100644 --- a/index.js +++ b/index.js @@ -35,15 +35,15 @@ ContentfulPull.prototype.sync = function(options) { return new Promise(function(resolve, reject){ var client = contentful.createClient({ - space: this.settings.space, - accessToken: this.settings.accessToken + space: _this.settings.space, + accessToken: _this.settings.accessToken }) - var isInitial = this.currentSyncToken ? false : true; + var isInitial = _this.currentSyncToken ? false : true; var spacePromise = client.getSpace(); var contentTypesPromise = client.getContentTypes(); - var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: this.currentSyncToken}); + var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: _this.currentSyncToken}); Promise.all([spacePromise, contentTypesPromise, syncPromise]).then(function(result) { var handledData = _this.handleSyncResponse({ From b967a863bd4085698ffd3c6eac48c77abd983735 Mon Sep 17 00:00:00 2001 From: Kasper Kuijpers Date: Mon, 2 Jan 2017 16:12:05 +0100 Subject: [PATCH 5/5] more this references --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 52e179a..78cc8d1 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ ContentfulPull.prototype.sync = function(options) { var _this = this; console.log("ContentfulPull | Syncing..."); + console.log(_this.settings); return new Promise(function(resolve, reject){ var client = contentful.createClient({ @@ -198,7 +199,7 @@ ContentfulPull.prototype.transformData = function(data, options) { ContentfulPull.prototype.getFromFile = function(options) { var _this = this; return new Promise(function(resolve, reject){ - fs.readFile(this.settings.path, "utf8", function(err, resp) { + fs.readFile(_this.settings.path, "utf8", function(err, resp) { if (err) reject(err); if (!err) { _this.data = JSON.parse(resp); @@ -236,10 +237,11 @@ ContentfulPull.prototype.get = function(options) { */ ContentfulPull.prototype.saveLocal = function(data) { console.log("ContentfulPull | Saving to local file..."); + var _this = this; // Write to local storage return new Promise(function(resolve, reject){ - fs.writeFile(this.settings.path, JSON.stringify(data), "utf8", function(err) { + fs.writeFile(_this.settings.path, JSON.stringify(data), "utf8", function(err) { if (err) { console.log("ContentfulPull | An error occurred while saving local file."); console.log(err.stack);