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

*Allow* periodic writes of the views to avoid loss of work when restarting #16

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function isFunction (f) {
function id (e) { return e }

module.exports = function (Store) {
return function (version, reduce, map, codec, initial) {
return function (version, reduce, map, codec, initial, writeInterval = null) {
var opts
if(isObject(reduce)) {
opts = reduce
Expand Down Expand Up @@ -143,14 +143,24 @@ return function (version, reduce, map, codec, initial) {
},
createSink: function (cb) {
closeStream = cb;
let count = 0
return Drain(function (data) {
var _data = map(data.value, data.seq)
if(_data != null) value.set(reduce(value.value, _data, data.seq))
since.set(data.seq)
notify(_data)
//if we are now in sync with the log, maybe write.
if(since.value === log.since.value)

// If we are now in sync with the log, write.
const inSyncWithLog = since.value === log.since.value

// Alternatively, write every 10,000 messages.
const periodicWrite = writeInterval && count % writeInterval == 0

if(inSyncWithLog || periodicWrite) {
write()
}

count += 1
}, cb)
},
destroy: function (cb) {
Expand Down