Subtheme of barrio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1 lines
15 KiB

{"version":3,"file":"WebSocketSubject.js","sourceRoot":"","sources":["../../../src/observable/dom/WebSocketSubject.ts"],"names":[],"mappings":";;;;;;AAAA,wBAA0C,eAAe,CAAC,CAAA;AAC1D,2BAA2B,kBAAkB,CAAC,CAAA;AAC9C,2BAA2B,kBAAkB,CAAC,CAAA;AAC9C,6BAA6B,oBAAoB,CAAC,CAAA;AAElD,qBAAqB,iBAAiB,CAAC,CAAA;AACvC,8BAA8B,qBAAqB,CAAC,CAAA;AAEpD,yBAAyB,qBAAqB,CAAC,CAAA;AAC/C,4BAA4B,wBAAwB,CAAC,CAAA;AACrD,uBAAuB,mBAAmB,CAAC,CAAA;AAa3C;;;;GAIG;AACH;IAAyC,oCAAmB;IA2D1D,0BAAY,iBAAkE,EAAE,WAAyB;QACvG,EAAE,CAAC,CAAC,iBAAiB,YAAY,uBAAU,CAAC,CAAC,CAAC;YAC5C,kBAAM,WAAW,EAAkB,iBAAiB,CAAC,CAAC;QACxD,CAAC;QAAC,IAAI,CAAC,CAAC;YACN,iBAAO,CAAC;YACR,IAAI,CAAC,aAAa,GAAG,WAAI,CAAC,SAAS,CAAC;YACpC,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAO,EAAK,CAAC;YAChC,EAAE,CAAC,CAAC,OAAO,iBAAiB,KAAK,QAAQ,CAAC,CAAC,CAAC;gBAC1C,IAAI,CAAC,GAAG,GAAG,iBAAiB,CAAC;YAC/B,CAAC;YAAC,IAAI,CAAC,CAAC;gBACN,gEAAgE;gBAChE,eAAM,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;YAClC,CAAC;YACD,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC;gBACxB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;YAC3D,CAAC;YACD,IAAI,CAAC,WAAW,GA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AAC;oBACtC,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjB,CAAC;gBACD,KAAI,CAAC,WAAW,EAAE,CAAC;YACrB,CAAC;QACH,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,YAAY,CAAC;IACtB,CAAC;IAED,sCAAW,GAAX;QACE,IAAA,SAA+B,EAAvB,kBAAM,EAAE,kBAAM,CAAU;QAChC,EAAE,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,UAAU,KAAK,CAAC,CAAC,CAAC,CAAC;YACtC,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,WAAW,EAAE,CAAC;QACrB,CAAC;QACD,gBAAK,CAAC,WAAW,WAAE,CAAC;QACpB,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;YACZ,IAAI,CAAC,WAAW,GAAG,IAAI,6BAAa,EAAE,CAAC;QACzC,CAAC;IACH,CAAC;IACH,uBAAC;AAAD,CAAC,AA3PD,CAAyC,0BAAgB,GA2PxD;AA3PY,wBAAgB,mBA2P5B,CAAA","sourcesContent":["import { Subject, AnonymousSubject } from '../../Subject';\nimport { Subscriber } from '../../Subscriber';\nimport { Observable } from '../../Observable';\nimport { Subscription } from '../../Subscription';\nimport { Operator } from '../../Operator';\nimport { root } from '../../util/root';\nimport { ReplaySubject } from '../../ReplaySubject';\nimport { Observer, NextObserver } from '../../Observer';\nimport { tryCatch } from '../../util/tryCatch';\nimport { errorObject } from '../../util/errorObject';\nimport { assign } from '../../util/assign';\n\nexport interface WebSocketSubjectConfig {\n url: string;\n protocol?: string | Array<string>;\n resultSelector?: <T>(e: MessageEvent) => T;\n openObserver?: NextObserver<Event>;\n closeObserver?: NextObserver<CloseEvent>;\n closingObserver?: NextObserver<void>;\n WebSocketCtor?: { new(url: string, protocol?: string|Array<string>): WebSocket };\n binaryType?: 'blob' | 'arraybuffer';\n}\n\n/**\n * We need this JSDoc comment for affecting ESDoc.\n * @extends {Ignored}\n * @hide true\n */\nexport class WebSocketSubject<T> extends AnonymousSubject<T> {\n\n url: string;\n protocol: string|Array<string>;\n socket: WebSocket;\n openObserver: NextObserver<Event>;\n closeObserver: NextObserver<CloseEvent>;\n closingObserver: NextObserver<void>;\n WebSocketCtor: { new(url: string, protocol?: string|Array<string>): WebSocket };\n binaryType?: 'blob' | 'arraybuffer';\n\n private _output: Subject<T>;\n\n resultSelector(e: MessageEvent) {\n return JSON.parse(e.data);\n }\n\n /**\n * Wrapper around the w3c-compatible WebSocket object provided by the browser.\n *\n * @example <caption>Wraps browser WebSocket</caption>\n *\n * let socket$ = Observable.webSocket('ws://localhost:8081');\n *\n * socket$.subscribe(\n * (msg) => console.log('message received: ' + msg),\n * (err) => console.log(err),\n * () => console.log('complete')\n * );\n *\n * socket$.next(JSON.stringify({ op: 'hello' }));\n *\n * @example <caption>Wraps WebSocket from nodejs-websocket (using node.js)</caption>\n *\n * import { w3cwebsocket } from 'websocket';\n *\n * let socket$ = Observable.webSocket({\n * url: 'ws://localhost:8081',\n * WebSocketCtor: w3cwebsocket\n * });\n *\n * socket$.subscribe(\n * (msg) => console.log('message received: ' + msg),\n * (err) => console.log(err),\n * () => console.log('complete')\n * );\n *\n * socket$.next(JSON.stringify({ op: 'hello' }));\n *\n * @param {string | WebSocketSubjectConfig} urlConfigOrSource the source of the websocket as an url or a structure defining the websocket object\n * @return {WebSocketSubject}\n * @static true\n * @name webSocket\n * @owner Observable\n */\n static create<T>(urlConfigOrSource: string | WebSocketSubjectConfig): WebSocketSubject<T> {\n return new WebSocketSubject<T>(urlConfigOrSource);\n }\n\n constructor(urlConfigOrSource: string | WebSocketSubjectConfig | Observable<T>, destination?: Observer<T>) {\n if (urlConfigOrSource instanceof Observable) {\n super(destination, <Observable<T>> urlConfigOrSource);\n } else {\n super();\n this.WebSocketCtor = root.WebSocket;\n this._output = new Subject<T>();\n if (typeof urlConfigOrSource === 'string') {\n this.url = urlConfigOrSource;\n } else {\n // WARNING: config object could override important members here.\n assign(this, urlConfigOrSource);\n }\n if (!this.WebSocketCtor) {\n throw new Error('no WebSocket constructor can be found');\n }\n this.destination = new ReplaySubject();\n }\n }\n\n lift<R>(operator: Operator<T, R>): WebSocketSubject<R> {\n const sock = new WebSocketSubject<R>(this, <any> this.destination);\n sock.operator = operator;\n return sock;\n }\n\n private _resetState() {\n this.socket = null;\n if (!this.source) {\n this.destination = new ReplaySubject();\n }\n this._output = new Subject<T>();\n }\n\n // TODO: factor this out to be a proper Operator/Subscriber implementation and eliminate closures\n multiplex(subMsg: () => any, unsubMsg: () => any, messageFilter: (value: T) => boolean) {\n const self = this;\n return new Observable((observer: Observer<any>) => {\n const result = tryCatch(subMsg)();\n if (result === errorObject) {\n observer.error(errorObject.e);\n } else {\n self.next(result);\n }\n\n let subscription = self.subscribe(x => {\n const result = tryCatch(messageFilter)(x);\n if (result === errorObject) {\n observer.error(errorObject.e);\n } else if (result) {\n observer.next(x);\n }\n },\n err => observer.error(err),\n () => observer.complete());\n\n return () => {\n const result = tryCatch(unsubMsg)();\n if (result === errorObject) {\n observer.error(errorObject.e);\n } else {\n self.next(result);\n }\n subscription.unsubscribe();\n };\n });\n }\n\n private _connectSocket() {\n const { WebSocketCtor } = this;\n const observer = this._output;\n\n let socket: WebSocket = null;\n try {\n socket = this.protocol ?\n new WebSocketCtor(this.url, this.protocol) :\n new WebSocketCtor(this.url);\n this.socket = socket;\n if (this.binaryType) {\n this.socket.binaryType = this.binaryType;\n }\n } catch (e) {\n observer.error(e);\n return;\n }\n\n const subscription = new Subscription(() => {\n this.socket = null;\n if (socket && socket.readyState === 1) {\n socket.close();\n }\n });\n\n socket.onopen = (e: Event) => {\n const openObserver = this.openObserver;\n if (openObserver) {\n openObserver.next(e);\n }\n\n const queue = this.destination;\n\n this.destination = Subscriber.create(\n (x) => socket.readyState === 1 && socket.send(x),\n (e) => {\n const closingObserver = this.closingObserver;\n if (closingObserver) {\n closingObserver.next(undefined);\n }\n if (e && e.code) {\n socket.close(e.code, e.reason);\n } else {\n observer.error(new TypeError('WebSocketSubject.error must be called with an object with an error code, ' +\n 'and an optional reason: { code: number, reason: string }'));\n }\n this._resetState();\n },\n ( ) => {\n const closingObserver = this.closingObserver;\n if (closingObserver) {\n closingObserver.next(undefined);\n }\n socket.close();\n this._resetState();\n }\n );\n\n if (queue && queue instanceof ReplaySubject) {\n subscription.add((<ReplaySubject<T>>queue).subscribe(this.destination));\n }\n };\n\n socket.onerror = (e: Event) => {\n this._resetState();\n observer.error(e);\n };\n\n socket.onclose = (e: CloseEvent) => {\n this._resetState();\n const closeObserver = this.closeObserver;\n if (closeObserver) {\n closeObserver.next(e);\n }\n if (e.wasClean) {\n observer.complete();\n } else {\n observer.error(e);\n }\n };\n\n socket.onmessage = (e: MessageEvent) => {\n const result = tryCatch(this.resultSelector)(e);\n if (result === errorObject) {\n observer.error(errorObject.e);\n } else {\n observer.next(result);\n }\n };\n }\n\n /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<T>): Subscription {\n const { source } = this;\n if (source) {\n return source.subscribe(subscriber);\n }\n if (!this.socket) {\n this._connectSocket();\n }\n let subscription = new Subscription();\n subscription.add(this._output.subscribe(subscriber));\n subscription.add(() => {\n const { socket } = this;\n if (this._output.observers.length === 0) {\n if (socket && socket.readyState === 1) {\n socket.close();\n }\n this._resetState();\n }\n });\n return subscription;\n }\n\n unsubscribe() {\n const { source, socket } = this;\n if (socket && socket.readyState === 1) {\n socket.close();\n this._resetState();\n }\n super.unsubscribe();\n if (!source) {\n this.destination = new ReplaySubject();\n }\n }\n}\n"]}