forked from nativescript-vue/nativescript-vue-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (76 loc) · 2.21 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const devtools = require('@vue/devtools');
const {isAndroid} = require('tns-core-modules/platform')
if (!global.performance) {
global.performance = {};
}
if (!global.performance.now) {
const nowOffset = Date.now();
global.performance.now = function now() {
return Date.now() - nowOffset;
}
}
if (!global.requestAnimationFrame) {
global.requestAnimationFrame = function raf(cb) {
return setTimeout(cb, 1000 / 60)
}
}
/**
* Returns the correct address for the host machine when running on emulator
* @param host
* @param port
* @returns {string}
*/
function getServerIpAddress(host, port) {
if (host) {
return `${host}:${port}`
}
if (isAndroid) {
const FINGERPRINT = android.os.Build.FINGERPRINT
if (FINGERPRINT.includes("vbox")) {
// running on genymotion
return `10.0.3.2:${port}`
} else if (FINGERPRINT.includes("generic")) {
// running on android emulator
return `10.0.2.2:${port}`
}
}
// ios simulator uses localhost
return `127.0.0.1:${port}`
}
// Wrap the toast message in a try, devtool still work without toaster
const showToast = (message) => {
try {
const Toasty = require('nativescript-toasty').Toasty;
new Toasty(message).show();
} catch (error) {
console.log(error);
}
}
module.exports = function install(Vue, {debug = false, host = null, port = 8098} = {}) {
const startApp = Vue.prototype.$start
Vue.prototype.$start = function () {
const setupDevtools = () => {
devtools.connect('ws://localhost', port, {
app: this,
showToast,
io() {
const address = `http://${getServerIpAddress(host, port)}`
const SocketIO = require('nativescript-socketio').SocketIO
let socketIO = new SocketIO(address, { debug: debug })
socketIO.connect()
return socketIO
}
})
devtools.init(Vue);
}
if (isAndroid) {
setupDevtools()
} else {
// on ios we need to delay the call because the socket connection is not established
// if called too early in the application startup process
// we might need to add a delay to the setTimeout in the future
setTimeout(setupDevtools)
}
return startApp.call(this)
}
}