microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.1.0-preview-0006

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Events/EventEmitter.cs

85lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Apps.Plugins;
5
6namespace Microsoft.Teams.Apps.Events;
7
8internal class EventEmitter
9{
10 protected Dictionary<string, Topic> Topics { get; set; } = [];
11
12 public EventEmitter On(string name, Action<IPlugin, Event> handler)
13 {
14 if (!Topics.ContainsKey(name))
15 {
16 Topics[name] = [];
17 }
18
19 Topics[name].Add((plugin, @event, cancellationToken) =>
20 {
21 handler(plugin, @event);
22 return Task.FromResult<object?>(null);
23 });
24
25 return this;
26 }
27
28 public EventEmitter On<TResult>(string name, Func<IPlugin, Event, TResult> handler)
29 {
30 if (!Topics.ContainsKey(name))
31 {
32 Topics[name] = [];
33 }
34
35 Topics[name].Add((plugin, @event, cancellationToken) =>
36 {
37 var res = handler(plugin, @event);
38 return Task.FromResult<object?>(res);
39 });
40
41 return this;
42 }
43
44 public EventEmitter On(string name, Func<IPlugin, Event, CancellationToken, Task> handler)
45 {
46 if (!Topics.ContainsKey(name))
47 {
48 Topics[name] = [];
49 }
50
51 Topics[name].Add(async (plugin, @event, cancellationToken) =>
52 {
53 await handler(plugin, @event, cancellationToken).ConfigureAwait(false);
54 return null;
55 });
56
57 return this;
58 }
59
60 public EventEmitter On<TResult>(string name, Func<IPlugin, Event, CancellationToken, Task<TResult>> handler)
61 {
62 if (!Topics.ContainsKey(name))
63 {
64 Topics[name] = [];
65 }
66
67 Topics[name].Add(async (plugin, @event, cancellationToken) =>
68 {
69 var res = await handler(plugin, @event, cancellationToken).ConfigureAwait(false);
70 return res;
71 });
72
73 return this;
74 }
75
76 public async Task<object?> Emit(IPlugin plugin, string name, Event? @event = null, CancellationToken cancellationToken = default)
77 {
78 if (!Topics.ContainsKey(name))
79 {
80 Topics[name] = [];
81 }
82
83 return await Topics[name].Emit(plugin, @event, cancellationToken).ConfigureAwait(false);
84 }
85}