Skip to content

Commit 5903579

Browse files
authored
Merge pull request #72 from lightninglabs/avoid-global-scope
wasm: allow non-global scope callbacks
2 parents b3af0fe + 9a111d9 commit 5903579

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

cmd/wasm-client/main.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,32 @@ func main() {
9090
}
9191

9292
// Setup JS callbacks.
93-
callbacks := js.ValueOf(make(map[string]interface{}))
94-
callbacks.Set("wasmClientIsReady", js.FuncOf(wc.IsReady))
95-
callbacks.Set("wasmClientConnectServer", js.FuncOf(wc.ConnectServer))
96-
callbacks.Set("wasmClientIsConnected", js.FuncOf(wc.IsConnected))
97-
callbacks.Set("wasmClientDisconnect", js.FuncOf(wc.Disconnect))
98-
callbacks.Set("wasmClientInvokeRPC", js.FuncOf(wc.InvokeRPC))
99-
callbacks.Set("wasmClientStatus", js.FuncOf(wc.Status))
100-
callbacks.Set("wasmClientGetExpiry", js.FuncOf(wc.GetExpiry))
101-
callbacks.Set("wasmClientHasPerms", js.FuncOf(wc.HasPermissions))
102-
callbacks.Set("wasmClientIsReadOnly", js.FuncOf(wc.IsReadOnly))
103-
callbacks.Set("wasmClientIsCustom", js.FuncOf(wc.IsCustom))
104-
js.Global().Set(cfg.NameSpace, callbacks)
93+
callbacks := make(map[string]interface{})
94+
callbacks["wasmClientIsReady"] = js.FuncOf(wc.IsReady)
95+
callbacks["wasmClientConnectServer"] = js.FuncOf(wc.ConnectServer)
96+
callbacks["wasmClientIsConnected"] = js.FuncOf(wc.IsConnected)
97+
callbacks["wasmClientDisconnect"] = js.FuncOf(wc.Disconnect)
98+
callbacks["wasmClientInvokeRPC"] = js.FuncOf(wc.InvokeRPC)
99+
callbacks["wasmClientStatus"] = js.FuncOf(wc.Status)
100+
callbacks["wasmClientGetExpiry"] = js.FuncOf(wc.GetExpiry)
101+
callbacks["wasmClientHasPerms"] = js.FuncOf(wc.HasPermissions)
102+
callbacks["wasmClientIsReadOnly"] = js.FuncOf(wc.IsReadOnly)
103+
callbacks["wasmClientIsCustom"] = js.FuncOf(wc.IsCustom)
104+
105+
// Check if a JS object for the namespace already exists on the global
106+
// scope.
107+
nsObj := js.Global().Get(cfg.NameSpace)
108+
if !isEmptyObject(nsObj) {
109+
// If it exists, set the callbacks on the existing object. This
110+
// prevents overwriting the existing object, removing any
111+
// vars/funcs that may have been set by the user.
112+
for cb, name := range callbacks {
113+
nsObj.Set(cb, name)
114+
}
115+
} else {
116+
// If not, create the JS object and set the callbacks.
117+
js.Global().Set(cfg.NameSpace, js.ValueOf(callbacks))
118+
}
105119

106120
for _, registration := range litclient.Registrations {
107121
registration(wc.registry)
@@ -572,7 +586,17 @@ func parseKeys(onLocalPrivCreate, localPrivKey, remotePubKey string) (
572586
}
573587

574588
func callJsCallback(callbackName string, value string) error {
575-
retValue := js.Global().Call(callbackName, value)
589+
// callbackName can be in the form of "namespace.callbackName" or just
590+
// "callbackName". If it is in the former form, we need to get the
591+
// namespace object and call the callback on that object.
592+
jsScope := js.Global()
593+
parts := strings.Split(callbackName, ".")
594+
if len(parts) > 1 {
595+
jsScope = jsScope.Get(parts[0])
596+
callbackName = parts[1]
597+
}
598+
599+
retValue := jsScope.Call(callbackName, value)
576600

577601
if isEmptyObject(retValue) || isEmptyObject(retValue.Get("err")) {
578602
return nil

example/index.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,21 @@
4141
}
4242
}
4343

44+
// Set the callbacks on the namespace object
45+
window[namespace] = {
46+
onAuthData: onAuthData,
47+
onLocalKeyCreate: onLocalKeyCreate,
48+
onRemoteKeyReceive: onRemoteKeyReceive,
49+
};
50+
4451
console.clear();
4552
go.argv = [
4653
'wasm-client',
4754
'--debuglevel=trace',
48-
'--namespace='+namespace,
49-
'--onlocalprivcreate=onLocalKeyCreate',
50-
'--onremotekeyreceive=onRemoteKeyReceive',
51-
'--onauthdata=onAuthData',
55+
'--namespace=' + namespace,
56+
'--onlocalprivcreate=' + namespace + '.onLocalKeyCreate',
57+
'--onremotekeyreceive=' + namespace + '.onRemoteKeyReceive',
58+
'--onauthdata=' + namespace + '.onAuthData',
5259
];
5360
let readyTicker = null;
5461
let isReady = function () {

0 commit comments

Comments
 (0)