UNPKG

angular-t9n

Version:

A translation tool for Angular i18n

2 lines (1 loc) 3.3 kB
import{I as g,O as E,X as O,_ as W,a as k,b as y,d as S,g as d,h as w,i as j,j as p,z as x}from"./chunk-IRWV2DJQ.js";var v={url:"",deserializer:c=>JSON.parse(c.data),serializer:c=>JSON.stringify(c)},C="WebSocketSubject.error must be called with an object with an error code, and an optional reason: { code: number, reason: string }",l=class c extends w{constructor(t,i){if(super(),this._socket=null,t instanceof S)this.destination=i,this.source=t;else{let e=this._config=Object.assign({},v);if(this._output=new d,typeof t=="string")e.url=t;else for(let r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);if(!e.WebSocketCtor&&WebSocket)e.WebSocketCtor=WebSocket;else if(!e.WebSocketCtor)throw new Error("no WebSocket constructor can be found");this.destination=new p}}lift(t){let i=new c(this._config,this.destination);return i.operator=t,i.source=this,i}_resetState(){this._socket=null,this.source||(this.destination=new p),this._output=new d}multiplex(t,i,e){let r=this;return new S(s=>{try{r.next(t())}catch(a){s.error(a)}let o=r.subscribe({next:a=>{try{e(a)&&s.next(a)}catch(n){s.error(n)}},error:a=>s.error(a),complete:()=>s.complete()});return()=>{try{r.next(i())}catch(a){s.error(a)}o.unsubscribe()}})}_connectSocket(){let{WebSocketCtor:t,protocol:i,url:e,binaryType:r}=this._config,s=this._output,o=null;try{o=i?new t(e,i):new t(e),this._socket=o,r&&(this._socket.binaryType=r)}catch(n){s.error(n);return}let a=new k(()=>{this._socket=null,o&&o.readyState===1&&o.close()});o.onopen=n=>{let{_socket:u}=this;if(!u){o.close(),this._resetState();return}let{openObserver:m}=this._config;m&&m.next(n);let b=this.destination;this.destination=y.create(h=>{if(o.readyState===1)try{let{serializer:f}=this._config;o.send(f(h))}catch(f){this.destination.error(f)}},h=>{let{closingObserver:f}=this._config;f&&f.next(void 0),h&&h.code?o.close(h.code,h.reason):s.error(new TypeError(C)),this._resetState()},()=>{let{closingObserver:h}=this._config;h&&h.next(void 0),o.close(),this._resetState()}),b&&b instanceof p&&a.add(b.subscribe(this.destination))},o.onerror=n=>{this._resetState(),s.error(n)},o.onclose=n=>{o===this._socket&&this._resetState();let{closeObserver:u}=this._config;u&&u.next(n),n.wasClean?s.complete():s.error(n)},o.onmessage=n=>{try{let{deserializer:u}=this._config;s.next(u(n))}catch(u){s.error(u)}}}_subscribe(t){let{source:i}=this;return i?i.subscribe(t):(this._socket||this._connectSocket(),this._output.subscribe(t),t.add(()=>{let{_socket:e}=this;this._output.observers.length===0&&(e&&(e.readyState===1||e.readyState===0)&&e.close(),this._resetState())}),t)}unsubscribe(){let{_socket:t}=this;t&&(t.readyState===1||t.readyState===0)&&t.close(),this._resetState(),super.unsubscribe()}};function _(c){return new l(c)}var T={production:!0,translationServer:"",translationSocket:`ws://${location.host}`};var $=(()=>{let t=class t{constructor(){this._projectSubject=new j(null),this.project=this._projectSubject.pipe(g((e,r)=>JSON.stringify(e)===JSON.stringify(r))),this.projectChange=this.project.pipe(x(e=>!!e)),_(T.translationSocket).pipe(O({error:()=>this._projectSubject.next(null)}),E({delay:1e3})).subscribe(e=>this._projectSubject.next(e))}};t.\u0275fac=function(r){return new(r||t)},t.\u0275prov=W({token:t,factory:t.\u0275fac,providedIn:"root"});let c=t;return c})();export{T as a,$ as b};