forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (28 loc) · 734 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { readonly, ref, unref, watch } from 'vue-demi'
import { tryOnScopeDispose } from '@vueuse/shared'
import type { MaybeRef } from '@vueuse/shared'
/**
* Reactive URL representing an object.
*
* @see https://vueuse.org/useObjectUrl
* @param object
*/
export function useObjectUrl(object: MaybeRef<Blob | MediaSource | undefined>) {
const url = ref<string | undefined>()
const release = () => {
if (url.value)
URL.revokeObjectURL(url.value)
url.value = undefined
}
watch(
() => unref(object),
(newObject) => {
release()
if (newObject)
url.value = URL.createObjectURL(newObject)
},
{ immediate: true },
)
tryOnScopeDispose(release)
return readonly(url)
}