-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1af8316
commit b7bdce4
Showing
8 changed files
with
107 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
import { IBlazor } from '../GlobalExports'; | ||
|
||
// The base Blazor event type. | ||
// Properties listed here get assigned by the event registry in 'dispatchEvent'. | ||
interface BlazorEvent { | ||
blazor: IBlazor; | ||
type: keyof BlazorEventMap; | ||
} | ||
|
||
// Maps Blazor event names to the argument type passed to registered listeners. | ||
export interface BlazorEventMap { | ||
'enhancedload': BlazorEvent; | ||
} | ||
|
||
export class EventRegistry { | ||
private readonly _eventListeners = new Map<string, Set<(ev: any) => void>>(); | ||
|
||
private _blazor: IBlazor | null = null; | ||
|
||
public attachBlazorInstance(blazor: IBlazor) { | ||
this._blazor = blazor; | ||
} | ||
|
||
public addEventListener<K extends keyof BlazorEventMap>(type: K, listener: (ev: BlazorEventMap[K]) => void): void { | ||
let listenersForEventType = this._eventListeners.get(type); | ||
if (!listenersForEventType) { | ||
listenersForEventType = new Set(); | ||
this._eventListeners.set(type, listenersForEventType); | ||
} | ||
|
||
listenersForEventType.add(listener); | ||
} | ||
|
||
public removeEventListener<K extends keyof BlazorEventMap>(type: K, listener: (ev: BlazorEventMap[K]) => void): void { | ||
const listenersForEventType = this._eventListeners.get(type); | ||
if (!listenersForEventType) { | ||
return; | ||
} | ||
|
||
listenersForEventType.delete(listener); | ||
} | ||
|
||
public dispatchEvent<K extends keyof BlazorEventMap>(type: K, ev: Omit<BlazorEventMap[K], keyof BlazorEvent>): void { | ||
if (this._blazor === null) { | ||
throw new Error('Blazor events cannot be dispatched until a Blazor instance gets attached'); | ||
} | ||
|
||
const listenersForEventType = this._eventListeners.get(type); | ||
if (!listenersForEventType) { | ||
return; | ||
} | ||
|
||
const event: BlazorEventMap[K] = { | ||
...ev, | ||
blazor: this._blazor, | ||
type, | ||
}; | ||
|
||
for (const listener of listenersForEventType) { | ||
listener(event); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters