-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,590 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ node_modules/ | |
|
||
# coverage | ||
coverage/ | ||
!frontend/src/assets/inspector/coverage/ | ||
|
||
package-lock.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
coverage/ | ||
!frontend/public/assets/inspector/coverage/ | ||
|
||
.git/ | ||
.vscode/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/@weex/plugins/debug/frontend/runtime/EventEmitter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
function EventEmitter() { | ||
this._handlers = {}; | ||
} | ||
EventEmitter.prototype = { | ||
constructor: EventEmitter, | ||
off: function (method, handler) { | ||
if (handler) { | ||
for (var i = 0; i < this._handlers[method].length; i++) { | ||
if (this._handlers[method][i] === handler) { | ||
this._handlers[method].splice(i, 1); | ||
i--; | ||
} | ||
} | ||
} | ||
else { | ||
this._handlers[method] = []; | ||
} | ||
}, | ||
once: function (method, handler) { | ||
var self = this; | ||
var fired = false; | ||
|
||
function g() { | ||
self.off(method, g); | ||
if (!fired) { | ||
fired = true; | ||
handler.apply(self, Array.prototype.slice.call(arguments)); | ||
} | ||
} | ||
|
||
this.on(method, g); | ||
}, | ||
on: function (method, handler) { | ||
if (this._handlers[method]) { | ||
this._handlers[method].push(handler); | ||
} | ||
else { | ||
this._handlers[method] = [handler]; | ||
} | ||
}, | ||
|
||
_emit: function (method, args, context) { | ||
var handlers = this._handlers[method]; | ||
if (handlers && handlers.length > 0) { | ||
handlers.forEach(function (handler) { | ||
handler.apply(context, args) | ||
}); | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}, | ||
|
||
emit: function (method) { | ||
var context = {}; | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
if (!this._emit(method, args, context)) { | ||
this._emit('*', args, context) | ||
} | ||
this._emit('$finally', args, context); | ||
return context; | ||
} | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/@weex/plugins/debug/frontend/runtime/WebsocketClient.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function WebsocketClient(url) { | ||
this.connect(url); | ||
} | ||
WebsocketClient.prototype = { | ||
constructor: WebsocketClient, | ||
connect: function (url) { | ||
var self = this; | ||
self.isSocketReady = false; | ||
self._sended = []; | ||
self._received = []; | ||
if (self.ws) { | ||
self.ws.onopen = null; | ||
self.ws.onmessage = null; | ||
self.ws.onclose = null; | ||
if (self.ws.readyState == WebSocket.OPEN) { | ||
self.ws.close(); | ||
} | ||
} | ||
var ws = new WebSocket(url); | ||
self.ws = ws; | ||
ws.onopen = function () { | ||
self.isSocketReady = true; | ||
self.emit('socketOpened'); | ||
}; | ||
ws.onmessage = function (e) { | ||
var message = JSON.parse(e.data); | ||
if (message.method) { | ||
self.emit(message.method, message); | ||
} | ||
}; | ||
ws.onclose = function () { | ||
self.isSocketReady = false; | ||
self.emit('socketClose'); | ||
}; | ||
|
||
}, | ||
send: function (data) { | ||
var self = this; | ||
if (self.isSocketReady) { | ||
self.ws.send(JSON.stringify(data)); | ||
} | ||
else { | ||
self.once('socketOpened', function () { | ||
self.ws.send(JSON.stringify(data)) | ||
}); | ||
} | ||
}, | ||
close: function () { | ||
this.ws && this.ws.close(); | ||
} | ||
}; | ||
WebsocketClient.prototype.__proto__ = new EventEmitter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
var workers = {}; | ||
var instanceMaps = {}; | ||
var RuntimeSocket | ||
var BrowserChannelId | ||
var cacheWeexEnv; | ||
var cacheJsbundleImportMessage; | ||
var cacheRegisterLoop = []; | ||
var cacheSyncList = []; | ||
var activeWorkerId; | ||
var EntrySocket = new WebsocketClient('ws://' + location.host + '/page/entry'); | ||
|
||
EntrySocket.on('WxDebug.startDebugger', function (message) { | ||
if (!RuntimeSocket) { | ||
location.href = `http://${location.host}/runtime/runtime.html?channelId=${message.params}` | ||
} | ||
else if(RuntimeSocket && BrowserChannelId!==message.params){ | ||
location.href = `http://${location.host}/runtime/runtime.html?channelId=${message.params}` | ||
} | ||
}) | ||
|
||
BrowserChannelId = new URLSearchParams(location.search).get('channelId'); | ||
|
||
if (BrowserChannelId) { | ||
connect(BrowserChannelId) | ||
} | ||
|
||
function connect(channelId) { | ||
RuntimeSocket = new WebsocketClient('ws://' + window.location.host + '/debugProxy/runtime/' + channelId); | ||
|
||
RuntimeSocket.on('*', function (message) { | ||
if (!message) return; | ||
var domain = message.method.split('.')[0]; | ||
if (domain === 'WxDebug') { | ||
var instanceId; | ||
if (message && message.params) { | ||
instanceId = message.params && message.params.args && message.params.args[0]; | ||
} | ||
else { | ||
instanceId = activeWorkerId | ||
} | ||
if (workers[instanceId]) { | ||
workers[instanceId].postMessage(message); | ||
} | ||
} | ||
}); | ||
|
||
RuntimeSocket.on('WxDebug.deviceDisconnect', function () { | ||
location.href = `http://${location.host}/runtime/runtime.html` | ||
}) | ||
|
||
RuntimeSocket.on('WxDebug.refresh', function () { | ||
location.reload(); | ||
}); | ||
|
||
RuntimeSocket.on('WxDebug.callJS', function (message) { | ||
var instanceId = message.params.args[0]; | ||
if (message.params.method === 'createInstanceContext') { | ||
destroyJSRuntime(message) | ||
message.channelId = BrowserChannelId; | ||
message.method = 'WxDebug.initSandboxWorker'; | ||
message.params.env = cacheWeexEnv; | ||
message.params.syncList = cacheSyncList.splice(0, cacheSyncList.length); | ||
initJSRuntime(message) | ||
} | ||
else if(message.params.method === 'createInstance') { | ||
destroyJSRuntime(message) | ||
message.channelId = BrowserChannelId; | ||
message.method = 'WxDebug.initWorker'; | ||
message.params.env = cacheWeexEnv; | ||
initJSRuntime(message) | ||
} | ||
else if(message.params.method === 'importScript') { | ||
if (workers[instanceId]) { | ||
workers[instanceId].postMessage(message) | ||
} | ||
else { | ||
cacheJsbundleImportMessage = message; | ||
} | ||
} | ||
else if(message.params.method === 'destroyInstance') { | ||
destroyJSRuntime(message); | ||
} | ||
else if (message.params.args && (message.params.method === 'registerComponents' || message.params.method === 'registerModules' || message.params.method === 'getJSFMVersion' || message.params.method === 'getJSFMVersion')) { | ||
cacheRegisterLoop.push(message); | ||
} | ||
else { | ||
if (message.params && message.params.args && message.params.args[0] && workers[message.params.args[0]]) { | ||
workers[message.params.args[0]].postMessage(message); | ||
} | ||
else if (activeWorkerId && workers[activeWorkerId]) { | ||
workers[activeWorkerId].postMessage(message); | ||
} | ||
} | ||
}); | ||
|
||
RuntimeSocket.on('WxDebug.initJSRuntime', function (message) { | ||
var logLevel = localStorage.getItem('logLevel'); | ||
if (logLevel) { | ||
message.params.env.WXEnvironment.logLevel = logLevel; | ||
} | ||
cacheWeexEnv = message.params.env; | ||
cacheRegisterLoop = []; | ||
}); | ||
} | ||
|
||
function destroyJSRuntime(message) { | ||
var instanceId = message.params.args[0]; | ||
var workerjs = message.params.workerjs; | ||
if (workerjs) { | ||
instanceId = instanceMaps[workerjs] | ||
} | ||
if (workers[instanceId]) { | ||
if (workers[instanceId].prev) { | ||
activeWorkerId = workers[instanceId].prev; | ||
} | ||
else { | ||
activeWorkerId = null; | ||
} | ||
workers[instanceId].terminate(); | ||
delete workers[instanceId]; | ||
} | ||
} | ||
|
||
function initJSRuntime(message) { | ||
var instanceId = activeWorkerId = message.params.args[0]; | ||
instanceMaps[message.params.workerjs] = instanceId; | ||
workers[instanceId] = new Worker(message.params.workerjs); | ||
workers[instanceId]['prev'] = getPrevWorker(workers); | ||
workers[instanceId].onmessage = function (message) { | ||
message = message.data; | ||
RuntimeSocket.send(message); | ||
}; | ||
cacheRegisterLoop.forEach(function(message) { | ||
workers[instanceId].postMessage(message) | ||
}) | ||
if (cacheJsbundleImportMessage) { | ||
workers[instanceId].postMessage(message); | ||
} | ||
workers[instanceId].postMessage(message); | ||
} | ||
|
||
function getPrevWorker(workers) { | ||
var lists = Object.keys(workers); | ||
if (lists.length === 0) return null; | ||
for(var i = lists.length - 2; i >=0; i--) { | ||
if (workers[lists[lists.length - 2]]) { | ||
return lists[lists.length - 2]; | ||
} | ||
} | ||
return null; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.