diff --git a/wasm/channels.go b/wasm/channels.go index d32737321c0efbab9ff732917b185a7b2cfbea99..2535e7c0965eed81a1ac6f939d75812732d16042 100644 --- a/wasm/channels.go +++ b/wasm/channels.go @@ -626,9 +626,9 @@ func GetChannelInfo(_ js.Value, args []js.Value) any { // 1 = private, and 2 = secret. Refer to the comment below for more // information. // -// Returns: -// - The pretty print of the channel (string). -// - Throws a TypeError if generating the channel fails. +// Returns a promise: +// - Resolves to the pretty print of the channel (string). +// - Rejected with an error if generating the channel fails. // // The [broadcast.PrivacyLevel] of a channel indicates the level of channel // information revealed when sharing it via URL. For any channel besides public @@ -640,14 +640,21 @@ func GetChannelInfo(_ js.Value, args []js.Value) any { // description. // - A privacy level of [broadcast.Secret] reveals nothing. func (cm *ChannelsManager) GenerateChannel(_ js.Value, args []js.Value) any { - prettyPrint, err := cm.api.GenerateChannel( - args[0].String(), args[1].String(), args[2].Int()) - if err != nil { - utils.Throw(utils.TypeError, err) - return nil + name := args[0].String() + description := args[1].String() + privacyLevel := args[2].Int() + + promiseFn := func(resolve, reject func(args ...any) js.Value) { + prettyPrint, err := + cm.api.GenerateChannel(name, description, privacyLevel) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve(prettyPrint) + } } - return prettyPrint + return utils.CreatePromise(promiseFn) } // JoinChannel joins the given channel. It will return the error diff --git a/wasm/cmix.go b/wasm/cmix.go index cd994706e956c7a3f618cb50aa6d25b83097e73c..88c9853db7654fb065ab91a6fa9b0dc1fcbddd54 100644 --- a/wasm/cmix.go +++ b/wasm/cmix.go @@ -83,19 +83,25 @@ func newCmixJS(api *bindings.Cmix) map[string]any { // - args[2] - Password used for storage (Uint8Array). // - args[3] - Registration code (string). // -// Returns: -// - Throws a TypeError if creating new [Cmix] fails. +// Returns a promise: +// - Resolves on success. +// - Rejected with an error if creating a new cMix client fails. func NewCmix(_ js.Value, args []js.Value) any { + ndfJSON := args[0].String() + storageDir := args[1].String() password := utils.CopyBytesToGo(args[2]) + registrationCode := args[3].String() - err := bindings.NewCmix( - args[0].String(), args[1].String(), password, args[3].String()) - if err != nil { - utils.Throw(utils.TypeError, err) - return nil + promiseFn := func(resolve, reject func(args ...any) js.Value) { + err := bindings.NewCmix(ndfJSON, storageDir, password, registrationCode) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve() + } } - return nil + return utils.CreatePromise(promiseFn) } // LoadCmix will load an existing user storage from the storageDir using the