Showing
10 changed files
with
49 additions
and
21 deletions
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
| ... | @@ -6,19 +6,30 @@ interface ProgramRepository { | ... | @@ -6,19 +6,30 @@ interface ProgramRepository { |
| 6 | } | 6 | } |
| 7 | 7 | ||
| 8 | interface Player { | 8 | interface Player { |
| 9 | + new() : Player; | ||
| 10 | + | ||
| 9 | on(name:string, any); | 11 | on(name:string, any); |
| 12 | + once(name:string, any); | ||
| 13 | + | ||
| 10 | start() : void; | 14 | start() : void; |
| 11 | stop() : void; | 15 | stop() : void; |
| 16 | + | ||
| 17 | + programRepository: ProgramRepository; | ||
| 18 | + programManager: ProgramManager; | ||
| 12 | } | 19 | } |
| 13 | 20 | ||
| 14 | interface ProgramManager { | 21 | interface ProgramManager { |
| 22 | + new() : ProgramManager; | ||
| 23 | + | ||
| 15 | programRepository: ProgramRepository; | 24 | programRepository: ProgramRepository; |
| 16 | } | 25 | } |
| 17 | 26 | ||
| 18 | interface ProgramItemFactory { | 27 | interface ProgramItemFactory { |
| 19 | - basePath: string; | 28 | + new() : ProgramItemFactory; |
| 20 | - programRepository: ProgramRepository; | ||
| 21 | getProgramItem(programItemId:string) : Promise<any>; | 29 | getProgramItem(programItemId:string) : Promise<any>; |
| 30 | + | ||
| 31 | + basePath: string; | ||
| 32 | + programRepository: ProgramRepository; | ||
| 22 | } | 33 | } |
| 23 | 34 | ||
| 24 | declare var digsigPlayer: { | 35 | declare var digsigPlayer: { | ... | ... |
| ... | @@ -15,8 +15,8 @@ var rollupConfig = { | ... | @@ -15,8 +15,8 @@ var rollupConfig = { |
| 15 | dest: 'build/bundle.js', | 15 | dest: 'build/bundle.js', |
| 16 | 16 | ||
| 17 | plugins: [ | 17 | plugins: [ |
| 18 | - builtins(), | 18 | + // builtins(), |
| 19 | - globals(), | 19 | + // globals(), |
| 20 | typescript() | 20 | typescript() |
| 21 | ] | 21 | ] |
| 22 | 22 | ... | ... |
| ... | @@ -15,7 +15,7 @@ export class DummyProgramRepository implements ProgramRepository { | ... | @@ -15,7 +15,7 @@ export class DummyProgramRepository implements ProgramRepository { |
| 15 | return null; | 15 | return null; |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | - replicate() : Promise<void> { | 18 | + replicate() : Promise<boolean> { |
| 19 | return null; | 19 | return null; |
| 20 | } | 20 | } |
| 21 | 21 | ... | ... |
| ... | @@ -31,8 +31,7 @@ describe('Player repository triggers', () => { | ... | @@ -31,8 +31,7 @@ describe('Player repository triggers', () => { |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | player.triggerReplication().then(() => { | 33 | player.triggerReplication().then(() => { |
| 34 | - | 34 | + expect(player.trigger).toHaveBeenCalled(); |
| 35 | - expect(player.trigger).toHaveBeenCalledWith(player.triggerProgramItemId, Util.calculateNextMinute()); | ||
| 36 | done(); | 35 | done(); |
| 37 | }); | 36 | }); |
| 38 | }); | 37 | }); |
| ... | @@ -44,10 +43,12 @@ describe('Player repository triggers', () => { | ... | @@ -44,10 +43,12 @@ describe('Player repository triggers', () => { |
| 44 | }); | 43 | }); |
| 45 | }); | 44 | }); |
| 46 | 45 | ||
| 47 | - player.triggerReplication().then(() => { | 46 | + player.once('error', () => { |
| 48 | - expect(player.trigger).toHaveBeenCalledWith(player.triggerReplication, player.replicationRetry); | 47 | + expect(player.trigger).toHaveBeenCalled(); |
| 49 | done(); | 48 | done(); |
| 50 | }); | 49 | }); |
| 50 | + | ||
| 51 | + player.triggerReplication(); | ||
| 51 | }); | 52 | }); |
| 52 | 53 | ||
| 53 | }); | 54 | }); | ... | ... |
| ... | @@ -13,7 +13,8 @@ describe("Util", () => { | ... | @@ -13,7 +13,8 @@ describe("Util", () => { |
| 13 | }); | 13 | }); |
| 14 | 14 | ||
| 15 | it('should get the remaining seconds', () => { | 15 | it('should get the remaining seconds', () => { |
| 16 | - expect(Util.calculateNextMinute()).toEqual((60 - (new Date()).getSeconds()) * 1000); | 16 | + let now = (60 - (new Date()).getSeconds()); |
| 17 | + expect(Util.calculateNextMinute() / 1000).toBeCloseTo(now); | ||
| 17 | }); | 18 | }); |
| 18 | 19 | ||
| 19 | }); | 20 | }); |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -8,9 +8,13 @@ const STATE_STOP = "stop"; | ... | @@ -8,9 +8,13 @@ const STATE_STOP = "stop"; |
| 8 | 8 | ||
| 9 | export class Player extends EventEmitter { | 9 | export class Player extends EventEmitter { |
| 10 | 10 | ||
| 11 | + constructor() { | ||
| 12 | + super(); | ||
| 13 | + } | ||
| 14 | + | ||
| 11 | protected _programRepository:ProgramRepository; | 15 | protected _programRepository:ProgramRepository; |
| 12 | protected _programManager:ProgramManager; | 16 | protected _programManager:ProgramManager; |
| 13 | - protected _minutesReplication:number = 5; | 17 | + protected _minutesReplication:number = 3; |
| 14 | protected _replicationRetry:number = 10000; | 18 | protected _replicationRetry:number = 10000; |
| 15 | 19 | ||
| 16 | protected _currentProgramItemId:string = ''; | 20 | protected _currentProgramItemId:string = ''; |
| ... | @@ -54,17 +58,22 @@ export class Player extends EventEmitter { | ... | @@ -54,17 +58,22 @@ export class Player extends EventEmitter { |
| 54 | } | 58 | } |
| 55 | 59 | ||
| 56 | triggerReplication() : Promise<void> { | 60 | triggerReplication() : Promise<void> { |
| 61 | + console.info("digsig-player-service: trigger replication"); | ||
| 62 | + | ||
| 57 | return this.programRepository.replicate() | 63 | return this.programRepository.replicate() |
| 58 | - .then(() => { | 64 | + .then(changes => { |
| 59 | this._currentReplicationCounter = 0; | 65 | this._currentReplicationCounter = 0; |
| 60 | - this.trigger(this.triggerProgramItemId, Util.calculateNextMinute()); | 66 | + this.trigger(() => { this.triggerProgramItemId(changes); }); |
| 61 | }) | 67 | }) |
| 62 | - .catch(() => { | 68 | + .catch(error => { |
| 63 | - this.trigger(this.triggerReplication, this.replicationRetry); | 69 | + this.trigger(() => { this.triggerReplication(); }, this.replicationRetry); |
| 70 | + this.emit('error', error); | ||
| 64 | }); | 71 | }); |
| 65 | } | 72 | } |
| 66 | 73 | ||
| 67 | - triggerProgramItemId() { | 74 | + triggerProgramItemId(changes:boolean = false) { |
| 75 | + console.info("digsig-player-service: trigger program item id"); | ||
| 76 | + | ||
| 68 | this.programManager.getCurrentProgramItemId() | 77 | this.programManager.getCurrentProgramItemId() |
| 69 | .then(programItemId => { | 78 | .then(programItemId => { |
| 70 | this._currentReplicationCounter++; | 79 | this._currentReplicationCounter++; |
| ... | @@ -73,18 +82,20 @@ export class Player extends EventEmitter { | ... | @@ -73,18 +82,20 @@ export class Player extends EventEmitter { |
| 73 | // else (1) calculate next potential program change point | 82 | // else (1) calculate next potential program change point |
| 74 | // or (2) trigger replication | 83 | // or (2) trigger replication |
| 75 | 84 | ||
| 76 | - if (programItemId != this._currentProgramItemId) { | 85 | + if (programItemId && (programItemId != this._currentProgramItemId || changes)) { |
| 77 | this._currentProgramItemId = programItemId; | 86 | this._currentProgramItemId = programItemId; |
| 78 | this.emit('play', programItemId); | 87 | this.emit('play', programItemId); |
| 79 | - } else if (this._currentReplicationCounter >= this._minutesReplication) { | 88 | + } |
| 89 | + | ||
| 90 | + if (this._currentReplicationCounter >= this._minutesReplication) { | ||
| 80 | this.triggerReplication(); | 91 | this.triggerReplication(); |
| 81 | } else { | 92 | } else { |
| 82 | - this.trigger(this.triggerProgramItemId, Util.calculateNextMinute()); | 93 | + this.trigger(() => { this.triggerProgramItemId(); }, Util.calculateNextMinute()); |
| 83 | } | 94 | } |
| 84 | }); | 95 | }); |
| 85 | } | 96 | } |
| 86 | 97 | ||
| 87 | - trigger(func:Function, milliseconds:number) { | 98 | + trigger(func:Function, milliseconds:number = 0) { |
| 88 | if (this._state === STATE_START) { | 99 | if (this._state === STATE_START) { |
| 89 | setTimeout(() => { func(); }, milliseconds); | 100 | setTimeout(() => { func(); }, milliseconds); |
| 90 | } | 101 | } | ... | ... |
| ... | @@ -33,6 +33,7 @@ export class ProgramManager { | ... | @@ -33,6 +33,7 @@ export class ProgramManager { |
| 33 | findCurrentProgramItem(schedule:any, dateInMinutes:number) : string { | 33 | findCurrentProgramItem(schedule:any, dateInMinutes:number) : string { |
| 34 | let timeList:any = []; | 34 | let timeList:any = []; |
| 35 | let tmpSchedule:any = {}; | 35 | let tmpSchedule:any = {}; |
| 36 | + dateInMinutes--; // make it not so strict, which will start one minute earlier | ||
| 36 | 37 | ||
| 37 | for (let startTime in schedule) { | 38 | for (let startTime in schedule) { |
| 38 | if (schedule.hasOwnProperty(startTime)) { | 39 | if (schedule.hasOwnProperty(startTime)) { | ... | ... |
| ... | @@ -6,6 +6,9 @@ export interface ProgramRepository { | ... | @@ -6,6 +6,9 @@ export interface ProgramRepository { |
| 6 | 6 | ||
| 7 | findByType(type:string) : Promise<Array<any>>; | 7 | findByType(type:string) : Promise<Array<any>>; |
| 8 | 8 | ||
| 9 | - replicate() : Promise<void>; | 9 | + /** |
| 10 | + * returns true / false depending on if there where any changes to download | ||
| 11 | + */ | ||
| 12 | + replicate() : Promise<boolean>; | ||
| 10 | 13 | ||
| 11 | } | 14 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment