Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event timing #56

Merged
merged 5 commits into from Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions helpers/eventify.js
@@ -0,0 +1,31 @@
module.exports = eventify

/**
* runs a method from the API and triggers events on each object.
*
* Note that we didn't implement this pased on PouchDB's .changes()
* API on purpose, because of the timeing the events would get triggered.
* See https://github.com/hoodiehq/pouchdb-hoodie-api/issues/54
**/
function eventify (db, state, method, eventName) {
return function () {
return method.apply(db, arguments).then(function (result) {
if (Array.isArray(result)) {
result.forEach(triggerEvent.bind(null, state, eventName))
} else {
triggerEvent(state, eventName, result)
}

return result
})
}
}

function triggerEvent (state, eventName, object) {
if (!eventName) {
eventName = parseInt(object._rev, 10) > 1 ? 'update' : 'add'
}

state.emitter.emit(eventName, object)
state.emitter.emit('change', eventName, object)
}
26 changes: 0 additions & 26 deletions helpers/start-listen-to-changes.js

This file was deleted.

19 changes: 9 additions & 10 deletions index.js
Expand Up @@ -3,26 +3,25 @@
var exports = module.exports = { hoodieApi: hoodieApi }

var EventEmitter = require('events').EventEmitter
var startListenToChanges = require('./helpers/start-listen-to-changes')

var eventify = require('./helpers/eventify')

function hoodieApi (options) {
var state = {
emitter: options && options.emitter || new EventEmitter()
}

state.emitter.once('newListener', startListenToChanges.bind(this, state))

return {
db: this,
add: require('./add').bind(this),
add: eventify(this, state, require('./add')),
find: require('./find').bind(this),
findAll: require('./find-all').bind(this),
findOrAdd: require('./find-or-add').bind(this),
update: require('./update').bind(this),
updateOrAdd: require('./update-or-add').bind(this),
updateAll: require('./update-all').bind(this),
remove: require('./remove').bind(this),
removeAll: require('./remove-all').bind(this),
findOrAdd: eventify(this, state, require('./find-or-add')),
update: eventify(this, state, require('./update')),
updateOrAdd: eventify(this, state, require('./update-or-add')),
updateAll: eventify(this, state, require('./update-all')),
remove: eventify(this, state, require('./remove'), 'remove'),
removeAll: eventify(this, state, require('./remove-all'), 'remove'),
on: require('./lib/on').bind(this, state),
one: require('./lib/one').bind(this, state),
off: require('./lib/off').bind(this, state),
Expand Down
5 changes: 2 additions & 3 deletions remove-all.js
Expand Up @@ -14,9 +14,8 @@ module.exports = removeAll
*/
function removeAll (filter) {
var objects
var db = this

return db.allDocs({
return this.allDocs({
include_docs: true
})

Expand All @@ -36,7 +35,7 @@ function removeAll (filter) {
})
})

.then(db.bulkDocs.bind(db))
.then(this.bulkDocs.bind(this))

.then(function (results) {
return results.map(function (result, i) {
Expand Down