-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding toasts support with event emitter
- Loading branch information
Showing
5 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { EventEmitter } from "@/lib/EventEmitter"; | ||
import type { ToastProps } from ".."; | ||
|
||
export const toastsEventEmitter = new EventEmitter<ToastProps>(); | ||
|
||
export const createToast = (toast: ToastProps): void => { | ||
toastsEventEmitter.emit(toast); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export interface Listener<T> { | ||
(event: T): void; | ||
} | ||
|
||
export interface Disposable { | ||
dispose: () => void; | ||
} | ||
|
||
/** passes through events as they happen. You will not get events from before you start listening */ | ||
export class EventEmitter<T> { | ||
private listeners: Listener<T>[] = []; | ||
|
||
on = (listener: Listener<T>): Disposable => { | ||
this.listeners.push(listener); | ||
return { | ||
dispose: () => this.off(listener) | ||
}; | ||
} | ||
|
||
off = (listener: Listener<T>) => { | ||
const callbackIndex = this.listeners.indexOf(listener); | ||
if (callbackIndex > -1) { | ||
this.listeners.splice(callbackIndex, 1); | ||
} | ||
} | ||
|
||
emit = (event: T) => { | ||
/** Update any general listeners */ | ||
this.listeners.forEach((listener: Listener<T>) => listener(event)); | ||
} | ||
} |