Skip to content

Commit

Permalink
Wait for event handlers to be ready before emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
WietseWind committed Dec 20, 2022
1 parent 2a9877d commit da8a726
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sample/jsmodule.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h2 id="sub">... (please sign in)</h2>
<button id="auth">Auth</button>
<button id="logout">Logout</button>
<script type="module">
import 'https://xumm.app/assets/cdn/xumm-oauth2-pkce.min.js?v=2.5.0'
import 'https://xumm.app/assets/cdn/xumm-oauth2-pkce.min.js?v=2.6.0'

const xumm = new XummPkce('47d328db-0b34-4451-a258-393480c9b4cd', {
implicit: true, // Implicit: allows to e.g. move from social browser to stock browser
Expand Down
52 changes: 51 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ export declare interface XummPkceThread {
// ): boolean;
}

const EventReadyPromise = (event: keyof XummPkceEvent) => {
let _resolve = (value?: unknown): void => {};
const promise = new Promise((resolve) => {
_resolve = resolve;
});
return {
promise,
resolve: (value?: unknown) => {
console.log("XummPKCE <Resolving eventReadyPromise>", event);
return _resolve(value);
},
};
};

export class XummPkceThread extends EventEmitter {
private pkce: PKCE;
private options: XummPkceOptions;
Expand All @@ -75,6 +89,12 @@ export class XummPkceThread extends EventEmitter {
private mobileRedirectFlow: boolean = false;
private urlParams?: URLSearchParams;

private eventPromises = {
retrieved: EventReadyPromise("retrieved"),
error: EventReadyPromise("error"),
success: EventReadyPromise("success"),
};

constructor(
xummApiKey: string,
optionsOrRedirectUrl?: string | Partial<XummPkceOptions>
Expand Down Expand Up @@ -311,14 +331,43 @@ export class XummPkceThread extends EventEmitter {
}
}

public emit<U extends keyof XummPkceEvent>(event: U, ...args: any[]) {
// console.log("emitting event", event, ...args);
// console.log("subscribers for event", event, this.listenerCount(event));
this.eventPromises[event].promise.then(() => {
// Emit when subscribed
return super.emit(event, ...args);
});
return true;
}

public on<U extends keyof XummPkceEvent>(
event: U,
listener: XummPkceEvent[U]
) {
// console.log("event added, on", event);
this.eventPromises[event].resolve();
return super.on(event, listener);
}

public off<U extends keyof XummPkceEvent>(
event: U,
listener: XummPkceEvent[U]
) {
// console.log("event removed, off", event);
// Reset promise
this.eventPromises[event] = EventReadyPromise(event);
return super.off(event, listener);
}

// Todo: document, e.g. custom flow, plugin
public authorizeUrl() {
return this.pkce.authorizeUrl();
}

private handleMobileGrant() {
if (this.urlParams && this.mobileRedirectFlow) {
console.log("Send message event");
// console.log("Send message event");

const messageEventData = {
data: JSON.stringify(
Expand Down Expand Up @@ -434,6 +483,7 @@ export class XummPkceThread extends EventEmitter {
}

public logout() {
console.log("PKCE Logout");
try {
this.resolved = false;
this.resolvedSuccessfully = undefined;
Expand Down

0 comments on commit da8a726

Please sign in to comment.