-
Notifications
You must be signed in to change notification settings - Fork 2
/
committable.coffee
34 lines (30 loc) · 1.06 KB
/
committable.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
es = require 'event-stream'
CommitStream = require './commit-stream'
createCommit = (headers = []) ->
cfg = @
now = new Date()
utc = Date.UTC now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes(),
now.getSeconds(),
now.getMilliseconds()
checkRevision: cfg.streamRevision ? 0
streamId: cfg.streamId
streamRevision: (cfg.streamRevision ? 0) + (cfg?.events?.length ? 0)
timestamp: utc
headers: headers
payload: cfg.events
module.exports = (source, storage, cfg = {}) ->
source.createCommit = ->
unless source.committable
throw new Error 'source is not committable. this is likely due to not having been read from storage.'
commit = createCommit.apply source, []
(commit[k]=cfg[k]) for k,v of commit when ~k.indexOf 'headers'
commit
unless source.streamId
throw new Error 'streamId is required'
streamable = new CommitStream source
streamable.pipe(storage.commitStream())
streamable