UNPKG

@litert/televoke

Version:
105 lines 4.05 kB
"use strict"; /** * Copyright 2025 Angus.Fenying <fenying@litert.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TvBinaryStreamManager = void 0; exports.createChannelStreamManagerFactory = createChannelStreamManagerFactory; exports.createSharedStreamManagerFactory = createSharedStreamManagerFactory; exports.createDisabledStreamManagerFactory = createDisabledStreamManagerFactory; const Constants = require("./Constants"); const E = require("./Errors"); const BinaryStream_1 = require("./BinaryStream"); class TvBinaryStreamManager { constructor(defaultTimeout, maxStreams) { this.defaultTimeout = defaultTimeout; this.maxStreams = maxStreams; this._closed = false; this._streams = {}; } _generateStreamId() { do { const id = Math.floor(Math.random() * 4294967295); if (id in this._streams) { continue; } return id; } while (true); } close() { this._closed = true; for (const id of Object.keys(this._streams)) { this._streams[id].abort(); delete this._streams[id]; } } get(id) { return this._streams[id] ?? null; } create() { if (this.maxStreams === 0) { throw new E.errors.cmd_not_impl(); } else if (this._closed) { throw new E.errors.stream_closed(); } else if (this.maxStreams >= 0 && Object.keys(this._streams).length === this.maxStreams) { throw new E.errors.system_busy(); } const stream = new BinaryStream_1.TvBinaryReadStream(this._generateStreamId(), this.defaultTimeout); this._streams[stream.id] = stream.on('close', () => { delete this._streams[stream.id]; }); return stream; } } exports.TvBinaryStreamManager = TvBinaryStreamManager; /** * Create a factory function to create a `IStreamManager` instance, associated to a single channel. * When the associated channel is closed, all streams insides the stream manager will be aborted. * * @param defaultTimeout The default timeout value of each new binary stream. * @returns The factory function. */ function createChannelStreamManagerFactory(defaultTimeout = Constants.DEFAULT_STREAM_TIMEOUT, maxStreams = Constants.DEFAULT_MAX_STREAMS) { return (ch) => { const ret = new TvBinaryStreamManager(defaultTimeout, maxStreams); ch.on('close', () => { ret.close(); }); return ret; }; } /** * Create a factory function to create a `IStreamManager` instance, shared between all channels. * So that the streams created could be used in different channels. * * @param defaultTimeout The default timeout value of each new binary stream. * @returns The factory function. */ function createSharedStreamManagerFactory(defaultTimeout = Constants.DEFAULT_STREAM_TIMEOUT, maxStreams = Constants.DEFAULT_MAX_STREAMS) { const ret = new TvBinaryStreamManager(defaultTimeout, maxStreams); return () => ret; } /** * Create a factory function to create a `IStreamManager` instance, disabling all binary streams. * * > This is useful when you don't need binary streams. * * @returns The factory function. */ function createDisabledStreamManagerFactory() { const mgr = new TvBinaryStreamManager(0, 0); return () => mgr; } //# sourceMappingURL=BinaryStreamManager.js.map