forked from espruino/EspruinoTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespruino.js
113 lines (99 loc) · 4.17 KB
/
espruino.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
Copyright 2014 Gordon Williams (gw@pur3.co.uk)
This Source Code is subject to the terms of the Mozilla Public
License, v2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------
Initialisation code
------------------------------------------------------------------
**/
"use strict";
var Espruino;
(function() {
/** List of processors. These are functions that are called one
* after the other with the data received from the last one.
*
* Common processors are:
*
* sending - sending code to Espruino (no data)
* transformForEspruino - transform code ready to be sent to Espruino
* transformModuleForEspruino - transform module code before it's sent to Espruino with Modules.addCached (we only do this if we don't think it's been minified before)
* connected - connected to Espruino (no data)
* disconnected - disconnected from Espruino (no data)
* environmentVar - Board's process.env loaded (object to be saved into Espruino.Env.environmentData)
* boardJSONLoaded - Board's JSON was loaded into environmentVar
* getModule - Called with data={moduleName:"foo", moduleCode:undefined} - moduleCode should be filled in if the module can be found
* getURL - Called with data={url:"http://....", data:undefined) - data should be filled in if the URL is handled (See Espruino.Core.Utils.getURL to use this)
* terminalClear - terminal has been cleared
* terminalPrompt - we've received a '>' character (eg, `>` or `debug>`). The argument is the current line's contents.
* terminalNewLine - When we get a new line on the terminal, this gets called with the last line's contents
* debugMode - called with true or false when debug mode is entered or left
* editorHover - called with { node : htmlNode, showTooltip : function(htmlNode) } when something is hovered over
**/
var processors = {};
function init() {
Espruino.Core.Config.loadConfiguration(function() {
// Initialise all modules
function initModule(modName, mod) {
console.log("Initialising "+modName);
if (mod.init !== undefined)
mod.init();
}
var module;
for (module in Espruino.Core) initModule(module, Espruino.Core[module]);
for (module in Espruino.Plugins) initModule(module, Espruino.Plugins[module]);
callProcessor("initialised", undefined, function() {
// We need the delay because of background.js's url_handler...
setTimeout(function() {
Espruino.initialised = true;
}, 1000);
});
});
}
// workaround for broken chrome on Mac
if (navigator.userAgent.indexOf("Mac OS X")>=0 &&
navigator.userAgent.indexOf("Chrome/33.0.1750")>=0) {
$(document).ready(function() { window.setTimeout(init,100); });
} else {
$(document).ready(init);
}
/** Add a processor function of type function(data,callback) */
function addProcessor(eventType, processor) {
if (processors[eventType]===undefined)
processors[eventType] = [];
processors[eventType].push(processor);
}
/** Call a processor function */
function callProcessor(eventType, data, callback) {
var p = processors[eventType];
// no processors
if (p===undefined || p.length==0) {
if (callback!==undefined) callback(data);
return;
}
// now go through all processors
var n = 0;
var cbCalled = false;
var cb = function(inData) {
if (cbCalled) throw new Error("Internal error in "+eventType+" processor. Callback is called TWICE.");
cbCalled = true;
if (n < p.length) {
cbCalled = false;
p[n++](inData, cb);
} else {
if (callback!==undefined) callback(inData);
}
};
cb(data);
}
// -----------------------------------
Espruino = {
Core : { },
Plugins : { },
addProcessor : addProcessor,
callProcessor : callProcessor,
initialised : false,
init : init, // just in case we need to initialise this by hand
};
return Espruino;
})();