@digitalpersona/devices
Version:
DigitalPersona Security Devices support library
59 lines (41 loc) • 1.75 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[](./index.md) > [@digitalpersona/devices](./devices.md) > [CardsReader](./devices.cardsreader.md) > [on](./devices.cardsreader.on.md)
Adds an event handler for the event. This is a multicast subscription, i.e. many handlers can be registered at once.
<b>Signature:</b>
```typescript
on<E extends Event>(event: string, handler: Handler<E>): Handler<E>;
```
| Parameter | Type | Description |
| --- | --- | --- |
| event | <code>string</code> | a name of the event to subscribe, e.g. "CardInserted" |
| handler | <code>Handler<E></code> | an event handler. |
<b>Returns:</b>
`Handler<E>`
an event handler reference. Store the reference and pass it to the [CardsReader.off()](./devices.cardsreader.off.md) to unsubscribe from the event.
```
class CardComponent
{
private reader: CardsReader;
private onCardInserted = (event: CardInserted) => { ... }
private onCardRemoved = (event: CardRemoved) => { ... }
...
public async $onInit() {
this.reader = new CardsReader();
this.reader.on("CardInserted", this.onCardInserted);
this.reader.on("CardRemoved", this.onCardRemoved);
...
await this.cardReader.subscribe()
}
public async $onDestroy() {
await this.cardReader.unsubscribe();
this.reader.off("CardInserted", this.onCardInserted);
this.reader.off("CardRemoved", this.onCardRemoved);
...
// alternatively, call this.reader.off() to unsubscribe from all events at once.
delete this.reader;
}
}
```