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
6938b69d
Commit
6938b69d
authored
2 years ago
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Fix LocalStorage.ClearPrefix bug
parent
6afbf09a
No related branches found
No related tags found
1 merge request
!18
XX-4272 / Purge
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
utils/localStorage.go
+24
-19
24 additions, 19 deletions
utils/localStorage.go
utils/localStorage_test.go
+19
-19
19 additions, 19 deletions
utils/localStorage_test.go
with
43 additions
and
38 deletions
utils/localStorage.go
+
24
−
19
View file @
6938b69d
...
...
@@ -98,29 +98,34 @@ func (ls *LocalStorage) Clear() {
ls
.
clear
()
}
// ClearWASM clears all the keys in storage created by WASM.
// TODO: add test.
func
(
ls
*
LocalStorage
)
ClearWASM
()
{
for
i
:=
0
;
i
<
ls
.
Length
();
i
++
{
v
:=
ls
.
key
(
i
)
if
!
v
.
IsNull
()
&&
strings
.
HasPrefix
(
v
.
String
(),
localStorageWasmPrefix
)
{
ls
.
RemoveItem
(
strings
.
TrimPrefix
(
v
.
String
(),
localStorageWasmPrefix
))
// Decrement to account for reduced length from removed key
i
--
// ClearPrefix clears all keys with the given prefix.
func
(
ls
*
LocalStorage
)
ClearPrefix
(
prefix
string
)
{
// Get a copy of all key names at once
keys
:=
js
.
Global
()
.
Get
(
"Object"
)
.
Call
(
"keys"
,
ls
.
v
)
// Loop through each key
for
i
:=
0
;
i
<
keys
.
Length
();
i
++
{
if
v
:=
keys
.
Index
(
i
);
!
v
.
IsNull
()
{
keyName
:=
strings
.
TrimPrefix
(
v
.
String
(),
localStorageWasmPrefix
)
if
strings
.
HasPrefix
(
keyName
,
prefix
)
{
ls
.
RemoveItem
(
keyName
)
}
}
}
}
// ClearPrefix clears all keys with the given prefix.
func
(
ls
*
LocalStorage
)
ClearPrefix
(
prefix
string
)
{
for
i
:=
0
;
i
<
ls
.
Length
();
i
++
{
keyName
,
err
:=
ls
.
Key
(
i
)
if
err
==
nil
&&
strings
.
HasPrefix
(
keyName
,
prefix
)
{
ls
.
RemoveItem
(
keyName
)
// ClearWASM clears all the keys in storage created by WASM.
func
(
ls
*
LocalStorage
)
ClearWASM
()
{
// Get a copy of all key names at once
keys
:=
js
.
Global
()
.
Get
(
"Object"
)
.
Call
(
"keys"
,
ls
.
v
)
// Decrement to account for reduced length from removed key
i
--
// Loop through each key
for
i
:=
0
;
i
<
keys
.
Length
();
i
++
{
if
v
:=
keys
.
Index
(
i
);
!
v
.
IsNull
()
{
keyName
:=
v
.
String
()
if
strings
.
HasPrefix
(
keyName
,
localStorageWasmPrefix
)
{
ls
.
RemoveItem
(
strings
.
TrimPrefix
(
keyName
,
localStorageWasmPrefix
))
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
utils/localStorage_test.go
+
19
−
19
View file @
6938b69d
...
...
@@ -85,28 +85,31 @@ func TestLocalStorage_Clear(t *testing.T) {
}
}
// Tests that LocalStorage.Clear
WASM
deletes
all the WASM keys from storage and
//
does not remove any others
func
TestLocalStorage_Clear
WASM
(
t
*
testing
.
T
)
{
// Tests that LocalStorage.Clear
Prefix
deletes
only the keys with the given
//
prefix.
func
TestLocalStorage_Clear
Prefix
(
t
*
testing
.
T
)
{
jsStorage
.
clear
()
prng
:=
rand
.
New
(
rand
.
NewSource
(
11
))
var
yesPrefix
,
noPrefix
[]
string
prefix
:=
"keyNamePrefix/"
for
i
:=
0
;
i
<
10
;
i
++
{
keyName
:=
"keyNum"
+
strconv
.
Itoa
(
i
)
if
prng
.
Intn
(
2
)
==
0
{
keyName
=
prefix
+
keyName
yesPrefix
=
append
(
yesPrefix
,
keyName
)
jsStorage
.
SetItem
(
keyName
,
[]
byte
(
strconv
.
Itoa
(
i
)))
}
else
{
noPrefix
=
append
(
noPrefix
,
keyName
)
jsStorage
.
setItem
(
keyName
,
strconv
.
Itoa
(
i
))
}
jsStorage
.
SetItem
(
keyName
,
[]
byte
(
strconv
.
Itoa
(
i
)))
}
jsStorage
.
Clear
WASM
(
)
jsStorage
.
Clear
Prefix
(
prefix
)
for
_
,
keyName
:=
range
noPrefix
{
if
v
:=
jsStorage
.
g
etItem
(
keyName
);
v
.
IsNull
()
{
t
.
Errorf
(
"Could not get keyName %q
.
"
,
keyName
)
if
_
,
err
:=
jsStorage
.
G
etItem
(
keyName
);
err
!=
nil
{
t
.
Errorf
(
"Could not get keyName %q
: %+v
"
,
keyName
,
err
)
}
}
for
_
,
keyName
:=
range
yesPrefix
{
...
...
@@ -117,31 +120,28 @@ func TestLocalStorage_ClearWASM(t *testing.T) {
}
}
// Tests that LocalStorage.Clear
Prefix
deletes
only the keys with the given
//
prefix.
func
TestLocalStorage_Clear
Prefix
(
t
*
testing
.
T
)
{
// Tests that LocalStorage.Clear
WASM
deletes
all the WASM keys from storage and
//
does not remove any others
func
TestLocalStorage_Clear
WASM
(
t
*
testing
.
T
)
{
jsStorage
.
clear
()
prng
:=
rand
.
New
(
rand
.
NewSource
(
11
))
var
yesPrefix
,
noPrefix
[]
string
prefix
:=
"keyNamePrefix/"
for
i
:=
0
;
i
<
10
;
i
++
{
keyName
:=
"keyNum"
+
strconv
.
Itoa
(
i
)
if
prng
.
Intn
(
2
)
==
0
{
keyName
=
prefix
+
keyName
yesPrefix
=
append
(
yesPrefix
,
keyName
)
jsStorage
.
SetItem
(
keyName
,
[]
byte
(
strconv
.
Itoa
(
i
)))
}
else
{
noPrefix
=
append
(
noPrefix
,
keyName
)
jsStorage
.
setItem
(
keyName
,
strconv
.
Itoa
(
i
))
}
jsStorage
.
SetItem
(
keyName
,
[]
byte
(
strconv
.
Itoa
(
i
)))
}
jsStorage
.
Clear
Prefix
(
prefix
)
jsStorage
.
Clear
WASM
(
)
for
_
,
keyName
:=
range
noPrefix
{
if
_
,
err
:=
jsStorage
.
G
etItem
(
keyName
);
err
!=
nil
{
t
.
Errorf
(
"Could not get keyName %q
: %+v
"
,
keyName
,
err
)
if
v
:=
jsStorage
.
g
etItem
(
keyName
);
v
.
IsNull
()
{
t
.
Errorf
(
"Could not get keyName %q
.
"
,
keyName
)
}
}
for
_
,
keyName
:=
range
yesPrefix
{
...
...
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