-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): compatible with unstorage
- Loading branch information
Showing
17 changed files
with
704 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@webx-kit/storage": patch | ||
--- | ||
|
||
feat(storage): compatible with unstorage |
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
2 changes: 1 addition & 1 deletion
2
...ages/storage/demo/pages/options/index.tsx → ...ages/storage/demo/pages/jotai-2/index.tsx
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
import { useUnstorage } from '../../shared/unstorage'; | ||
|
||
const App = () => { | ||
const [apiKey] = useUnstorage('apiKey', 'DEFAULT'); | ||
return ( | ||
<div> | ||
<div data-testid="apiKey">{apiKey}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
createRoot(document.getElementById('root')!).render(<App />); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
import { useUnstorage } from '../../shared/unstorage'; | ||
|
||
const App = () => { | ||
const [apiKey, setAPIKey] = useUnstorage('apiKey', 'DEFAULT'); | ||
return ( | ||
<div> | ||
<div data-testid="apiKey">{apiKey}</div> | ||
<button | ||
data-testid="change" | ||
onClick={() => { | ||
setAPIKey('Changed'); | ||
}} | ||
> | ||
Change | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
createRoot(document.getElementById('root')!).render(<App />); |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { createStorage } from 'unstorage'; | ||
import { createDriver } from '@/unstorage'; | ||
|
||
export const storage = createStorage({ | ||
driver: createDriver(), | ||
}); | ||
|
||
export function useUnstorage(key: string, defaultValue?: any) { | ||
const [value, setValue] = useState(defaultValue); | ||
|
||
useEffect(() => { | ||
const refresh = () => storage.getItem(key).then((v) => setValue(v ?? defaultValue)); | ||
refresh(); | ||
const unwatch = storage.watch((_type, changedKey) => changedKey === key && refresh()); | ||
return () => void unwatch.then((unwatch) => unwatch()); | ||
}, [key]); | ||
|
||
const handleSetValue = useCallback( | ||
(value: any) => { | ||
storage.setItem(key, value).then(() => { | ||
setValue(value); | ||
}); | ||
}, | ||
[key] | ||
); | ||
|
||
return [value, handleSetValue] as const; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect, test } from './context'; | ||
|
||
test('Single page', async ({ getURL, page }) => { | ||
await page.goto(await getURL('unstorage.html')); | ||
|
||
await expect(page.getByTestId('apiKey')).toHaveText('DEFAULT'); | ||
await page.getByTestId('change').click(); | ||
await expect(page.getByTestId('apiKey')).toHaveText('Changed'); | ||
|
||
await page.reload(); | ||
await expect(page.getByTestId('apiKey')).toHaveText('Changed'); | ||
}); | ||
|
||
test('Cross pages', async ({ getURL, context }) => { | ||
const jotaiPage = await context.newPage(); | ||
const jotai2Page = await context.newPage(); | ||
|
||
await jotaiPage.goto(await getURL('unstorage.html')); | ||
await jotai2Page.goto(await getURL('unstorage-2.html')); | ||
|
||
await expect(jotaiPage.getByTestId('apiKey')).toHaveText('DEFAULT'); | ||
await expect(jotai2Page.getByTestId('apiKey')).toHaveText('DEFAULT'); | ||
|
||
await jotaiPage.getByTestId('change').click(); | ||
await expect(jotaiPage.getByTestId('apiKey')).toHaveText('Changed'); | ||
await expect(jotai2Page.getByTestId('apiKey')).toHaveText('Changed'); | ||
|
||
await Promise.all([jotaiPage.reload(), jotai2Page.reload()]); | ||
await expect(jotaiPage.getByTestId('apiKey')).toHaveText('Changed'); | ||
await expect(jotai2Page.getByTestId('apiKey')).toHaveText('Changed'); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ChromeStorage, createStorage } from './index'; | ||
|
||
export function createDriver(): ChromeStorage { | ||
const storage = createStorage(); | ||
return { | ||
...storage, | ||
getItem: (key: string) => storage.getItem(key), | ||
}; | ||
} |
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
Oops, something went wrong.