如何使用更新的类属性值?

时间:2019-05-10 08:37:28

标签: javascript typescript

我有一些代码代表宏系统,我有一个包含宏的集合,我可以删除宏,此后我想更新该集合,以便它使用新的宏,但是我似乎无法获取更新的集合

export default class CommandHandler {
    private static instance: CommandHandler;
    public macros = new Collection<string, Macro>();

    public static start(): CommandHandler {
        if (!CommandHandler.instance) {
            CommandHandler.instance = new CommandHandler();
            debug.info('Successfully started command handler instance');
        }

        return CommandHandler.instance;
    }

    public reloadMacros = async () => {
        const repository = new MacroRepository(db, 'macros');
        const macros = await repository.findAll();
        for (const macro of macros) {
            const macroResult: Macro = await repository.findOne({ name: macro.name });
            this.macros.set(macroResult.name, macro);
            // Macros are set the way I want
        }
    }

    public async handle(message: Message): Promise<BotCommand | Macro> {
        const args: string[] = message.content.slice(config.prefix.length).split(/ +/);
        const commandName: string = args.shift().toLowerCase();
        const command: BotCommand = validateCommand(commandName, this.commands);
        const macro: Macro = validateMacro(commandName, this.macros);
        console.log(this.macros);
        // Returns empty collection

我更新收藏集的方式:

CommandHandler.start().macros = new Collection();
await new CommandHandler().reloadMacros();

如何在handle函数中使用更新的集合?

1 个答案:

答案 0 :(得分:0)

我不得不这样做:

CommandHandler.start().macros = new Collection();
await CommandHandler.start().reloadMacros();```