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
e5f1ee07
Commit
e5f1ee07
authored
2 years ago
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Fix ID counter so there is one counter for tag
parent
487a6b70
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!67
fix for latest client release
,
!52
XX-4382 / Move indexedDb databases to web workers
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
indexedDb/messageHandler.go
+4
-0
4 additions, 0 deletions
indexedDb/messageHandler.go
indexedDbWorker/worker.go
+52
-43
52 additions, 43 deletions
indexedDbWorker/worker.go
indexedDbWorker/worker_test.go
+70
-0
70 additions, 0 deletions
indexedDbWorker/worker_test.go
with
126 additions
and
43 deletions
indexedDb/messageHandler.go
+
4
−
0
View file @
e5f1ee07
...
...
@@ -124,6 +124,10 @@ func (mh *MessageHandler) RegisterHandler(tag worker.Tag, handler HandlerFn) {
mh
.
mux
.
Unlock
()
}
////////////////////////////////////////////////////////////////////////////////
// Javascript Call Wrappers //
////////////////////////////////////////////////////////////////////////////////
// addEventListeners adds the event listeners needed to receive a message from
// the worker. Two listeners were added; one to receive messages from the worker
// and the other to receive errors.
...
...
This diff is collapsed.
Click to expand it.
indexedDbWorker/worker.go
+
52
−
43
View file @
e5f1ee07
...
...
@@ -20,10 +20,10 @@ import (
)
// TODO:
// 1.
F
ix
ID coun
te
r
// 2.
Get path to JS file from binding
s
// 3.
restructure package
s
// 4.
fix tag system
// 1.
f
ix
tag sys
te
m
// 2.
restructure package
s
// 3.
Get path to JS file from binding
s
// 4.
Add tests for worker.go and messageHandler.go
// InitID is the ID for the first item in the handler list. If the list only
// contains one handler, then this is the ID of that handler. If the list has
...
...
@@ -57,8 +57,10 @@ type WorkerHandler struct {
// ID. If the message is not a reply, then it appears on InitID.
handlers
map
[
Tag
]
map
[
uint64
]
HandlerFn
// idCount tracks the newest ID to assign to new handlers.
idCount
uint64
// handlerIDs is a list of the newest ID to assign to each handler when
// registered. The IDs are used to connect a reply from the worker to the
// original message sent by the main thread.
handlerIDs
map
[
Tag
]
uint64
// name describes the worker. It is used for debugging and logging purposes.
name
string
...
...
@@ -83,7 +85,7 @@ func NewWorkerHandler(aURL, name string) (*WorkerHandler, error) {
wh
:=
&
WorkerHandler
{
worker
:
js
.
Global
()
.
Get
(
"Worker"
)
.
New
(
aURL
,
opts
),
handlers
:
make
(
map
[
Tag
]
map
[
uint64
]
HandlerFn
),
idCount
:
InitID
,
handlerIDs
:
make
(
map
[
Tag
]
uint64
)
,
name
:
name
,
}
...
...
@@ -157,6 +159,32 @@ func (wh *WorkerHandler) receiveMessage(data []byte) error {
return
nil
}
// getHandler returns the handler with the given ID or returns an error if no
// handler is found. The handler is deleted from the map if specified in
// deleteAfterReceiving. This function is thread safe.
func
(
wh
*
WorkerHandler
)
getHandler
(
tag
Tag
,
id
uint64
)
(
HandlerFn
,
error
)
{
wh
.
mux
.
Lock
()
defer
wh
.
mux
.
Unlock
()
handlers
,
exists
:=
wh
.
handlers
[
tag
]
if
!
exists
{
return
nil
,
errors
.
Errorf
(
"no handlers found for tag %q"
,
tag
)
}
handler
,
exists
:=
handlers
[
id
]
if
!
exists
{
return
nil
,
errors
.
Errorf
(
"no %q handler found for ID %d"
,
tag
,
id
)
}
if
_
,
exists
=
deleteAfterReceiving
[
tag
];
exists
{
delete
(
wh
.
handlers
[
tag
],
id
)
if
len
(
wh
.
handlers
[
tag
])
==
0
{
delete
(
wh
.
handlers
,
tag
)
}
}
return
handler
,
nil
}
// RegisterHandler registers the handler for the given tag and ID unless autoID
// is true, in which case a unique ID is used. Returns the ID that was
// registered. If a previous handler was registered, it is overwritten.
...
...
@@ -166,10 +194,8 @@ func (wh *WorkerHandler) RegisterHandler(
wh
.
mux
.
Lock
()
defer
wh
.
mux
.
Unlock
()
// FIXME: This ID system only really works correctly when used with a
// single tag. This should probably be fixed.
if
autoID
{
id
=
wh
.
getNextID
()
id
=
wh
.
getNextID
(
tag
)
}
jww
.
DEBUG
.
Printf
(
"[WW] [%s] Main registering handler for tag %q and ID %d "
+
...
...
@@ -183,13 +209,22 @@ func (wh *WorkerHandler) RegisterHandler(
return
id
}
// getNextID returns the next unique ID. This function is not thread-safe.
func
(
wh
*
WorkerHandler
)
getNextID
()
uint64
{
id
:=
wh
.
idCount
wh
.
idCount
++
// getNextID returns the next unique ID for the given tag. This function is not
// thread-safe.
func
(
wh
*
WorkerHandler
)
getNextID
(
tag
Tag
)
uint64
{
if
_
,
exists
:=
wh
.
handlerIDs
[
tag
];
!
exists
{
wh
.
handlerIDs
[
tag
]
=
InitID
}
id
:=
wh
.
handlerIDs
[
tag
]
wh
.
handlerIDs
[
tag
]
++
return
id
}
////////////////////////////////////////////////////////////////////////////////
// Javascript Call Wrappers //
////////////////////////////////////////////////////////////////////////////////
// addEventListeners adds the event listeners needed to receive a message from
// the worker. Two listeners were added; one to receive messages from the worker
// and the other to receive errors.
...
...
@@ -222,32 +257,6 @@ func (wh *WorkerHandler) addEventListeners() {
wh
.
worker
.
Call
(
"addEventListener"
,
"messageerror"
,
messageError
)
}
// getHandler returns the handler with the given ID or returns an error if no
// handler is found. The handler is deleted from the map if specified in
// deleteAfterReceiving. This function is thread safe.
func
(
wh
*
WorkerHandler
)
getHandler
(
tag
Tag
,
id
uint64
)
(
HandlerFn
,
error
)
{
wh
.
mux
.
Lock
()
defer
wh
.
mux
.
Unlock
()
handlers
,
exists
:=
wh
.
handlers
[
tag
]
if
!
exists
{
return
nil
,
errors
.
Errorf
(
"no handlers found for tag %q"
,
tag
)
}
handler
,
exists
:=
handlers
[
id
]
if
!
exists
{
return
nil
,
errors
.
Errorf
(
"no %q handler found for ID %d"
,
tag
,
id
)
}
if
_
,
exists
=
deleteAfterReceiving
[
tag
];
exists
{
delete
(
wh
.
handlers
[
tag
],
id
)
if
len
(
wh
.
handlers
[
tag
])
==
0
{
delete
(
wh
.
handlers
,
tag
)
}
}
return
handler
,
nil
}
// postMessage sends a message to the worker.
//
// message is the object to deliver to the worker; this will be in the data
...
...
This diff is collapsed.
Click to expand it.
indexedDbWorker/worker_test.go
0 → 100644
+
70
−
0
View file @
e5f1ee07
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package
indexedDbWorker
import
(
"testing"
)
func
TestNewWorkerHandler
(
t
*
testing
.
T
)
{
}
func
TestWorkerHandler_SendMessage
(
t
*
testing
.
T
)
{
}
func
TestWorkerHandler_receiveMessage
(
t
*
testing
.
T
)
{
}
func
TestWorkerHandler_getHandler
(
t
*
testing
.
T
)
{
}
func
TestWorkerHandler_RegisterHandler
(
t
*
testing
.
T
)
{
}
// Tests that WorkerHandler.getNextID returns the expected ID for various Tags.
func
TestWorkerHandler_getNextID
(
t
*
testing
.
T
)
{
wh
:=
&
WorkerHandler
{
handlers
:
make
(
map
[
Tag
]
map
[
uint64
]
HandlerFn
),
handlerIDs
:
make
(
map
[
Tag
]
uint64
),
}
for
_
,
tag
:=
range
[]
Tag
{
ReadyTag
,
NewWASMEventModelTag
,
EncryptionStatusTag
,
StoreDatabaseNameTag
,
JoinChannelTag
,
LeaveChannelTag
,
ReceiveMessageTag
,
ReceiveReplyTag
,
ReceiveReactionTag
,
UpdateFromUUIDTag
,
UpdateFromMessageIDTag
,
GetMessageTag
,
DeleteMessageTag
,
ReceiveTag
,
ReceiveTextTag
,
UpdateSentStatusTag
,
}
{
id
:=
wh
.
getNextID
(
tag
)
if
id
!=
InitID
{
t
.
Errorf
(
"ID for new tag %q is not InitID."
+
"
\n
expected: %d
\n
received: %d"
,
tag
,
InitID
,
id
)
}
for
j
:=
uint64
(
1
);
j
<
100
;
j
++
{
id
=
wh
.
getNextID
(
tag
)
if
id
!=
j
{
t
.
Errorf
(
"Unexpected ID for tag %q."
+
"
\n
expected: %d
\n
received: %d"
,
tag
,
j
,
id
)
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Javascript Call Wrappers //
////////////////////////////////////////////////////////////////////////////////
func
TestWorkerHandler_addEventListeners
(
t
*
testing
.
T
)
{
}
func
TestWorkerHandler_postMessage
(
t
*
testing
.
T
)
{
}
func
Test_newWorkerOptions
(
t
*
testing
.
T
)
{
}
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