Skip to content

Commit

Permalink
feat: changed data to be more flexible type
Browse files Browse the repository at this point in the history
  • Loading branch information
moberhauer committed Jul 20, 2020
1 parent 0f8b857 commit 50414f9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/lifecycleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import { AnyJson, Dictionary } from '@salesforce/ts-types';
import * as Debug from 'debug';

type callback = (data: AnyJson) => Promise<void>;
// Data of any type can be passed to the callback. Can be cast to any type that is given in emit().
// tslint:disable-next-line:no-any
type callback = (data: any) => Promise<void>;

/**
* An asynchronous event listener and emitter that follows the singleton pattern. The singleton pattern allows lifecycle
Expand Down Expand Up @@ -74,7 +76,7 @@ export class Lifecycle {
* @param eventName The name of the event that is being listened for
* @param cb The callback function to run when the event is emitted
*/
public on<T extends AnyJson>(eventName: string, cb: (data: T | AnyJson) => Promise<void>) {
public on<T = AnyJson>(eventName: string, cb: (data: T) => Promise<void>) {
const listeners = this.getListeners(eventName);
if (listeners.length !== 0) {
this.debug(
Expand All @@ -92,7 +94,7 @@ export class Lifecycle {
* @param eventName The name of the event to emit
* @param data The argument to be passed to the callback function
*/
public async emit(eventName: string, data: AnyJson) {
public async emit<T = AnyJson>(eventName: string, data: T) {
const listeners = this.getListeners(eventName);
if (listeners.length === 0) {
this.debug(
Expand Down

0 comments on commit 50414f9

Please sign in to comment.