Skip to content

Commit

Permalink
feat(epg): use getNamedModule
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh committed Jan 12, 2024
1 parent 7ce52e5 commit 0394daf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/modules/register.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// To organize imports in a better way
/* eslint-disable import/order */
import 'reflect-metadata'; // include once in the app for inversify (see: https://github.com/inversify/InversifyJS/blob/master/README.md#-installation)
import { INTEGRATION } from '#src/config';
import { EPG_TYPE, INTEGRATION } from '#src/config';
import { container } from '#src/modules/container';

import ApiService from '#src/services/api.service';
Expand Down Expand Up @@ -51,10 +51,6 @@ container.bind(GenericEntitlementService).toSelf();
container.bind(ApiService).toSelf();
container.bind(SettingsService).toSelf();

// EPG services
container.bind(EpgService).to(JWEpgService);
container.bind(EpgService).to(ViewNexaEpgService);

// Common controllers
container.bind(AppController).toSelf();
container.bind(WatchHistoryController).toSelf();
Expand All @@ -70,6 +66,10 @@ container.bind('INTEGRATION_TYPE').toDynamicValue((context) => {
return context.container.get(AppController).getIntegrationType();
});

// EPG services
container.bind(EpgService).to(JWEpgService).whenTargetNamed(EPG_TYPE.JWP);
container.bind(EpgService).to(ViewNexaEpgService).whenTargetNamed(EPG_TYPE.VIEW_NEXA);

// Cleeng integration
container.bind(CleengService).toSelf();
container.bind(AccountService).to(CleengAccountService).whenTargetNamed(INTEGRATION.CLEENG);
Expand Down
10 changes: 2 additions & 8 deletions src/services/epg/epg.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { EpgProgram, EpgScheduleType } from '#types/epg';
import type { EpgProgram } from '#types/epg';
import type { PlaylistItem } from '#types/playlist';

export default abstract class EpgProviderService {
readonly type: EpgScheduleType;

protected constructor(type: EpgScheduleType) {
this.type = type;
}

export default abstract class EpgService {
/**
* Fetch the schedule data for the given PlaylistItem
*/
Expand Down
5 changes: 0 additions & 5 deletions src/services/epg/jw.epg.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { PlaylistItem } from '#types/playlist';
import { getDataOrThrow } from '#src/utils/api';
import { logDev } from '#src/utils/common';
import type { EpgProgram } from '#types/epg';
import { EPG_TYPE } from '#src/config';

const AUTHENTICATION_HEADER = 'API-KEY';

Expand All @@ -31,10 +30,6 @@ const jwEpgProgramSchema = object().shape({

@injectable()
export default class JWEpgService extends EpgService {
constructor() {
super(EPG_TYPE.JWP);
}

transformProgram = async (data: unknown): Promise<EpgProgram> => {
const program = await jwEpgProgramSchema.validate(data);
const image = program.chapterPointCustomProperties?.find((item) => item.key === 'image')?.value || undefined;
Expand Down
11 changes: 0 additions & 11 deletions src/services/epg/viewNexa.epg.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import EpgService from './epg.service';
import type { PlaylistItem } from '#types/playlist';
import { logDev } from '#src/utils/common';
import type { EpgProgram } from '#types/epg';
import { EPG_TYPE } from '#src/config';

const viewNexaEpgProgramSchema = object().shape({
'episode-num': object().shape({
Expand All @@ -30,13 +29,6 @@ const parseData = (date: string): string => parse(date, 'yyyyMdHms xxxx', new Da

@injectable()
export default class ViewNexaEpgService extends EpgService {
constructor() {
super(EPG_TYPE.VIEW_NEXA);
}

/**
* Validate the given data with the viewNexaProgramSchema and transform it into an EpgProgram
*/
transformProgram = async (data: unknown): Promise<EpgProgram> => {
const program = await viewNexaEpgProgramSchema.validate(data);

Expand All @@ -51,9 +43,6 @@ export default class ViewNexaEpgService extends EpgService {
};
};

/**
* Fetch the schedule data for the given PlaylistItem
*/
fetchSchedule = async (item: PlaylistItem) => {
const { XMLParser } = await import('fast-xml-parser');

Expand Down
22 changes: 10 additions & 12 deletions src/stores/EpgController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ import EpgController from './EpgController';

import channel1 from '#test/epg/jwChannel.json';
import livePlaylistFixture from '#test/fixtures/livePlaylist.json';
import EpgService from '#src/services/epg/epg.service';
import type { Playlist } from '#types/playlist';
import { EPG_TYPE } from '#src/config';

const livePlaylist = livePlaylistFixture as Playlist;

const transformProgram = vi.fn();
const fetchSchedule = vi.fn();

const epgService = { transformProgram, fetchSchedule };
const jwpEpgService = {
...epgService,
type: EPG_TYPE.JWP,
};

const viewNexaEpgService = {
...epgService,
type: EPG_TYPE.VIEW_NEXA,
};
vi.mock('#src/modules/container', () => ({
getNamedModule: (type: typeof EpgService) => {
switch (type) {
case EpgService:
return { transformProgram, fetchSchedule };
}
},
}));

const epgController = new EpgController([jwpEpgService, viewNexaEpgService]);
const epgController = new EpgController();

const mockProgram1 = {
id: 'test',
Expand Down
11 changes: 3 additions & 8 deletions src/stores/EpgController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { addDays, differenceInDays } from 'date-fns';
import { injectable, multiInject } from 'inversify';
import { injectable } from 'inversify';

import EpgService from '#src/services/epg/epg.service';
import { logDev } from '#src/utils/common';
import type { PlaylistItem } from '#types/playlist';
import type { EpgProgram, EpgChannel } from '#types/epg';
import { EPG_TYPE } from '#src/config';
import { getNamedModule } from '#src/modules/container';

export const isFulfilled = <T>(input: PromiseSettledResult<T>): input is PromiseFulfilledResult<T> => {
if (input.status === 'fulfilled') {
Expand All @@ -18,12 +19,6 @@ export const isFulfilled = <T>(input: PromiseSettledResult<T>): input is Promise

@injectable()
export default class EpgController {
private epgServices: EpgService[];

public constructor(@multiInject(EpgService) epgServices: EpgService[]) {
this.epgServices = epgServices || [];
}

/**
* Update the start and end time properties of the given programs with the current date.
* This can be used when having a static schedule or while developing
Expand Down Expand Up @@ -101,7 +96,7 @@ export default class EpgController {

getEpgService = (item: PlaylistItem) => {
const scheduleType = item?.scheduleType || EPG_TYPE.JWP;
const service = this.epgServices.find((service) => service.type === scheduleType);
const service = getNamedModule(EpgService, scheduleType);

if (!service) {
throw Error(`No epg service was added for the ${scheduleType} schedule type`);
Expand Down

0 comments on commit 0394daf

Please sign in to comment.