Skip to content

Commit

Permalink
feat(project): change the way of DI
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh committed Nov 20, 2023
1 parent 9a71457 commit 4154488
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/modules/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SettingsService from '#src/services/settings.service';

// Epg services
import EpgClientService from '#src/services/epg/epgClient.service';
import EpgProvider from '#src/services/epg/epgProvider.service';
import EpgProviderService from '#src/services/epg/epgProvider.service';
import ViewNexaEpgService from '#src/services/epg/viewNexaEpg.service';
import JWEpgService from '#src/services/epg/jwEpg.service';

Expand Down Expand Up @@ -80,5 +80,11 @@ container.bind(ProfileService).to(InplayerProfileService).whenTargetNamed(INTEGR

// EPG integration
container.bind(EpgClientService).toSelf();
container.bind(EpgProvider).to(JWEpgService).whenTargetNamed(EPG_TYPE.JW);
container.bind(EpgProvider).to(ViewNexaEpgService).whenTargetNamed(EPG_TYPE.VIEW_NEXA);
container
.bind(EpgProviderService)
.to(ViewNexaEpgService)
.when((request) => request.target.name.equals(EPG_TYPE.VIEW_NEXA));
container
.bind(EpgProviderService)
.to(JWEpgService)
.when((request) => request.target.name.equals(EPG_TYPE.JW));
4 changes: 3 additions & 1 deletion src/services/epg/epgClient.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import type { Playlist } from '#types/playlist';
import { EPG_TYPE } from '#src/config';

const livePlaylist = livePlaylistFixture as Playlist;
const epgService = new EpgClientService();

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

const epgProvider = { transformProgram, fetchSchedule };
const epgService = new EpgClientService(epgProvider, epgProvider);

const mockProgram1 = {
id: 'test',
title: 'Test',
Expand Down
16 changes: 10 additions & 6 deletions src/services/epg/epgClient.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { addDays, differenceInDays } from 'date-fns';
import { injectable } from 'inversify';
import { injectable, targetName } from 'inversify';

import EpgProviderService from './epgProvider.service';

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

Expand All @@ -20,6 +19,14 @@ export const isFulfilled = <T>(input: PromiseSettledResult<T>): input is Promise

@injectable()
export default class EpgClientService {
private viewNexaProvider: EpgProviderService;
private jwProvider: EpgProviderService;

public constructor(@targetName(EPG_TYPE.JW) jwProvider: EpgProviderService, @targetName(EPG_TYPE.VIEW_NEXA) viewNexaProvider: EpgProviderService) {
this.viewNexaProvider = viewNexaProvider;
this.jwProvider = jwProvider;
}

/**
* 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 @@ -96,10 +103,7 @@ export default class EpgClientService {
};

getScheduleProvider = (item: PlaylistItem) => {
const scheduleType = item.scheduleType || EPG_TYPE.JW;
const scheduleProvider = getNamedModule(EpgProviderService, scheduleType);

return scheduleProvider;
return item?.scheduleType === EPG_TYPE.VIEW_NEXA ? this.viewNexaProvider : this.jwProvider;
};

/**
Expand Down

0 comments on commit 4154488

Please sign in to comment.