From 3f2e860ee2b9b0cb68abb40e529351eae489bf45 Mon Sep 17 00:00:00 2001 From: vvo Date: Sun, 14 May 2017 21:39:25 +0200 Subject: [PATCH] fix(memleak): in watch mode, do not use promise chain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Promise chains are great for things that WILL resolve. If we know that this will never resolve (like watch) then we should use a simple callback based approach like queue/async. This also re-ensures us that every changes are applied sequentially (while with our previous check it was no more the case). I hope this just works™ --- index.js | 13 +++++++++---- package.json | 1 + yarn.lock | 8 +++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 43f0e3aec..dad519c5e 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ import PouchDB from 'pouchdb-http'; import * as npm from './npm.js'; import log from './log.js'; import ms from 'ms'; +import queue from 'async/queue'; log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰'); @@ -168,9 +169,8 @@ function watch({ seq }) { limit: undefined, }); - changes.on('change', change => { - Promise.resolve() - .then(() => saveDocs([change]), reject) + const q = queue((change, done) => { + saveDocs([change]) .then(() => infoChange(change.seq, 1, '🛰')) .then(() => stateManager.save({ @@ -195,7 +195,12 @@ function watch({ seq }) { }); } }) - .catch(reject); + .then(() => done(null)) + .catch(done); + }, 1); + + changes.on('change', change => { + q.push(change); }); changes.on('error', reject); }); diff --git a/package.json b/package.json index 600a463d0..60af03fdc 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "license": "MIT", "dependencies": { "algoliasearch": "^3.22.1", + "async": "^2.4.0", "babel-cli": "^6.24.1", "babel-preset-env": "^1.4.0", "babel-preset-stage-2": "^6.24.1", diff --git a/yarn.lock b/yarn.lock index 53d39601b..d6f064825 100644 --- a/yarn.lock +++ b/yarn.lock @@ -177,6 +177,12 @@ async-each@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d" +async@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/async/-/async-2.4.0.tgz#4990200f18ea5b837c2cc4f8c031a6985c385611" + dependencies: + lodash "^4.14.0" + asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" @@ -2581,7 +2587,7 @@ lodash.isnull@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/lodash.isnull/-/lodash.isnull-3.0.0.tgz#fafbe59ea1dca27eed786534039dd84c2e07c56e" -lodash@^4.0.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: +lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"