Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InversifyJS: Injecting the class which extends non-injectable external module

Tags:

inversifyjs

Need help on implementation related to Inversify. I am creating a class which is extending EventEmitter from node. when I try to use inversify it says EventEmitter is not injectable. Following is the sample code

//Interface

export interface ISubscriber {
Connect(callback: Function);
on(event: string, listener: Function): this;
emit(event: string, ...args: any[]): boolean;
}

//Class

import {EventEmitter} from 'events';


@injectable()
class Subscriber extends EventEmitter implements ISubscriber {
logProvider: SCLogging.ILogger;
public constructor(
    @inject(TYPES.ILogger) logProvider: SCLogging.ILogger,
    @inject(TYPES.IConfig) config: IConfig
) {
    super();
    //Some Implementation
}

public Connect(callback) {
//Some Implementation
}

public on(event: string, listener: Function): this {
    super.on(event, listener);
    return this;
}

public emit(event: string, ...args: any[]): boolean {
    return super.emit(event, ...args);
}
}
export { ISubscriber, Subscriber }

//Define Binding

kernel.bind<SCLogging.ILogger>(TYPES.ILogger).to(Logger);
kernel.bind<IConfig>(TYPES.IConfig).to(Config);
kernel.bind<ISubscriber>(TYPES.ISubscriber).to(Subscriber);

I get error

Error: Missing required @injectable annotation in: EventEmitter.
like image 355
Hem Upreti Avatar asked Jul 29 '16 23:07

Hem Upreti


2 Answers

a very similar question has been already answered on the InversifyJS issues on Github:

You can invoke the decorator using the decorate function:

import { decorate, injectable } from "inversify";
decorate(injectable(), ClassName)

Check out https://github.com/inversify/InversifyJS/blob/master/wiki/basic_js_example.md for more info.

Please refer to the issue on Github for more information.

like image 169
Remo H. Jansen Avatar answered Sep 21 '22 10:09

Remo H. Jansen


Setting skipBaseClassChecks: true in the container options disables this "feature" of inversify.

See this PR for more details https://github.com/inversify/InversifyJS/pull/841

like image 45
Charlie Bamford Avatar answered Sep 21 '22 10:09

Charlie Bamford