Skip to content

Commit 756af85

Browse files
authored
feat: add support for manualReloadFromPropChanges prop (#67)
1 parent 53e8b85 commit 756af85

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This library has [the same support characteristics as the Brightcove Player Load
2222
- [Props](#props)
2323
- [`attrs`](#attrs)
2424
- [`baseUrl`](#baseurl)
25+
- [`manualReloadFromPropChanges`](#manualreloadfrompropchanges)
2526
- [Other Props](#other-props)
2627
- [Effects of Prop Changes](#effects-of-prop-changes)
2728
- [View the Demo](#view-the-demo)
@@ -96,6 +97,14 @@ Used to override the base URL for the Brightcove Player being embedded.
9697

9798
Most users will never need this prop. By default, players are loaded from Brightcove's player CDN (`players.brightcove.net`).
9899

100+
### `manualReloadFromPropChanges`
101+
102+
Type: `boolean`
103+
104+
Used to specify if reloading the player after prop changes will be handled manually. This can be done by calling `refToReactPlayerLoader.loadPlayer()`.
105+
106+
See [Effects of Prop Changes](#effects-of-prop-changes) below for the effects of prop changes.
107+
99108
### Other Props
100109

101110
All props not specified above are passed to the [Brightcove Player Loader](https://github.com/brightcove/player-loader#parameters) with a few differences:
@@ -119,7 +128,7 @@ The following props will update the player's state _without_ a reload:
119128
- `playlistVideoId`
120129
- `videoId`
121130

122-
All other prop changes will cause a complete dispose/reload.
131+
All other prop changes, excluding props that are `function`'s, will cause a complete dispose/reload.
123132

124133
## View the Demo
125134

src/index.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ class ReactPlayerLoader extends React.Component {
4545
* @param {Object} [props.attrs]
4646
* Used to set attributes on the component element that contains the
4747
* embedded Brightcove Player.
48+
*
49+
* @param {boolean} [props.manualReloadFromPropChanges]
50+
* Used to specify if reloading the player after prop changes will be handled manually.
51+
*
4852
*/
4953
constructor(props) {
5054
super(props);
5155
this.refNode = null;
5256
this.setRefNode = ref => {
5357
this.refNode = ref;
5458
};
59+
this.loadPlayer = this.loadPlayer.bind(this);
5560
}
5661

5762
/**
@@ -123,6 +128,7 @@ class ReactPlayerLoader extends React.Component {
123128
// Delete props that are not meant to be passed to player-loader.
124129
delete options.attrs;
125130
delete options.baseUrl;
131+
delete options.manualReloadFromPropChanges;
126132

127133
// If a base URL is provided, it should only apply to this player load.
128134
// This means we need to back up the original base URL and restore it
@@ -325,17 +331,32 @@ class ReactPlayerLoader extends React.Component {
325331
const previous = prevProps[key];
326332
const current = this.props[key];
327333

334+
// Do not compare functions
335+
if (typeof current === 'function') {
336+
return acc;
337+
}
338+
339+
if (typeof current === 'object' && current !== null) {
340+
if (JSON.stringify(current) !== JSON.stringify(previous)) {
341+
acc[key] = true;
342+
}
343+
344+
return acc;
345+
}
346+
328347
if (current !== previous) {
329348
acc[key] = true;
330349
}
331350

332351
return acc;
333352
}, {});
334353

335-
// Dispose and recreate the player if any changed keys cannot be handled.
336-
if (Object.keys(changes).some(k => UPDATEABLE_PROPS.indexOf(k) === -1)) {
337-
this.loadPlayer();
338-
return;
354+
if (!this.props.manualReloadFromPropChanges) {
355+
// Dispose and recreate the player if any changed keys cannot be handled.
356+
if (Object.keys(changes).some(k => UPDATEABLE_PROPS.indexOf(k) === -1)) {
357+
this.loadPlayer();
358+
return;
359+
}
339360
}
340361

341362
this.updatePlayer(changes);

test/index.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,66 @@ QUnit.module('ReactPlayerLoader', {
623623

624624
rerender = render(React.createElement(ReactPlayerLoader, props)).rerender;
625625
});
626+
627+
QUnit.test('loadPlayer() method reloads player', function(assert) {
628+
const done = assert.async(2);
629+
630+
const props = {
631+
accountId: '1',
632+
applicationId: 'foo',
633+
onSuccess: ({ref, type}) => {
634+
assert.ok(true, 'the success callback was called');
635+
done();
636+
}
637+
};
638+
639+
const reactPlayerLoader = ReactDOM.render(
640+
React.createElement(ReactPlayerLoader, props),
641+
this.fixture
642+
);
643+
644+
reactPlayerLoader.loadPlayer();
645+
});
646+
647+
QUnit.test('set manualReloadFromPropChanges to true', function(assert) {
648+
const done = assert.async(2);
649+
let rerender;
650+
const props = {
651+
accountId: '1',
652+
applicationId: 'foo',
653+
manualReloadFromPropChanges: true,
654+
onSuccess: ({ref, type}) => {
655+
if (props.applicationId !== 'bar') {
656+
props.applicationId = 'bar';
657+
done();
658+
}
659+
rerender(React.createElement(ReactPlayerLoader, props));
660+
assert.ok(true, 'the success callback was called');
661+
done();
662+
}
663+
};
664+
665+
rerender = render(React.createElement(ReactPlayerLoader, props)).rerender;
666+
});
667+
668+
QUnit.test('set manualReloadFromPropChanges to false', function(assert) {
669+
const done = assert.async(3);
670+
let rerender;
671+
const props = {
672+
accountId: '1',
673+
applicationId: 'foo',
674+
manualReloadFromPropChanges: false,
675+
onSuccess: ({ref, type}) => {
676+
if (props.applicationId !== 'bar') {
677+
props.applicationId = 'bar';
678+
done();
679+
}
680+
rerender(React.createElement(ReactPlayerLoader, props));
681+
assert.ok(true, 'the success callback was called');
682+
done();
683+
}
684+
};
685+
686+
rerender = render(React.createElement(ReactPlayerLoader, props)).rerender;
687+
});
626688
});

0 commit comments

Comments
 (0)