Skip to content

Commit

Permalink
simplify DelayTimer API
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed Jan 17, 2024
1 parent 1a72c0f commit 71dada2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
6 changes: 2 additions & 4 deletions src/script/E2EIdentity/DelayTimer/DelayTimer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import {FIFTEEN_MINUTES, FOUR_HOURS, ONE_HOUR, ONE_MINUTE} from './delay';
import {DelayTimerService} from './DelayTimer'; // Update this with your module's actual path
import {DelayTimerService} from './DelayTimer';

describe('createGracePeriodTimer', () => {
let timer: DelayTimerService | undefined;
Expand All @@ -27,16 +27,14 @@ describe('createGracePeriodTimer', () => {
jest.clearAllMocks();
jest.useFakeTimers();
global.localStorage.clear();
timer = DelayTimerService?.getInstance({
timer = new DelayTimerService({
gracePeriodInMS: 0,
gracePeriodExpiredCallback: jest.fn(),
delayPeriodExpiredCallback: jest.fn(),
});
});

afterEach(() => {
timer?.resetInstance();
timer = undefined;
jest.useRealTimers();
});

Expand Down
37 changes: 2 additions & 35 deletions src/script/E2EIdentity/DelayTimer/DelayTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,21 @@ interface CreateGracePeriodTimerParams {
delayPeriodExpiredCallback: () => void;
}

class DelayTimerService {
private static instance: DelayTimerService | null = null;
export class DelayTimerService {
private gracePeriodInMS: number;
private gracePeriodExpiredCallback: () => void;
private delayPeriodExpiredCallback: () => void;
private readonly logger = logdown('@wireapp/core/DelayTimer');
private delayPeriodTimerKey: string = 'E2EIdentity_DelayTimer';
private gracePeriodTimerKey: string = 'E2EIdentity_GracePeriodTimer';

private constructor({
gracePeriodInMS,
gracePeriodExpiredCallback,
delayPeriodExpiredCallback,
}: CreateGracePeriodTimerParams) {
constructor({gracePeriodInMS, gracePeriodExpiredCallback, delayPeriodExpiredCallback}: CreateGracePeriodTimerParams) {
this.gracePeriodInMS = gracePeriodInMS;
this.gracePeriodExpiredCallback = gracePeriodExpiredCallback;
this.delayPeriodExpiredCallback = delayPeriodExpiredCallback;
this.initialize();
}

/**
* Get the singleton instance of GracePeriodTimer or create a new one
* For the first time, params are required to create the instance
* @param params The params to create the grace period timer
* @returns The singleton instance of GracePeriodTimer
*/
public static getInstance(params?: CreateGracePeriodTimerParams) {
if (!DelayTimerService.instance) {
if (!params) {
throw new Error('DelayTimerService is not initialized. Please call getInstance with params.');
}
DelayTimerService.instance = new DelayTimerService(params);
}
return DelayTimerService.instance;
}

/**
* @param CreateGracePeriodTimerParams The params to create the grace period timer
*/
Expand Down Expand Up @@ -263,16 +242,6 @@ class DelayTimerService {
: 0;
}

/**
* Reset the instance
*/
public resetInstance() {
DelayTimerStore.clear.all();
this.clearGracePeriodTimer();
this.clearDelayPeriodTimer();
DelayTimerService.instance = null;
}

public isDelayTimerActive() {
return TaskScheduler.hasActiveTask(this.delayPeriodTimerKey);
}
Expand All @@ -283,5 +252,3 @@ class DelayTimerService {
return remainingTime - delayTime > 0;
}
}

export {DelayTimerService};
2 changes: 1 addition & 1 deletion src/script/E2EIdentity/E2EIdentityEnrollment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
this.config = {
discoveryUrl,
gracePeriodInMs,
timer: DelayTimerService.getInstance({
timer: new DelayTimerService({
gracePeriodInMS: gracePeriodInMs,
gracePeriodExpiredCallback: () => null,
delayPeriodExpiredCallback: () => null,
Expand Down

0 comments on commit 71dada2

Please sign in to comment.