Skip to content

Commit

Permalink
feat!: simplify mount usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 13, 2021
1 parent 6185464 commit 3eccf84
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Maps data to real filesystem using directory structure for nested keys. Supports
```js
import fsDriver from 'unstorage/drivers/memory'

await storage.mount('/tmp', fsDriver({ base: './tmp' }))
storage.mount('/tmp', fsDriver({ base: './tmp' }))
```

**Options:**
Expand All @@ -261,7 +261,7 @@ Store data in [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Wi
```js
import lsDriver from 'unstorage/drivers/memory'

await storage.mount('local', lsDriver({ base: 'myapp' }))
storage.mount('local', lsDriver({ base: 'myapp' }))
```

**Options:**
Expand Down Expand Up @@ -289,7 +289,7 @@ Use a remote HTTP/HTTPS endpoint as data storage. Supports built-in [http server
```js
import httpDriver from 'unstorage/drivers/http'

await storage.mount('local', lsDriver({ base: 'http://myapp.com' }))
storage.mount('local', lsDriver({ base: 'http://myapp.com' }))
```

**Options:**
Expand Down Expand Up @@ -337,7 +337,7 @@ const myStorageDriver = defineDriver((_opts) => {
}
})

await storage.mount('/custom', myStorageDriver())
storage.mount('/custom', myStorageDriver())
```

## Contribution
Expand Down
6 changes: 2 additions & 4 deletions demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { createStorage } from '../src'
import httpDriver from '../src/drivers/http'
import App from './App.vue'

async function main () {
function main () {
const storage = createStorage()
await storage.mount('/', httpDriver({
base: location.origin + '/storage'
}))
.mount('/', httpDriver({ base: location.origin + '/storage' }))
const app = createApp(App)
app.provide('storage', storage)
app.mount('#app')
Expand Down
4 changes: 2 additions & 2 deletions demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export default defineConfig({
vue(),
{
name: 'app',
async configureServer (server) {
configureServer (server) {
const storage = createStorage()
const storageServer = createStorageServer(storage)
await storage.mount('/src', fsdriver({ base: resolve(__dirname, '..') }))
storage.mount('/src', fsdriver({ base: resolve(__dirname, '..') }))
server.middlewares.use('/storage', storageServer.handle)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function main () {
const storageServer = createStorageServer(storage)

const rootDir = resolve(args._[0] || '.')
await storage.mount('/', fsDriver({ base: rootDir }))
storage.mount('/', fsDriver({ base: rootDir }))

await listen(storageServer.handle, {
name: 'Storage server',
Expand Down
14 changes: 7 additions & 7 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ export function createStorage (): Storage {
async dispose () {
await Promise.all(Object.values(ctx.mounts).map(driver => dispose(driver)))
},
async mount (base, driver, initialState) {
mount (base, driver) {
base = normalizeBase(base)
if (!ctx.mountpoints.includes(base)) {
if (base && ctx.mounts[base]) {
throw new Error(`already mounted at ${base}`)
}
if (base) {
ctx.mountpoints.push(base)
ctx.mountpoints.sort((a, b) => b.length - a.length)
}
await storage.unmount(base)
ctx.mounts[base] = driver
if (ctx.watching) {
await watch(driver, onChange, base)
}
if (initialState) {
await storage.setItems(base, initialState)
Promise.resolve(watch(driver, onChange, base)).catch(console.error)
}
return storage
},
async unmount (base: string, _dispose = true) {
base = normalizeBase(base)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface Storage {
removeItem: (key: string) => Promise<void>
getKeys: (base?: string) => Promise<string[]>
clear: (base?: string) => Promise<void>
mount: (base: string, driver: Driver, initialState?: Record<string, StorageValue>) => Promise<void>
mount: (base: string, driver: Driver) => Storage
unmount: (base: string, dispose?: boolean) => Promise<void>
dispose: () => Promise<void>
watch: (callback: WatchCallback) => Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion test/drivers/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('drivers: http', () => {
const server = createStorageServer(storage)

const { url, close } = await listen(server.handle)
await storage.mount('/http', driver({ base: url }))
storage.mount('/http', driver({ base: url }))

expect(await storage.hasItem('/http/foo')).toBe(false)

Expand Down
3 changes: 2 additions & 1 deletion test/drivers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function testDriver (opts: TestOptions) {
}

it('init', async () => {
await ctx.storage.mount('/', ctx.driver, { initial: 'works' })
await ctx.storage.mount('/', ctx.driver)
await ctx.storage.setItems('', { initial: 'works' })
expect(await ctx.storage.getItem('initial')).toBe('works')
await ctx.storage.clear()
})
Expand Down
7 changes: 3 additions & 4 deletions test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const data = {

describe('storage', () => {
it('mount/unmount', async () => {
const storage = createStorage()
await storage.mount('/mnt', memory(), data)
const storage = createStorage().mount('/mnt', memory())
await storage.setItems('/mnt', data)
expect(await snapshot(storage, '/mnt')).toMatchObject(data)
})

Expand All @@ -21,8 +21,7 @@ describe('storage', () => {

it('watch', async () => {
const onChange = jest.fn()
const storage = createStorage()
await storage.mount('/mnt', memory(), data)
const storage = createStorage().mount('/mnt', memory())
await storage.watch(onChange)
await storage.setItems('/mnt', data)
expect(onChange).toHaveBeenCalledWith('update', 'mnt:data:foo')
Expand Down

0 comments on commit 3eccf84

Please sign in to comment.