-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
355 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.