stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
83 lines • 3.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AudioPlayerPool = void 0;
var _streamChat = require("stream-chat");
var _audioPlayer = require("./audio-player");
function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
class AudioPlayerPool {
state = new _streamChat.StateStore({
activeAudioPlayer: null
});
constructor({
allowConcurrentAudioPlayback
}) {
this.pool = new Map();
this.allowConcurrentAudioPlayback = allowConcurrentAudioPlayback != null ? allowConcurrentAudioPlayback : false;
}
get players() {
return Array.from(this.pool.values());
}
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;
}
setActivePlayer(activeAudioPlayer) {
this.state.partialNext({
activeAudioPlayer
});
}
getActivePlayer() {
return this.state.getLatestValue().activeAudioPlayer;
}
removePlayer(id) {
var _this$getActivePlayer;
var player = this.pool.get(id);
if (!player) return;
player.onRemove();
this.pool.delete(id);
if (((_this$getActivePlayer = this.getActivePlayer()) == null ? void 0 : _this$getActivePlayer.id) === id) {
this.setActivePlayer(null);
}
}
deregister(id) {
if (this.pool.has(id)) {
this.pool.delete(id);
}
}
clear() {
for (var _iterator = _createForOfIteratorHelperLoose(this.pool.values()), _step; !(_step = _iterator()).done;) {
var player = _step.value;
this.removePlayer(player.id);
}
this.setActivePlayer(null);
}
requestPlay(id) {
var _this$getActivePlayer2;
if (this.allowConcurrentAudioPlayback) return;
if (((_this$getActivePlayer2 = this.getActivePlayer()) == null ? void 0 : _this$getActivePlayer2.id) !== id) {
var currentPlayer = this.getActivePlayer();
if (currentPlayer && currentPlayer.isPlaying) {
currentPlayer.pause();
}
}
var activePlayer = this.pool.get(id);
if (activePlayer) {
this.setActivePlayer(activePlayer);
}
}
notifyPaused() {
this.setActivePlayer(null);
}
}
exports.AudioPlayerPool = AudioPlayerPool;
//# sourceMappingURL=audio-player-pool.js.map