UNPKG

@lightningkite/ktor-batteries

Version:
187 lines 9.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.multiplexedSocket = exports.multiplexedSocketReified = exports.WebSocketIsh = exports.MultiplexedWebsocketPart = exports.sharedSocket = exports.set_overrideWebSocketProvider = exports.get_overrideWebSocketProvider = exports.__overrideWebSocketProvider = exports.setSharedSocketShouldBeActive = exports.getSharedSocketShouldBeActive = exports._sharedSocketShouldBeActive = void 0; // Package: com.lightningkite.ktordb.live // Generated by Khrysalis - this file will be overwritten. const MultiplexMessage_1 = require("../db/MultiplexMessage"); const khrysalis_runtime_1 = require("@lightningkite/khrysalis-runtime"); const rxjs_plus_1 = require("@lightningkite/rxjs-plus"); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const uuid_1 = require("uuid"); //! Declares com.lightningkite.ktordb.live.sharedSocketShouldBeActive exports._sharedSocketShouldBeActive = (0, rxjs_1.of)(true); function getSharedSocketShouldBeActive() { return exports._sharedSocketShouldBeActive; } exports.getSharedSocketShouldBeActive = getSharedSocketShouldBeActive; function setSharedSocketShouldBeActive(value) { exports._sharedSocketShouldBeActive = value; } exports.setSharedSocketShouldBeActive = setSharedSocketShouldBeActive; let retryTime = 1000; let lastRetry = 0; //! Declares com.lightningkite.ktordb.live._overrideWebSocketProvider exports.__overrideWebSocketProvider = null; function get_overrideWebSocketProvider() { return exports.__overrideWebSocketProvider; } exports.get_overrideWebSocketProvider = get_overrideWebSocketProvider; function set_overrideWebSocketProvider(value) { exports.__overrideWebSocketProvider = value; } exports.set_overrideWebSocketProvider = set_overrideWebSocketProvider; const sharedSocketCache = new Map(); //! Declares com.lightningkite.ktordb.live.sharedSocket function sharedSocket(url) { return (0, khrysalis_runtime_1.xMutableMapGetOrPut)(sharedSocketCache, url, () => (getSharedSocketShouldBeActive() .pipe((0, operators_1.distinctUntilChanged)(khrysalis_runtime_1.safeEq)) .pipe((0, operators_1.switchMap)((it) => { const shortUrl = (0, khrysalis_runtime_1.xStringSubstringBefore)(url, '?', undefined); return (() => { var _a; if ((!it)) { return rxjs_1.NEVER; } else { console.log(`Creating socket to ${url}`); return ((_a = (0, khrysalis_runtime_1.runOrNull)(get_overrideWebSocketProvider(), _ => _(url))) !== null && _a !== void 0 ? _a : rxjs_plus_1.HttpClient.INSTANCE.webSocket(url)) .pipe((0, operators_1.switchMap)((it) => { lastRetry = Date.now(); // println("Connection to $shortUrl established, starting pings") // Only have this observable until it fails const pingMessages = (0, rxjs_1.interval)(30000) .pipe((0, operators_1.map)((_0) => { // println("Sending ping to $url") return it.write.next({ text: " ", binary: null }); })).pipe((0, operators_1.switchMap)((it) => (rxjs_1.NEVER))); const timeoutAfterSeconds = it.read .pipe((0, operators_1.tap)((it) => { // println("Got message from $shortUrl: ${it}") if (Date.now() > lastRetry + 60000) { retryTime = 1000; } })) .pipe((0, operators_1.timeout)(40000)) .pipe((0, operators_1.switchMap)((it) => (rxjs_1.NEVER))); return (0, rxjs_1.merge)((0, rxjs_1.of)(it), pingMessages, timeoutAfterSeconds); })) .pipe((0, operators_1.tap)(undefined, (it) => { console.log(`Socket to ${shortUrl} FAILED with ${it}`); })) .pipe((0, operators_1.retryWhen)((it) => { const temp = retryTime; retryTime = temp * 2; return it.pipe((0, operators_1.delay)(temp)); })) .pipe((0, operators_1.tap)({ unsubscribe: () => { console.log(`Disconnecting socket to ${shortUrl}`); } })); } })(); })) .pipe((0, operators_1.publishReplay)(1)) .pipe((0, operators_1.refCount)()))); } exports.sharedSocket = sharedSocket; //! Declares com.lightningkite.ktordb.live.MultiplexedWebsocketPart class MultiplexedWebsocketPart { constructor(messages, send) { this.messages = messages; this.send = send; } } exports.MultiplexedWebsocketPart = MultiplexedWebsocketPart; //! Declares com.lightningkite.ktordb.live.WebSocketIsh class WebSocketIsh { constructor(messages, send) { this.messages = messages; this.send = send; } } exports.WebSocketIsh = WebSocketIsh; //! Declares com.lightningkite.ktordb.live.multiplexedSocket function multiplexedSocketReified(IN, OUT, url, path) { return multiplexedSocket(url, path, IN, OUT); } exports.multiplexedSocketReified = multiplexedSocketReified; //! Declares com.lightningkite.ktordb.live.multiplexedSocket function multiplexedSocket(url, path, inType, outType) { const shortUrl = (0, khrysalis_runtime_1.xStringSubstringBefore)(url, '?', undefined); const channel = (0, uuid_1.v4)(); return sharedSocket(url) .pipe((0, operators_1.switchMap)((sharedSocket) => { // println("Setting up channel $channel to $shortUrl with $path") const multiplexedIn = sharedSocket.read.pipe((0, rxjs_1.map)((it) => { const text = it.text; if (text === null) { return null; } if ((0, khrysalis_runtime_1.xCharSequenceIsBlank)(text)) { return null; } return rxjs_plus_1.JSON2.parse(text, [MultiplexMessage_1.MultiplexMessage]); }), (0, rxjs_1.filter)(rxjs_plus_1.isNonNull)) .pipe((0, operators_1.filter)((it) => (it.channel === channel))); let current = new rxjs_1.Subject(); return multiplexedIn .pipe((0, rxjs_1.map)((message) => ((() => { if (message.start) { // println("Channel ${message.channel} established with $sharedSocket") return new WebSocketIsh(current, (message) => { // println("Sending $message to $channel") sharedSocket.write.next({ text: JSON.stringify(new MultiplexMessage_1.MultiplexMessage(channel, undefined, undefined, undefined, JSON.stringify(message))), binary: null }); }); } else if (message.data !== null) { // console.log("Got ${message.data} to ${message.channel}") const temp53 = message.data; if (temp53 === null || temp53 === undefined) { return null; } current.next(rxjs_plus_1.JSON2.parse(temp53, inType)); return null; } else if (message.end) { // println("Channel ${message.channel} terminated") current = new rxjs_1.Subject(); sharedSocket.write.next({ text: JSON.stringify(new MultiplexMessage_1.MultiplexMessage(channel, path, true, undefined, undefined)), binary: null }); return null; } else { return null; } })())), (0, rxjs_1.filter)(rxjs_plus_1.isNonNull)) .pipe((0, rxjs_plus_1.doOnSubscribe)((_0) => { // println("Sending onSubscribe Startup Message") sharedSocket.write.next({ text: JSON.stringify(new MultiplexMessage_1.MultiplexMessage(channel, path, true, undefined, undefined)), binary: null }); })) .pipe((0, operators_1.tap)({ unsubscribe: () => { // println("Disconnecting channel on socket to $shortUrl with $path") sharedSocket.write.next({ text: JSON.stringify(new MultiplexMessage_1.MultiplexMessage(channel, path, undefined, true, undefined)), binary: null }); } })) .pipe((0, operators_1.retryWhen)((it) => { const temp = retryTime; retryTime = temp * 2; return it.pipe((0, operators_1.delay)(temp)); })); })); } exports.multiplexedSocket = multiplexedSocket; //# sourceMappingURL=sockets.js.map