-
Notifications
You must be signed in to change notification settings - Fork 189
/
common.js
63 lines (54 loc) · 2.09 KB
/
common.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
/**
* Entry module for building bonsai for a common environment.
*
* It takes care to select the best-suited runner (iframe or worker) environment.
*/
define([
'../player',
'../../message_channel',
'../context/iframe/bootstrap',
'../context/iframe/context',
'../context/worker/bootstrap',
'../context/worker/context',
'../../renderer/svg/svg',
'../../tools',
'../util'
], function(player, MessageChannel, bootstrapIframe, IframeRunnerContext, bootstrapWorker, WorkerRunnerContext, SvgRenderer, tools, bootstrapUtil) {
'use strict';
if(typeof window != 'undefined' && window.messageChannel) {
// This needs to execute async (setTimeout:1) so that
// IFrameContext.init finishes before this executes:
setTimeout(function() {
var messageChannel = window.messageChannel;
delete window.messageChannel;
bootstrapIframe(messageChannel, window);
}, 1);
} else if(typeof importScripts != 'undefined') {
// init worker bootstrap
var messageChannel;
var notifyRenderer = function(message) {postMessage(message);};
var onMessage = function(message) {messageChannel.notify(message.data);};
var disconnect = function() {
removeEventListener('message', onMessage);
messageChannel = null;
};
messageChannel = new MessageChannel(notifyRenderer, disconnect);
addEventListener('message', onMessage);
bootstrapWorker(messageChannel);
} else {
window['bonsai'] = player;
var runnerUrl = bootstrapUtil.getUrl(__bonsaiRunnerCode__);
player.Renderer = SvgRenderer;
player.IframeRunnerContext = IframeRunnerContext;
player.WorkerRunnerContext = WorkerRunnerContext;
var originalPlayerRun = player.run;
player.run = tools.hitch(player, function(node, url, options) {
player.setup({
baseUrl: player._baseUrl || tools.baseUri(document),
runnerContext: player.RunnerContext || (runnerUrl ? WorkerRunnerContext : IframeRunnerContext),
runnerUrl: player.runnerUrl || runnerUrl || ('' + __bonsaiRunnerCode__)
});
return originalPlayerRun.apply(player, arguments);
});
}
});