-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (45 loc) · 1.27 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
import { intervalFn, toReadable, toWritable, unstore } from "@sveu/shared"
import { support } from "../support"
import { MemoryInfo, MemoryOptions, PerformanceMemory } from "../utils"
/**
* Reactive Memory Info.
*
* @param options - Options to use:
* - `interval` - The interval in seconds to check the memory.
* - `IntervalFnOptions` - Options from `intervalfn`.
*
* @example
* ```ts
* const { supported, result } = memory()
* ```
*
* @returns
* - `supported` - If the browser supports the `memory` API.
* - `result` - A readable store with the memory info.
*
*/
export function memory(options: MemoryOptions = {}) {
const _memory = toWritable<MemoryInfo | undefined>(undefined)
const supported = support("memory", "performance")
if (unstore(supported)) {
const { interval = 1 } = options
intervalFn(
() => {
_memory.set({
jsHeapSizeLimit: (performance as PerformanceMemory).memory
.jsHeapSizeLimit,
totalJSHeapSize: (performance as PerformanceMemory).memory
.totalJSHeapSize,
usedJSHeapSize: (performance as PerformanceMemory).memory
.usedJSHeapSize,
})
},
interval,
{
immediate: options.immediate,
immediateCallback: options.immediateCallback,
}
)
}
return { supported, result: toReadable(_memory) }
}