stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
99 lines • 2.92 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AudioPlayerPool = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _streamChat = require("stream-chat");
var _audioPlayer = require("./audio-player");
var AudioPlayerPool = exports.AudioPlayerPool = function () {
function AudioPlayerPool({
allowConcurrentAudioPlayback
}) {
(0, _classCallCheck2.default)(this, AudioPlayerPool);
this.state = new _streamChat.StateStore({
activeAudioPlayer: null
});
this.pool = new Map();
this.allowConcurrentAudioPlayback = allowConcurrentAudioPlayback ?? false;
}
return (0, _createClass2.default)(AudioPlayerPool, [{
key: "players",
get: function () {
return Array.from(this.pool.values());
}
}, {
key: "getOrAddPlayer",
value: function getOrAddPlayer(params) {
var player = this.pool.get(params.id);
if (player) {
player.setOnError(params.onError);
return player;
}
var newPlayer = new _audioPlayer.AudioPlayer(params);
newPlayer.pool = this;
this.pool.set(params.id, newPlayer);
return newPlayer;
}
}, {
key: "setActivePlayer",
value: function setActivePlayer(activeAudioPlayer) {
this.state.partialNext({
activeAudioPlayer
});
}
}, {
key: "getActivePlayer",
value: function getActivePlayer() {
return this.state.getLatestValue().activeAudioPlayer;
}
}, {
key: "removePlayer",
value: function removePlayer(id) {
var player = this.pool.get(id);
if (!player) return;
player.onRemove();
this.pool.delete(id);
if (this.getActivePlayer()?.id === id) {
this.setActivePlayer(null);
}
}
}, {
key: "deregister",
value: function deregister(id) {
if (this.pool.has(id)) {
this.pool.delete(id);
}
}
}, {
key: "clear",
value: function clear() {
for (var player of this.pool.values()) {
this.removePlayer(player.id);
}
this.setActivePlayer(null);
}
}, {
key: "requestPlay",
value: function requestPlay(id) {
if (this.allowConcurrentAudioPlayback) return;
if (this.getActivePlayer()?.id !== id) {
var currentPlayer = this.getActivePlayer();
if (currentPlayer && currentPlayer.isPlaying) {
currentPlayer.pause();
}
}
var activePlayer = this.pool.get(id);
if (activePlayer) {
this.setActivePlayer(activePlayer);
}
}
}, {
key: "notifyPaused",
value: function notifyPaused() {
this.setActivePlayer(null);
}
}]);
}();
//# sourceMappingURL=audio-player-pool.js.map