From 8967cced0131576647d724e4874414c629c11b90 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 24 Jul 2015 13:23:35 -0400 Subject: [PATCH] wip --- helpers/eventify.js | 17 ++++++----------- index.js | 4 ++-- tests/specs/events.js | 2 ++ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/helpers/eventify.js b/helpers/eventify.js index c5109f9..939e4c5 100644 --- a/helpers/eventify.js +++ b/helpers/eventify.js @@ -7,15 +7,13 @@ 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 @@ -23,12 +21,9 @@ function eventify (db, state, method) { } } -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) diff --git a/index.js b/index.js index bdee188..b0270cd 100644 --- a/index.js +++ b/index.js @@ -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), diff --git a/tests/specs/events.js b/tests/specs/events.js index f58622d..74751ab 100644 --- a/tests/specs/events.js +++ b/tests/specs/events.js @@ -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],