Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xxdk-wasm
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
xxdk-wasm
Commits
e2c2fbbb
Commit
e2c2fbbb
authored
2 years ago
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Implement single.go
parent
85bc267d
No related branches found
No related tags found
1 merge request
!60
Revert "Fail a test to be sure it works"
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
main.go
+4
-0
4 additions, 0 deletions
main.go
wasm/e2e.go
+1
-1
1 addition, 1 deletion
wasm/e2e.go
wasm/e2e_test.go
+0
-1
0 additions, 1 deletion
wasm/e2e_test.go
wasm/single.go
+128
-0
128 additions, 0 deletions
wasm/single.go
wasm/single_test.go
+39
-0
39 additions, 0 deletions
wasm/single_test.go
with
172 additions
and
2 deletions
main.go
+
4
−
0
View file @
e2c2fbbb
...
...
@@ -104,6 +104,10 @@ func main() {
js
.
Global
()
.
Set
(
"AsyncRequestRestLike"
,
js
.
FuncOf
(
wasm
.
AsyncRequestRestLike
))
// bindings/single.go
js
.
Global
()
.
Set
(
"TransmitSingleUse"
,
js
.
FuncOf
(
wasm
.
TransmitSingleUse
))
js
.
Global
()
.
Set
(
"Listen"
,
js
.
FuncOf
(
wasm
.
Listen
))
<-
make
(
chan
bool
)
os
.
Exit
(
0
)
}
This diff is collapsed.
Click to expand it.
wasm/e2e.go
+
1
−
1
View file @
e2c2fbbb
...
...
@@ -83,7 +83,7 @@ func (e *E2e) GetID(js.Value, []js.Value) interface{} {
// - args[0] - ID of Cmix object in tracker (int).
// - args[1] - Javascript object that has functions that implement the
// [bindings.AuthCallbacks] interface.
// - args[2] - JSON of the [xxdk.ReceptionIdentity]
object
(Uint8Array).
// - args[2] - JSON of the [xxdk.ReceptionIdentity] (Uint8Array).
// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array).
//
// Returns:
...
...
This diff is collapsed.
Click to expand it.
wasm/e2e_test.go
+
0
−
1
View file @
e2c2fbbb
...
...
@@ -6,7 +6,6 @@
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
// +build js,wasm
package
wasm
...
...
This diff is collapsed.
Click to expand it.
wasm/single.go
0 → 100644
+
128
−
0
View file @
e2c2fbbb
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package
wasm
import
(
"gitlab.com/elixxir/client/bindings"
"syscall/js"
)
////////////////////////////////////////////////////////////////////////////////
// Public Wrapper Methods //
////////////////////////////////////////////////////////////////////////////////
// TransmitSingleUse transmits payload to recipient via single-use.
//
// Parameters:
// - args[0] - ID of E2e object in tracker (int).
// - args[1] - JSON of recipient [contact.Contact] (Uint8Array).
// - args[2] - tag that identifies the single-use message (string).
// - args[3] - message contents (Uint8Array).
// - args[4] - JSON of [single.RequestParams] (Uint8Array).
// - args[5] - the callback that will be called when a response is received. It
// is a Javascript object that has functions that implement the
// [bindings.SingleUseResponse] interface.
//
// Returns:
// - JSON [bindings.SingleUseSendReport], which can be passed into
// Cmix.WaitForRoundResult to see if the send succeeded (Uint8Array).
// - Throws a TypeError if transmission fails.
func
TransmitSingleUse
(
_
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
e2eID
:=
args
[
0
]
.
Int
()
recipient
:=
CopyBytesToGo
(
args
[
1
])
tag
:=
args
[
2
]
.
String
()
payload
:=
CopyBytesToGo
(
args
[
3
])
paramsJSON
:=
CopyBytesToGo
(
args
[
4
])
responseCB
:=
&
singleUseResponse
{
args
[
5
]
.
Get
(
"Callback"
)
.
Invoke
}
report
,
err
:=
bindings
.
TransmitSingleUse
(
e2eID
,
recipient
,
tag
,
payload
,
paramsJSON
,
responseCB
)
if
err
!=
nil
{
Throw
(
TypeError
,
err
.
Error
())
return
nil
}
return
CopyBytesToJS
(
report
)
}
// Listen starts a single-use listener on a given tag using the passed in E2e
// object and SingleUseCallback func.
//
// Parameters:
// - args[0] - ID of E2e object in tracker (int).
// - args[1] - tag that identifies the single-use message (string).
// - args[2] - the callback that will be called when a response is received. It
// is a Javascript object that has functions that implement the
// [bindings.SingleUseCallback] interface.
//
// Returns:
// - Javascript representation of the Stopper object, an interface containing a
// function used to stop the listener.
// - Throws a TypeError if listening fails.
func
Listen
(
_
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
cb
:=
&
singleUseCallback
{
args
[
2
]
.
Get
(
"Callback"
)
.
Invoke
}
api
,
err
:=
bindings
.
Listen
(
args
[
0
]
.
Int
(),
args
[
1
]
.
String
(),
cb
)
if
err
!=
nil
{
Throw
(
TypeError
,
err
.
Error
())
return
nil
}
return
newStopperJS
(
api
)
}
////////////////////////////////////////////////////////////////////////////////
// Function Types //
////////////////////////////////////////////////////////////////////////////////
// Stopper wraps the [bindings.Stopper] interface so its methods can be wrapped
// to be Javascript compatible.
type
Stopper
struct
{
api
bindings
.
Stopper
}
// newStopperJS creates a new Javascript compatible object
// (map[string]interface{}) that matches the Stopper structure.
func
newStopperJS
(
api
bindings
.
Stopper
)
map
[
string
]
interface
{}
{
s
:=
Stopper
{
api
}
stopperMap
:=
map
[
string
]
interface
{}{
"Stop"
:
js
.
FuncOf
(
s
.
Stop
),
}
return
stopperMap
}
func
(
s
*
Stopper
)
Stop
(
js
.
Value
,
[]
js
.
Value
)
interface
{}
{
s
.
api
.
Stop
()
return
nil
}
////////////////////////////////////////////////////////////////////////////////
// Callback Wrappers //
////////////////////////////////////////////////////////////////////////////////
// singleUseCallback wraps Javascript callbacks to adhere to the
// [bindings.SingleUseCallback] interface.
type
singleUseCallback
struct
{
callback
func
(
args
...
interface
{})
js
.
Value
}
func
(
suc
*
singleUseCallback
)
Callback
(
callbackReport
[]
byte
,
err
error
)
{
suc
.
callback
(
CopyBytesToJS
(
callbackReport
),
err
.
Error
())
}
// singleUseResponse wraps Javascript callbacks to adhere to the
// [bindings.SingleUseResponse] interface.
type
singleUseResponse
struct
{
callback
func
(
args
...
interface
{})
js
.
Value
}
func
(
sur
*
singleUseResponse
)
Callback
(
responseReport
[]
byte
,
err
error
)
{
sur
.
callback
(
CopyBytesToJS
(
responseReport
),
err
.
Error
())
}
This diff is collapsed.
Click to expand it.
wasm/single_test.go
0 → 100644
+
39
−
0
View file @
e2c2fbbb
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package
wasm
import
(
"reflect"
"testing"
)
// Tests that the map representing Stopper returned by newStopperJS contains all
// of the methods on Stopper.
func
Test_newStopperJS
(
t
*
testing
.
T
)
{
stopperType
:=
reflect
.
TypeOf
(
&
Stopper
{})
s
:=
newStopperJS
(
&
stopper
{})
if
len
(
s
)
!=
stopperType
.
NumMethod
()
{
t
.
Errorf
(
"Stopper JS object does not have all methods."
+
"
\n
expected: %d
\n
received: %d"
,
stopperType
.
NumMethod
(),
len
(
s
))
}
for
i
:=
0
;
i
<
stopperType
.
NumMethod
();
i
++
{
method
:=
stopperType
.
Method
(
i
)
if
_
,
exists
:=
s
[
method
.
Name
];
!
exists
{
t
.
Errorf
(
"Method %s does not exist."
,
method
.
Name
)
}
}
}
type
stopper
struct
{}
func
(
s
*
stopper
)
Stop
()
{}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment