Skip to content

Commit

Permalink
feat(FEC-8153): save reference to player instances and expose API to …
Browse files Browse the repository at this point in the history
…get them (#174)

Save reference to each instantiated player.
Add methods to get all players or a player by ID.
Make sure to delete the reference upon destroying a player.

will add tests once review is done.
still deliberating if to add this code in its own module. doing so will add some boilerplate code of module management which i'm not sure is worth the addition, i.e. need to add register player and delete player methods....
  • Loading branch information
OrenMe authored Nov 14, 2018
1 parent d74dd69 commit 764849b
Show file tree
Hide file tree
Showing 9 changed files with 775 additions and 706 deletions.
Binary file removed .DS_Store
Binary file not shown.
14 changes: 7 additions & 7 deletions dist/kaltura-ovp-player.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kaltura-ovp-player.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions dist/kaltura-tv-player.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kaltura-tv-player.js.map

Large diffs are not rendered by default.

1,407 changes: 719 additions & 688 deletions docs/api.md

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions flow-typed/types/kaltura-players.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {KalturaPlayer} from '../../src/kaltura-player';

/**
* @type {Object.<string, KalturaPlayer>}
* @name KalturaPlayers
* @description a map of player instances by player ids
*/
type _KalturaPlayers = {[id: string]: KalturaPlayer};
declare type KalturaPlayers = _KalturaPlayers;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '@playkit-js/playkit-js-kava';
import * as shaka from 'shaka-player';
// Import setup method
import {setup} from './setup';
export {getPlayers, getPlayer} from './proxy';
// Import cast framework
import {cast} from './common/cast';
// Import playlist
Expand Down
32 changes: 30 additions & 2 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@
import {KalturaPlayer} from './kaltura-player';
import {FakeEventTarget} from '@playkit-js/playkit-js';

const Players: KalturaPlayers = {};
/**
* get all instantiated players
* @returns {KalturaPlayers} - map of player ids and their respective instantiated player
*/
function getPlayers(): KalturaPlayers {
return Players;
}

/**
* get a player instance by id
* @param {string} id - the player ID
* @returns {KalturaPlayer | null} - the player if found by the supplied ID or null if key doesn't exist
*/
function getPlayer(id: string): ?KalturaPlayer {
if (Players[id]) {
return Players[id];
}
return null;
}

const proxyIgnoredProps: Array<string> = ['_remotePlayer', '_listeners', '_uiWrapper'];
const proxyHandler: Object = {
get(kp: KalturaPlayer, prop: string) {
if (prop === 'destroy') {
const playerId = kp.config.targetId;
delete Players[playerId];
}

if (prop in FakeEventTarget.prototype || proxyIgnoredProps.includes(prop)) {
// $FlowFixMe
return kp[prop];
Expand All @@ -30,7 +56,9 @@ const proxyHandler: Object = {

const getPlayerProxy = (options: KPOptionsObject) => {
const player = new KalturaPlayer(options);
return new Proxy(player, proxyHandler);
const proxy = new Proxy(player, proxyHandler);
Players[options.targetId] = proxy;
return proxy;
};

export {getPlayerProxy};
export {getPlayerProxy, getPlayer, getPlayers};

0 comments on commit 764849b

Please sign in to comment.