-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.js
More file actions
33 lines (26 loc) · 700 Bytes
/
Store.js
File metadata and controls
33 lines (26 loc) · 700 Bytes
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
const compose = require('./proxies/compose')
const {edit, edits} = require('./Editor')
class Store {
constructor () {
this.version = 0 // increments every time a commit is accepted
this.commits = [{}]
this.transactions = new Set()
}
async transact (executor) {
const transaction = {
version: this.version,
state: edit(compose(...this.commits))
}
this.transactions.add(transaction)
try {
const result = await executor(transaction.state)
const delta = edits(transaction.state)
if (delta) { this.commits.push(delta) }
return result
} catch (err) {
// TODO: Cleanup
throw err
}
}
}
module.exports = Store