forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: experiment with a light version of event contract
- Loading branch information
1 parent
5b4970b
commit dc5bbca
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/core/primitives/event-dispatch/src/event_contract_light.ts
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,48 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {createEventInfoFromParameters, EventInfo} from './event_info'; | ||
|
||
export class EventContractLight { | ||
public events: EventInfo[] = []; | ||
// ['click', clickHandler, 'input', inputHandler, ...] | ||
public listeners: Array<string | EventListener> = []; | ||
|
||
constructor( | ||
public namespace: string, | ||
public container: Element, | ||
) {} | ||
|
||
addEvent(eventType: string) { | ||
const container = this.container; | ||
const listener = (event: Event) => { | ||
const target = event.target as Element; | ||
const eventInfo = createEventInfoFromParameters( | ||
/* eventType= */ eventType, | ||
/* event= */ event, | ||
/* targetElement= */ target, | ||
/* container= */ container, | ||
/* timestamp= */ Date.now(), | ||
); | ||
this.events.push(eventInfo); | ||
}; | ||
this.listeners.push(eventType, listener); | ||
container.addEventListener(eventType, listener); | ||
} | ||
} | ||
|
||
export function disposeEventContract(ec: EventContractLight) { | ||
const listeners = ec.listeners; | ||
for (let i = 0; i < listeners.length; i += 2) { | ||
const eventType = listeners[i] as string; | ||
const listener = listeners[i + 1] as EventListener; | ||
ec.container.removeEventListener(eventType, listener); | ||
} | ||
ec.listeners = []; | ||
ec.events = []; | ||
} |
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 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