Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add a bridge to the ITimers interface #439

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/definition/accessors/IAppAccessors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IEnvironmentRead, IHttp, IRead } from '../../definition/accessors';
import { IEnvironmentRead, IHttp, IRead, ITimers } from '../../definition/accessors';
import { IApiEndpointMetadata } from '../api';

export interface IAppAccessors {
readonly environmentReader: IEnvironmentRead;
readonly reader: IRead;
readonly http: IHttp;
readonly providedApiEndpoints: Array<IApiEndpointMetadata>;
readonly timers: ITimers;
}
3 changes: 3 additions & 0 deletions src/definition/accessors/ITimers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ITimers {
call(nameMethod: string): (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => NodeJS.Timeout;
}
2 changes: 2 additions & 0 deletions src/definition/accessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { ISettingRead } from './ISettingRead';
import { ISettingsExtend } from './ISettingsExtend';
import { ISlashCommandsExtend } from './ISlashCommandsExtend';
import { ISlashCommandsModify } from './ISlashCommandsModify';
import { ITimers } from './ITimers';
import { IUIController } from './IUIController';
import { IUploadCreator } from './IUploadCreator';
import { IUploadRead } from './IUploadRead';
Expand Down Expand Up @@ -84,6 +85,7 @@ export {
IRoomBuilder,
IRoomExtender,
IRoomRead,
ITimers,
IServerSettingRead,
IServerSettingsModify,
ISettingRead,
Expand Down
6 changes: 5 additions & 1 deletion src/server/accessors/AppAccessors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAppAccessors, IEnvironmentRead, IHttp, IRead } from '../../definition/accessors';
import { IAppAccessors, IEnvironmentRead, IHttp, IRead, ITimers } from '../../definition/accessors';
import { IApiEndpointMetadata } from '../../definition/api';
import { AppManager } from '../AppManager';
import { AppAccessorManager } from '../managers/AppAccessorManager';
Expand Down Expand Up @@ -28,4 +28,8 @@ export class AppAccessors implements IAppAccessors {
public get providedApiEndpoints(): Array<IApiEndpointMetadata> {
return this.apiManager.listApis(this.appId);
}

public get timers(): ITimers {
return this.accessorManager.getTimers(this.appId);
}
}
9 changes: 9 additions & 0 deletions src/server/accessors/Timers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ITimers } from '../../definition/accessors/ITimers';
import { AppBridges } from '../bridges/AppBridges';

