aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/window.js
blob: 5009c77fa88d8d1e1475c959947e36710f438931 (plain)
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
export const globals = {
  window: typeof window === 'undefined' ? null : window,
  document: typeof document === 'undefined' ? null : document
}

export function registerWindow(win = null, doc = null) {
  globals.window = win
  globals.document = doc
}

const save = {}

export function saveWindow() {
  save.window = globals.window
  save.document = globals.document
}

export function restoreWindow() {
  globals.window = save.window
  globals.document = save.document
}

export function withWindow(win, fn) {
  saveWindow()
  registerWindow(win, win.document)
  fn(win, win.document)
  restoreWindow()
}

export function getWindow() {
  return globals.window
}