-
Notifications
You must be signed in to change notification settings - Fork 27
/
tracer.js
86 lines (71 loc) · 2.07 KB
/
tracer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This is a simple little OT library which wraps another OT library, but
// traces all calls so you can work out where everything came from.
const type = require('./lib/json1')
const {inspect} = require('util')
const log = require('./lib/log')
const deps = new Map // map from artifact to [...deps]
const meta = new Map // artifact to user information
const printInfo = (item, prefix = '', depth = 5) => {
const m = meta.get(item)
if (m == null) return log(prefix + 'NO DATA', item)
log(prefix + m.fn + ' ->', item, m)
if (depth > 0) {
const ds = deps.get(item)
if (ds == null) return
log(prefix + 'deps:', ...ds)
ds.forEach(d => printInfo(d, prefix + ' ', depth - 1))
}
}
module.exports = (type, genOp) => ({
...type,
create(data) { return type.create(data) },
apply(snapshot, op) {
try {
const result = type.apply(snapshot, op)
if (result !== snapshot) {
deps.set(result, [snapshot, op])
meta.set(result, {fn:'apply'})
}
return result
} catch (e) {
console.error('************************************* APPLY FAILED!')
console.error(e.stack)
log.quiet = false
printInfo(snapshot)
printInfo(op)
throw e
}
},
transform(op1, op2, side) {
try {
const result = type.transform(op1, op2, side)
if (result !== op1) {
deps.set(result, [op1, op2])
meta.set(result, {fn:'transform', side})
}
return result
} catch (e) {
console.error('************************************* TRANSFORM FAILED!')
console.error(e.stack)
log.quiet = false
printInfo(op1)
printInfo(op2)
throw e
}
},
genOp(snapshot) {
try {
const [op, result] = genOp(snapshot)
deps.set(op, [snapshot])
deps.set(result, [snapshot])
meta.set(op, {fn:'genop', result})
meta.set(result, {fn:'genop', op})
return [op, result]
} catch (e) {
console.error('************************************* OOPSIE!')
console.error(e.stack)
printInfo(snapshot)
throw e
}
},
})