Skip to content

Commit

Permalink
refactor(utils): rename global to globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 28, 2023
1 parent ef6e5e5 commit 8c3ef77
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/utils/global.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
export function assignGlobalReference(newInstance, referenceKey) {
if (
!newInstance.constructor ||
(global[referenceKey] && !global[referenceKey].constructor)
(globalThis[referenceKey] && !globalThis[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.constructor && !globalThis[referenceKey]) {
globalThis[referenceKey] = newInstance;
} else if (
!(
newInstance instanceof global[referenceKey].constructor ||
global[referenceKey] instanceof newInstance.constructor
newInstance instanceof globalThis[referenceKey].constructor ||
globalThis[referenceKey] instanceof newInstance.constructor
)
) {
throw new TypeError(
`Not a ${global[referenceKey].constructor.name} instance`
`Not a ${globalThis[referenceKey].constructor.name} instance`
);
}

const oldInstance = Object.create(global[referenceKey]);
const oldInstance = Object.create(globalThis[referenceKey]);

for (const prop in global[referenceKey]) {
oldInstance[prop] = global[referenceKey][prop];
delete global[referenceKey][prop];
for (const prop in globalThis[referenceKey]) {
oldInstance[prop] = globalThis[referenceKey][prop];
delete globalThis[referenceKey][prop];
}

for (const prop of Object.getOwnPropertySymbols(global[referenceKey])) {
oldInstance[prop] = global[referenceKey][prop];
delete global[referenceKey][prop];
for (const prop of Object.getOwnPropertySymbols(globalThis[referenceKey])) {
oldInstance[prop] = globalThis[referenceKey][prop];
delete globalThis[referenceKey][prop];
}

for (const prop in newInstance) {
global[referenceKey][prop] = newInstance[prop];
globalThis[referenceKey][prop] = newInstance[prop];
}

for (const prop of Object.getOwnPropertySymbols(newInstance)) {
global[referenceKey][prop] = newInstance[prop];
globalThis[referenceKey][prop] = newInstance[prop];
}

return oldInstance;
Expand Down

0 comments on commit 8c3ef77

Please sign in to comment.