@univerjs/rpc 0.15.5 → 0.16.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/lib/cjs/index.js +1 -1
- package/lib/es/index.js +112 -109
- package/lib/index.js +112 -109
- package/lib/types/services/rpc/rpc.service.d.ts +1 -0
- package/lib/umd/index.js +1 -1
- package/package.json +3 -3
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var x=Object.defineProperty;var L=(i,t,e)=>t in i?x(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>L(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs"),k="rpc.main-thread.config",W={},D="rpc.worker-thread.config",T={};var H=Object.getOwnPropertyDescriptor,q=(i,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},g=(i,t)=>(e,n)=>t(e,n,i);const R="rpc.remote-sync.service",v=o.createIdentifier(R);exports.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t,e){const{fromCollab:n,...s}=e||{};return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{...s,onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=q([g(0,o.ICommandService)],exports.RemoteSyncPrimaryService);const I="univer.remote-instance-service",m=o.createIdentifier(I);exports.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t,e){return this._applyMutation(t.mutationInfo,e)}async createInstance(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);const{type:e,snapshot:n}=t;try{if(e===o.UniverInstanceType.UNIVER_SHEET)return this._univerInstanceService.createUnit(o.UniverInstanceType.UNIVER_SHEET,n),!0;throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${t.unitID}`),this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t,e){const{id:n,params:s}=t,{fromCollab:r,...c}=e||{};return this._commandService.syncExecuteCommand(n,s,{...c,onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=q([g(0,o.IUniverInstanceService),g(1,o.ICommandService),g(2,o.ILogService)],exports.WebWorkerRemoteInstanceService);function E(i){const t=i;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let r=n?s.apply(t,n):s.call(t);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const r=n?s.apply(t,n):s.call(t);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function U(i){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return A(e)?i.subscribe(e,n):i.call(e,n)}}})}function A(i){return i.endsWith("$")}class O extends o.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,r){return n._disposed?Promise.reject():n._remoteCall(e,s,r)},subscribe(s,r){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:n,args:s},a=this;return new Promise((f,d)=>{const $={handle(b){switch(b.type){case 201:a._pendingRequests.delete(r),f(b.data);break;case 202:a._pendingRequests.delete(r),d(b.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,$),this._sendRequest(h)})}_remoteSubscribe(e,n,s){return new _.Observable(r=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const a={seq:c,type:101,channelName:e,method:n,args:s},f={handle(d){switch(d.type){case 300:r.next(d.data);break;case 301:r.error(d.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,f),this._sendRequest(a)}),()=>{if(c===-1)return;const h={type:102,seq:c,channelName:e,method:n};this._sendRequest(h)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var r;const{type:n,seq:s}=e;switch(n){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:c}=this;(r=c.get(s))==null||r.handle(e),n!==300&&c.delete(s);break}}}}class j extends o.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:r}=e,c=this._channels.get(n);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${n} not found!`);h=r?c.call(s,r):c.call(s)}catch(a){h=Promise.reject(a)}h.then(a=>{this._sendResponse({seq:e.seq,type:201,data:a})}).catch(a=>{a instanceof Error?this._sendResponse({seq:e.seq,type:202,data:a.message}):this._sendResponse({seq:e.seq,type:202,data:String(a)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:a=>{this._sendResponse({seq:s,type:300,data:a})},error:a=>{this._sendResponse({seq:s,type:301,data:a.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}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 n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const S=o.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new O(t),this._server=new j(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var G=Object.getOwnPropertyDescriptor,F=(i,t,e,n)=>{for(var s=n>1?void 0:n?G(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends o.RxDisposable{constructor(e,n,s,r,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),o.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,E(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>U(this._rpcChannelService.requestChannel(I))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:o.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===o.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}};exports.DataSyncPrimaryController=F([p(0,o.Inject(o.Injector)),p(1,o.ICommandService),p(2,o.IUniverInstanceService),p(3,S),p(4,v)],exports.DataSyncPrimaryController);var V=Object.getOwnPropertyDescriptor,z=(i,t,e,n)=>{for(var s=n>1?void 0:n?V(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},y=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends o.Disposable{constructor(e,n,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(I,E(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>U(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===o.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}};exports.DataSyncReplicaController=z([y(0,o.Inject(o.Injector)),y(1,m),y(2,o.ICommandService),y(3,S)],exports.DataSyncReplicaController);function K(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const t=e=>{i.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(_.shareReplay(1))}}function Y(i){return{send(t){i.postMessage(t)},onMessage:new _.Observable(t=>{const e=n=>{t.next(n.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var B=Object.getOwnPropertyDescriptor,N=(i,t,e,n)=>{for(var s=n>1?void 0:n?B(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends o.Plugin{constructor(e=W,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=o.merge({},W,this._config);this._configService.setConfig(k,r)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=Y(n);[[S,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>this._injector.add(c)),this._injector.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=N([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends o.Plugin{constructor(t=T,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=o.merge({},T,this._config);this._configService.setConfig(D,s)}onStarting(){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new M(K())}],[m,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(exports.DataSyncReplicaController)}},l(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w);exports.UniverRPCWorkerThreadPlugin=N([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=O;exports.ChannelServer=j;exports.ChannelService=M;exports.IRPCChannelService=S;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.PLUGIN_CONFIG_KEY_MAIN_THREAD=k;exports.PLUGIN_CONFIG_KEY_WORKER_THREAD=D;exports.RemoteInstanceServiceName=I;exports.RemoteSyncServiceName=R;exports.fromModule=E;exports.toModule=U;
|
|
1
|
+
"use strict";var x=Object.defineProperty;var L=(i,n,e)=>n in i?x(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var l=(i,n,e)=>L(i,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs"),D="rpc.main-thread.config",T={},q="rpc.worker-thread.config",k={};var H=Object.getOwnPropertyDescriptor,O=(i,n,e,t)=>{for(var s=t>1?void 0:t?H(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},g=(i,n)=>(e,t)=>n(e,t,i);const R="rpc.remote-sync.service",v=o.createIdentifier(R);exports.RemoteSyncPrimaryService=class{constructor(n){this._commandService=n}async syncMutation(n,e){const{fromCollab:t,...s}=e||{};return this._commandService.syncExecuteCommand(n.mutationInfo.id,n.mutationInfo.params,{...s,onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=O([g(0,o.ICommandService)],exports.RemoteSyncPrimaryService);const I="univer.remote-instance-service",m=o.createIdentifier(I);exports.WebWorkerRemoteInstanceService=class{constructor(n,e,t){this._univerInstanceService=n,this._commandService=e,this._logService=t}whenReady(){return Promise.resolve(!0)}async syncMutation(n,e){return this._applyMutation(n.mutationInfo,e)}async createInstance(n){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${n.unitID}`);const{type:e,snapshot:t}=n;try{if(e===o.UniverInstanceType.UNIVER_SHEET)return this._univerInstanceService.createUnit(o.UniverInstanceType.UNIVER_SHEET,t),!0;throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(n){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${n.unitID}`),this._univerInstanceService.disposeUnit(n.unitID)}_applyMutation(n,e){const{id:t,params:s}=n,{fromCollab:r,...c}=e||{};return this._commandService.syncExecuteCommand(t,s,{...c,onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=O([g(0,o.IUniverInstanceService),g(1,o.ICommandService),g(2,o.ILogService)],exports.WebWorkerRemoteInstanceService);function U(i){const n=i;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let r=t?s.apply(n,t):s.call(n);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const r=t?s.apply(n,t):s.call(n);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function M(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return A(e)?i.subscribe(e,t):i.call(e,t)}}})}function A(i){return i.endsWith("$")}class j extends o.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}dispose(){this._pendingRequests.clear()}getChannel(e){const t=this;return{call(s,r){return t._disposed?Promise.reject():t._remoteCall(e,s,r)},subscribe(s,r){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}_remoteCall(e,t,s){return this._initialized.getValue()?this._doRemoteCall(e,t,s):this._whenReady().then(()=>this._doRemoteCall(e,t,s))}_doRemoteCall(e,t,s){const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:t,args:s},a=this;return new Promise((f,b)=>{const d={handle(P){switch(P.type){case 201:a._pendingRequests.delete(r),f(P.data);break;case 202:a._pendingRequests.delete(r),b(P.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,d),this._sendRequest(h)})}_remoteSubscribe(e,t,s){return new _.Observable(r=>{let c=-1;const h=()=>{c=++this._lastRequestCounter;const f={seq:c,type:101,channelName:e,method:t,args:s},b={handle(d){switch(d.type){case 300:r.next(d.data);break;case 301:r.error(d.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,b),this._sendRequest(f)};return this._initialized.getValue()?h():this._whenReady().then(h),()=>{if(c===-1)return;const a={type:102,seq:c,channelName:e,method:t};this._sendRequest(a)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var r;const{type:t,seq:s}=e;switch(t){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:c}=this;(r=c.get(s))==null||r.handle(e),t!==300&&c.delete(s);break}}}}class N extends o.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:t,method:s,args:r}=e,c=this._channels.get(t);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);h=r?c.call(s,r):c.call(s)}catch(a){h=Promise.reject(a)}h.then(a=>{this._sendResponse({seq:e.seq,type:201,data:a})}).catch(a=>{a instanceof Error?this._sendResponse({seq:e.seq,type:202,data:a.message}):this._sendResponse({seq:e.seq,type:202,data:String(a)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,r=this._channels.get(t);try{if(!r)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:a=>{this._sendResponse({seq:s,type:300,data:a})},error:a=>{this._sendResponse({seq:s,type:301,data:a.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}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 S=o.createIdentifier("IRPCChannelService");class W{constructor(n){l(this,"_client");l(this,"_server");this._client=new j(n),this._server=new N(n)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var G=Object.getOwnPropertyDescriptor,V=(i,n,e,t)=>{for(var s=t>1?void 0:t?G(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},p=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncPrimaryController=class extends o.RxDisposable{constructor(e,t,s,r,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=t,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),o.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>M(this._rpcChannelService.requestChannel(I))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:o.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{const{type:s,params:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===o.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(t!=null&&t.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e},t)}))}};exports.DataSyncPrimaryController=V([p(0,o.Inject(o.Injector)),p(1,o.ICommandService),p(2,o.IUniverInstanceService),p(3,S),p(4,v)],exports.DataSyncPrimaryController);var F=Object.getOwnPropertyDescriptor,z=(i,n,e,t)=>{for(var s=t>1?void 0:t?F(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},y=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends o.Disposable{constructor(e,t,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(I,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>M(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{e.type===o.CommandType.MUTATION&&!(t!=null&&t.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},t)}))}};exports.DataSyncReplicaController=z([y(0,o.Inject(o.Injector)),y(1,m),y(2,o.ICommandService),y(3,S)],exports.DataSyncReplicaController);function K(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const n=e=>{i.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function Y(i){return{send(n){i.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var B=Object.getOwnPropertyDescriptor,$=(i,n,e,t)=>{for(var s=t>1?void 0:t?B(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},C=(i,n)=>(e,t)=>n(e,t,i),w;exports.UniverRPCMainThreadPlugin=(w=class extends o.Plugin{constructor(e=T,t,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=t,this._configService=s;const{...r}=o.merge({},T,this._config);this._configService.setConfig(D,r)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const t=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:t;const s=Y(t);[[S,{useFactory:()=>new W(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>this._injector.add(c)),this._injector.get(exports.DataSyncPrimaryController)}},l(w,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),w);exports.UniverRPCMainThreadPlugin=$([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCMainThreadPlugin);var E;exports.UniverRPCWorkerThreadPlugin=(E=class extends o.Plugin{constructor(n=k,e,t){super(),this._config=n,this._injector=e,this._configService=t;const{...s}=o.merge({},k,this._config);this._configService.setConfig(q,s)}onStarting(){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new W(K())}],[m,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(n=>this._injector.add(n)),this._injector.get(exports.DataSyncReplicaController)}},l(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E);exports.UniverRPCWorkerThreadPlugin=$([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=j;exports.ChannelServer=N;exports.ChannelService=W;exports.IRPCChannelService=S;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.PLUGIN_CONFIG_KEY_MAIN_THREAD=D;exports.PLUGIN_CONFIG_KEY_WORKER_THREAD=q;exports.RemoteInstanceServiceName=I;exports.RemoteSyncServiceName=R;exports.fromModule=U;exports.toModule=M;
|
package/lib/es/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
var X = Object.defineProperty;
|
|
2
2
|
var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
|
|
3
3
|
var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
-
import { createIdentifier as
|
|
4
|
+
import { createIdentifier as O, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as H, ILogService as ee, RxDisposable as W, Inject as C, Injector as R, toDisposable as te, CommandType as V, Disposable as ne, IConfigService as A, Plugin as z, merge as F } from "@univerjs/core";
|
|
5
5
|
import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
|
|
6
|
-
import { BehaviorSubject as ie, firstValueFrom as
|
|
7
|
-
const he = "rpc.main-thread.config",
|
|
8
|
-
var _e = Object.getOwnPropertyDescriptor,
|
|
9
|
-
for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1,
|
|
10
|
-
(
|
|
6
|
+
import { BehaviorSubject as ie, firstValueFrom as oe, Observable as T, isObservable as ce, of as ae, shareReplay as G } from "rxjs";
|
|
7
|
+
const he = "rpc.main-thread.config", j = {}, le = "rpc.worker-thread.config", N = {};
|
|
8
|
+
var _e = Object.getOwnPropertyDescriptor, Y = (s, t, e, n) => {
|
|
9
|
+
for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
10
|
+
(o = s[i]) && (r = o(r) || r);
|
|
11
11
|
return r;
|
|
12
12
|
}, p = (s, t) => (e, n) => t(e, n, s);
|
|
13
|
-
const
|
|
14
|
-
let
|
|
13
|
+
const D = "rpc.remote-sync.service", g = O(D);
|
|
14
|
+
let q = class {
|
|
15
15
|
constructor(s) {
|
|
16
16
|
this._commandService = s;
|
|
17
17
|
}
|
|
@@ -24,11 +24,11 @@ let M = class {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
q = Y([
|
|
28
28
|
p(0, b)
|
|
29
|
-
],
|
|
30
|
-
const
|
|
31
|
-
let
|
|
29
|
+
], q);
|
|
30
|
+
const $ = "univer.remote-instance-service", m = O($);
|
|
31
|
+
let k = class {
|
|
32
32
|
constructor(s, t, e) {
|
|
33
33
|
this._univerInstanceService = s, this._commandService = t, this._logService = e;
|
|
34
34
|
}
|
|
@@ -63,12 +63,12 @@ let q = class {
|
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
|
-
|
|
67
|
-
p(0,
|
|
66
|
+
k = Y([
|
|
67
|
+
p(0, H),
|
|
68
68
|
p(1, b),
|
|
69
69
|
p(2, ee)
|
|
70
|
-
],
|
|
71
|
-
function
|
|
70
|
+
], k);
|
|
71
|
+
function B(s) {
|
|
72
72
|
const t = s;
|
|
73
73
|
return new class {
|
|
74
74
|
call(e, n) {
|
|
@@ -83,13 +83,13 @@ function Y(s) {
|
|
|
83
83
|
const r = t[e];
|
|
84
84
|
if (typeof r == "function") {
|
|
85
85
|
const i = n ? r.apply(t, n) : r.call(t);
|
|
86
|
-
return
|
|
86
|
+
return ce(i) ? i : ae(i);
|
|
87
87
|
}
|
|
88
88
|
throw new Error(`[RPC]: observable method not found for ${e}!`);
|
|
89
89
|
}
|
|
90
90
|
}();
|
|
91
91
|
}
|
|
92
|
-
function
|
|
92
|
+
function K(s) {
|
|
93
93
|
return new Proxy({}, {
|
|
94
94
|
get(t, e) {
|
|
95
95
|
if (e !== "dispose")
|
|
@@ -102,7 +102,7 @@ function B(s) {
|
|
|
102
102
|
function ue(s) {
|
|
103
103
|
return s.endsWith("$");
|
|
104
104
|
}
|
|
105
|
-
class de extends
|
|
105
|
+
class de extends W {
|
|
106
106
|
constructor(e) {
|
|
107
107
|
super();
|
|
108
108
|
a(this, "_initialized", new ie(!1));
|
|
@@ -130,40 +130,42 @@ class de extends O {
|
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
132
|
_whenReady() {
|
|
133
|
-
return
|
|
133
|
+
return oe(
|
|
134
134
|
this._initialized.pipe(
|
|
135
135
|
se((e) => e),
|
|
136
136
|
re(1)
|
|
137
137
|
)
|
|
138
138
|
);
|
|
139
139
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
_remoteCall(e, n, r) {
|
|
141
|
+
return this._initialized.getValue() ? this._doRemoteCall(e, n, r) : this._whenReady().then(() => this._doRemoteCall(e, n, r));
|
|
142
|
+
}
|
|
143
|
+
_doRemoteCall(e, n, r) {
|
|
144
|
+
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args: r }, c = this;
|
|
145
|
+
return new Promise((w, E) => {
|
|
146
|
+
const l = {
|
|
147
|
+
handle(P) {
|
|
148
|
+
switch (P.type) {
|
|
147
149
|
case 201:
|
|
148
|
-
|
|
150
|
+
c._pendingRequests.delete(i), w(P.data);
|
|
149
151
|
break;
|
|
150
152
|
case 202:
|
|
151
|
-
|
|
153
|
+
c._pendingRequests.delete(i), E(P.data);
|
|
152
154
|
break;
|
|
153
155
|
default:
|
|
154
156
|
throw new Error("[ChannelClient]: unknown response type!");
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
159
|
};
|
|
158
|
-
this._pendingRequests.set(i,
|
|
160
|
+
this._pendingRequests.set(i, l), this._sendRequest(h);
|
|
159
161
|
});
|
|
160
162
|
}
|
|
161
163
|
_remoteSubscribe(e, n, r) {
|
|
162
|
-
return new
|
|
163
|
-
let
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
164
|
+
return new T((i) => {
|
|
165
|
+
let o = -1;
|
|
166
|
+
const h = () => {
|
|
167
|
+
o = ++this._lastRequestCounter;
|
|
168
|
+
const w = { seq: o, type: 101, channelName: e, method: n, args: r }, E = {
|
|
167
169
|
handle(l) {
|
|
168
170
|
switch (l.type) {
|
|
169
171
|
case 300:
|
|
@@ -180,17 +182,18 @@ class de extends O {
|
|
|
180
182
|
}
|
|
181
183
|
}
|
|
182
184
|
};
|
|
183
|
-
this._pendingRequests.set(
|
|
184
|
-
}
|
|
185
|
-
|
|
185
|
+
this._pendingRequests.set(o, E), this._sendRequest(w);
|
|
186
|
+
};
|
|
187
|
+
return this._initialized.getValue() ? h() : this._whenReady().then(h), () => {
|
|
188
|
+
if (o === -1)
|
|
186
189
|
return;
|
|
187
|
-
const
|
|
190
|
+
const c = {
|
|
188
191
|
type: 102,
|
|
189
|
-
seq:
|
|
192
|
+
seq: o,
|
|
190
193
|
channelName: e,
|
|
191
194
|
method: n
|
|
192
195
|
};
|
|
193
|
-
this._sendRequest(
|
|
196
|
+
this._sendRequest(c);
|
|
194
197
|
};
|
|
195
198
|
});
|
|
196
199
|
}
|
|
@@ -209,14 +212,14 @@ class de extends O {
|
|
|
209
212
|
case 300:
|
|
210
213
|
case 302:
|
|
211
214
|
case 301: {
|
|
212
|
-
const { _pendingRequests:
|
|
213
|
-
(i =
|
|
215
|
+
const { _pendingRequests: o } = this;
|
|
216
|
+
(i = o.get(r)) == null || i.handle(e), n !== 300 && o.delete(r);
|
|
214
217
|
break;
|
|
215
218
|
}
|
|
216
219
|
}
|
|
217
220
|
}
|
|
218
221
|
}
|
|
219
|
-
class pe extends
|
|
222
|
+
class pe extends W {
|
|
220
223
|
constructor(e) {
|
|
221
224
|
super();
|
|
222
225
|
a(this, "_channels", /* @__PURE__ */ new Map());
|
|
@@ -253,19 +256,19 @@ class pe extends O {
|
|
|
253
256
|
});
|
|
254
257
|
}
|
|
255
258
|
_onMethodCall(e) {
|
|
256
|
-
const { channelName: n, method: r, args: i } = e,
|
|
259
|
+
const { channelName: n, method: r, args: i } = e, o = this._channels.get(n);
|
|
257
260
|
let h;
|
|
258
261
|
try {
|
|
259
|
-
if (!
|
|
262
|
+
if (!o)
|
|
260
263
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
261
|
-
h = i ?
|
|
262
|
-
} catch (
|
|
263
|
-
h = Promise.reject(
|
|
264
|
+
h = i ? o.call(r, i) : o.call(r);
|
|
265
|
+
} catch (c) {
|
|
266
|
+
h = Promise.reject(c);
|
|
264
267
|
}
|
|
265
|
-
h.then((
|
|
266
|
-
this._sendResponse({ seq: e.seq, type: 201, data:
|
|
267
|
-
}).catch((
|
|
268
|
-
|
|
268
|
+
h.then((c) => {
|
|
269
|
+
this._sendResponse({ seq: e.seq, type: 201, data: c });
|
|
270
|
+
}).catch((c) => {
|
|
271
|
+
c instanceof Error ? this._sendResponse({ seq: e.seq, type: 202, data: c.message }) : this._sendResponse({ seq: e.seq, type: 202, data: String(c) });
|
|
269
272
|
});
|
|
270
273
|
}
|
|
271
274
|
_onSubscribe(e) {
|
|
@@ -274,11 +277,11 @@ class pe extends O {
|
|
|
274
277
|
if (!i)
|
|
275
278
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
276
279
|
const h = i.subscribe(e.method, e.args).subscribe({
|
|
277
|
-
next: (
|
|
278
|
-
this._sendResponse({ seq: r, type: 300, data:
|
|
280
|
+
next: (c) => {
|
|
281
|
+
this._sendResponse({ seq: r, type: 300, data: c });
|
|
279
282
|
},
|
|
280
|
-
error: (
|
|
281
|
-
this._sendResponse({ seq: r, type: 301, data:
|
|
283
|
+
error: (c) => {
|
|
284
|
+
this._sendResponse({ seq: r, type: 301, data: c.message }), this._sendResponse({
|
|
282
285
|
seq: r,
|
|
283
286
|
type: 302
|
|
284
287
|
/* SUBSCRIBE_COMPLETE */
|
|
@@ -293,8 +296,8 @@ class pe extends O {
|
|
|
293
296
|
}
|
|
294
297
|
});
|
|
295
298
|
this._subscriptions.set(e.seq, h);
|
|
296
|
-
} catch (
|
|
297
|
-
|
|
299
|
+
} catch (o) {
|
|
300
|
+
o instanceof Error ? this._sendResponse({ seq: e.seq, type: 301, data: o.message }) : this._sendResponse({ seq: e.seq, type: 301, data: String(o) });
|
|
298
301
|
}
|
|
299
302
|
}
|
|
300
303
|
_onUnsubscribe(e) {
|
|
@@ -305,8 +308,8 @@ class pe extends O {
|
|
|
305
308
|
this._protocol.send(e);
|
|
306
309
|
}
|
|
307
310
|
}
|
|
308
|
-
const I =
|
|
309
|
-
class
|
|
311
|
+
const I = O("IRPCChannelService");
|
|
312
|
+
class J {
|
|
310
313
|
constructor(t) {
|
|
311
314
|
a(this, "_client");
|
|
312
315
|
a(this, "_server");
|
|
@@ -323,11 +326,11 @@ class K {
|
|
|
323
326
|
}
|
|
324
327
|
}
|
|
325
328
|
var ve = Object.getOwnPropertyDescriptor, ge = (s, t, e, n) => {
|
|
326
|
-
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1,
|
|
327
|
-
(
|
|
329
|
+
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
330
|
+
(o = s[i]) && (r = o(r) || r);
|
|
328
331
|
return r;
|
|
329
332
|
}, _ = (s, t) => (e, n) => t(e, n, s);
|
|
330
|
-
let f = class extends
|
|
333
|
+
let f = class extends W {
|
|
331
334
|
constructor(t, e, n, r, i) {
|
|
332
335
|
super();
|
|
333
336
|
a(this, "_remoteInstanceService");
|
|
@@ -346,9 +349,9 @@ let f = class extends O {
|
|
|
346
349
|
return this._syncingUnits.add(t), te(() => this._syncingUnits.delete(t));
|
|
347
350
|
}
|
|
348
351
|
_initRPCChannels() {
|
|
349
|
-
this._rpcChannelService.registerChannel(
|
|
352
|
+
this._rpcChannelService.registerChannel(D, B(this._remoteSyncService)), this._injector.add([
|
|
350
353
|
m,
|
|
351
|
-
{ useFactory: () =>
|
|
354
|
+
{ useFactory: () => K(this._rpcChannelService.requestChannel($)) }
|
|
352
355
|
]), this._remoteInstanceService = this._injector.get(m);
|
|
353
356
|
}
|
|
354
357
|
_init() {
|
|
@@ -363,9 +366,9 @@ let f = class extends O {
|
|
|
363
366
|
unitID: t.getUnitId()
|
|
364
367
|
});
|
|
365
368
|
}), this.disposeWithMe(this._commandService.onCommandExecuted((t, e) => {
|
|
366
|
-
const { type: n, params: r, id: i } = t,
|
|
367
|
-
n ===
|
|
368
|
-
(!
|
|
369
|
+
const { type: n, params: r, id: i } = t, o = (r == null ? void 0 : r.unitId) || "";
|
|
370
|
+
n === V.MUTATION && // only sync mutations to the worker thread
|
|
371
|
+
(!o || this._syncingUnits.has(o)) && // do not sync mutations from the web worker back to the web worker
|
|
369
372
|
!(e != null && e.fromSync) && // do not sync mutations those are not meant to be synced
|
|
370
373
|
this._syncingMutations.has(i) && this._remoteInstanceService.syncMutation({ mutationInfo: t }, e);
|
|
371
374
|
}));
|
|
@@ -374,13 +377,13 @@ let f = class extends O {
|
|
|
374
377
|
f = ge([
|
|
375
378
|
_(0, C(R)),
|
|
376
379
|
_(1, b),
|
|
377
|
-
_(2,
|
|
380
|
+
_(2, H),
|
|
378
381
|
_(3, I),
|
|
379
382
|
_(4, g)
|
|
380
383
|
], f);
|
|
381
384
|
var me = Object.getOwnPropertyDescriptor, fe = (s, t, e, n) => {
|
|
382
|
-
for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1,
|
|
383
|
-
(
|
|
385
|
+
for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
386
|
+
(o = s[i]) && (r = o(r) || r);
|
|
384
387
|
return r;
|
|
385
388
|
}, d = (s, t) => (e, n) => t(e, n, s);
|
|
386
389
|
let y = class extends ne {
|
|
@@ -390,16 +393,16 @@ let y = class extends ne {
|
|
|
390
393
|
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init();
|
|
391
394
|
}
|
|
392
395
|
_initRPCChannels() {
|
|
393
|
-
this._rpcChannelService.registerChannel(
|
|
396
|
+
this._rpcChannelService.registerChannel($, B(this._remoteInstanceService)), this._injector.add([
|
|
394
397
|
g,
|
|
395
|
-
{ useFactory: () =>
|
|
398
|
+
{ useFactory: () => K(this._rpcChannelService.requestChannel(D)) }
|
|
396
399
|
]), this._remoteSyncService = this._injector.get(g);
|
|
397
400
|
}
|
|
398
401
|
_init() {
|
|
399
402
|
this.disposeWithMe(
|
|
400
403
|
// Mutations executed on the main thread should be synced to the worker thread.
|
|
401
404
|
this._commandService.onCommandExecuted((t, e) => {
|
|
402
|
-
t.type ===
|
|
405
|
+
t.type === V.MUTATION && !(e != null && e.fromSync) && this._remoteSyncService.syncMutation({
|
|
403
406
|
mutationInfo: t
|
|
404
407
|
}, e);
|
|
405
408
|
})
|
|
@@ -417,12 +420,12 @@ function ye() {
|
|
|
417
420
|
send(s) {
|
|
418
421
|
postMessage(s);
|
|
419
422
|
},
|
|
420
|
-
onMessage: new
|
|
423
|
+
onMessage: new T((s) => {
|
|
421
424
|
const t = (e) => {
|
|
422
425
|
s.next(e.data);
|
|
423
426
|
};
|
|
424
427
|
return addEventListener("message", t), () => removeEventListener("message", t);
|
|
425
|
-
}).pipe(
|
|
428
|
+
}).pipe(G(1))
|
|
426
429
|
};
|
|
427
430
|
}
|
|
428
431
|
function Se(s) {
|
|
@@ -430,27 +433,27 @@ function Se(s) {
|
|
|
430
433
|
send(t) {
|
|
431
434
|
s.postMessage(t);
|
|
432
435
|
},
|
|
433
|
-
onMessage: new
|
|
436
|
+
onMessage: new T((t) => {
|
|
434
437
|
const e = (n) => {
|
|
435
438
|
t.next(n.data);
|
|
436
439
|
};
|
|
437
440
|
return s.addEventListener("message", e), () => s.removeEventListener("message", e);
|
|
438
|
-
}).pipe(
|
|
441
|
+
}).pipe(G(1))
|
|
439
442
|
};
|
|
440
443
|
}
|
|
441
|
-
var be = Object.getOwnPropertyDescriptor,
|
|
442
|
-
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1,
|
|
443
|
-
(
|
|
444
|
+
var be = Object.getOwnPropertyDescriptor, Q = (s, t, e, n) => {
|
|
445
|
+
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
446
|
+
(o = s[i]) && (r = o(r) || r);
|
|
444
447
|
return r;
|
|
445
|
-
}, S = (s, t) => (e, n) => t(e, n, s),
|
|
446
|
-
let
|
|
447
|
-
constructor(t =
|
|
448
|
+
}, S = (s, t) => (e, n) => t(e, n, s), U;
|
|
449
|
+
let x = (U = class extends z {
|
|
450
|
+
constructor(t = j, e, n) {
|
|
448
451
|
super();
|
|
449
452
|
a(this, "_internalWorker", null);
|
|
450
453
|
this._config = t, this._injector = e, this._configService = n;
|
|
451
454
|
const { ...r } = F(
|
|
452
455
|
{},
|
|
453
|
-
|
|
456
|
+
j,
|
|
454
457
|
this._config
|
|
455
458
|
);
|
|
456
459
|
this._configService.setConfig(he, r);
|
|
@@ -469,25 +472,25 @@ let N = (P = class extends V {
|
|
|
469
472
|
[
|
|
470
473
|
I,
|
|
471
474
|
{
|
|
472
|
-
useFactory: () => new
|
|
475
|
+
useFactory: () => new J(n)
|
|
473
476
|
}
|
|
474
477
|
],
|
|
475
478
|
[f],
|
|
476
|
-
[g, { useClass:
|
|
479
|
+
[g, { useClass: q }]
|
|
477
480
|
].forEach((i) => this._injector.add(i)), this._injector.get(f);
|
|
478
481
|
}
|
|
479
|
-
}, a(
|
|
480
|
-
|
|
482
|
+
}, a(U, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), U);
|
|
483
|
+
x = Q([
|
|
481
484
|
S(1, C(R)),
|
|
482
485
|
S(2, A)
|
|
483
|
-
],
|
|
484
|
-
var
|
|
485
|
-
let
|
|
486
|
-
constructor(s =
|
|
486
|
+
], x);
|
|
487
|
+
var M;
|
|
488
|
+
let L = (M = class extends z {
|
|
489
|
+
constructor(s = N, t, e) {
|
|
487
490
|
super(), this._config = s, this._injector = t, this._configService = e;
|
|
488
491
|
const { ...n } = F(
|
|
489
492
|
{},
|
|
490
|
-
|
|
493
|
+
N,
|
|
491
494
|
this._config
|
|
492
495
|
);
|
|
493
496
|
this._configService.setConfig(le, n);
|
|
@@ -498,21 +501,21 @@ let x = (U = class extends V {
|
|
|
498
501
|
[
|
|
499
502
|
I,
|
|
500
503
|
{
|
|
501
|
-
useFactory: () => new
|
|
504
|
+
useFactory: () => new J(ye())
|
|
502
505
|
}
|
|
503
506
|
],
|
|
504
|
-
[m, { useClass:
|
|
507
|
+
[m, { useClass: k }]
|
|
505
508
|
].forEach((s) => this._injector.add(s)), this._injector.get(y);
|
|
506
509
|
}
|
|
507
|
-
}, a(
|
|
508
|
-
|
|
510
|
+
}, a(M, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), M);
|
|
511
|
+
L = Q([
|
|
509
512
|
S(1, C(R)),
|
|
510
513
|
S(2, A)
|
|
511
|
-
],
|
|
514
|
+
], L);
|
|
512
515
|
export {
|
|
513
516
|
de as ChannelClient,
|
|
514
517
|
pe as ChannelServer,
|
|
515
|
-
|
|
518
|
+
J as ChannelService,
|
|
516
519
|
f as DataSyncPrimaryController,
|
|
517
520
|
y as DataSyncReplicaController,
|
|
518
521
|
I as IRPCChannelService,
|
|
@@ -520,12 +523,12 @@ export {
|
|
|
520
523
|
g as IRemoteSyncService,
|
|
521
524
|
he as PLUGIN_CONFIG_KEY_MAIN_THREAD,
|
|
522
525
|
le as PLUGIN_CONFIG_KEY_WORKER_THREAD,
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
526
|
+
$ as RemoteInstanceServiceName,
|
|
527
|
+
q as RemoteSyncPrimaryService,
|
|
528
|
+
D as RemoteSyncServiceName,
|
|
529
|
+
x as UniverRPCMainThreadPlugin,
|
|
530
|
+
L as UniverRPCWorkerThreadPlugin,
|
|
531
|
+
k as WebWorkerRemoteInstanceService,
|
|
532
|
+
B as fromModule,
|
|
533
|
+
K as toModule
|
|
531
534
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
var X = Object.defineProperty;
|
|
2
2
|
var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
|
|
3
3
|
var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
-
import { createIdentifier as
|
|
4
|
+
import { createIdentifier as O, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as H, ILogService as ee, RxDisposable as W, Inject as C, Injector as R, toDisposable as te, CommandType as V, Disposable as ne, IConfigService as A, Plugin as z, merge as F } from "@univerjs/core";
|
|
5
5
|
import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
|
|
6
|
-
import { BehaviorSubject as ie, firstValueFrom as
|
|
7
|
-
const he = "rpc.main-thread.config",
|
|
8
|
-
var _e = Object.getOwnPropertyDescriptor,
|
|
9
|
-
for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1,
|
|
10
|
-
(
|
|
6
|
+
import { BehaviorSubject as ie, firstValueFrom as oe, Observable as T, isObservable as ce, of as ae, shareReplay as G } from "rxjs";
|
|
7
|
+
const he = "rpc.main-thread.config", j = {}, le = "rpc.worker-thread.config", N = {};
|
|
8
|
+
var _e = Object.getOwnPropertyDescriptor, Y = (s, t, e, n) => {
|
|
9
|
+
for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
10
|
+
(o = s[i]) && (r = o(r) || r);
|
|
11
11
|
return r;
|
|
12
12
|
}, p = (s, t) => (e, n) => t(e, n, s);
|
|
13
|
-
const
|
|
14
|
-
let
|
|
13
|
+
const D = "rpc.remote-sync.service", g = O(D);
|
|
14
|
+
let q = class {
|
|
15
15
|
constructor(s) {
|
|
16
16
|
this._commandService = s;
|
|
17
17
|
}
|
|
@@ -24,11 +24,11 @@ let M = class {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
q = Y([
|
|
28
28
|
p(0, b)
|
|
29
|
-
],
|
|
30
|
-
const
|
|
31
|
-
let
|
|
29
|
+
], q);
|
|
30
|
+
const $ = "univer.remote-instance-service", m = O($);
|
|
31
|
+
let k = class {
|
|
32
32
|
constructor(s, t, e) {
|
|
33
33
|
this._univerInstanceService = s, this._commandService = t, this._logService = e;
|
|
34
34
|
}
|
|
@@ -63,12 +63,12 @@ let q = class {
|
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
|
-
|
|
67
|
-
p(0,
|
|
66
|
+
k = Y([
|
|
67
|
+
p(0, H),
|
|
68
68
|
p(1, b),
|
|
69
69
|
p(2, ee)
|
|
70
|
-
],
|
|
71
|
-
function
|
|
70
|
+
], k);
|
|
71
|
+
function B(s) {
|
|
72
72
|
const t = s;
|
|
73
73
|
return new class {
|
|
74
74
|
call(e, n) {
|
|
@@ -83,13 +83,13 @@ function Y(s) {
|
|
|
83
83
|
const r = t[e];
|
|
84
84
|
if (typeof r == "function") {
|
|
85
85
|
const i = n ? r.apply(t, n) : r.call(t);
|
|
86
|
-
return
|
|
86
|
+
return ce(i) ? i : ae(i);
|
|
87
87
|
}
|
|
88
88
|
throw new Error(`[RPC]: observable method not found for ${e}!`);
|
|
89
89
|
}
|
|
90
90
|
}();
|
|
91
91
|
}
|
|
92
|
-
function
|
|
92
|
+
function K(s) {
|
|
93
93
|
return new Proxy({}, {
|
|
94
94
|
get(t, e) {
|
|
95
95
|
if (e !== "dispose")
|
|
@@ -102,7 +102,7 @@ function B(s) {
|
|
|
102
102
|
function ue(s) {
|
|
103
103
|
return s.endsWith("$");
|
|
104
104
|
}
|
|
105
|
-
class de extends
|
|
105
|
+
class de extends W {
|
|
106
106
|
constructor(e) {
|
|
107
107
|
super();
|
|
108
108
|
a(this, "_initialized", new ie(!1));
|
|
@@ -130,40 +130,42 @@ class de extends O {
|
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
132
|
_whenReady() {
|
|
133
|
-
return
|
|
133
|
+
return oe(
|
|
134
134
|
this._initialized.pipe(
|
|
135
135
|
se((e) => e),
|
|
136
136
|
re(1)
|
|
137
137
|
)
|
|
138
138
|
);
|
|
139
139
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
_remoteCall(e, n, r) {
|
|
141
|
+
return this._initialized.getValue() ? this._doRemoteCall(e, n, r) : this._whenReady().then(() => this._doRemoteCall(e, n, r));
|
|
142
|
+
}
|
|
143
|
+
_doRemoteCall(e, n, r) {
|
|
144
|
+
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args: r }, c = this;
|
|
145
|
+
return new Promise((w, E) => {
|
|
146
|
+
const l = {
|
|
147
|
+
handle(P) {
|
|
148
|
+
switch (P.type) {
|
|
147
149
|
case 201:
|
|
148
|
-
|
|
150
|
+
c._pendingRequests.delete(i), w(P.data);
|
|
149
151
|
break;
|
|
150
152
|
case 202:
|
|
151
|
-
|
|
153
|
+
c._pendingRequests.delete(i), E(P.data);
|
|
152
154
|
break;
|
|
153
155
|
default:
|
|
154
156
|
throw new Error("[ChannelClient]: unknown response type!");
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
159
|
};
|
|
158
|
-
this._pendingRequests.set(i,
|
|
160
|
+
this._pendingRequests.set(i, l), this._sendRequest(h);
|
|
159
161
|
});
|
|
160
162
|
}
|
|
161
163
|
_remoteSubscribe(e, n, r) {
|
|
162
|
-
return new
|
|
163
|
-
let
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
164
|
+
return new T((i) => {
|
|
165
|
+
let o = -1;
|
|
166
|
+
const h = () => {
|
|
167
|
+
o = ++this._lastRequestCounter;
|
|
168
|
+
const w = { seq: o, type: 101, channelName: e, method: n, args: r }, E = {
|
|
167
169
|
handle(l) {
|
|
168
170
|
switch (l.type) {
|
|
169
171
|
case 300:
|
|
@@ -180,17 +182,18 @@ class de extends O {
|
|
|
180
182
|
}
|
|
181
183
|
}
|
|
182
184
|
};
|
|
183
|
-
this._pendingRequests.set(
|
|
184
|
-
}
|
|
185
|
-
|
|
185
|
+
this._pendingRequests.set(o, E), this._sendRequest(w);
|
|
186
|
+
};
|
|
187
|
+
return this._initialized.getValue() ? h() : this._whenReady().then(h), () => {
|
|
188
|
+
if (o === -1)
|
|
186
189
|
return;
|
|
187
|
-
const
|
|
190
|
+
const c = {
|
|
188
191
|
type: 102,
|
|
189
|
-
seq:
|
|
192
|
+
seq: o,
|
|
190
193
|
channelName: e,
|
|
191
194
|
method: n
|
|
192
195
|
};
|
|
193
|
-
this._sendRequest(
|
|
196
|
+
this._sendRequest(c);
|
|
194
197
|
};
|
|
195
198
|
});
|
|
196
199
|
}
|
|
@@ -209,14 +212,14 @@ class de extends O {
|
|
|
209
212
|
case 300:
|
|
210
213
|
case 302:
|
|
211
214
|
case 301: {
|
|
212
|
-
const { _pendingRequests:
|
|
213
|
-
(i =
|
|
215
|
+
const { _pendingRequests: o } = this;
|
|
216
|
+
(i = o.get(r)) == null || i.handle(e), n !== 300 && o.delete(r);
|
|
214
217
|
break;
|
|
215
218
|
}
|
|
216
219
|
}
|
|
217
220
|
}
|
|
218
221
|
}
|
|
219
|
-
class pe extends
|
|
222
|
+
class pe extends W {
|
|
220
223
|
constructor(e) {
|
|
221
224
|
super();
|
|
222
225
|
a(this, "_channels", /* @__PURE__ */ new Map());
|
|
@@ -253,19 +256,19 @@ class pe extends O {
|
|
|
253
256
|
});
|
|
254
257
|
}
|
|
255
258
|
_onMethodCall(e) {
|
|
256
|
-
const { channelName: n, method: r, args: i } = e,
|
|
259
|
+
const { channelName: n, method: r, args: i } = e, o = this._channels.get(n);
|
|
257
260
|
let h;
|
|
258
261
|
try {
|
|
259
|
-
if (!
|
|
262
|
+
if (!o)
|
|
260
263
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
261
|
-
h = i ?
|
|
262
|
-
} catch (
|
|
263
|
-
h = Promise.reject(
|
|
264
|
+
h = i ? o.call(r, i) : o.call(r);
|
|
265
|
+
} catch (c) {
|
|
266
|
+
h = Promise.reject(c);
|
|
264
267
|
}
|
|
265
|
-
h.then((
|
|
266
|
-
this._sendResponse({ seq: e.seq, type: 201, data:
|
|
267
|
-
}).catch((
|
|
268
|
-
|
|
268
|
+
h.then((c) => {
|
|
269
|
+
this._sendResponse({ seq: e.seq, type: 201, data: c });
|
|
270
|
+
}).catch((c) => {
|
|
271
|
+
c instanceof Error ? this._sendResponse({ seq: e.seq, type: 202, data: c.message }) : this._sendResponse({ seq: e.seq, type: 202, data: String(c) });
|
|
269
272
|
});
|
|
270
273
|
}
|
|
271
274
|
_onSubscribe(e) {
|
|
@@ -274,11 +277,11 @@ class pe extends O {
|
|
|
274
277
|
if (!i)
|
|
275
278
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
276
279
|
const h = i.subscribe(e.method, e.args).subscribe({
|
|
277
|
-
next: (
|
|
278
|
-
this._sendResponse({ seq: r, type: 300, data:
|
|
280
|
+
next: (c) => {
|
|
281
|
+
this._sendResponse({ seq: r, type: 300, data: c });
|
|
279
282
|
},
|
|
280
|
-
error: (
|
|
281
|
-
this._sendResponse({ seq: r, type: 301, data:
|
|
283
|
+
error: (c) => {
|
|
284
|
+
this._sendResponse({ seq: r, type: 301, data: c.message }), this._sendResponse({
|
|
282
285
|
seq: r,
|
|
283
286
|
type: 302
|
|
284
287
|
/* SUBSCRIBE_COMPLETE */
|
|
@@ -293,8 +296,8 @@ class pe extends O {
|
|
|
293
296
|
}
|
|
294
297
|
});
|
|
295
298
|
this._subscriptions.set(e.seq, h);
|
|
296
|
-
} catch (
|
|
297
|
-
|
|
299
|
+
} catch (o) {
|
|
300
|
+
o instanceof Error ? this._sendResponse({ seq: e.seq, type: 301, data: o.message }) : this._sendResponse({ seq: e.seq, type: 301, data: String(o) });
|
|
298
301
|
}
|
|
299
302
|
}
|
|
300
303
|
_onUnsubscribe(e) {
|
|
@@ -305,8 +308,8 @@ class pe extends O {
|
|
|
305
308
|
this._protocol.send(e);
|
|
306
309
|
}
|
|
307
310
|
}
|
|
308
|
-
const I =
|
|
309
|
-
class
|
|
311
|
+
const I = O("IRPCChannelService");
|
|
312
|
+
class J {
|
|
310
313
|
constructor(t) {
|
|
311
314
|
a(this, "_client");
|
|
312
315
|
a(this, "_server");
|
|
@@ -323,11 +326,11 @@ class K {
|
|
|
323
326
|
}
|
|
324
327
|
}
|
|
325
328
|
var ve = Object.getOwnPropertyDescriptor, ge = (s, t, e, n) => {
|
|
326
|
-
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1,
|
|
327
|
-
(
|
|
329
|
+
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
330
|
+
(o = s[i]) && (r = o(r) || r);
|
|
328
331
|
return r;
|
|
329
332
|
}, _ = (s, t) => (e, n) => t(e, n, s);
|
|
330
|
-
let f = class extends
|
|
333
|
+
let f = class extends W {
|
|
331
334
|
constructor(t, e, n, r, i) {
|
|
332
335
|
super();
|
|
333
336
|
a(this, "_remoteInstanceService");
|
|
@@ -346,9 +349,9 @@ let f = class extends O {
|
|
|
346
349
|
return this._syncingUnits.add(t), te(() => this._syncingUnits.delete(t));
|
|
347
350
|
}
|
|
348
351
|
_initRPCChannels() {
|
|
349
|
-
this._rpcChannelService.registerChannel(
|
|
352
|
+
this._rpcChannelService.registerChannel(D, B(this._remoteSyncService)), this._injector.add([
|
|
350
353
|
m,
|
|
351
|
-
{ useFactory: () =>
|
|
354
|
+
{ useFactory: () => K(this._rpcChannelService.requestChannel($)) }
|
|
352
355
|
]), this._remoteInstanceService = this._injector.get(m);
|
|
353
356
|
}
|
|
354
357
|
_init() {
|
|
@@ -363,9 +366,9 @@ let f = class extends O {
|
|
|
363
366
|
unitID: t.getUnitId()
|
|
364
367
|
});
|
|
365
368
|
}), this.disposeWithMe(this._commandService.onCommandExecuted((t, e) => {
|
|
366
|
-
const { type: n, params: r, id: i } = t,
|
|
367
|
-
n ===
|
|
368
|
-
(!
|
|
369
|
+
const { type: n, params: r, id: i } = t, o = (r == null ? void 0 : r.unitId) || "";
|
|
370
|
+
n === V.MUTATION && // only sync mutations to the worker thread
|
|
371
|
+
(!o || this._syncingUnits.has(o)) && // do not sync mutations from the web worker back to the web worker
|
|
369
372
|
!(e != null && e.fromSync) && // do not sync mutations those are not meant to be synced
|
|
370
373
|
this._syncingMutations.has(i) && this._remoteInstanceService.syncMutation({ mutationInfo: t }, e);
|
|
371
374
|
}));
|
|
@@ -374,13 +377,13 @@ let f = class extends O {
|
|
|
374
377
|
f = ge([
|
|
375
378
|
_(0, C(R)),
|
|
376
379
|
_(1, b),
|
|
377
|
-
_(2,
|
|
380
|
+
_(2, H),
|
|
378
381
|
_(3, I),
|
|
379
382
|
_(4, g)
|
|
380
383
|
], f);
|
|
381
384
|
var me = Object.getOwnPropertyDescriptor, fe = (s, t, e, n) => {
|
|
382
|
-
for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1,
|
|
383
|
-
(
|
|
385
|
+
for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
386
|
+
(o = s[i]) && (r = o(r) || r);
|
|
384
387
|
return r;
|
|
385
388
|
}, d = (s, t) => (e, n) => t(e, n, s);
|
|
386
389
|
let y = class extends ne {
|
|
@@ -390,16 +393,16 @@ let y = class extends ne {
|
|
|
390
393
|
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init();
|
|
391
394
|
}
|
|
392
395
|
_initRPCChannels() {
|
|
393
|
-
this._rpcChannelService.registerChannel(
|
|
396
|
+
this._rpcChannelService.registerChannel($, B(this._remoteInstanceService)), this._injector.add([
|
|
394
397
|
g,
|
|
395
|
-
{ useFactory: () =>
|
|
398
|
+
{ useFactory: () => K(this._rpcChannelService.requestChannel(D)) }
|
|
396
399
|
]), this._remoteSyncService = this._injector.get(g);
|
|
397
400
|
}
|
|
398
401
|
_init() {
|
|
399
402
|
this.disposeWithMe(
|
|
400
403
|
// Mutations executed on the main thread should be synced to the worker thread.
|
|
401
404
|
this._commandService.onCommandExecuted((t, e) => {
|
|
402
|
-
t.type ===
|
|
405
|
+
t.type === V.MUTATION && !(e != null && e.fromSync) && this._remoteSyncService.syncMutation({
|
|
403
406
|
mutationInfo: t
|
|
404
407
|
}, e);
|
|
405
408
|
})
|
|
@@ -417,12 +420,12 @@ function ye() {
|
|
|
417
420
|
send(s) {
|
|
418
421
|
postMessage(s);
|
|
419
422
|
},
|
|
420
|
-
onMessage: new
|
|
423
|
+
onMessage: new T((s) => {
|
|
421
424
|
const t = (e) => {
|
|
422
425
|
s.next(e.data);
|
|
423
426
|
};
|
|
424
427
|
return addEventListener("message", t), () => removeEventListener("message", t);
|
|
425
|
-
}).pipe(
|
|
428
|
+
}).pipe(G(1))
|
|
426
429
|
};
|
|
427
430
|
}
|
|
428
431
|
function Se(s) {
|
|
@@ -430,27 +433,27 @@ function Se(s) {
|
|
|
430
433
|
send(t) {
|
|
431
434
|
s.postMessage(t);
|
|
432
435
|
},
|
|
433
|
-
onMessage: new
|
|
436
|
+
onMessage: new T((t) => {
|
|
434
437
|
const e = (n) => {
|
|
435
438
|
t.next(n.data);
|
|
436
439
|
};
|
|
437
440
|
return s.addEventListener("message", e), () => s.removeEventListener("message", e);
|
|
438
|
-
}).pipe(
|
|
441
|
+
}).pipe(G(1))
|
|
439
442
|
};
|
|
440
443
|
}
|
|
441
|
-
var be = Object.getOwnPropertyDescriptor,
|
|
442
|
-
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1,
|
|
443
|
-
(
|
|
444
|
+
var be = Object.getOwnPropertyDescriptor, Q = (s, t, e, n) => {
|
|
445
|
+
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, o; i >= 0; i--)
|
|
446
|
+
(o = s[i]) && (r = o(r) || r);
|
|
444
447
|
return r;
|
|
445
|
-
}, S = (s, t) => (e, n) => t(e, n, s),
|
|
446
|
-
let
|
|
447
|
-
constructor(t =
|
|
448
|
+
}, S = (s, t) => (e, n) => t(e, n, s), U;
|
|
449
|
+
let x = (U = class extends z {
|
|
450
|
+
constructor(t = j, e, n) {
|
|
448
451
|
super();
|
|
449
452
|
a(this, "_internalWorker", null);
|
|
450
453
|
this._config = t, this._injector = e, this._configService = n;
|
|
451
454
|
const { ...r } = F(
|
|
452
455
|
{},
|
|
453
|
-
|
|
456
|
+
j,
|
|
454
457
|
this._config
|
|
455
458
|
);
|
|
456
459
|
this._configService.setConfig(he, r);
|
|
@@ -469,25 +472,25 @@ let N = (P = class extends V {
|
|
|
469
472
|
[
|
|
470
473
|
I,
|
|
471
474
|
{
|
|
472
|
-
useFactory: () => new
|
|
475
|
+
useFactory: () => new J(n)
|
|
473
476
|
}
|
|
474
477
|
],
|
|
475
478
|
[f],
|
|
476
|
-
[g, { useClass:
|
|
479
|
+
[g, { useClass: q }]
|
|
477
480
|
].forEach((i) => this._injector.add(i)), this._injector.get(f);
|
|
478
481
|
}
|
|
479
|
-
}, a(
|
|
480
|
-
|
|
482
|
+
}, a(U, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), U);
|
|
483
|
+
x = Q([
|
|
481
484
|
S(1, C(R)),
|
|
482
485
|
S(2, A)
|
|
483
|
-
],
|
|
484
|
-
var
|
|
485
|
-
let
|
|
486
|
-
constructor(s =
|
|
486
|
+
], x);
|
|
487
|
+
var M;
|
|
488
|
+
let L = (M = class extends z {
|
|
489
|
+
constructor(s = N, t, e) {
|
|
487
490
|
super(), this._config = s, this._injector = t, this._configService = e;
|
|
488
491
|
const { ...n } = F(
|
|
489
492
|
{},
|
|
490
|
-
|
|
493
|
+
N,
|
|
491
494
|
this._config
|
|
492
495
|
);
|
|
493
496
|
this._configService.setConfig(le, n);
|
|
@@ -498,21 +501,21 @@ let x = (U = class extends V {
|
|
|
498
501
|
[
|
|
499
502
|
I,
|
|
500
503
|
{
|
|
501
|
-
useFactory: () => new
|
|
504
|
+
useFactory: () => new J(ye())
|
|
502
505
|
}
|
|
503
506
|
],
|
|
504
|
-
[m, { useClass:
|
|
507
|
+
[m, { useClass: k }]
|
|
505
508
|
].forEach((s) => this._injector.add(s)), this._injector.get(y);
|
|
506
509
|
}
|
|
507
|
-
}, a(
|
|
508
|
-
|
|
510
|
+
}, a(M, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), M);
|
|
511
|
+
L = Q([
|
|
509
512
|
S(1, C(R)),
|
|
510
513
|
S(2, A)
|
|
511
|
-
],
|
|
514
|
+
], L);
|
|
512
515
|
export {
|
|
513
516
|
de as ChannelClient,
|
|
514
517
|
pe as ChannelServer,
|
|
515
|
-
|
|
518
|
+
J as ChannelService,
|
|
516
519
|
f as DataSyncPrimaryController,
|
|
517
520
|
y as DataSyncReplicaController,
|
|
518
521
|
I as IRPCChannelService,
|
|
@@ -520,12 +523,12 @@ export {
|
|
|
520
523
|
g as IRemoteSyncService,
|
|
521
524
|
he as PLUGIN_CONFIG_KEY_MAIN_THREAD,
|
|
522
525
|
le as PLUGIN_CONFIG_KEY_WORKER_THREAD,
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
526
|
+
$ as RemoteInstanceServiceName,
|
|
527
|
+
q as RemoteSyncPrimaryService,
|
|
528
|
+
D as RemoteSyncServiceName,
|
|
529
|
+
x as UniverRPCMainThreadPlugin,
|
|
530
|
+
L as UniverRPCWorkerThreadPlugin,
|
|
531
|
+
k as WebWorkerRemoteInstanceService,
|
|
532
|
+
B as fromModule,
|
|
533
|
+
K as toModule
|
|
531
534
|
};
|
|
@@ -51,6 +51,7 @@ export declare class ChannelClient extends RxDisposable implements IChannelClien
|
|
|
51
51
|
getChannel<T extends IChannel>(channelName: string): T;
|
|
52
52
|
private _whenReady;
|
|
53
53
|
private _remoteCall;
|
|
54
|
+
private _doRemoteCall;
|
|
54
55
|
private _remoteSubscribe;
|
|
55
56
|
private _sendRequest;
|
|
56
57
|
private _onMessage;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs/operators","rxjs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.UniverRpc={},r.UniverCore,r.rxjs.operators,r.rxjs))})(this,(function(r,a,u,d){"use strict";var J=Object.defineProperty;var Q=(r,a,u)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[a]=u;var h=(r,a,u)=>Q(r,typeof a!="symbol"?a+"":a,u);var U,E;const W="rpc.main-thread.config",k={},D="rpc.worker-thread.config",q={};var L=Object.getOwnPropertyDescriptor,O=(c,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},g=(c,t)=>(e,n)=>t(e,n,c);const C="rpc.remote-sync.service",v=a.createIdentifier(C);r.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t,e){const{fromCollab:n,...s}=e||{};return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{...s,onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=O([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const f="univer.remote-instance-service",m=a.createIdentifier(f);r.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t,e){return this._applyMutation(t.mutationInfo,e)}async createInstance(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);const{type:e,snapshot:n}=t;try{if(e===a.UniverInstanceType.UNIVER_SHEET)return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${t.unitID}`),this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t,e){const{id:n,params:s}=t,{fromCollab:i,...o}=e||{};return this._commandService.syncExecuteCommand(n,s,{...o,onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=O([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=n?s.apply(t,n):s.call(t);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=n?s.apply(t,n):s.call(t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return H(e)?c.subscribe(e,n):c.call(e,n)}}})}function H(c){return c.endsWith("$")}class j extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new d.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}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 d.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,_={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((M,S)=>{const B={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),S(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,B),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},M={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,M),this._sendRequest(l)}),()=>{if(o===-1)return;const _={type:102,seq:o,channelName:e,method:n};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var i;const{type:n,seq:s}=e;switch(n){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:o}=this;(i=o.get(s))==null||i.handle(e),n!==300&&o.delete(s);break}}}}class N extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:i}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=i?o.call(s,i):o.call(s)}catch(l){_=Promise.reject(l)}_.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,i=this._channels.get(n);try{if(!i)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:s,type:300,data:l})},error:l=>{this._sendResponse({seq:s,type:301,data:l.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,_)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const p=a.createIdentifier("IRPCChannelService");class w{constructor(t){h(this,"_client");h(this,"_server");this._client=new j(t),this._server=new N(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var A=Object.getOwnPropertyDescriptor,G=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},y=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>P(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:a.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:i,id:o}=e,_=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}},r.DataSyncPrimaryController=G([y(0,a.Inject(a.Injector)),y(1,a.ICommandService),y(2,a.IUniverInstanceService),y(3,p),y(4,v)],r.DataSyncPrimaryController);var F=Object.getOwnPropertyDescriptor,V=(c,t,e,n)=>{for(var s=n>1?void 0:n?F(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},R=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===a.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}},r.DataSyncReplicaController=V([R(0,a.Inject(a.Injector)),R(1,m),R(2,a.ICommandService),R(3,p)],r.DataSyncReplicaController);function z(){return{send(c){postMessage(c)},onMessage:new d.Observable(c=>{const t=e=>{c.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function K(c){return{send(t){c.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return c.addEventListener("message",e),()=>c.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var Y=Object.getOwnPropertyDescriptor,$=(c,t,e,n)=>{for(var s=n>1?void 0:n?Y(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=k,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},k,this._config);this._configService.setConfig(W,i)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=K(n);[[p,{useFactory:()=>new w(s)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>this._injector.add(o)),this._injector.get(r.DataSyncPrimaryController)}},h(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),r.UniverRPCMainThreadPlugin=$([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=q,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},q,this._config);this._configService.setConfig(D,s)}onStarting(){[[r.DataSyncReplicaController],[p,{useFactory:()=>new w(z())}],[m,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(r.DataSyncReplicaController)}},h(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),r.UniverRPCWorkerThreadPlugin=$([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=j,r.ChannelServer=N,r.ChannelService=w,r.IRPCChannelService=p,r.IRemoteInstanceService=m,r.IRemoteSyncService=v,r.PLUGIN_CONFIG_KEY_MAIN_THREAD=W,r.PLUGIN_CONFIG_KEY_WORKER_THREAD=D,r.RemoteInstanceServiceName=f,r.RemoteSyncServiceName=C,r.fromModule=b,r.toModule=P,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs/operators","rxjs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.UniverRpc={},r.UniverCore,r.rxjs.operators,r.rxjs))})(this,(function(r,a,u,d){"use strict";var J=Object.defineProperty;var Q=(r,a,u)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[a]=u;var h=(r,a,u)=>Q(r,typeof a!="symbol"?a+"":a,u);var U,E;const k="rpc.main-thread.config",D={},q="rpc.worker-thread.config",O={};var H=Object.getOwnPropertyDescriptor,j=(c,s,e,n)=>{for(var t=n>1?void 0:n?H(s,e):s,i=c.length-1,o;i>=0;i--)(o=c[i])&&(t=o(t)||t);return t},g=(c,s)=>(e,n)=>s(e,n,c);const C="rpc.remote-sync.service",v=a.createIdentifier(C);r.RemoteSyncPrimaryService=class{constructor(s){this._commandService=s}async syncMutation(s,e){const{fromCollab:n,...t}=e||{};return this._commandService.syncExecuteCommand(s.mutationInfo.id,s.mutationInfo.params,{...t,onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=j([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const f="univer.remote-instance-service",m=a.createIdentifier(f);r.WebWorkerRemoteInstanceService=class{constructor(s,e,n){this._univerInstanceService=s,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(s,e){return this._applyMutation(s.mutationInfo,e)}async createInstance(s){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${s.unitID}`);const{type:e,snapshot:n}=s;try{if(e===a.UniverInstanceType.UNIVER_SHEET)return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}catch(t){throw t instanceof Error?t:new TypeError(`${t}`)}}async disposeInstance(s){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${s.unitID}`),this._univerInstanceService.disposeUnit(s.unitID)}_applyMutation(s,e){const{id:n,params:t}=s,{fromCollab:i,...o}=e||{};return this._commandService.syncExecuteCommand(n,t,{...o,onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=j([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const s=c;return new class{call(e,n){const t=s[e];if(typeof t=="function"){let i=n?t.apply(s,n):t.call(s);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const t=s[e];if(typeof t=="function"){const i=n?t.apply(s,n):t.call(s);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(s,e){if(e!=="dispose")return function(...n){return A(e)?c.subscribe(e,n):c.call(e,n)}}})}function A(c){return c.endsWith("$")}class N extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new d.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(t,i){return n._disposed?Promise.reject():n._remoteCall(e,t,i)},subscribe(t,i){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,t,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}_remoteCall(e,n,t){return this._initialized.getValue()?this._doRemoteCall(e,n,t):this._whenReady().then(()=>this._doRemoteCall(e,n,t))}_doRemoteCall(e,n,t){const i=++this._lastRequestCounter,_={seq:i,type:100,channelName:e,method:n,args:t},l=this;return new Promise((M,T)=>{const y={handle(W){switch(W.type){case 201:l._pendingRequests.delete(i),M(W.data);break;case 202:l._pendingRequests.delete(i),T(W.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,y),this._sendRequest(_)})}_remoteSubscribe(e,n,t){return new d.Observable(i=>{let o=-1;const _=()=>{o=++this._lastRequestCounter;const M={seq:o,type:101,channelName:e,method:n,args:t},T={handle(y){switch(y.type){case 300:i.next(y.data);break;case 301:i.error(y.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,T),this._sendRequest(M)};return this._initialized.getValue()?_():this._whenReady().then(_),()=>{if(o===-1)return;const l={type:102,seq:o,channelName:e,method:n};this._sendRequest(l)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var i;const{type:n,seq:t}=e;switch(n){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:o}=this;(i=o.get(t))==null||i.handle(e),n!==300&&o.delete(t);break}}}}class $ extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:t,args:i}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=i?o.call(t,i):o.call(t)}catch(l){_=Promise.reject(l)}_.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:n,seq:t}=e,i=this._channels.get(n);try{if(!i)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:t,type:300,data:l})},error:l=>{this._sendResponse({seq:t,type:301,data:l.message}),this._sendResponse({seq:t,type:302})},complete:()=>{this._sendResponse({seq:t,type:302})}});this._subscriptions.set(e.seq,_)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const p=a.createIdentifier("IRPCChannelService");class w{constructor(s){h(this,"_client");h(this,"_server");this._client=new N(s),this._server=new $(s)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(s){return this._client.getChannel(s)}registerChannel(s,e){this._server.registerChannel(s,e)}}var G=Object.getOwnPropertyDescriptor,V=(c,s,e,n)=>{for(var t=n>1?void 0:n?G(s,e):s,i=c.length-1,o;i>=0;i--)(o=c[i])&&(t=o(t)||t);return t},S=(c,s)=>(e,n)=>s(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,t,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=t,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>P(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:a.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:t,params:i,id:o}=e,_=(i==null?void 0:i.unitId)||"";t===a.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}},r.DataSyncPrimaryController=V([S(0,a.Inject(a.Injector)),S(1,a.ICommandService),S(2,a.IUniverInstanceService),S(3,p),S(4,v)],r.DataSyncPrimaryController);var F=Object.getOwnPropertyDescriptor,z=(c,s,e,n)=>{for(var t=n>1?void 0:n?F(s,e):s,i=c.length-1,o;i>=0;i--)(o=c[i])&&(t=o(t)||t);return t},R=(c,s)=>(e,n)=>s(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,t,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=t,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===a.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}},r.DataSyncReplicaController=z([R(0,a.Inject(a.Injector)),R(1,m),R(2,a.ICommandService),R(3,p)],r.DataSyncReplicaController);function K(){return{send(c){postMessage(c)},onMessage:new d.Observable(c=>{const s=e=>{c.next(e.data)};return addEventListener("message",s),()=>removeEventListener("message",s)}).pipe(d.shareReplay(1))}}function Y(c){return{send(s){c.postMessage(s)},onMessage:new d.Observable(s=>{const e=n=>{s.next(n.data)};return c.addEventListener("message",e),()=>c.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var B=Object.getOwnPropertyDescriptor,L=(c,s,e,n)=>{for(var t=n>1?void 0:n?B(s,e):s,i=c.length-1,o;i>=0;i--)(o=c[i])&&(t=o(t)||t);return t},I=(c,s)=>(e,n)=>s(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=D,n,t){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=t;const{...i}=a.merge({},D,this._config);this._configService.setConfig(k,i)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const t=Y(n);[[p,{useFactory:()=>new w(t)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>this._injector.add(o)),this._injector.get(r.DataSyncPrimaryController)}},h(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),r.UniverRPCMainThreadPlugin=L([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(s=O,e,n){super(),this._config=s,this._injector=e,this._configService=n;const{...t}=a.merge({},O,this._config);this._configService.setConfig(q,t)}onStarting(){[[r.DataSyncReplicaController],[p,{useFactory:()=>new w(K())}],[m,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(s=>this._injector.add(s)),this._injector.get(r.DataSyncReplicaController)}},h(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),r.UniverRPCWorkerThreadPlugin=L([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=N,r.ChannelServer=$,r.ChannelService=w,r.IRPCChannelService=p,r.IRemoteInstanceService=m,r.IRemoteSyncService=v,r.PLUGIN_CONFIG_KEY_MAIN_THREAD=k,r.PLUGIN_CONFIG_KEY_WORKER_THREAD=q,r.RemoteInstanceServiceName=f,r.RemoteSyncServiceName=C,r.fromModule=b,r.toModule=P,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "DreamNum <developer@univer.ai>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -47,14 +47,14 @@
|
|
|
47
47
|
"rxjs": ">=7.0.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@univerjs/core": "0.
|
|
50
|
+
"@univerjs/core": "0.16.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"rxjs": "^7.8.2",
|
|
54
54
|
"typescript": "^5.9.3",
|
|
55
55
|
"vite": "^7.3.1",
|
|
56
56
|
"vitest": "^4.0.18",
|
|
57
|
-
"@univerjs-infra/shared": "0.
|
|
57
|
+
"@univerjs-infra/shared": "0.16.0"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"test": "vitest run",
|