-
Notifications
You must be signed in to change notification settings - Fork 90
/
espruino.js
131 lines (115 loc) · 4.93 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
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:
*
* jsCodeChanged - called when the code in JS editor changes with {code, editor}
* xmlCodeChanged - called when the code in the blockly editor changes with {code}
* sending - sending code to Espruino (no data)
* sendModeChanged - called when the send mode (RAM/Flash/Storage) changes
* transformForEspruino - transform code ready to be sent to Espruino
* transformModuleForEspruino({code,name})
* - 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
* notification - called with { mdg, type:"success","error"/"warning"/"info" }
* webcam - called when webcam is visible or not { visible : true , stream: MediaStream }
**/
var processors = {};
function init() {
Espruino.Core.Config.loadConfiguration(function() {
// Initialise all modules
function initModule(modName, mod) {
console.log("Initialising "+modName);
if (mod.init !== undefined) {
try {
mod.init();
} catch (e) {
console.warn("Module initialisation failed for "+modName, e);
}
}
}
var module;
function initModules(moduleList) {
var moduleNames = Object.keys(moduleList).sort(function(a,b) {
return (0|moduleList[a].sortOrder) - (0|moduleList[b].sortOrder);
});
//console.log(moduleNames);
moduleNames.forEach(function(module) {
initModule(module, moduleList[module]);
});
}
initModules(Espruino.Core);
initModules(Espruino.Plugins);
callProcessor("initialised", undefined, function() {
// We need the delay because of background.js's url_handler...
setTimeout(function() {
Espruino.initialised = true;
}, 1000);
});
});
}
// Automatically start up when all is loaded
if (typeof document!=="undefined")
document.addEventListener("DOMContentLoaded", 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;
})();