Skip to content

Commit

Permalink
Merge pull request #530 from lifeomic/emit-last-invite-state-on-subsc…
Browse files Browse the repository at this point in the history
…ribe

Emit Latest Invite State Value on Subscribe
  • Loading branch information
sternetj committed Feb 14, 2024
2 parents 7ca29b5 + 567ee17 commit 9c6c3a3
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/components/Invitations/InviteNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import { PendingInvite } from './InviteProvider';

export type InviteParams = PendingInvite;

type InviteState = {
loading?: boolean;
failedToDecode?: boolean;
failureMessage?: string;
};

export type EventTypeHandlers = {
inviteDetected: (inviteParams: InviteParams) => void;
inviteLoadingStateChanged: (state: {
loading?: boolean;
failedToDecode?: boolean;
failureMessage?: string;
}) => void;
inviteLoadingStateChanged: (state: InviteState) => void;
};

export type EventTypes = keyof EventTypeHandlers;
export type EventTypeHandler<T extends EventTypes> = EventTypeHandlers[T];
export class InviteNotifier {
private emitter = new EventEmitter();
private lastInviteDetectedParams: InviteParams | undefined;
private lastInviteState: InviteState | undefined;

public addListener<T extends EventTypes>(
eventType: T,
Expand All @@ -26,6 +29,13 @@ export class InviteNotifier {
(listener as EventTypeHandler<'inviteDetected'>)(
this.lastInviteDetectedParams,
);
} else if (
eventType === 'inviteLoadingStateChanged' &&
this.lastInviteState
) {
(listener as EventTypeHandler<'inviteLoadingStateChanged'>)(
this.lastInviteState,
);
}
return this.emitter.addListener(eventType, listener);
}
Expand Down

0 comments on commit 9c6c3a3

Please sign in to comment.