Skip to content

Commit

Permalink
async fix after rebase
Browse files Browse the repository at this point in the history
async
.
  • Loading branch information
denysoblohin-okta committed Jun 1, 2022
1 parent b484fa8 commit c609644
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 126 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -903,11 +903,15 @@ This is accomplished by selecting a single tab to handle the network requests to

### `start()`

> :hourglass: async
Starts the `OktaAuth` service. See [running as a service](#running-as-a-service) for more details.

### `stop()`

Starts the `OktaAuth` service. See [running as a service](#running-as-a-service) for more details.
> :hourglass: async
Stops the `OktaAuth` service. See [running as a service](#running-as-a-service) for more details.

### `signIn(options)`

Expand Down
4 changes: 2 additions & 2 deletions lib/OktaAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ class OktaAuth implements OktaAuthInterface, SigninAPI, SignoutAPI {
this.serviceManager = new ServiceManager(this, args.services);
}

start() {
this.serviceManager.start();
async start() {
await this.serviceManager.start();
// TODO: review tokenManager.start
this.tokenManager.start();
if (!this.token.isLoginRedirect()) {
Expand Down
18 changes: 9 additions & 9 deletions lib/ServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export class ServiceManager implements ServiceManagerInterface {
});
}

private onLeader() {
private async onLeader() {
if (this.started) {
// Start services that requires leadership
this.startServices();
await this.startServices();
}
}

Expand All @@ -85,30 +85,30 @@ export class ServiceManager implements ServiceManagerInterface {
if (this.started) {
return; // noop if services have already started
}
this.startServices();
await this.startServices();
this.started = true;
}

stop() {
this.stopServices();
async stop() {
await this.stopServices();
this.started = false;
}

getService(name: string): ServiceInterface | undefined {
return this.services.get(name);
}

private startServices() {
private async startServices() {
for (const [name, srv] of this.services.entries()) {
if (this.canStartService(name, srv)) {
srv.start();
await srv.start();
}
}
}

private stopServices() {
private async stopServices() {
for (const srv of this.services.values()) {
srv.stop();
await srv.stop();
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/services/AutoRenewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ export class AutoRenewService implements ServiceInterface {
return (!!this.options.autoRenew || !!this.options.autoRemove);
}

start() {
async start() {
if (this.canStart()) {
this.stop();
await this.stop();
this.tokenManager.on(EVENT_EXPIRED, this.onTokenExpiredHandler);
this.started = true;
}
}

stop() {
async stop() {
if (this.started) {
this.tokenManager.off(EVENT_EXPIRED, this.onTokenExpiredHandler);
this.renewTimeQueue = [];
Expand Down
28 changes: 16 additions & 12 deletions lib/services/LeaderElectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from 'broadcast-channel';
import { isBrowser } from '../features';

declare type OnLeaderHandler = (() => void);
declare type OnLeaderHandler = (() => Promise<void>);
declare type ServiceOptions = ServiceManagerOptions & {
onLeader?: OnLeaderHandler;
};
Expand All @@ -39,8 +39,8 @@ export class LeaderElectionService implements ServiceInterface {
private onLeaderDuplicate() {
}

private onLeader() {
this.options.onLeader?.();
private async onLeader() {
await this.options.onLeader?.();
}

isLeader() {
Expand All @@ -51,8 +51,8 @@ export class LeaderElectionService implements ServiceInterface {
return !!this.elector?.hasLeader;
}

start() {
this.stop();
async start() {
await this.stop();
if (this.canStart()) {
const { electionChannelName } = this.options;
this.channel = new BroadcastChannel(electionChannelName as string);
Expand All @@ -63,14 +63,18 @@ export class LeaderElectionService implements ServiceInterface {
}
}

stop() {
async stop() {
if (this.started) {
this.elector?.die();
this.elector = undefined;
this.channel?.close();
// Workaround to fix error `Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed`
(this.channel as any).postInternal = () => Promise.resolve();
this.channel = undefined;
if (this.elector) {
await this.elector.die();
this.elector = undefined;
}
if (this.channel) {
// Workaround to fix error `Failed to execute 'postMessage' on 'BroadcastChannel': Channel is closed`
(this.channel as any).postInternal = () => Promise.resolve();
await this.channel.close();
this.channel = undefined;
}
this.started = false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/services/SyncStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export class SyncStorageService implements ServiceInterface {
return !!this.options.syncStorage && isBrowser();
}

start() {
async start() {
if (this.canStart()) {
this.stop();
await this.stop();
const { syncChannelName } = this.options;
this.channel = new BroadcastChannel(syncChannelName as string);
this.tokenManager.on(EVENT_ADDED, this.onTokenAddedHandler);
Expand All @@ -68,14 +68,14 @@ export class SyncStorageService implements ServiceInterface {
}
}

stop() {
async stop() {
if (this.started) {
this.tokenManager.off(EVENT_ADDED, this.onTokenAddedHandler);
this.tokenManager.off(EVENT_REMOVED, this.onTokenRemovedHandler);
this.tokenManager.off(EVENT_RENEWED, this.onTokenRenewedHandler);
this.tokenManager.off(EVENT_SET_STORAGE, this.onSetStorageHandler);
this.channel?.removeEventListener('message', this.onSyncMessageHandler);
this.channel?.close();
await this.channel?.close();
this.channel = undefined;
this.started = false;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types/Service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// only add methods needed internally
export interface ServiceInterface {
start(): void;
stop(): void;
start(): Promise<void>;
stop(): Promise<void>;
isStarted(): boolean;
canStart(): boolean;
requiresLeadership(): boolean;
Expand Down
2 changes: 1 addition & 1 deletion test/spec/AuthStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('AuthStateManager', () => {
const auth = createAuth();
auth.authStateManager.updateAuthState = jest.fn();
auth.tokenManager.start(); // uses TokenService / crossTabs
auth.serviceManager.start();
await auth.serviceManager.start();
// simulate change from other dom context
const channel = new BroadcastChannel('syncChannel');
await channel.postMessage({
Expand Down
12 changes: 6 additions & 6 deletions test/spec/OktaAuth/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ describe('OktaAuth (api)', function() {
});

describe('start', () => {
it('starts the token service', () => {
it('starts the token service', async () => {
jest.spyOn(auth.tokenManager, 'start');
auth.start();
await auth.start();
expect(auth.tokenManager.start).toHaveBeenCalled();
});
it('updates auth state', () => {
it('updates auth state', async () => {
jest.spyOn(auth.authStateManager, 'updateAuthState');
auth.start();
await auth.start();
expect(auth.authStateManager.updateAuthState).toHaveBeenCalled();
});
it('should not update auth state during login redirect', () => {
it('should not update auth state during login redirect', async () => {
jest.spyOn(auth.authStateManager, 'updateAuthState');
jest.spyOn(auth.token, 'isLoginRedirect').mockReturnValue(true);
auth.start();
await auth.start();
expect(auth.authStateManager.updateAuthState).not.toHaveBeenCalled();
});
});
Expand Down
Loading

0 comments on commit c609644

Please sign in to comment.