@univerjs/rpc 0.1.0-alpha.2 → 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/cjs/index.js +1 -3
- package/lib/es/index.js +254 -1047
- package/lib/types/controllers/data-sync/data-sync-primary.controller.d.ts +2 -2
- package/lib/types/index.d.ts +1 -0
- package/lib/types/plugin.d.ts +0 -1
- package/lib/types/services/remote-instance/remote-instance.service.d.ts +7 -4
- package/lib/types/services/rpc/rpc.service.d.ts +1 -1
- package/lib/umd/index.js +1 -3
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @univerjs/rpc
|
|
2
2
|
|
|
3
|
-
[](https://npmjs.org/
|
|
3
|
+
[](https://npmjs.org/package/@univerjs/rpc)
|
|
4
4
|
[](https://img.shields.io/npm/l/@univerjs/rpc)
|
|
5
5
|
|
|
6
6
|
## Introduction
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`+e.map(function(n,s){return s+1+") "+n.toString()}).join(`
|
|
3
|
-
`):"",this.name="UnsubscriptionError",this.errors=e}});function ee(t,r){if(t){var e=t.indexOf(r);0<=e&&t.splice(e,1)}}var J=function(){function t(r){this.initialTeardown=r,this.closed=!1,this._parentage=null,this._finalizers=null}return t.prototype.unsubscribe=function(){var r,e,n,s,i;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var a=R(o),u=a.next();!u.done;u=a.next()){var c=u.value;c.remove(this)}}catch(d){r={error:d}}finally{try{u&&!u.done&&(e=a.return)&&e.call(a)}finally{if(r)throw r.error}}else o.remove(this);var l=this.initialTeardown;if(p(l))try{l()}catch(d){i=d instanceof A?d.errors:[d]}var b=this._finalizers;if(b){this._finalizers=null;try{for(var f=R(b),v=f.next();!v.done;v=f.next()){var g=v.value;try{te(g)}catch(d){i=i??[],d instanceof A?i=M(M([],O(i)),O(d.errors)):i.push(d)}}}catch(d){n={error:d}}finally{try{v&&!v.done&&(s=f.return)&&s.call(f)}finally{if(n)throw n.error}}}if(i)throw new A(i)}},t.prototype.add=function(r){var e;if(r&&r!==this)if(this.closed)te(r);else{if(r instanceof t){if(r.closed||r._hasParent(this))return;r._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(r)}},t.prototype._hasParent=function(r){var e=this._parentage;return e===r||Array.isArray(e)&&e.includes(r)},t.prototype._addParent=function(r){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(r),e):e?[e,r]:r},t.prototype._removeParent=function(r){var e=this._parentage;e===r?this._parentage=null:Array.isArray(e)&&ee(e,r)},t.prototype.remove=function(r){var e=this._finalizers;e&&ee(e,r),r instanceof t&&r._removeParent(this)},t.EMPTY=function(){var r=new t;return r.closed=!0,r}(),t}();J.EMPTY;function ie(t){return t instanceof J||t&&"closed"in t&&p(t.remove)&&p(t.add)&&p(t.unsubscribe)}function te(t){p(t)?t():t.unsubscribe()}var se={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},W={setTimeout:function(t,r){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];var s=W.delegate;return s!=null&&s.setTimeout?s.setTimeout.apply(s,M([t,r],O(e))):setTimeout.apply(void 0,M([t,r],O(e)))},clearTimeout:function(t){var r=W.delegate;return((r==null?void 0:r.clearTimeout)||clearTimeout)(t)},delegate:void 0};function oe(t){W.setTimeout(function(){throw t})}function F(){}function Ce(t){t()}var Q=function(t){B(r,t);function r(e){var n=t.call(this)||this;return n.isStopped=!1,e?(n.destination=e,ie(e)&&e.add(n)):n.destination=Te,n}return r.create=function(e,n,s){return new H(e,n,s)},r.prototype.next=function(e){this.isStopped||this._next(e)},r.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},r.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},r.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,t.prototype.unsubscribe.call(this),this.destination=null)},r.prototype._next=function(e){this.destination.next(e)},r.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},r.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},r}(J),Re=Function.prototype.bind;function D(t,r){return Re.call(t,r)}var Ee=function(){function t(r){this.partialObserver=r}return t.prototype.next=function(r){var e=this.partialObserver;if(e.next)try{e.next(r)}catch(n){x(n)}},t.prototype.error=function(r){var e=this.partialObserver;if(e.error)try{e.error(r)}catch(n){x(n)}else x(r)},t.prototype.complete=function(){var r=this.partialObserver;if(r.complete)try{r.complete()}catch(e){x(e)}},t}(),H=function(t){B(r,t);function r(e,n,s){var i=t.call(this)||this,o;if(p(e)||!e)o={next:e??void 0,error:n??void 0,complete:s??void 0};else{var a;i&&se.useDeprecatedNextContext?(a=Object.create(e),a.unsubscribe=function(){return i.unsubscribe()},o={next:e.next&&D(e.next,a),error:e.error&&D(e.error,a),complete:e.complete&&D(e.complete,a)}):o=e}return i.destination=new Ee(o),i}return r}(Q);function x(t){oe(t)}function xe(t){throw t}var Te={closed:!0,next:F,error:xe,complete:F},X=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Oe(t){return t}function Me(t){return t.length===0?Oe:t.length===1?t[0]:function(e){return t.reduce(function(n,s){return s(n)},e)}}var S=function(){function t(r){r&&(this._subscribe=r)}return t.prototype.lift=function(r){var e=new t;return e.source=this,e.operator=r,e},t.prototype.subscribe=function(r,e,n){var s=this,i=Ue(r)?r:new H(r,e,n);return Ce(function(){var o=s,a=o.operator,u=o.source;i.add(a?a.call(i,u):u?s._subscribe(i):s._trySubscribe(i))}),i},t.prototype._trySubscribe=function(r){try{return this._subscribe(r)}catch(e){r.error(e)}},t.prototype.forEach=function(r,e){var n=this;return e=re(e),new e(function(s,i){var o=new H({next:function(a){try{r(a)}catch(u){i(u),o.unsubscribe()}},error:i,complete:s});n.subscribe(o)})},t.prototype._subscribe=function(r){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(r)},t.prototype[X]=function(){return this},t.prototype.pipe=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return Me(r)(this)},t.prototype.toPromise=function(r){var e=this;return r=re(r),new r(function(n,s){var i;e.subscribe(function(o){return i=o},function(o){return s(o)},function(){return n(i)})})},t.create=function(r){return new t(r)},t}();function re(t){var r;return(r=t??se.Promise)!==null&&r!==void 0?r:Promise}function qe(t){return t&&p(t.next)&&p(t.error)&&p(t.complete)}function Ue(t){return t&&t instanceof Q||qe(t)&&ie(t)}function je(t){return p(t[X])}function Ae(t){return Symbol.asyncIterator&&p(t==null?void 0:t[Symbol.asyncIterator])}function De(t){return new TypeError("You provided "+(t!==null&&typeof t=="object"?"an invalid object":"'"+t+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function $e(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Le=$e();function ke(t){return p(t==null?void 0:t[Le])}function We(t){return Se(this,arguments,function(){var e,n,s,i;return ne(this,function(o){switch(o.label){case 0:e=t.getReader(),o.label=1;case 1:o.trys.push([1,,9,10]),o.label=2;case 2:return[4,w(e.read())];case 3:return n=o.sent(),s=n.value,i=n.done,i?[4,w(void 0)]:[3,5];case 4:return[2,o.sent()];case 5:return[4,w(s)];case 6:return[4,o.sent()];case 7:return o.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function Fe(t){return p(t==null?void 0:t.getReader)}function He(t){if(t instanceof S)return t;if(t!=null){if(je(t))return Ge(t);if(ge(t))return Ye(t);if(Pe(t))return Ve(t);if(Ae(t))return ce(t);if(ke(t))return ze(t);if(Fe(t))return Be(t)}throw De(t)}function Ge(t){return new S(function(r){var e=t[X]();if(p(e.subscribe))return e.subscribe(r);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function Ye(t){return new S(function(r){for(var e=0;e<t.length&&!r.closed;e++)r.next(t[e]);r.complete()})}function Ve(t){return new S(function(r){t.then(function(e){r.closed||(r.next(e),r.complete())},function(e){return r.error(e)}).then(null,oe)})}function ze(t){return new S(function(r){var e,n;try{for(var s=R(t),i=s.next();!i.done;i=s.next()){var o=i.value;if(r.next(o),r.closed)return}}catch(a){e={error:a}}finally{try{i&&!i.done&&(n=s.return)&&n.call(s)}finally{if(e)throw e.error}}r.complete()})}function ce(t){return new S(function(r){Je(t,r).catch(function(e){return r.error(e)})})}function Be(t){return ce(We(t))}function Je(t,r){var e,n,s,i;return _e(this,void 0,void 0,function(){var o,a;return ne(this,function(u){switch(u.label){case 0:u.trys.push([0,5,6,11]),e=we(t),u.label=1;case 1:return[4,e.next()];case 2:if(n=u.sent(),!!n.done)return[3,4];if(o=n.value,r.next(o),r.closed)return[2];u.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return a=u.sent(),s={error:a},[3,11];case 6:return u.trys.push([6,,9,10]),n&&!n.done&&(i=e.return)?[4,i.call(e)]:[3,8];case 7:u.sent(),u.label=8;case 8:return[3,10];case 9:if(s)throw s.error;return[7];case 10:return[7];case 11:return r.complete(),[2]}})})}function Z(t,r,e,n,s){return new Qe(t,r,e,n,s)}var Qe=function(t){B(r,t);function r(e,n,s,i,o,a){var u=t.call(this,e)||this;return u.onFinalize=o,u.shouldUnsubscribe=a,u._next=n?function(c){try{n(c)}catch(l){e.error(l)}}:t.prototype._next,u._error=i?function(c){try{i(c)}catch(l){e.error(l)}finally{this.unsubscribe()}}:t.prototype._error,u._complete=s?function(){try{s()}catch(c){e.error(c)}finally{this.unsubscribe()}}:t.prototype._complete,u}return r.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var n=this.closed;t.prototype.unsubscribe.call(this),!n&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},r}(Q),Xe=new S(function(t){return t.complete()});function Ze(t){return t<=0?function(){return Xe}:z(function(r,e){var n=0;r.subscribe(Z(e,function(s){++n<=t&&(e.next(s),t<=n&&e.complete())}))})}function Ke(t,r){return z(function(e,n){var s=0;e.subscribe(Z(n,function(i){return t.call(r,i,s++)&&n.next(i)}))})}function q(t){return z(function(r,e){He(t).subscribe(Z(e,function(){return e.complete()},F)),!e.closed&&r.subscribe(e)})}var Ne=Object.defineProperty,et=Object.getOwnPropertyDescriptor,ae=(t,r,e,n)=>{for(var s=n>1?void 0:n?et(r,e):r,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(n?o(r,e,s):o(s))||s);return n&&s&&Ne(r,e,s),s},G=(t,r)=>(e,n)=>r(e,n,t);const K="univer.remote-sync-service",U=m.createIdentifier(K);let Y=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{local:!0,fromSync:!0})}};Y=ae([G(0,h.ICommandService)],Y);const N="univer.remote-instance-service",j=m.createIdentifier(N);let V=class{constructor(t,r){this._univerInstanceService=t,this._commandService=r}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{local:!0,fromSync:!0})}async createInstance(t){const{type:r,snapshot:e}=t;try{switch(r){case h.DocumentType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${r}.`)}}catch(n){throw n instanceof Error?n:new Error(`${n}`)}}async disposeInstance(t){return this._univerInstanceService.disposeDocument(t.unitID)}};V=ae([G(0,h.IUniverInstanceService),G(1,h.ICommandService)],V);function ue(t){const r=t;return new class{call(e,n){const s=r[e];if(typeof s=="function"){let i=s.apply(r,[n]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=r[e];if(typeof s=="function"){const i=s.apply(r,n);return _.isObservable(i)?i:_.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function le(t){return new Proxy({},{get(r,e){return function(...n){return tt(e)?t.subscribe(e,n[0]):t.call(e,n[0])}}})}function tt(t){return t.endsWith("$")}class fe extends h.RxDisposable{constructor(e){super();y(this,"_initialized",new _.BehaviorSubject(!1));y(this,"_lastRequestCounter",0);y(this,"_pendingRequests",new Map);y(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(q(this.dispose$)).subscribe(n=>this._onResponse(n))}getChannel(e){const n=this;return{call(s,i){return n._disposed?Promise.reject():n._remoteCall(e,s,i)},subscribe(s,i){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,i)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(Ke(e=>e),Ze(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,a={seq:i,type:100,channelName:e,method:n,args:s},u=this;return new Promise((c,l)=>{const b={handle(f){switch(f.type){case 100:u._pendingRequests.delete(i),c(f.data);break;case 101:u._pendingRequests.delete(i),l(f.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,b),this._sendRequest(a)})}_remoteSubscribe(e,n,s){return new _.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const u={seq:o,type:101,channelName:e,method:n,args:s},c={handle(l){switch(l.type){case 200:i.next(l.data);break;case 201:i.error(l.data);break;case 202:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,c),this._sendRequest(u)}),()=>{if(o===-1)return;const a={type:102,seq:o,channelName:e,method:n};this._sendRequest(a)}})}_sendRequest(e){this._protocol.send(e)}_onResponse(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 100:case 101:case 200:case 202:case 201:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class he extends h.RxDisposable{constructor(e){super();y(this,"_channels",new Map);y(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(q(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendResponse({seq:-1,type:0})}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const n=this._channels.get(e.channelName);let s;try{if(!n)throw new Error("[ChannelServer]: Channel not found!");s=n.call(e.method,e.args)}catch(i){s=Promise.reject(i)}s.then(i=>{this._sendResponse({seq:e.seq,type:100,data:i})}).catch(i=>{i instanceof Error?this._sendResponse({seq:e.seq,type:101,data:i.message}):this._sendResponse({seq:e.seq,type:101,data:String(i)})})}_onSubscribe(e){const n=this._channels.get(e.channelName);try{if(!n)throw new Error("[ChannelServer]: Channel not found!");const i=n.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:e.seq,type:200,data:o})},error:o=>{this._sendResponse({seq:e.seq,type:201,data:o.message}),this._sendResponse({seq:e.seq,type:202})},complete:()=>{this._sendResponse({seq:e.seq,type:202})}});this._subscriptions.set(e.seq,i)}catch(s){s instanceof Error?this._sendResponse({seq:e.seq,type:201,data:s.message}):this._sendResponse({seq:e.seq,type:201,data:String(s)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const E=m.createIdentifier("IRPChannelService");class pe{constructor(r){y(this,"_client");y(this,"_server");this._client=new fe(r),this._server=new he(r)}requestChannel(r){return this._client.getChannel(r)}registerChannel(r,e){this._server.registerChannel(r,e)}}var rt=Object.defineProperty,nt=Object.getOwnPropertyDescriptor,it=(t,r,e,n)=>{for(var s=n>1?void 0:n?nt(r,e):r,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(n?o(r,e,s):o(s))||s);return n&&s&&rt(r,e,s),s},P=(t,r)=>(e,n)=>r(e,n,t);let I=class extends h.RxDisposable{constructor(r,e,n,s,i,o){super();y(this,"_remoteInstanceService");this._unsyncMutations=r,this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(K,ue(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>le(this._rpcChannelService.requestChannel(N))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.sheetAdded$.pipe(q(this.dispose$)).subscribe(r=>{this._remoteInstanceService.createInstance({unitID:r.getUnitId(),type:h.DocumentType.SHEET,snapshot:r.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(q(this.dispose$)).subscribe(r=>{this._remoteInstanceService.disposeInstance({unitID:r.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((r,e)=>{r.type===h.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&!this._unsyncMutations.has(r.id)&&this._remoteInstanceService.syncMutation({mutationInfo:r})}))}};I=it([h.OnLifecycle(h.LifecycleStages.Starting,I),P(1,m.Inject(m.Injector)),P(2,h.ICommandService),P(3,h.IUniverInstanceService),P(4,E),P(5,U)],I);var st=Object.defineProperty,ot=Object.getOwnPropertyDescriptor,ct=(t,r,e,n)=>{for(var s=n>1?void 0:n?ot(r,e):r,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(n?o(r,e,s):o(s))||s);return n&&s&&st(r,e,s),s},T=(t,r)=>(e,n)=>r(e,n,t);let C=class extends h.Disposable{constructor(r,e,n,s){super();y(this,"_remoteSyncService");this._injector=r,this._remoteInstanceService=e,this._commandService=n,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(N,ue(this._remoteInstanceService)),this._injector.add([U,{useFactory:()=>le(this._rpcChannelService.requestChannel(K))}]),this._remoteSyncService=this._injector.get(U)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((r,e)=>{r.type===h.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:r})}))}};C=ct([h.OnLifecycle(h.LifecycleStages.Starting,C),T(0,m.Inject(m.Injector)),T(1,j),T(2,h.ICommandService),T(3,E)],C);function at(){return{send(t){postMessage(t)},onMessage:new _.Observable(t=>{const r=e=>{t.next(e.data)};return addEventListener("message",r),()=>removeEventListener("message",r)}).pipe(_.shareReplay(1))}}function ut(t){return{send(r){t.postMessage(r)},onMessage:new _.Observable(r=>{const e=n=>{r.next(n.data)};return t.addEventListener("message",e),()=>t.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var lt=Object.defineProperty,ft=Object.getOwnPropertyDescriptor,de=(t,r,e,n)=>{for(var s=n>1?void 0:n?ft(r,e):r,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(n?o(r,e,s):o(s))||s);return n&&s&<(r,e,s),s},ye=(t,r)=>(e,n)=>r(e,n,t),$;exports.UniverRPCMainThreadPlugin=($=class extends h.Plugin{constructor(r,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=r,this._injector=e}async onStarting(r){const e=new Worker(this._config.workerURL),n=ut(e);[[E,{useFactory:()=>new pe(n)}],[I,{useFactory:()=>{var i;return r.createInstance(I,((i=this._config)==null?void 0:i.unsyncMutations)??new Set)}}],[U,{useClass:Y}]].forEach(i=>r.add(i))}},y($,"type",h.PluginType.Univer),$);exports.UniverRPCMainThreadPlugin=de([ye(1,m.Inject(m.Injector))],exports.UniverRPCMainThreadPlugin);var L;exports.UniverRPCWorkerThreadPlugin=(L=class extends h.Plugin{constructor(r,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=r,this._injector=e}onStarting(r){[[C],[E,{useFactory:()=>new pe(at())}],[j,{useClass:V}]].forEach(e=>r.add(e)),r.get(C)}},y(L,"type",h.PluginType.Univer),L);exports.UniverRPCWorkerThreadPlugin=de([ye(1,m.Inject(m.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=fe;exports.ChannelServer=he;exports.IRPChannelService=E;
|
|
1
|
+
"use strict";var A=Object.defineProperty;var H=(r,n,e)=>n in r?A(r,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[n]=e;var h=(r,n,e)=>(H(r,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),l=require("@wendellhu/redi"),d=require("rxjs/operators"),_=require("rxjs");var F=Object.defineProperty,k=Object.getOwnPropertyDescriptor,O=(r,n,e,t)=>{for(var s=t>1?void 0:t?k(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&F(n,e,s),s},w=(r,n)=>(e,t)=>n(e,t,r);const E="univer.remote-sync-service",C=l.createIdentifier(E);let U=class{constructor(r){this._commandService=r}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};U=O([w(0,a.ICommandService)],U);const M="univer.remote-instance-service",S=l.createIdentifier(M);let q=class{constructor(r,n){this._univerInstanceService=r,this._commandService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}async createInstance(r){const{type:n,snapshot:e}=r;try{switch(n){case a.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(t){throw t instanceof Error?t:new Error(`${t}`)}}async disposeInstance(r){return this._univerInstanceService.disposeDocument(r.unitID)}};q=O([w(0,a.IUniverInstanceService),w(1,a.ICommandService)],q);function j(r){const n=r;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[t]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const i=s.apply(n,t);return _.isObservable(i)?i:_.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function $(r){return new Proxy({},{get(n,e){return function(...t){return z(e)?r.subscribe(e,t[0]):r.call(e,t[0])}}})}function z(r){return r.endsWith("$")}class T extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new _.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);h(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,i){return t._disposed?Promise.reject():t._remoteCall(e,s,i)},subscribe(s,i){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,i)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(d.filter(e=>e),d.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:t,args:s},o=this;return new Promise((g,p)=>{const N={handle(R){switch(R.type){case 201:o._pendingRequests.delete(i),g(R.data);break;case 202:o._pendingRequests.delete(i),p(R.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,N),this._sendRequest(u)})}_remoteSubscribe(e,t,s){return new _.Observable(i=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},g={handle(p){switch(p.type){case 300:i.next(p.data);break;case 301:i.error(p.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,g),this._sendRequest(o)}),()=>{if(c===-1)return;const u={type:102,seq:c,channelName:e,method:t};this._sendRequest(u)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class D extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:i}=e,c=this._channels.get(t);let u;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);u=c.call(s,i)}catch(o){u=Promise.reject(o)}u.then(o=>{this._sendResponse({seq:e.seq,type:201,data:o})}).catch(o=>{o instanceof Error?this._sendResponse({seq:e.seq,type:202,data:o.message}):this._sendResponse({seq:e.seq,type:202,data:String(o)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,i=this._channels.get(t);try{if(!i)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const u=i.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,u)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const f=l.createIdentifier("IRPChannelService");class x{constructor(n){h(this,"_client");h(this,"_server");this._client=new T(n),this._server=new D(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var V=Object.defineProperty,G=Object.getOwnPropertyDescriptor,B=(r,n,e,t)=>{for(var s=t>1?void 0:t?G(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&V(n,e,s),s},v=(r,n)=>(e,t)=>n(e,t,r);let m=class extends a.RxDisposable{constructor(n,e,t,s,i){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);this._injector=n,this._commandService=e,this._univerInstanceService=t,this._rpcChannelService=s,this._remoteSyncService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(E,j(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>$(this._rpcChannelService.requestChannel(M))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.sheetAdded$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:a.UniverInstanceType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.delete(n.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{const{type:t,params:s}=n,i=(s==null?void 0:s.unitId)||"";t===a.CommandType.MUTATION&&(!i||this._syncingUnits.has(i))&&!(e!=null&&e.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};m=B([a.OnLifecycle(a.LifecycleStages.Starting,m),v(0,l.Inject(l.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,f),v(4,C)],m);var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,X=(r,n,e,t)=>{for(var s=t>1?void 0:t?Q(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&J(n,e,s),s},b=(r,n)=>(e,t)=>n(e,t,r);let y=class extends a.Disposable{constructor(n,e,t,s){super();h(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=t,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(M,j(this._remoteInstanceService)),this._injector.add([C,{useFactory:()=>$(this._rpcChannelService.requestChannel(E))}]),this._remoteSyncService=this._injector.get(C)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===a.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};y=X([a.OnLifecycle(a.LifecycleStages.Starting,y),b(0,l.Inject(l.Injector)),b(1,S),b(2,a.ICommandService),b(3,f)],y);function Y(){return{send(r){postMessage(r)},onMessage:new _.Observable(r=>{const n=e=>{r.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function Z(r){return{send(n){r.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return r.addEventListener("message",e),()=>r.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var K=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,W=(r,n,e,t)=>{for(var s=t>1?void 0:t?ee(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&K(n,e,s),s},L=(r,n)=>(e,t)=>n(e,t,r),I;exports.UniverRPCMainThreadPlugin=(I=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const e=new Worker(this._config.workerURL),t=Z(e);[[f,{useFactory:()=>new x(t)}],[m],[C,{useClass:U}]].forEach(i=>n.add(i)),n.get(m)}},h(I,"type",a.PluginType.Univer),I);exports.UniverRPCMainThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCMainThreadPlugin);var P;exports.UniverRPCWorkerThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[y],[f,{useFactory:()=>new x(Y())}],[S,{useClass:q}]].forEach(e=>n.add(e)),n.get(y)}},h(P,"type",a.PluginType.Univer),P);exports.UniverRPCWorkerThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=T;exports.ChannelServer=D;exports.IRPChannelService=f;exports.IRemoteInstanceService=S;
|