-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client): Transferred lots of client logic to context, adding…
… Electron support via postMessage
- Loading branch information
Showing
19 changed files
with
324 additions
and
201 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
npm-debug.log | ||
static/context.js | ||
static/karma.js | ||
.idea/* | ||
*.iml | ||
|
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
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
File renamed without changes.
File renamed without changes.
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,140 @@ | ||
// Load our dependencies | ||
var stringify = require('../common/stringify') | ||
|
||
// Define our context Karma constructor | ||
// TODO: We prob don't need a class, do we...? | ||
var ContextKarma = function (callParentKarmaMethod) { | ||
// Define local variables | ||
var hasError = false | ||
var self = this | ||
|
||
// Define our loggers | ||
// DEV: These are intentionally repeated in client and context | ||
this.log = function (type, args) { | ||
var values = [] | ||
|
||
for (var i = 0; i < args.length; i++) { | ||
values.push(this.stringify(args[i], 3)) | ||
} | ||
|
||
this.info({log: values.join(', '), type: type}) | ||
} | ||
|
||
this.stringify = stringify | ||
|
||
// Define our proxy error handler | ||
// DEV: We require one in our context to track `hasError` | ||
this.error = function () { | ||
hasError = true | ||
callParentKarmaMethod('error', [].slice.call(arguments)) | ||
return false | ||
} | ||
|
||
// Define our start handler | ||
var UNIMPLEMENTED_START = function () { | ||
this.error('You need to include some adapter that implements __karma__.start method!') | ||
} | ||
// all files loaded, let's start the execution | ||
this.loaded = function () { | ||
// has error -> cancel | ||
if (!hasError) { | ||
this.start(this.config) | ||
} | ||
|
||
// remove reference to child iframe | ||
this.start = UNIMPLEMENTED_START | ||
} | ||
// supposed to be overriden by the context | ||
// TODO(vojta): support multiple callbacks (queue) | ||
this.start = UNIMPLEMENTED_START | ||
|
||
// Define proxy methods | ||
// DEV: This is a closured `for` loop (same as a `forEach`) for IE support | ||
var proxyMethods = ['complete', 'info', 'result'] | ||
for (var i = 0; i < proxyMethods.length; i++) { | ||
(function bindProxyMethod (methodName) { | ||
self[methodName] = function boundProxyMethod () { | ||
callParentKarmaMethod(methodName, [].slice.call(arguments)) | ||
} | ||
}(proxyMethods[i])) | ||
} | ||
|
||
// Define bindings for context window | ||
this.setupContext = function (contextWindow) { | ||
// If we clear the context after every run and we already had an error | ||
// then stop now. Otherwise, carry on. | ||
if (self.config.clearContext && hasError) { | ||
return | ||
} | ||
|
||
// Perform window level bindings | ||
// DEV: We return `self.error` since we want to `return false` to ignore errors | ||
contextWindow.onerror = function () { | ||
return self.error.apply(self, arguments) | ||
} | ||
// DEV: We must defined a function since we don't want to pass the event object | ||
contextWindow.onbeforeunload = function (e, b) { | ||
callParentKarmaMethod('onbeforeunload', []) | ||
} | ||
|
||
contextWindow.dump = function () { | ||
self.log('dump', arguments) | ||
} | ||
|
||
contextWindow.alert = function (msg) { | ||
self.log('alert', [msg]) | ||
} | ||
|
||
// If we want to overload our console, then do it | ||
var getConsole = function (currentWindow) { | ||
return currentWindow.console || { | ||
log: function () {}, | ||
info: function () {}, | ||
warn: function () {}, | ||
error: function () {}, | ||
debug: function () {} | ||
} | ||
} | ||
if (self.config.captureConsole) { | ||
// patch the console | ||
var localConsole = contextWindow.console = getConsole(contextWindow) | ||
var logMethods = ['log', 'info', 'warn', 'error', 'debug'] | ||
var patchConsoleMethod = function (method) { | ||
var orig = localConsole[method] | ||
if (!orig) { | ||
return | ||
} | ||
localConsole[method] = function () { | ||
self.log(method, arguments) | ||
return Function.prototype.apply.call(orig, localConsole, arguments) | ||
} | ||
} | ||
for (var i = 0; i < logMethods.length; i++) { | ||
patchConsoleMethod(logMethods[i]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Define call/proxy methods | ||
ContextKarma.getDirectCallParentKarmaMethod = function (parentWindow) { | ||
return function directCallParentKarmaMethod (method, args) { | ||
// If the method doesn't exist, then error out | ||
if (!parentWindow.karma[method]) { | ||
parentWindow.karma.error('Expected Karma method "' + method + '" to exist but it doesn\'t') | ||
return | ||
} | ||
|
||
// Otherwise, run our method | ||
parentWindow.karma[method].apply(parentWindow.karma, args) | ||
} | ||
} | ||
ContextKarma.getPostMessageCallParentKarmaMethod = function (parentWindow) { | ||
// TODO: The postMessage implementation of `callParentKarmaMethod` is untested. Please test it | ||
return function postMessageCallParentKarmaMethod (method, args) { | ||
parentWindow.postMessage({method: method, arguments: args}, window.location.origin) | ||
} | ||
} | ||
|
||
// Export our module | ||
module.exports = ContextKarma |
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,24 @@ | ||
// TODO: Be sure that we lint this... | ||
// Load in our dependencies | ||
var ContextKarma = require('./karma') | ||
|
||
// Resolve our parent window | ||
var parentWindow = window.opener || window.parent | ||
|
||
// Define a remote call method for Karma | ||
var callParentKarmaMethod = ContextKarma.getDirectCallParentKarmaMethod(parentWindow) | ||
|
||
// If we don't have access to the window, then use `postMessage` | ||
// DEV: In Electron, we don't have access to the parent window due to it being in a separate process | ||
// DEV: We avoid using this in Internet Explorer as they only support strings | ||
// http://caniuse.com/#search=postmessage | ||
var haveParentAccess = false | ||
try { haveParentAccess = !!parentWindow.window } catch (err) { /* Ignore errors (likely permisison errors) */ } | ||
if (!haveParentAccess) { | ||
// TODO: In PhantomJS, we had to use `window.parent` not `window.opener`. | ||
// If we run into issues, try moving to `window.parent` | ||
callParentKarmaMethod = ContextKarma.getPostMessageCallParentKarmaMethod(parentWindow) | ||
} | ||
|
||
// Define a window-scoped Karma | ||
window.__karma__ = new ContextKarma(callParentKarmaMethod) |
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.