export class Timers implements ITimers {
constructor(private readonly bridges: AppBridges) { }
public call(nameMethod: string): (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => NodeJS.Timeout {
return this.bridges.getTimersBridge().call(nameMethod);
}
}
2 changes: 2 additions & 0 deletions src/server/accessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { SettingRead } from './SettingRead';
import { SettingsExtend } from './SettingsExtend';
import { SlashCommandsExtend } from './SlashCommandsExtend';
import { SlashCommandsModify } from './SlashCommandsModify';
import { Timers } from './Timers';
import { UploadRead } from './UploadRead';
import { UserBuilder } from './UserBuilder';
import { UserRead } from './UserRead';
Expand Down Expand Up @@ -65,6 +66,7 @@ export {
SettingsExtend,
SlashCommandsExtend,
SlashCommandsModify,
Timers,
UploadRead,
UserBuilder,
UserRead,
Expand Down
2 changes: 2 additions & 0 deletions src/server/bridges/AppBridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PersistenceBridge } from './PersistenceBridge';
import { RoomBridge } from './RoomBridge';
import { SchedulerBridge } from './SchedulerBridge';
import { ServerSettingBridge } from './ServerSettingBridge';
import { TimersBridge } from './TimersBridge';
import { UiInteractionBridge } from './UiInteractionBridge';
import { UploadBridge } from './UploadBridge';
import { UserBridge } from './UserBridge';
Expand Down Expand Up @@ -48,6 +49,7 @@ export abstract class AppBridges {
public abstract getRoomBridge(): RoomBridge;
public abstract getInternalBridge(): IInternalBridge;
public abstract getServerSettingBridge(): ServerSettingBridge;
public abstract getTimersBridge(): TimersBridge;
public abstract getUploadBridge(): UploadBridge;
public abstract getUserBridge(): UserBridge;
public abstract getUiInteractionBridge(): UiInteractionBridge;
Expand Down
6 changes: 6 additions & 0 deletions src/server/bridges/TimersBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseBridge } from './BaseBridge';

export abstract class TimersBridge extends BaseBridge {

public abstract call(nameMethod: string): (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => NodeJS.Timeout;
}
2 changes: 2 additions & 0 deletions src/server/bridges/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PersistenceBridge } from './PersistenceBridge';
import { RoomBridge } from './RoomBridge';
import { SchedulerBridge } from './SchedulerBridge';
import { ServerSettingBridge } from './ServerSettingBridge';
import { TimersBridge } from './TimersBridge';
import { UiInteractionBridge } from './UiInteractionBridge';
import { UploadBridge } from './UploadBridge';
import { UserBridge } from './UserBridge';
Expand All @@ -34,6 +35,7 @@ export {
RoomBridge,
IInternalBridge,
ServerSettingBridge,
TimersBridge,
UserBridge,
UploadBridge,
UiInteractionBridge,
Expand Down
13 changes: 13 additions & 0 deletions src/server/managers/AppAccessorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IModify,
IPersistence,
IRead,
ITimers,
} from '../../definition/accessors';
import {
ApiExtend,
Expand All @@ -33,6 +34,7 @@ import {
SettingsExtend,
SlashCommandsExtend,
SlashCommandsModify,
Timers,
UploadRead,
UserRead,
} from '../accessors';
Expand All @@ -49,6 +51,7 @@ export class AppAccessorManager {
private readonly modifiers: Map<string, IModify>;
private readonly persists: Map<string, IPersistence>;
private readonly https: Map<string, IHttp>;
private readonly timers: Map<string, ITimers>;

constructor(private readonly manager: AppManager) {
this.bridges = this.manager.getBridges();
Expand All @@ -59,6 +62,7 @@ export class AppAccessorManager {
this.modifiers = new Map<string, IModify>();
this.persists = new Map<string, IPersistence>();
this.https = new Map<string, IHttp>();
this.timers = new Map<string, Timers>();
}

/**
Expand All @@ -74,6 +78,7 @@ export class AppAccessorManager {
this.modifiers.delete(appId);
this.persists.delete(appId);
this.https.delete(appId);
this.timers.delete(appId);
}

public getConfigurationExtend(appId: string): IConfigurationExtend {
Expand Down Expand Up @@ -176,4 +181,12 @@ export class AppAccessorManager {

return this.https.get(appId);
}

public getTimers(appId: string): ITimers {
if (!this.timers.has(appId)) {
this.timers.set(appId, new Timers(this.bridges));
}
return this.timers.get(appId);
}

}
37 changes: 37 additions & 0 deletions tests/server/accessors/Timers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Expect, SetupFixture, SpyOn, Test} from 'alsatian';

import { Timers } from '../../../src/server/accessors';
import { AppBridges, TimersBridge } from '../../../src/server/bridges';

export class TimersAccessorsTestFixture {
private mockAppBridge: AppBridges;
private mockTimersBridge: TimersBridge;
private mockTimeout: NodeJS.Timeout;

@SetupFixture
public setupFixture() {
const timeout = this.mockTimeout as NodeJS.Timeout;
this.mockTimersBridge = {
call(nameMethod: string): (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => NodeJS.Timeout {
return (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => timeout;
},
} as TimersBridge;

const timersBridge = this.mockTimersBridge;
this.mockAppBridge = { getTimersBridge(): TimersBridge {
return timersBridge;
},
} as AppBridges;
}

@Test()
public useTimers() {
Expect(() => new Timers(this.mockAppBridge)).not.toThrow();

const timers = new Timers(this.mockAppBridge);
Expect(timers.call('nameMethod')).toBeDefined();

SpyOn(this.mockTimersBridge, 'call');
SpyOn(timers, 'call');
}
}
8 changes: 8 additions & 0 deletions tests/test-data/bridges/appBridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RoomBridge,
SchedulerBridge,
ServerSettingBridge,
TimersBridge,
UiInteractionBridge,
UploadBridge,
UserBridge,
Expand All @@ -31,6 +32,7 @@ import { TestsPersisBridge } from './persisBridge';
import { TestsRoomBridge } from './roomBridge';
import { TestSchedulerBridge } from './schedulerBridge';
import { TestsServerSettingBridge } from './serverSettingBridge';
import { TestsTimersBridge } from './timersBridge';
import { TestsUiIntegrationBridge } from './uiIntegrationBridge';
import { TestUploadBridge } from './uploadBridge';
import { TestsUserBridge } from './userBridge';
Expand All @@ -51,6 +53,7 @@ export class TestsAppBridges extends AppBridges {
private readonly livechatBridge: TestLivechatBridge;
private readonly uploadBridge: TestUploadBridge;
private readonly uiIntegrationBridge: TestsUiIntegrationBridge;
private readonly timersBridge: TestsTimersBridge;
private readonly schedulerBridge: TestSchedulerBridge;
private readonly cloudWorkspaceBridge: TestAppCloudWorkspaceBridge;

Expand All @@ -71,6 +74,7 @@ export class TestsAppBridges extends AppBridges {
this.livechatBridge = new TestLivechatBridge();
this.uploadBridge = new TestUploadBridge();
this.uiIntegrationBridge = new TestsUiIntegrationBridge();
this.timersBridge = new TestsTimersBridge();
this.schedulerBridge = new TestSchedulerBridge();
this.cloudWorkspaceBridge = new TestAppCloudWorkspaceBridge();
}
Expand Down Expand Up @@ -139,6 +143,10 @@ export class TestsAppBridges extends AppBridges {
return this.uiIntegrationBridge;
}

public getTimersBridge(): TimersBridge {
return this.timersBridge;
}

public getSchedulerBridge(): SchedulerBridge {
return this.schedulerBridge;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/test-data/bridges/timersBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TimersBridge } from '../../../src/server/bridges/TimersBridge';

export class TestsTimersBridge extends TimersBridge {
public call(nameMethod: string): (callback: (...args: Array<any>) => void, ms: number, ...args: Array<any>) => NodeJS.Timeout {
throw new Error('Method not implemented.');
}
}