-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequencer.js
39 lines (34 loc) · 956 Bytes
/
sequencer.js
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
35
36
37
38
39
// Sequences handle ordering a list of references.
// Sequences can only grow, and are not allowed to shrink.
// Sequences can be re-ordered by creating a reference to a reference.
export const create = () => {
return {
references: [],
sequences: {},
createdBy: {},
}
}
export const insert = (ram, list, value, userId, sequence) => {
const { address } = ram.set(value, userId)
const { references, sequences, createdBy } = list
sequences[address] = sequence
createdBy[address] = userId
if (insertedAt.length === 0) {
references.push(address)
} else {
references.splice(
references.findIndex(
(indexAddress) =>
sequences[indexAddress] <= sequence &&
createdBy[indexAddress] < userId
),
0,
address
)
}
return address
}
export const getValues = (ram, sequencer) => {
const { references } = sequencer
return references.map((address) => ram.get(address))
}