-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsneak.js
253 lines (219 loc) · 5.92 KB
/
sneak.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(function(Rx) {
var proto = Rx.Observable.prototype
var map = proto.map
var id = 0
var names = ['map', 'flatMap', 'select', 'startWith', 'combineLatest', 'merge']
function make() {
var div = document.createElement('div')
document.body.appendChild(div)
div.addThing = function (value, lok, cls) {
var child = document.createElement('div')
div.appendChild(child)
if ('string' !== typeof value) {
value = safe(value)
}
if (!lok) {
value = ('' + value).slice(0, 100)
}
if (cls) {
child.className = cls
}
child.innerText = value
}
return div
}
function get_all_things(all_thingss) {
var res = {}
for (var name in all_things) {
var old = all_things[name]
res[name] = {
tid: old.tid,
previf: old.previd,
name: old.name,
values: old.values.map(function (value) {
return {
value: safe(value.value).slice(0, 100),
type: value.type,
uid: value.uid, async: value.async, ts: value.ts}
})
}
}
return res
}
function safe(value) {
try {return JSON.stringify(value)}
catch(e){}
try {return value+''}
catch(e){}
return 'value cannot be previewed'
}
function setupLogger(name, tid, previd) {
var logger = make()
logger.addThing('Stream ' + tid + ': ' + name + ' : ' + previd, true, 'title')
var stack = getStack()
logger.addThing(stack, true, 'stack')
logger.nameish = name + ':' + tid + ':' + previd
logger.thisid = tid
logger.fromid = previd
return logger
}
var _uid = 10000;
function uid() {
return _uid++
}
var all_things = {}
// window.all_things = all_things
window.RxVision = {
dump: function () {
return get_all_things(all_things)
},
all_things: all_things
}
function mapit(prefix, logger, unwrap) {
var holder
if (!all_things[logger.thisid]) {
holder = {
tid: logger.thisid,
previd: logger.fromid,
name: logger.nameish,
values: [],
insync: null,
}
all_things[holder.tid] = holder
} else {
holder = all_things[logger.thisid]
}
function clearsync() {
holder.insync = null
}
return function (value) {
logger.addThing(prefix + ' ' + safe(value))
//return value
console.log('wrap', unwrap, value, logger.nameish)
if (unwrap) {
holder.values.push({
type: 'recv',
value: value.value,
uid: value.uid,
ts: Date.now()
})
value = value.value
if (null === holder.insync) {
holder.insync = setTimeout(clearsync, 0)
}
} else {
value = {value: value, uid: uid()}
holder.values.push({
type: 'send',
value: value.value,
uid: value.uid,
ts: Date.now(),
async: null === holder.insync
})
}
return value
}
}
// the down and dirty
var chains = []
, chainmap = {}
var fromEvent = Rx.Observable.fromEvent
Rx.Observable.fromEvent = function () {
chains.push([])
var tid = 'ee' + id++
var logger = setupLogger('fromevent ' + arguments[1] + ' / ' + arguments[0].id + '.' + arguments[0].className, tid, null)
var result = map.call(fromEvent.apply(this, arguments), mapit('[e]', logger))
result.sneakid = tid
chainmap[result.sneakid] = chains.length - 1
return result
}
var share = proto.share
proto.share = function () {
var result = share.call(this, arguments)
result.sneakid = this.sneakid
return result
}
names.forEach(function (name) {
var old = proto[name]
proto[name] = function () {
var stack = getStack()
if (!stack || !this.sneakid) return old.apply(this, arguments)
/* TODO uncomment this to find missing observables
if (!this.sneakid) {
this.sneakid = '_aa_' + id++
}
*/
var tid = id++
var logger = setupLogger(name, tid, this.sneakid)
var args = [].slice.call(arguments)
// pick up the merging thing
if (name === 'merge') {
if ('number' !== typeof arguments[0]) {
var other = args[0]
other.sneakid = other.sneakid || '_mm_' + id++
args[0] = map.call(other, mapit('||' + other.sneakid, logger, true))
}
}
// pick up the combining thing
if (name === 'combineLatest') {
for (var i=0; i<args.length - 1; i++) {
if (Array.isArray(args[i])) continue // w/e
(function (i) {
var old = args[i]
old.sneakid = old.sneakid || '_cc_' + id++
args[i] = map.call(old, mapit('<>' + old.sneakid, logger, true))
})(i)
}
}
var result = map.call(
old.apply(map.call(this, mapit('>', logger, true)), args),
mapit('<', logger)
)
result.sneakid = tid
return result
}
})
function getStack() {
try {throw new Error()}
catch (e) {
return e.stack.split('\n').slice(2).filter(function (line) {
return line.indexOf('rx.all.js') === -1 && line.indexOf('sneak.js') === -1
}).join('\n')
}
}
var sub = proto.subscribe
proto.subscribe = function (fn, onErr, comp) {
if (!fn || typeof fn !== 'function') return sub.apply(this, arguments)
var stack = getStack()
if (!stack) return sub.apply(this, arguments)
var logger = make()
if (!this.sneakid) {
this.sneakid = '_ss_' + id++
}
var tid = id++
logger.addThing('Subscription ' + tid + ' : ' + this.sneakid, true, 'title')
var stack = getStack()
logger.addThing(stack, true, 'stack')
var args = [].slice.call(arguments)
args[0] = function (value) {
console.log(tid, 'got', value)
logger.addThing(value)
arguments[0] = value.value
return fn.apply(this, arguments)
}
return sub.apply(this, args)
/* TODO error, completion handling?
return sub.call(this, function (value) {
console.log(tid, 'got', value)
logger.addThing(value)
return fn && fn.apply(this, arguments)
}, function (err) {
logger.addThing('Error\n' + err.message + err.stack)
return onErr && onErr.apply(this, arguments)
}, function () {
logger.addThing('done forever', tid)
return comp && comp.apply(this, arguments)
})
*/
}
}(Rx))