Skip to content

Commit 3f2e860

Browse files
author
vvo
committed
fix(memleak): in watch mode, do not use promise chain
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™
1 parent 4484836 commit 3f2e860

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import PouchDB from 'pouchdb-http';
66
import * as npm from './npm.js';
77
import log from './log.js';
88
import ms from 'ms';
9+
import queue from 'async/queue';
910

1011
log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰');
1112

@@ -168,9 +169,8 @@ function watch({ seq }) {
168169
limit: undefined,
169170
});
170171

171-
changes.on('change', change => {
172-
Promise.resolve()
173-
.then(() => saveDocs([change]), reject)
172+
const q = queue((change, done) => {
173+
saveDocs([change])
174174
.then(() => infoChange(change.seq, 1, '🛰'))
175175
.then(() =>
176176
stateManager.save({
@@ -195,7 +195,12 @@ function watch({ seq }) {
195195
});
196196
}
197197
})
198-
.catch(reject);
198+
.then(() => done(null))
199+
.catch(done);
200+
}, 1);
201+
202+
changes.on('change', change => {
203+
q.push(change);
199204
});
200205
changes.on('error', reject);
201206
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"license": "MIT",
2525
"dependencies": {
2626
"algoliasearch": "^3.22.1",
27+
"async": "^2.4.0",
2728
"babel-cli": "^6.24.1",
2829
"babel-preset-env": "^1.4.0",
2930
"babel-preset-stage-2": "^6.24.1",

yarn.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ async-each@^1.0.0:
177177
version "1.0.1"
178178
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"
179179

180+
async@^2.4.0:
181+
version "2.4.0"
182+
resolved "https://registry.yarnpkg.com/async/-/async-2.4.0.tgz#4990200f18ea5b837c2cc4f8c031a6985c385611"
183+
dependencies:
184+
lodash "^4.14.0"
185+
180186
asynckit@^0.4.0:
181187
version "0.4.0"
182188
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -2581,7 +2587,7 @@ lodash.isnull@^3.0.0:
25812587
version "3.0.0"
25822588
resolved "https://registry.yarnpkg.com/lodash.isnull/-/lodash.isnull-3.0.0.tgz#fafbe59ea1dca27eed786534039dd84c2e07c56e"
25832589

2584-
lodash@^4.0.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
2590+
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
25852591
version "4.17.4"
25862592
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
25872593

0 commit comments

Comments
 (0)