Hem Upreti
Hem Upreti

Reputation: 51

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

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.

Upvotes: 5

Views: 4648

Answers (2)

Charlie Bamford
Charlie Bamford

Reputation: 1309

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

Upvotes: 4

Remo H. Jansen
Remo H. Jansen

Reputation: 24979

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.

Upvotes: 7

Related Questions