Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cenuij
Created April 26, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cenuij/99429ad6e3dd84f278239eb0645759ea to your computer and use it in GitHub Desktop.
Save cenuij/99429ad6e3dd84f278239eb0645759ea to your computer and use it in GitHub Desktop.
Remove listener instead of stopping feed
const nano = require("nano")("http://localhost:5984");
const name = "test";
nano.db.destroy(name, () => {
nano.db.create(name, () => {
const db = nano.use(name);
const feed = db.follow({ since: "now" }, () => {});
// add doc every second
const timer = setInterval(() => {
db.insert({}, (err, body) =>
console.log(err ? "ERROR" : "ADD " + body.id)
);
}, 1000);
const handler = change => console.log("GOT " + change.id);
// start listener after 5s
setTimeout(() => feed.on("change", handler), 5000);
// stop listener after 10s
setTimeout(() => {
feed.removeListener("change", handler);
clearInterval(timer);
}, 10000);
});
});
@cenuij
Copy link
Author

cenuij commented Apr 26, 2018

This sort of works but something keeps some reference that prevents the app from exiting normally ...

$ node test2.js
ADD ae1b3a77338a73da2f3cb4cf162837fb
ADD ae1b3a77338a73da2f3cb4cf16283a37
ADD ae1b3a77338a73da2f3cb4cf1628435d
ADD ae1b3a77338a73da2f3cb4cf1628463f
ADD ae1b3a77338a73da2f3cb4cf162851c8
GOT ae1b3a77338a73da2f3cb4cf162851c8
ADD ae1b3a77338a73da2f3cb4cf16285874
GOT ae1b3a77338a73da2f3cb4cf16285874
ADD ae1b3a77338a73da2f3cb4cf162861ec
GOT ae1b3a77338a73da2f3cb4cf162861ec
ADD ae1b3a77338a73da2f3cb4cf16286752
GOT ae1b3a77338a73da2f3cb4cf16286752
ADD ae1b3a77338a73da2f3cb4cf16287407
GOT ae1b3a77338a73da2f3cb4cf16287407




^C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment