0

In a class there are several static methods and the method to be called will be decided on the run time. How could I call this method dynamically?

export default class channel {

    // METHOD THAT WILL DYNAMICALLY CALL OTHER STATIC METHODS
    private static methodMap = {
        'channel-create' : 'create',
        'channel-user-count' : 'userCount',
        'channel-close' : 'close'
    };

    public static do(commandType:string,params: any) {
        if(channel.methodMap.hasOwnProperty(commandType)) {
            // GET NAME OF THE METHOD
            let method = channel.methodMap[commandType];
            // CALL METHOD ON THE FLY
            //return channel.call(method,params);
            // channel.userCount(params);
        }
    }
    /**
     * Adds channel to available channel list
     */
    private static create(channelName:string) {

    }

    /**
     * Returns count of users in the channel
     */
    private static userCount(channelName:string) {

    }

}
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

3 Answers3

5

You can dynamically invoke a method by using Classname['methodName'](param). As in your case, you can invoke create method as Channel['create']('MyChannel')

Here is the working example: Typescript Playground

class Channel {

    private static methodMap = {
        'channel-create' : 'create',
        'channel-user-count' : 'userCount',
        'channel-close' : 'close'
    };

    private static create(channelName:string) {
        alert('Called with ' + channelName);
    }

    private static userCount(channelName:string) {
        alert('Usercount called with ' + channelName);
    }

    public static do(commandType: string, params: any) {
        if(Channel.methodMap.hasOwnProperty(commandType)) {
            let method = Channel.methodMap[commandType];

            return Channel[method](params);
        }
    }
}

Channel.do('channel-create', 'MyChannel');
Channel.do('channel-user-count', 1000);

Edit: Even though the above approach works, As @Ryan mentioned in his answer, providing functions directly in map is much cleaner.

private static methodMap: MethodMap = {
    'channel-create': Channel.create,
    'channel-user-count': Channel.userCount,
    'channel-close': Channel.close,
};
Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
4

Store the functions directly in the map:

type MethodMap = { [name: string]: (any) => void };

private static methodMap: MethodMap = {
    'channel-create': Channel.create,
    'channel-user-count': Channel.userCount,
    'channel-close': Channel.close,
};

public static do(commandType: string, params: any) {
    if (channel.methodMap.hasOwnProperty(commandType)) {
        const method = channel.methodMap[commandType];
        method(params);
    }
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

To add to the answer by @HardikModha, you can also get the compiler to check the commandType against the possible values:

public static do(commandType: keyof typeof Channel.methodMap, params: any) {
    if(Channel.methodMap.hasOwnProperty(commandType)) {
        let method = Channel.methodMap[commandType];

        return Channel[method](params);
    }
}
...

Channel.do('channel-create', 'MyChannel'); // fine
Channel.do('channel-created', 'MyChannel'); // error

(code in playground)

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299