forked from vueuse/vueuse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
53 lines (46 loc) · 1.31 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { ref } from 'vue-demi'
import type { IntervalFnOptions } from '@vueuse/shared'
import { useIntervalFn } from '@vueuse/shared'
/**
* Performance.memory
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory
*/
export interface MemoryInfo {
/**
* The maximum size of the heap, in bytes, that is available to the context.
*/
readonly jsHeapSizeLimit: number
/**
* The total allocated heap size, in bytes.
*/
readonly totalJSHeapSize: number
/**
* The currently active segment of JS heap, in bytes.
*/
readonly usedJSHeapSize: number
[Symbol.toStringTag]: 'MemoryInfo'
}
export interface UseMemoryOptions extends IntervalFnOptions {
interval?: number
}
type PerformanceMemory = Performance & {
memory: MemoryInfo
}
/**
* Reactive Memory Info.
*
* @see https://vueuse.org/useMemory
* @param options
*/
export function useMemory(options: UseMemoryOptions = {}) {
const memory = ref<MemoryInfo>()
const isSupported = typeof performance !== 'undefined' && 'memory' in performance
if (isSupported) {
const { interval = 1000 } = options
useIntervalFn(() => {
memory.value = (performance as PerformanceMemory).memory
}, interval, { immediate: options.immediate, immediateCallback: options.immediateCallback })
}
return { isSupported, memory }
}