保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
export function assignGlobalReference (newInstance, referenceKey) {
  if (!newInstance.constructor || (global[referenceKey] && !global[referenceKey].constructor)) {
    throw new Error('Assigning to global reference is only supported for class instances')
  } else if (newInstance.constructor && !global[referenceKey]) {
    global[referenceKey] = newInstance
  } else if (!(
    newInstance instanceof global[referenceKey].constructor ||
    global[referenceKey] instanceof newInstance.constructor
  )) {
    throw new Error(`Not a ${global[referenceKey].constructor.name} instance`)
  }
 
  const oldInstance = Object.create(global[referenceKey])
 
  for (const prop in global[referenceKey]) {
    oldInstance[prop] = global[referenceKey][prop]
    delete global[referenceKey][prop]
  }
 
  for (const prop of Object.getOwnPropertySymbols(global[referenceKey])) {
    oldInstance[prop] = global[referenceKey][prop]
    delete global[referenceKey][prop]
  }
 
  for (const prop in newInstance) {
    global[referenceKey][prop] = newInstance[prop]
  }
 
  for (const prop of Object.getOwnPropertySymbols(newInstance)) {
    global[referenceKey][prop] = newInstance[prop]
  }
 
  return oldInstance
}
 
export function assignGlobalConsola (newConsola) {
  return assignGlobalReference(newConsola, 'consola')
}