refactor: allow requiring modules with no side effects (#17496)
This commit is contained in:
parent
4ee201c56e
commit
7b55ee9d36
10 changed files with 85 additions and 54 deletions
38
lib/browser/utils.ts
Normal file
38
lib/browser/utils.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { EventEmitter } from 'events'
|
||||
|
||||
/**
|
||||
* Creates a lazy instance of modules that can't be required before the
|
||||
* app 'ready' event by returning a proxy object to mitigate side effects
|
||||
* on 'require'
|
||||
*
|
||||
* @param {Function} creator - Function that creates a new module instance
|
||||
* @param {Object} holder - the object holding the module prototype
|
||||
* @param {Boolean} isEventEmitter - whether or not the module is an EventEmitter
|
||||
* @returns {Object} - a proxy object for the
|
||||
*/
|
||||
|
||||
export function createLazyInstance (
|
||||
creator: Function,
|
||||
holder: Object,
|
||||
isEventEmitter: Boolean
|
||||
) {
|
||||
let lazyModule: Object
|
||||
const module: any = {}
|
||||
for (const method in (holder as any).prototype) {
|
||||
module[method] = (...args: any) => {
|
||||
// create new instance of module at runtime if none exists
|
||||
if (!lazyModule) {
|
||||
lazyModule = creator()
|
||||
if (isEventEmitter) EventEmitter.call(lazyModule as any)
|
||||
}
|
||||
|
||||
// check for properties on the prototype chain that aren't functions
|
||||
if (typeof (lazyModule as any)[method] !== 'function') {
|
||||
return (lazyModule as any)[method]
|
||||
}
|
||||
|
||||
return (lazyModule as any)[method](...args)
|
||||
}
|
||||
}
|
||||
return module
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue