|
1 | 1 | import devtools from "@vue/devtools";
|
2 | 2 | import { isAndroid } from "@nativescript/core";
|
3 |
| -import * as toasty from "@triniwiz/nativescript-toasty"; |
4 |
| -import * as nsSocketIo from "@triniwiz/nativescript-socketio"; |
5 | 3 | import type { VueConstructor } from "vue";
|
6 | 4 |
|
7 | 5 | if (!global.performance) {
|
8 |
| - (global.performance as any) = {}; |
| 6 | + (global.performance as any) = {}; |
9 | 7 | }
|
10 | 8 |
|
11 | 9 | if (!global.performance.now) {
|
12 |
| - const nowOffset = Date.now(); |
| 10 | + const nowOffset = Date.now(); |
13 | 11 |
|
14 |
| - global.performance.now = function now() { |
15 |
| - return Date.now() - nowOffset; |
16 |
| - }; |
| 12 | + global.performance.now = function now() { |
| 13 | + return Date.now() - nowOffset; |
| 14 | + }; |
17 | 15 | }
|
18 | 16 |
|
19 | 17 | if (!global.requestAnimationFrame) {
|
20 |
| - global.requestAnimationFrame = function raf(cb) { |
21 |
| - return setTimeout(cb, 1000 / 60); |
22 |
| - }; |
| 18 | + global.requestAnimationFrame = function raf(cb) { |
| 19 | + return setTimeout(cb, 1000 / 60); |
| 20 | + }; |
23 | 21 | }
|
24 | 22 |
|
25 | 23 | // fixes a crash when running VueDevtools
|
26 | 24 | if (!global.HTMLElement) {
|
27 |
| - (global.HTMLElement as any) = function () { |
28 |
| - return false; |
29 |
| - }; |
| 25 | + (global.HTMLElement as any) = function () { |
| 26 | + return false; |
| 27 | + }; |
30 | 28 | }
|
31 | 29 |
|
32 | 30 | /**
|
33 | 31 | * Returns the correct address for the host machine when running on emulator
|
34 | 32 | */
|
35 | 33 | const getServerIpAddress = (host: string | null, port: number): string => {
|
36 |
| - if (host) { |
37 |
| - return `${host}:${port}`; |
| 34 | + if (host) { |
| 35 | + return `${host}:${port}`; |
| 36 | + } |
| 37 | + |
| 38 | + if (isAndroid) { |
| 39 | + // @ts-ignore |
| 40 | + const FINGERPRINT = android.os.Build.FINGERPRINT; |
| 41 | + if (FINGERPRINT.includes("vbox")) { |
| 42 | + // running on genymotion |
| 43 | + return `10.0.3.2:${port}`; |
| 44 | + } else if (FINGERPRINT.includes("generic")) { |
| 45 | + // running on android emulator |
| 46 | + return `10.0.2.2:${port}`; |
38 | 47 | }
|
| 48 | + } |
39 | 49 |
|
40 |
| - if (isAndroid) { |
41 |
| - // @ts-ignore |
42 |
| - const FINGERPRINT = android.os.Build.FINGERPRINT; |
43 |
| - if (FINGERPRINT.includes("vbox")) { |
44 |
| - // running on genymotion |
45 |
| - return `10.0.3.2:${port}`; |
46 |
| - } else if (FINGERPRINT.includes("generic")) { |
47 |
| - // running on android emulator |
48 |
| - return `10.0.2.2:${port}`; |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - // ios simulator uses localhost |
53 |
| - return `127.0.0.1:${port}`; |
| 50 | + // ios simulator uses localhost |
| 51 | + return `127.0.0.1:${port}`; |
54 | 52 | };
|
55 | 53 |
|
56 | 54 | // Wrap the toast message in a try, devtool still work without toaster
|
57 | 55 | const showToast = (message: string): void => {
|
58 |
| - try { |
59 |
| - const { Toasty } = toasty; |
60 |
| - new Toasty({ |
61 |
| - text: message, |
62 |
| - backgroundColor: "#53ba82", |
63 |
| - yAxisOffset: 50, |
64 |
| - }).show(); |
65 |
| - } catch (error) { |
66 |
| - console.log(error); |
67 |
| - } |
| 56 | + try { |
| 57 | + const { Toasty } = require("@triniwiz/nativescript-toasty"); |
| 58 | + new Toasty({ |
| 59 | + text: message, |
| 60 | + backgroundColor: "#53ba82", |
| 61 | + yAxisOffset: 50, |
| 62 | + }).show(); |
| 63 | + } catch (error) { |
| 64 | + console.log(`[nativescript-vue-devtools] ${message}`); |
| 65 | + } |
68 | 66 | };
|
69 | 67 |
|
70 | 68 | const install = (
|
71 |
| - Vue: VueConstructor, |
72 |
| - { debug = false, host = null, port = 8098 } = {} |
| 69 | + Vue: VueConstructor, |
| 70 | + { debug = false, host = null, port = 8098 } = {} |
73 | 71 | ): void => {
|
74 |
| - // ensure all dependencies are available and bail out if anything is missing |
75 |
| - try { |
76 |
| - require.resolve("@vue/devtools"); |
77 |
| - } catch (e) { |
78 |
| - console.log( |
79 |
| - "[nativescript-vue-devtools] skipping install due to missing dependencies.\n\n", |
80 |
| - e, |
81 |
| - "\n\n" |
82 |
| - ); |
83 |
| - return; |
84 |
| - } |
85 |
| - |
86 |
| - const startApp = Vue.prototype.$start; |
87 |
| - |
88 |
| - Vue.prototype.$start = function () { |
89 |
| - const setupDevtools = () => { |
90 |
| - const options = { |
91 |
| - app: this, |
92 |
| - showToast, |
93 |
| - io() { |
94 |
| - try { |
95 |
| - const { SocketIO } = nsSocketIo; |
96 |
| - const address = `http://${getServerIpAddress( |
97 |
| - host, |
98 |
| - port |
99 |
| - )}`; |
100 |
| - // this line causes a crash. the socketio plugin needs to be updated to nativescript version 8.0 |
101 |
| - let socketIO = new SocketIO(address, { debug: debug }); |
102 |
| - socketIO.connect(); |
103 |
| - return socketIO; |
104 |
| - } catch (error) { |
105 |
| - console.log(error); |
106 |
| - // prevent crashing by returning a noop stub |
107 |
| - return { |
108 |
| - on() {}, |
109 |
| - emit() {}, |
110 |
| - }; |
111 |
| - } |
112 |
| - }, |
| 72 | + // ensure all dependencies are available and bail out if anything is missing |
| 73 | + try { |
| 74 | + require.resolve("@vue/devtools"); |
| 75 | + require.resolve("@triniwiz/nativescript-socketio"); |
| 76 | + } catch (e) { |
| 77 | + console.log( |
| 78 | + "[nativescript-vue-devtools] skipping install due to missing dependencies.\n\n", |
| 79 | + e, |
| 80 | + "\n\n" |
| 81 | + ); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const startApp = Vue.prototype.$start; |
| 86 | + |
| 87 | + Vue.prototype.$start = function () { |
| 88 | + const setupDevtools = () => { |
| 89 | + const options = { |
| 90 | + app: this, |
| 91 | + showToast, |
| 92 | + io() { |
| 93 | + try { |
| 94 | + const { SocketIO } = require("@triniwiz/nativescript-socketio"); |
| 95 | + const address = `http://${getServerIpAddress(host, port)}`; |
| 96 | + // this line causes a crash. the socketio plugin needs to be updated to nativescript version 8.0 |
| 97 | + let socketIO = new SocketIO(address, { debug: debug }); |
| 98 | + socketIO.connect(); |
| 99 | + return socketIO; |
| 100 | + } catch (error) { |
| 101 | + console.log(error); |
| 102 | + // prevent crashing by returning a noop stub |
| 103 | + return { |
| 104 | + on() {}, |
| 105 | + emit() {}, |
113 | 106 | };
|
| 107 | + } |
| 108 | + }, |
| 109 | + }; |
114 | 110 |
|
115 |
| - // vue-dev-tools type files are outdated so this is to get around that |
116 |
| - (devtools as any).connect("ws://localhost", port, options); |
117 |
| - (devtools as any).init(Vue); |
118 |
| - }; |
119 |
| - |
120 |
| - if (isAndroid) { |
121 |
| - setupDevtools(); |
122 |
| - } else { |
123 |
| - // on ios we need to delay the call because the socket connection is not established |
124 |
| - // if called too early in the application startup process |
125 |
| - // we might need to add a delay to the setTimeout in the future |
126 |
| - setTimeout(setupDevtools); |
127 |
| - } |
128 |
| - |
129 |
| - return startApp.call(this); |
| 111 | + // vue-dev-tools type files are outdated so this is to get around that |
| 112 | + (devtools as any).connect("ws://localhost", port, options); |
| 113 | + (devtools as any).init(Vue); |
130 | 114 | };
|
| 115 | + |
| 116 | + if (isAndroid) { |
| 117 | + setupDevtools(); |
| 118 | + } else { |
| 119 | + // on ios we need to delay the call because the socket connection is not established |
| 120 | + // if called too early in the application startup process |
| 121 | + // we might need to add a delay to the setTimeout in the future |
| 122 | + setTimeout(setupDevtools); |
| 123 | + } |
| 124 | + |
| 125 | + return startApp.call(this); |
| 126 | + }; |
131 | 127 | };
|
132 | 128 |
|
133 | 129 | export default {
|
134 |
| - install, |
| 130 | + install, |
135 | 131 | };
|
0 commit comments