/
index.js
75 lines (62 loc) · 2.33 KB
/
index.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
import * as LightweightCharts from 'lightweight-charts';
import FunctionManager from './function-manager.js';
import ChartRegistrationFunctionsController from './chart-registration-functions-controller.js';
import PluginManager from './plugin-manager.js';
import { logger } from './logger.js';
const createChart = LightweightCharts.createChart
window.LightweightCharts = Object.assign({}, LightweightCharts)
delete window.LightweightCharts.createChart
logger.setLevel("warning")
onmessage = function (message) {
const connectionMessage = JSON.parse(message.data)
if (connectionMessage.messageType !== "Message::Connection") {
logger.e("Connection message is not valid")
return
}
const logLevel = connectionMessage.data.logLevel
logger.setLevel(logLevel)
logger.d("Received connection message", message)
const port = message.ports[0]
const functionManager = new FunctionManager(port)
const pluginManager = new PluginManager()
window.pluginManager = pluginManager
const functionsController = new ChartRegistrationFunctionsController(
window.chart,
functionManager,
pluginManager
)
functionsController.registerFunctions()
window.functionsController = functionsController
logger.d("Connection has been established")
port.onmessage = function (event) {
const nativeMessage = JSON.parse(event.data)
if (nativeMessage.data.fn) {
logger.d("function", nativeMessage.data.fn)
}
logger.d("data", JSON.stringify(nativeMessage.data))
switch (nativeMessage.messageType) {
case 'Message::Function':
functionManager.call(nativeMessage.data)
break;
case 'Message::Subscription':
functionManager.subscribe(nativeMessage.data)
break;
case 'Message::SubscriptionCancellation':
functionManager.unsubscribe(nativeMessage.data)
break;
}
}
}
window.onresize = () => {
window.chart.resize(window.innerWidth, window.innerHeight)
}
onload = () => {
window.chart = createChart(document.body, {
width: window.innerWidth,
height: window.innerHeight,
layout: {
//default value is transparent background
backgroundColor: "#00000000"
}
});
}