Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jul 24, 2015
1 parent 1b225d1 commit 8967cce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
17 changes: 6 additions & 11 deletions helpers/eventify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ module.exports = eventify
* 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) {
function eventify (db, state, method, eventName) {
return function () {
return method.apply(db, arguments).then(function (result) {
console.log('result')
console.log(result)
if (Array.isArray(result)) {
result.forEach(triggerEvent.bind(null, state))
result.forEach(triggerEvent.bind(null, state, eventName))
} else {
triggerEvent(state, result)
triggerEvent(state, eventName, result)
}

return result
})
}
}

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

state.emitter.emit(eventName, object)
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function hoodieApi (options) {
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')),
removeAll: eventify(this, state, require('./remove-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
2 changes: 2 additions & 0 deletions tests/specs/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ test('store.off returns store', function (t) {
})

function addEventToArray (array, object) {
console.log('addEventToArray')
console.log(arguments)
if (arguments.length > 2) {
arguments[0].push({
eventName: arguments[1],
Expand Down

0 comments on commit 8967cce

Please sign in to comment.