-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Rue-pro/core-add-listener-func-to-storage
Core add listener func to storage
- Loading branch information
Showing
5 changed files
with
49 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
import { browser } from '@shared/browser' | ||
import { TOnChangeListenerProps, browser } from '@shared/browser' | ||
import { TResult } from '@shared/libs/operationResult' | ||
|
||
export class Storage<StorageValue> { | ||
key: string | ||
defaultValue: StorageValue | ||
export const getStorage = <StorageValue>( | ||
key: string, | ||
defaultValue: StorageValue, | ||
) => { | ||
return { | ||
get() { | ||
return browser.storage.local.get<StorageValue>(key, defaultValue) | ||
}, | ||
|
||
constructor(key: string, defaultValue: StorageValue) { | ||
this.key = key | ||
this.defaultValue = defaultValue | ||
} | ||
|
||
get() { | ||
return browser.storage.local.get<StorageValue>(this.key, this.defaultValue) | ||
} | ||
set(value: StorageValue) { | ||
return browser.storage.local.set(key, value) | ||
}, | ||
|
||
set(value: StorageValue) { | ||
return browser.storage.local.set<StorageValue>(this.key, value) | ||
} | ||
onChanged: { | ||
addListener( | ||
callback: ( | ||
changes: TResult<{ | ||
newValue: StorageValue | ||
oldValue: StorageValue | ||
}>, | ||
) => void, | ||
) { | ||
return browser.storage.local.onChanged.addListener<StorageValue>( | ||
key, | ||
callback, | ||
defaultValue, | ||
) | ||
}, | ||
|
||
onChanged( | ||
callback: ( | ||
changes: TResult<{ | ||
newValue: StorageValue | ||
oldValue: StorageValue | ||
}>, | ||
) => void, | ||
) { | ||
browser.storage.local.onChanged<StorageValue>( | ||
this.key, | ||
callback, | ||
this.defaultValue, | ||
) | ||
removeListener( | ||
listener: (changes: TOnChangeListenerProps<StorageValue>) => void, | ||
) { | ||
browser.storage.local.onChanged.removeListener<StorageValue>(listener) | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { Storage } from './Storage' | ||
export { getStorage } from './Storage' |