Skip to content

Commit

Permalink
fix(VgPlaybackButton): Added ChangeDetectorRef and called detectChang…
Browse files Browse the repository at this point in the history
…es() in click handler

I injected `public cdr: ChangeDetectorRef` into VgPlaybackButton and called `detectChanges()` as the
last action in the `updatePlaybackSpeed()` method. This fixes videogular#845. I had to make some innocuous
changes to some spec files to get it to build because of compile-time errors when running `npm run
build`.

845
  • Loading branch information
vicatcu committed Mar 31, 2020
1 parent affcc4d commit a56c7da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/controls/vg-playback-button/vg-playback-button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { VgPlaybackButton } from './vg-playback-button';
import { VgAPI } from '../../core/services/vg-api';
import { ElementRef } from '@angular/core';
import { ElementRef, ChangeDetectorRef } from '@angular/core';
import { VgStates } from '../../core/states/vg-states';

describe('Playback Button', () => {
let playbackButton: VgPlaybackButton;
let ref: ElementRef;
let api: VgAPI;
let cdr: ChangeDetectorRef;

beforeEach(() => {
ref = {
Expand All @@ -27,7 +28,9 @@ describe('Playback Button', () => {
}
};

playbackButton = new VgPlaybackButton(ref, api);
cdr = { detectChanges: () => {}, markForCheck: () => {}, checkNoChanges: () => {}, detach: () => {}, reattach: () => {} };

playbackButton = new VgPlaybackButton(ref, api, cdr);
});

it('Should set playbackIndex default value to 1', () => {
Expand Down
14 changes: 12 additions & 2 deletions src/controls/vg-playback-button/vg-playback-button.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, ElementRef, HostListener, OnInit, ViewEncapsulation, OnDestroy } from '@angular/core';
import { Component, Input, ElementRef, HostListener, OnInit, ViewEncapsulation, OnDestroy, ChangeDetectorRef } from '@angular/core';
import { VgAPI } from '../../core/services/vg-api';
import { Subscription } from 'rxjs';

Expand Down Expand Up @@ -51,7 +51,7 @@ export class VgPlaybackButton implements OnInit, OnDestroy {

ariaValue = 1;

constructor(ref: ElementRef, public API: VgAPI) {
constructor(ref: ElementRef, public API: VgAPI, public cdr: ChangeDetectorRef) {
this.elem = ref.nativeElement;
this.playbackValues = [ '0.5', '1.0', '1.5', '2.0' ];
this.playbackIndex = 1;
Expand Down Expand Up @@ -93,6 +93,8 @@ export class VgPlaybackButton implements OnInit, OnDestroy {
else {
this.target.playbackRate[ this.vgFor ] = (this.playbackValues[ this.playbackIndex ]);
}

this.detectChanges();
}

getPlaybackRate() {
Expand All @@ -103,4 +105,12 @@ export class VgPlaybackButton implements OnInit, OnDestroy {
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}

detectChanges() {
try {
this.cdr.detectChanges();
} catch(e) {
console.warn(e);
}
}
}
8 changes: 4 additions & 4 deletions src/core/services/vg-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ describe('Videogular Player', () => {

api.seekTime(10);

expect(api.$$seek).toHaveBeenCalledWith({id: 'main'}, 10, false);
expect(api.$$seek).toHaveBeenCalledWith({id: 'secondary'}, 10, false);
expect(api.$$seek).toHaveBeenCalledWith({id: 'main'} as IPlayable, 10, false);
expect(api.$$seek).toHaveBeenCalledWith({id: 'secondary'} as IPlayable, 10, false);
});

it('Should seek to a specified time by percentage', () => {
Expand All @@ -262,8 +262,8 @@ describe('Videogular Player', () => {

api.seekTime(10, true);

expect(api.$$seek).toHaveBeenCalledWith({id: 'main'}, 10, true);
expect(api.$$seek).toHaveBeenCalledWith({id: 'secondary'}, 10, true);
expect(api.$$seek).toHaveBeenCalledWith({id: 'main'} as IPlayable, 10, true);
expect(api.$$seek).toHaveBeenCalledWith({id: 'secondary'} as IPlayable, 10, true);
});

it('Should seek media files to a specified time by second', () => {
Expand Down

0 comments on commit a56c7da

Please sign in to comment.