Skip to content
Snippets Groups Projects
Commit 3a8a86b9 authored by Jono Wenger's avatar Jono Wenger
Browse files

Wrap calls that start indexedDb with a promise to make them async

parent 15cf8ea0
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!8Updates to match the client fullyDecentrilizedChannels branch
...@@ -229,31 +229,33 @@ func LoadChannelsManager(_ js.Value, args []js.Value) interface{} { ...@@ -229,31 +229,33 @@ func LoadChannelsManager(_ js.Value, args []js.Value) interface{} {
// - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is // - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is
// generated by [GenerateChannelIdentity] (Uint8Array). // generated by [GenerateChannelIdentity] (Uint8Array).
// //
// Returns: // Returns a promise:
// - Javascript representation of the [ChannelsManager] object. // - Resolves to a Javascript representation of the [ChannelsManager] object.
// - Throws a TypeError if initialising indexedDb or created the new channel // - Rejected with an error if loading indexedDb or the manager fails.
// manager fails.
func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} { func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} {
privateIdentity := utils.CopyBytesToGo(args[1]) privateIdentity := utils.CopyBytesToGo(args[1])
emBuilder := func(path string) channels.EventModel { promiseFn := func(resolve, reject func(args ...interface{}) js.Value) {
em, err := indexedDb.NewWasmEventModel(path) emBuilder := func(path string) channels.EventModel {
if err != nil { em, err := indexedDb.NewWasmEventModel(path)
utils.Throw(utils.TypeError, err) if err != nil {
return nil reject(utils.JsTrace(err))
return nil
}
return em
} }
return em cm, err := bindings.NewChannelsManagerGoEventModel(
} args[0].Int(), privateIdentity, emBuilder)
if err != nil {
cm, err := bindings.NewChannelsManagerGoEventModel( reject(utils.JsTrace(err))
args[0].Int(), privateIdentity, emBuilder) } else {
if err != nil { resolve(newChannelsManagerJS(cm))
utils.Throw(utils.TypeError, err) }
return nil
} }
return newChannelsManagerJS(cm) return utils.CreatePromise(promiseFn)
} }
// LoadChannelsManagerWithIndexedDb loads an existing [ChannelsManager] using // LoadChannelsManagerWithIndexedDb loads an existing [ChannelsManager] using
...@@ -270,28 +272,31 @@ func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} { ...@@ -270,28 +272,31 @@ func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} {
// - args[1] - The storage tag associated with the previously created channel // - args[1] - The storage tag associated with the previously created channel
// manager and retrieved with [ChannelsManager.GetStorageTag] (string). // manager and retrieved with [ChannelsManager.GetStorageTag] (string).
// //
// Returns: // Returns a promise:
// - Javascript representation of the [ChannelsManager] object. // - Resolves to a Javascript representation of the [ChannelsManager] object.
// - Throws a TypeError if loading the manager fails. // - Rejected with an error if loading indexedDb or the manager fails.
func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} { func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) interface{} {
emBuilder := func(path string) channels.EventModel { promiseFn := func(resolve, reject func(args ...interface{}) js.Value) {
em, err := indexedDb.NewWasmEventModel(path) emBuilder := func(path string) channels.EventModel {
if err != nil { em, err := indexedDb.NewWasmEventModel(path)
utils.Throw(utils.TypeError, err) if err != nil {
return nil reject(utils.JsTrace(err))
return nil
}
return em
} }
return em cm, err := bindings.LoadChannelsManagerGoEventModel(
} args[0].Int(), args[1].String(), emBuilder)
if err != nil {
cm, err := bindings.LoadChannelsManagerGoEventModel( reject(utils.JsTrace(err))
args[0].Int(), args[1].String(), emBuilder) } else {
if err != nil { resolve(newChannelsManagerJS(cm))
utils.Throw(utils.TypeError, err) }
return nil
} }
return newChannelsManagerJS(cm) return utils.CreatePromise(promiseFn)
} }
// GenerateChannel is used to create a channel a new channel of which you are // GenerateChannel is used to create a channel a new channel of which you are
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment