UNPKG

rsocket-rpc-core

Version:
183 lines (152 loc) 4.48 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _rsocketFlowable = require('rsocket-flowable'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Copyright (c) 2017-present, Netifi Inc. * * 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 * * http://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. * * */ var MAX_REQUEST_N = 0x7fffffff; // uint31 var SwitchTransformOperator = function () { function SwitchTransformOperator(initial, transformer) { _classCallCheck(this, SwitchTransformOperator); this._transformer = transformer; this._outer = initial; } SwitchTransformOperator.prototype.cancel = function cancel() { if (this._canceled) { return; } this._canceled = true; this._first = undefined; this._subscription.cancel(); }; SwitchTransformOperator.prototype.subscribe = function subscribe(actual) { if (actual && !this._inner) { this._inner = actual; this._inner.onSubscribe(this); } else if (actual) { var a = actual; if (a.onSubscribe) { a.onSubscribe({ cancel: function cancel() {}, request: function request() { if (a.onError) { a.onError(new Error('SwitchTransform allows only one Subscriber')); } } }); } } }; SwitchTransformOperator.prototype.onSubscribe = function onSubscribe(subscription) { if (this._subscription) { subscription.cancel(); return; } this._subscription = subscription; this._subscription.request(1); }; SwitchTransformOperator.prototype.onNext = function onNext(value) { var _this = this; if (this._canceled || this._done) { return; } if (!this._inner) { try { this._first = value; var result = this._transformer(value, new _rsocketFlowable.Flowable(function (s) { return _this.subscribe(s); })); result.subscribe(this._outer); } catch (e) { this.onError(e); } return; } this._inner.onNext(value); }; SwitchTransformOperator.prototype.onError = function onError(error) { var _this2 = this; if (this._canceled || this._done) { return; } this._error = error; this._done = true; if (this._inner) { if (!this._first) { this._inner.onError(error); } } else { this._outer.onSubscribe({ cancel: function cancel() {}, request: function request() { _this2._outer.onError(error); } }); } }; SwitchTransformOperator.prototype.onComplete = function onComplete() { var _this3 = this; if (this._done || this._canceled) { return; } this._done = true; if (this._inner) { if (!this._first) { this._inner.onComplete(); } } else { this._outer.onSubscribe({ cancel: function cancel() {}, request: function request() { _this3._outer.onComplete(); } }); } }; SwitchTransformOperator.prototype.request = function request(n) { if (this._first) { var f = this._first; this._first = undefined; this._inner.onNext(f); if (this._done) { if (this._error) { this._inner.onError(this._error); } else { this._inner.onComplete(); } } if (MAX_REQUEST_N <= n) { this._subscription.request(MAX_REQUEST_N); } else if (--n > 0) { this._subscription.request(n); } } else { this._subscription.request(n); } }; SwitchTransformOperator.prototype.map = function map(fn) { var _this4 = this; return new _rsocketFlowable.Flowable(function (subscriber) { return _this4.subscribe(subscriber); }).map(fn); }; return SwitchTransformOperator; }(); exports.default = SwitchTransformOperator;