5e4e50c5eb
* fix: remove bad usages of for-in and guard against it * Apply suggestions from code review Co-Authored-By: Samuel Maddock <samuel.maddock@gmail.com> * Apply suggestions from code review Co-Authored-By: Jeremy Apthorp <jeremya@chromium.org> * Update remote.js Co-authored-by: Samuel Maddock <samuel.maddock@gmail.com> Co-authored-by: Jeremy Apthorp <jeremya@chromium.org>
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
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) { // eslint-disable-line guard-for-in
|
|
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
|
|
}
|