Skip to content

Commit

Permalink
Merge pull request #16 from Rue-pro/core-add-listener-func-to-storage
Browse files Browse the repository at this point in the history
Core add listener func to storage
  • Loading branch information
Rue-pro authored Apr 8, 2024
2 parents 465f978 + 4ee8446 commit 61fe9e0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
8 changes: 5 additions & 3 deletions src/entities/dictionary/model/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { atom, onMount, task } from 'nanostores'

import { TLanguageCode } from '@entities/language'
import { Storage } from '@entities/storage'
import { getStorage } from '@entities/storage'

import { browser } from '@shared/browser'
import { Result } from '@shared/libs/operationResult'
Expand All @@ -10,12 +10,14 @@ import { addToast, getErrorToast } from '@shared/ui/Toast'
import { DICTIONARIES } from './dictionaries'
import { TDictionaries } from './types'

export const DictionaryStorage = new Storage<TDictionaries>(
type StorageValue = TDictionaries

export const DictionaryStorage = getStorage<StorageValue>(
'dictionaries',
DICTIONARIES,
)

export const $dictionaries = atom<TDictionaries>(DICTIONARIES)
export const $dictionaries = atom<StorageValue>(DICTIONARIES)

onMount($dictionaries, () => {
task(async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/entities/language/model/store.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { atom, onMount, task } from 'nanostores'

import { ILanguage, TLanguageCode } from '@entities/language'
import { Storage } from '@entities/storage'
import { getStorage } from '@entities/storage'

import { browser } from '@shared/browser'
import { addToast, getErrorToast } from '@shared/ui/Toast'

import { LANGUAGES } from './languages'

type Languages = ILanguage[]
type StorageValue = ILanguage[]

export const LanguageStorage = new Storage<Languages>('languages', LANGUAGES)
export const LanguageStorage = getStorage<StorageValue>('languages', LANGUAGES)

export const $languages = atom<Languages>(LANGUAGES)
export const $languages = atom<StorageValue>(LANGUAGES)

onMount($languages, () => {
task(async () => {
Expand Down
13 changes: 6 additions & 7 deletions src/entities/note/model/store.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { atom, onMount, task } from 'nanostores'

import { TLanguageCode } from '@entities/language'
import { Storage } from '@entities/storage'
import { getStorage } from '@entities/storage'

import { browser } from '@shared/browser'
import { Result } from '@shared/libs/operationResult'
import { addToast, getErrorToast } from '@shared/ui/Toast'

import { INote } from './types'

const defaultNotes: Record<TLanguageCode, INote[]> = {
export type StorageValue = Record<TLanguageCode, INote[]>

const defaultNotes: StorageValue = {
en: [],
jp: [],
pt: [],
ko: [],
}

export const NoteStorage = new Storage<Record<TLanguageCode, INote[]>>(
`notes`,
defaultNotes,
)
export const NoteStorage = getStorage<StorageValue>(`notes`, defaultNotes)

export const $notes = atom<Record<TLanguageCode, INote[]>>(defaultNotes)
export const $notes = atom<StorageValue>(defaultNotes)

onMount($notes, () => {
task(async () => {
Expand Down
61 changes: 33 additions & 28 deletions src/entities/storage/model/Storage.ts
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)
},
},
}
}
2 changes: 1 addition & 1 deletion src/entities/storage/model/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Storage } from './Storage'
export { getStorage } from './Storage'

0 comments on commit 61fe9e0

Please sign in to comment.