forked from basicallydan/skifree.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlayEGI.js
87 lines (80 loc) · 2.44 KB
/
PlayEGI.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
// Dividat Play EGI
;(function() {
var play = null
var commandQueue = []
// Helper to send commands
function sendCommand(cmd) {
if (play) {
play.postMessage(cmd, '*')
} else {
commandQueue.push(cmd)
}
}
var signalHandler = null
// Setup listener for messages from Play
window.addEventListener('message', function(event) {
var signal = event.data
play = event.source
switch (signal.type) {
case 'SetupEGI':
// This signal is only to setup the interface it is not forwarded to consumers
// Forward any commands sent before connection had been established
while (commandQueue.length > 0) {
play.postMessage(commandQueue.shift(), '*')
}
break
default:
if (signalHandler) {
signalHandler(signal)
}
break
}
})
// Add an error handler
window.onerror = function(message, source, lineno, colno, error) {
sendCommand({
type: 'Error',
error: {
message: message,
source: source,
lineno: lineno,
colno: colno,
error: error.toString(),
stack:
typeof error.stack === 'string'
? error.stack.split('\n')
: null
}
})
}
window.PlayEGI = {
ready: () => {
sendCommand({ type: 'Ready' })
},
pong: () => {
sendCommand({ type: 'Pong' })
},
log: entry => {
sendCommand({ type: 'Log', entry: entry })
},
finish: (metrics, memory) => {
metrics = metrics || {}
sendCommand({ type: 'Finish', metrics: metrics, memory: memory })
},
onSignal: cb => {
signalHandler = cb
},
led: settings => {
sendCommand({ type: 'Led', settings: settings })
},
motor: presetOrSettings => {
switch (typeof presetOrSettings) {
case 'string':
sendCommand({ type: 'Motor', preset: presetOrSettings })
break
default:
sendCommand({ type: 'Motor', settings: presetOrSettings })
}
}
}
})()