-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiny.js
53 lines (44 loc) · 914 Bytes
/
tiny.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { deepCompare } from './utils.js'
export const create = (value, userId, version = 0) => {
return {
value,
version,
createdBy: userId,
lastUpdatedBy: userId,
}
}
export const shouldUpdate = (datum, version, userId) => {
if (datum.version > version) {
return false
}
if (datum.version === version) {
if (datum.lastUpdatedBy === userId) {
return false
}
if (datum.lastUpdatedBy > userId) {
return false
}
}
return true
}
export const update = (datum, value, version, userId) => {
if (!shouldUpdate(datum, version, userId)) {
return null
}
return {
value,
version,
createdBy: datum.createdBy,
lastUpdatedBy: userId,
}
}
export const getLocalChanges = (datum, value, userId) => {
if (deepCompare(datum.value, value)) {
return null
}
return {
value,
version: datum.version + 1,
userId,
}
}