Skip to content

Commit

Permalink
Refactor _onMessageFromForwarder to use a switch statement (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
BboyAkers authored Jul 13, 2020
1 parent c476b69 commit a4042ac
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,33 @@ export default class Onboarding {
}

async _onMessageFromForwarder (event: MessageEvent) {
if (this.state === ONBOARDING_STATE.RELOADING) {
console.debug('Ignoring message while reloading');
} else if (this.state === ONBOARDING_STATE.NOT_INSTALLED) {
console.debug('Reloading now to register with MetaMask');
this.state = ONBOARDING_STATE.RELOADING;
return location.reload();
} else if (this.state === ONBOARDING_STATE.INSTALLED) {
console.debug('Registering with MetaMask');
this.state = ONBOARDING_STATE.REGISTERING;
await Onboarding._register();
this.state = ONBOARDING_STATE.REGISTERED;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
event.source?.postMessage({ type: 'metamask:registrationCompleted' }, event.origin);
this.stopOnboarding();
} else if (this.state === ONBOARDING_STATE.REGISTERING) {
console.debug('Already registering - ignoring reload message');
} else if (this.state === ONBOARDING_STATE.REGISTERED) {
console.debug('Already registered - ignoring reload message');
} else {
throw new Error(`Unknown state: '${this.state}'`);
switch (this.state) {
case ONBOARDING_STATE.RELOADING:
console.debug('Ignoring message while reloading');
break;
case ONBOARDING_STATE.NOT_INSTALLED:
console.debug('Reloading now to register with MetaMask');
this.state = ONBOARDING_STATE.RELOADING;
return location.reload();

case ONBOARDING_STATE.INSTALLED:
console.debug('Registering with MetaMask');
this.state = ONBOARDING_STATE.REGISTERING;
await Onboarding._register();
this.state = ONBOARDING_STATE.REGISTERED;
if (event.source instanceof Window) {
event.source.postMessage({ type: 'metamask:registrationCompleted' }, event.origin);
}
this.stopOnboarding();
break;
case ONBOARDING_STATE.REGISTERING:
console.debug('Already registering - ignoring reload message');
break;
case ONBOARDING_STATE.REGISTERED:
console.debug('Already registered - ignoring reload message');
break;
default:
throw new Error(`Unknown state: '${this.state}'`);
}
return undefined;
}
Expand Down

0 comments on commit a4042ac

Please sign in to comment.