-
Notifications
You must be signed in to change notification settings - Fork 7
/
BridgeScript.js
189 lines (163 loc) · 6.27 KB
/
BridgeScript.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
if (!window.Bridge) {
const __bridgeInternal = window.__bridgeInternal;
delete window.__bridgeInternal; // HIDE.
function randomId() {
return Math.random().toString(28).replace(".", "");
}
let listeners = {};
const Bridge = {
on(type, callback) {
if (typeof type !== "string" || !(callback instanceof Function)) {
throw "`type` must be a string and `callback` must be a function.";
}
const callbackId = randomId();
type = type.toLowerCase();
const callbacks = listeners[type] || {};
callbacks[callbackId] = callback;
listeners[type] = callbacks;
return () => {
delete listeners[type][callbackId];
};
},
once(type, callback) {
const unregisterFunction = Bridge.on(type, (v) => {
unregisterFunction();
callback(v);
});
return unregisterFunction;
},
__internal: {
listeners: listeners,
sendMessageToJava(type, data) {
return __bridgeInternal(type, data);
},
defineObject(path, id) {
path = path.split(".");
const propertyName = path.pop();
let proxy;
const object = {
__stores: {
svelte(field) {
return {
subscribe(callback) {
return Bridge.__internal.mutate(id, field, callback);
},
set(val) {
proxy[field] = val;
},
};
},
},
__internal: {
id: id,
defineFunction(name) {
Object.defineProperty(object, name, {
value: function () {
return Bridge.__internal.invoke(
id,
name,
Array.from(arguments)
);
},
});
},
defineProperty(name) {
Object.defineProperty(object, name, {
value: null,
writable: true,
configurable: true,
});
},
},
};
const handler = {
get(obj, property) {
if (typeof obj[property] !== "undefined" && obj[property] !== null) {
return obj[property];
}
return Bridge.__internal.get(id, property);
},
set(obj, property, value) {
Bridge.__internal.set(id, property, value);
return value;
},
};
Object.freeze(object.__stores);
Object.freeze(object.__internal);
proxy = new Proxy(object, handler);
// Resolve the root object.
let root = window;
for (const part of path) {
root = root[part];
}
Object.defineProperty(root, propertyName, {
value: proxy,
writable: true,
configurable: true,
});
},
get(id, property) {
return Bridge.__internal.sendMessageToJava("GET", { id, property });
},
set(id, property, newValue) {
return Bridge.__internal.sendMessageToJava("SET", {
id,
property,
newValue,
});
},
mutate(id, property, callback) {
const listenerType = `__internal:mut:${id}:${property}`;
const unregisterFunction = Bridge.on(listenerType, callback);
Bridge.__internal.get(id, property).then(callback); // Initial set.
return () => unregisterFunction;
},
invoke(id, func, arguments) {
return Bridge.__internal.sendMessageToJava("INVOKE", {
id,
function: func,
arguments,
});
},
broadcast(type, data) {
// Broadcast under a wildcard.
const wildCardCallbacks = listeners["*"];
if (wildCardCallbacks) {
Object.values(wildCardCallbacks).forEach((callback) => {
try {
callback(type.toLowerCase(), data);
} catch (e) {
console.error(
"[Webview-Bridge]",
"A listener produced an exception: "
);
console.error(e);
}
});
}
// Broadcast under type.
const callbacks = listeners[type.toLowerCase()];
if (callbacks) {
Object.values(callbacks).forEach((callback) => {
try {
callback(data);
} catch (e) {
console.error(
"[Webview-Bridge]",
"A listener produced an exception: "
);
console.error(e);
}
});
}
},
},
};
Object.freeze(Bridge);
Object.freeze(Bridge.__internal);
Object.defineProperty(window, "Bridge", {
value: Bridge,
writable: false,
configurable: false,
});
}