Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
7ffe7fc4
Commit
7ffe7fc4
authored
May 17, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
May 17, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate LocalStorageService to TS
parent
e9ad3952
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
34 deletions
+18
-34
local-storage.ts
src/sidebar/services/local-storage.ts
+18
-34
No files found.
src/sidebar/services/local-storage.
j
s
→
src/sidebar/services/local-storage.
t
s
View file @
7ffe7fc4
type
BasicStorage
=
Omit
<
Storage
,
'length'
|
'clear'
|
'key'
>
;
/**
* Fallback in-memory store if `localStorage` is not read/writable.
*/
class
InMemoryStorage
{
class
InMemoryStorage
implements
BasicStorage
{
private
_store
:
Map
<
string
,
string
>
;
constructor
()
{
this
.
_store
=
new
Map
();
}
/** @param {string} key */
getItem
(
key
)
{
getItem
(
key
:
string
):
string
|
null
{
return
this
.
_store
.
get
(
key
)
??
null
;
}
/**
* @param {string} key
* @param {string} value
*/
setItem
(
key
,
value
)
{
setItem
(
key
:
string
,
value
:
string
)
{
this
.
_store
.
set
(
key
,
value
);
}
/** @param {string} key */
removeItem
(
key
)
{
removeItem
(
key
:
string
)
{
this
.
_store
.
delete
(
key
);
}
}
...
...
@@ -35,11 +33,10 @@ class InMemoryStorage {
*/
// @inject
export
class
LocalStorageService
{
/**
* @param {Window} $window
*/
constructor
(
$window
)
{
let
testKey
=
'hypothesis.testKey'
;
private
_storage
:
BasicStorage
;
constructor
(
$window
:
Window
)
{
const
testKey
=
'hypothesis.testKey'
;
try
{
// Test whether we can read/write localStorage.
...
...
@@ -54,51 +51,38 @@ export class LocalStorageService {
/**
* Look up a value in local storage.
*
* @param {string} key
* @return {string|null}
*/
getItem
(
key
)
{
getItem
(
key
:
string
):
string
|
null
{
return
this
.
_storage
.
getItem
(
key
);
}
/**
* Look up and deserialize a value from storage.
*
* @param {string} key
*/
getObject
(
key
)
{
getObject
<
T
=
any
>
(
key
:
string
):
T
|
null
{
const
item
=
this
.
_storage
.
getItem
(
key
);
return
item
?
JSON
.
parse
(
item
)
:
null
;
return
item
?
(
JSON
.
parse
(
item
)
as
T
)
:
null
;
}
/**
* Set a value in local storage.
*
* @param {string} key
* @param {string} value
*/
setItem
(
key
,
value
)
{
setItem
(
key
:
string
,
value
:
string
)
{
this
.
_storage
.
setItem
(
key
,
value
);
}
/**
* Serialize `value` to JSON and persist it.
*
* @param {string} key
* @param {any} value
*/
setObject
(
key
,
value
)
{
setObject
(
key
:
string
,
value
:
any
)
{
const
repr
=
JSON
.
stringify
(
value
);
this
.
_storage
.
setItem
(
key
,
repr
);
}
/**
* Remove an item from storage.
*
* @param {string} key
*/
removeItem
(
key
)
{
removeItem
(
key
:
string
)
{
this
.
_storage
.
removeItem
(
key
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment