File size: 907 Bytes
6b6ca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Widget, WidgetInput, WidgetOutput } from '../types';

class WidgetExecutor {
  static widgets = new Map<string, Widget>();

  static register(widget: Widget) {
    this.widgets.set(widget.type, widget);
  }

  static getWidget(type: string): Widget | undefined {
    return this.widgets.get(type);
  }

  static async executeAll(input: WidgetInput): Promise<WidgetOutput[]> {
    const results: WidgetOutput[] = [];

    await Promise.all(
      Array.from(this.widgets.values()).map(async (widget) => {
        try {
          if (widget.shouldExecute(input.classification)) {
            const output = await widget.execute(input);
            if (output) {
              results.push(output);
            }
          }
        } catch (e) {
          console.log(`Error executing widget ${widget.type}:`, e);
        }
      }),
    );

    return results;
  }
}

export default WidgetExecutor;