Skip to content

Commit

Permalink
Merge ad8afa9 into 5a5be71
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jun 26, 2017
2 parents 5a5be71 + ad8afa9 commit 8c17df3
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 48 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var EventEmitter = require('events').EventEmitter

var assign = require('lodash/assign')

var startListenToChanges = require('./lib/helpers/start-listen-to-changes')
var handleChanges = require('./lib/helpers/handle-changes')

function Store (dbName, options) {
if (!(this instanceof Store)) return new Store(dbName, options)
Expand Down Expand Up @@ -35,8 +35,6 @@ function Store (dbName, options) {
remote: options.remote
}

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

var api = {
db: state.db,
isPersistent: require('./lib/is-persistent').bind(null, state),
Expand Down Expand Up @@ -64,6 +62,8 @@ function Store (dbName, options) {

state.api = api

handleChanges(state)

return api
}

Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/add-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var clone = require('lodash/clone')
var uuid = require('pouchdb-utils').uuid

var addTimestamps = require('../utils/add-timestamps')
var bulkDocs = require('./db-bulk-docs')

module.exports = function addMany (state, docs, prefix) {
docs = docs.map(function (doc) {
Expand All @@ -16,7 +17,7 @@ module.exports = function addMany (state, docs, prefix) {
})
}

return state.db.bulkDocs(docs)
return bulkDocs(state, docs)

.then(function (responses) {
return responses.map(function (response, i) {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/add-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Promise = require('lie')
var uuid = require('pouchdb-utils').uuid

var addTimestamps = require('../utils/add-timestamps')
var put = require('./db-put')

module.exports = function addOne (state, doc, prefix) {
if (typeof doc !== 'object') {
Expand All @@ -22,7 +23,7 @@ module.exports = function addOne (state, doc, prefix) {

delete doc.hoodie

return state.db.put(addTimestamps(doc))
return put(state, addTimestamps(doc))

.then(function (response) {
doc._id = response.id
Expand Down
36 changes: 36 additions & 0 deletions lib/helpers/db-bulk-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = bulkDocs

function bulkDocs (state, docs) {
return state.db.bulkDocs(docs)

.then(function (result) {
if (result.length === 0) {
return result
}

var revs = result.map(toRev).filter(Boolean)

return new Promise(function (resolve) {
function handler (eventName, doc) {
var index = revs.indexOf(doc._rev)

if (index === -1) {
return
}

revs.splice(index, 1)

if (revs.length === 0) {
state.emitter.removeListener('change', handler)
resolve(result)
}
}

state.emitter.on('change', handler)
})
})
}

function toRev (result) {
return result.rev
}
18 changes: 18 additions & 0 deletions lib/helpers/db-put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = put

function put (state, doc) {
return state.db.put(doc)

.then(function (result) {
return new Promise(function (resolve) {
function handler (eventName, doc) {
if (doc._rev === result.rev) {
state.emitter.removeListener('change', handler)
resolve(result)
}
}

state.emitter.on('change', handler)
})
})
}
71 changes: 71 additions & 0 deletions lib/helpers/handle-changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module.exports = handleChanges

function handleChanges (state) {
var isBootstrapping = true
var changesDuringBootstrapping = []
var knownDocIds = {}

// we have to initiallo load all documents in order to differentiate add from
// change events by populating knownDocIds. This is something we wish would be
// simpler but unfortunately we can’t expose the necessery information with
// ouchDB directly, see https://github.com/pouchdb/pouchdb/pull/6553
state.db.allDocs()

.then(function (result) {
isBootstrapping = false

result.rows.forEach(function (row) {
knownDocIds[row.id] = 1
})

changesDuringBootstrapping.forEach(handleChange)
})

// we listen to the changes feed which we use to emit our own events
// if there happen to be events while we are still populating knownDocIds
// then we store the events in changesDuringBootstrapping which are handled
// once the initial bootstrap is done
state.db.changes({
since: 'now',
live: true,
include_docs: true
})
.on('change', function (change) {
if (isBootstrapping) {
changesDuringBootstrapping.push(change)
return
}

handleChange(change)
})

function handleChange (change) {
var doc = change.doc

if (!doc.hoodie) {
doc.hoodie = {}
}

if (change.deleted) {
// ignore deletes for unknown docs
if (!knownDocIds[change.id]) {
return
}

delete knownDocIds[change.id]
state.emitter.emit('remove', doc)
state.emitter.emit('change', 'remove', doc)
return
}

if (knownDocIds[change.id]) {
state.emitter.emit('update', doc)
state.emitter.emit('change', 'update', doc)
return
}

knownDocIds[change.id] = 1
state.emitter.emit('add', doc)
state.emitter.emit('change', 'add', doc)
}
}
20 changes: 0 additions & 20 deletions lib/helpers/start-listen-to-changes.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/helpers/update-many.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var assign = require('lodash/assign')
var PouchDBErrors = require('pouchdb-errors')

var changeObject = require('../utils/change-object')
var addTimestamps = require('../utils/add-timestamps')
var bulkDocs = require('./db-bulk-docs')
var changeObject = require('../utils/change-object')
var toId = require('../utils/to-id')

var findMany = require('./find-many')
Expand Down Expand Up @@ -39,7 +40,6 @@ module.exports = function updateMany (state, array, change, prefix) {
if (typeof passedDoc !== 'object') {
return PouchDBErrors.NOT_AN_OBJECT
}

return assign(doc, passedDoc, {_id: doc._id, _rev: doc._rev, hoodie: doc.hoodie})
})
})
Expand All @@ -50,7 +50,7 @@ module.exports = function updateMany (state, array, change, prefix) {
return !(doc instanceof Error)
})
validObjects.forEach(addTimestamps)
return state.db.bulkDocs(validObjects)
return bulkDocs(state, validObjects)
})

.then(function (responses) {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/update-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var changeObject = require('../utils/change-object')
var addTimestamps = require('../utils/add-timestamps')

var findOne = require('./find-one')
var put = require('./db-put')

module.exports = function updateOne (state, idOrDoc, change, prefix) {
var doc
Expand All @@ -25,7 +26,7 @@ module.exports = function updateOne (state, idOrDoc, change, prefix) {

.then(function (_doc) {
doc = _doc
return state.db.put(addTimestamps(doc))
return put(state, addTimestamps(doc))
})

.then(function (response) {
Expand Down
5 changes: 3 additions & 2 deletions lib/remove-all.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var isntDesignDoc = require('./utils/isnt-design-doc')
var addTimestamps = require('./utils/add-timestamps')
var bulkDocs = require('./helpers/db-bulk-docs')
var isntDesignDoc = require('./utils/isnt-design-doc')

module.exports = removeAll

Expand Down Expand Up @@ -42,7 +43,7 @@ function removeAll (state, prefix, filter) {
})
})

.then(state.db.bulkDocs.bind(state.db))
.then(bulkDocs.bind(null, state))

.then(function (results) {
return results.map(function (result, i) {
Expand Down
4 changes: 2 additions & 2 deletions lib/reset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = reset

var handleChanges = require('./helpers/handle-changes')
var disconnect = require('./disconnect')
var startListenToChanges = require('./helpers/start-listen-to-changes')

function reset (state) {
return disconnect(state)
Expand All @@ -12,6 +12,6 @@ function reset (state) {

.then(function () {
state.db = new state.PouchDB(state.dbName)
startListenToChanges(state)
handleChanges(state)
})
}
6 changes: 2 additions & 4 deletions lib/update-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assign = require('lodash/assign')
var Promise = require('lie')

var addTimestamps = require('./utils/add-timestamps')
var bulkDocs = require('./helpers/db-bulk-docs')
var isntDesignDoc = require('./utils/isnt-design-doc')

/**
Expand Down Expand Up @@ -54,10 +55,7 @@ function updateAll (state, prefix, changedProperties) {
})
})

.then(function (result) {
return result
})
.then(state.db.bulkDocs.bind(state.db))
.then(bulkDocs.bind(null, state))

.then(function (results) {
return results.map(function (result, i) {
Expand Down
2 changes: 1 addition & 1 deletion lib/with-id-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = withIdPrefix
var EventEmitter = require('events').EventEmitter

/**
* returns API with all CRUD methods scoped to id prefix
* returns API with all CRUD methods scoped to id prefix`
*
* @param {String} prefix String all ID’s are implicitly prefixed or
* expected to be prefixed with.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"lolex": "^1.5.1",
"memdown": "^1.1.0",
"mkdirp": "^0.5.1",
"pouchdb": "^6.2.0",
"pouchdb-adapter-memory": "^6.0.7",
"pouchdb-core": "^6.0.7",
"pouchdb-replication": "^6.0.7",
Expand Down
Loading

0 comments on commit 8c17df3

Please sign in to comment.