-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_pluginCore.js
94 lines (74 loc) · 2.12 KB
/
_pluginCore.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
/**
* Core plugin script loaded into the plugin process/thread.
*
* Initializes the plugin-site API global methods.
*/
(function(){
// localize
var site = new JailedSite(connection);
delete JailedSite;
delete connection;
site.onGetInterface(function(){
launchConnected();
});
site.onRemoteUpdate(function(){
var remote = application.remote = site.getRemote();
});
/**
* Simplified clone of Whenable instance (the object can not be
* placed into a shared script, because the main library needs it
* before the additional scripts may load)
*/
var connected = false;
var connectedHandlers = [];
var launchConnected = function() {
if (!connected) {
connected = true;
var handler;
while(handler = connectedHandlers.pop()) {
handler();
}
}
}
var checkHandler = function(handler){
var type = typeof handler;
if (type != 'function') {
var msg =
'A function may only be subsribed to the event, '
+ type
+ ' was provided instead'
throw new Error(msg);
}
return handler;
}
/**
* Sets a function executed after the connection to the
* application is estaplished, and the initial interface-exchange
* messaging is completed
*
* @param {Function} handler to be called upon initialization
*/
application.whenConnected = function(handler) {
handler = checkHandler(handler);
if (connected) {
handler();
} else {
connectedHandlers.push(handler);
}
}
/**
* Sets the plugin interface available to the application
*
* @param {Object} _interface to set
*/
application.setInterface = function(_interface) {
site.setInterface(_interface);
}
/**
* Disconnects the plugin from the application (sending
* notification message) and destroys itself
*/
application.disconnect = function(_interface) {
site.disconnect();
}
})();