@univerjs/rpc 0.5.1 → 0.5.2
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 +143 -135
- package/lib/umd/index.js +1 -1
- package/package.json +8 -5
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var O=Object.defineProperty;var $=(i,t,e)=>t in i?O(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>$(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"),N="rpc.main-thread.config",k={},x="rpc.worker-thread.config",L={};var H=Object.defineProperty,A=Object.getOwnPropertyDescriptor,T=(i,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&H(t,e,s),s},f=(i,t)=>(e,n)=>t(e,n,i);const g="rpc.remote-sync.service",v=o.createIdentifier(g);exports.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=T([f(0,o.ICommandService)],exports.RemoteSyncPrimaryService);const R="univer.remote-instance-service",m=o.createIdentifier(R);exports.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case o.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(o.UniverInstanceType.UNIVER_SHEET,n),!0;default: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._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=T([f(0,o.IUniverInstanceService),f(1,o.ICommandService),f(2,o.ILogService)],exports.WebWorkerRemoteInstanceService);function U(i){const t=i;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let r=s.apply(t,[n]);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=s.apply(t,n);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function E(i){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return V(e)?i.subscribe(e,n[0]):i.call(e,n[0])}}})}function V(i){return i.endsWith("$")}class q 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((I,d)=>{const D={handle(b){switch(b.type){case 201:a._pendingRequests.delete(r),I(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,D),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},I={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,I),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 n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class W 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=c.call(s,r)}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 q(t),this._server=new W(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,G=(i,t,e,n)=>{for(var s=n>1?void 0:n?z(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&F(t,e,s),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(g,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),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})}))}};exports.DataSyncPrimaryController=G([p(0,o.Inject(o.Injector)),p(1,o.ICommandService),p(2,o.IUniverInstanceService),p(3,S),p(4,v)],exports.DataSyncPrimaryController);var Y=Object.defineProperty,B=Object.getOwnPropertyDescriptor,K=(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=(n?c(t,e,s):c(s))||s);return n&&s&&Y(t,e,s),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(R,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>E(this._rpcChannelService.requestChannel(g))}]),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})}))}};exports.DataSyncReplicaController=K([y(0,o.Inject(o.Injector)),y(1,m),y(2,o.ICommandService),y(3,S)],exports.DataSyncReplicaController);function J(){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 Q(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 X=Object.defineProperty,Z=Object.getOwnPropertyDescriptor,j=(i,t,e,n)=>{for(var s=n>1?void 0:n?Z(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&X(t,e,s),s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends o.Plugin{constructor(e=k,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=this._config;this._configService.setConfig(N,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=Q(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=j([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends o.Plugin{constructor(t=L,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=this._config;this._configService.setConfig(x,s)}onStarting(){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new M(J())}],[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=j([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=q;exports.ChannelServer=W;exports.ChannelService=M;exports.IRPCChannelService=S;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=R;exports.RemoteSyncServiceName=g;exports.fromModule=U;exports.toModule=E;
|
|
1
|
+
"use strict";var N=Object.defineProperty;var k=(i,t,e)=>t in i?N(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>k(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs"),x="rpc.main-thread.config",T={},L="rpc.worker-thread.config",q={};var H=Object.defineProperty,A=Object.getOwnPropertyDescriptor,W=(i,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&H(t,e,s),s},f=(i,t)=>(e,n)=>t(e,n,i);const g="rpc.remote-sync.service",v=a.createIdentifier(g);exports.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=W([f(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const R="univer.remote-instance-service",m=a.createIdentifier(R);exports.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;default: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._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=W([f(0,a.IUniverInstanceService),f(1,a.ICommandService),f(2,a.ILogService)],exports.WebWorkerRemoteInstanceService);function U(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 E(i){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return V(e)?i.subscribe(e,n):i.call(e,n)}}})}function V(i){return i.endsWith("$")}class j extends a.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},o=this;return new Promise((I,d)=>{const $={handle(b){switch(b.type){case 201:o._pendingRequests.delete(r),I(b.data);break;case 202:o._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 o={seq:c,type:101,channelName:e,method:n,args:s},I={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,I),this._sendRequest(o)}),()=>{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 n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class D extends a.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(o){h=Promise.reject(o)}h.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: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: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,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=a.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new j(t),this._server=new D(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,G=(i,t,e,n)=>{for(var s=n>1?void 0:n?z(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&F(t,e,s),s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends a.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),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(g,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),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:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===a.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=G([p(0,a.Inject(a.Injector)),p(1,a.ICommandService),p(2,a.IUniverInstanceService),p(3,S),p(4,v)],exports.DataSyncPrimaryController);var Y=Object.defineProperty,B=Object.getOwnPropertyDescriptor,K=(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=(n?c(t,e,s):c(s))||s);return n&&s&&Y(t,e,s),s},y=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends a.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(R,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>E(this._rpcChannelService.requestChannel(g))}]),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})}))}};exports.DataSyncReplicaController=K([y(0,a.Inject(a.Injector)),y(1,m),y(2,a.ICommandService),y(3,S)],exports.DataSyncReplicaController);function J(){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 Q(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 X=Object.defineProperty,Z=Object.getOwnPropertyDescriptor,O=(i,t,e,n)=>{for(var s=n>1?void 0:n?Z(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&X(t,e,s),s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends a.Plugin{constructor(e=T,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=a.merge({},T,this._config);this._configService.setConfig(x,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=Q(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=O([C(1,a.Inject(a.Injector)),C(2,a.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=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(L,s)}onStarting(){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new M(J())}],[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=O([C(1,a.Inject(a.Injector)),C(2,a.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=j;exports.ChannelServer=D;exports.ChannelService=M;exports.IRPCChannelService=S;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=R;exports.RemoteSyncServiceName=g;exports.fromModule=U;exports.toModule=E;
|
package/lib/es/index.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var a = (
|
|
4
|
-
import { createIdentifier as O, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as
|
|
5
|
-
import { takeUntil as v, filter as
|
|
6
|
-
import { isObservable as
|
|
7
|
-
const
|
|
8
|
-
var
|
|
9
|
-
for (var
|
|
10
|
-
(c =
|
|
11
|
-
return n &&
|
|
12
|
-
}, p = (
|
|
1
|
+
var X = Object.defineProperty;
|
|
2
|
+
var Z = (r, t, e) => t in r ? X(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var a = (r, t, e) => Z(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { createIdentifier as O, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as H, ILogService as ee, RxDisposable as j, Inject as C, Injector as R, toDisposable as te, CommandType as k, Disposable as ne, Plugin as A, merge as V, IConfigService as F } from "@univerjs/core";
|
|
5
|
+
import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
|
|
6
|
+
import { isObservable as ie, of as ce, BehaviorSubject as oe, firstValueFrom as ae, Observable as $, shareReplay as z } from "rxjs";
|
|
7
|
+
const he = "rpc.main-thread.config", D = {}, _e = "rpc.worker-thread.config", N = {};
|
|
8
|
+
var le = Object.defineProperty, ue = Object.getOwnPropertyDescriptor, G = (r, t, e, n) => {
|
|
9
|
+
for (var s = n > 1 ? void 0 : n ? ue(t, e) : t, i = r.length - 1, c; i >= 0; i--)
|
|
10
|
+
(c = r[i]) && (s = (n ? c(t, e, s) : c(s)) || s);
|
|
11
|
+
return n && s && le(t, e, s), s;
|
|
12
|
+
}, p = (r, t) => (e, n) => t(e, n, r);
|
|
13
13
|
const T = "rpc.remote-sync.service", f = O(T);
|
|
14
14
|
let M = class {
|
|
15
|
-
constructor(
|
|
16
|
-
this._commandService =
|
|
15
|
+
constructor(r) {
|
|
16
|
+
this._commandService = r;
|
|
17
17
|
}
|
|
18
|
-
async syncMutation(
|
|
19
|
-
return this._commandService.syncExecuteCommand(
|
|
18
|
+
async syncMutation(r) {
|
|
19
|
+
return this._commandService.syncExecuteCommand(r.mutationInfo.id, r.mutationInfo.params, {
|
|
20
20
|
onlyLocal: !0,
|
|
21
21
|
fromSync: !0
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
|
-
M =
|
|
25
|
+
M = G([
|
|
26
26
|
p(0, b)
|
|
27
27
|
], M);
|
|
28
28
|
const W = "univer.remote-instance-service", m = O(W);
|
|
29
29
|
let q = class {
|
|
30
|
-
constructor(
|
|
31
|
-
this._univerInstanceService =
|
|
30
|
+
constructor(r, t, e) {
|
|
31
|
+
this._univerInstanceService = r, this._commandService = t, this._logService = e;
|
|
32
32
|
}
|
|
33
33
|
whenReady() {
|
|
34
34
|
return Promise.resolve(!0);
|
|
35
35
|
}
|
|
36
|
-
async syncMutation(
|
|
37
|
-
return this._applyMutation(
|
|
36
|
+
async syncMutation(r) {
|
|
37
|
+
return this._applyMutation(r.mutationInfo);
|
|
38
38
|
}
|
|
39
|
-
async createInstance(
|
|
40
|
-
const { type: t, snapshot: e } =
|
|
39
|
+
async createInstance(r) {
|
|
40
|
+
const { type: t, snapshot: e } = r;
|
|
41
41
|
try {
|
|
42
42
|
switch (t) {
|
|
43
43
|
case u.UNIVER_SHEET:
|
|
@@ -51,60 +51,60 @@ let q = class {
|
|
|
51
51
|
throw n instanceof Error ? n : new TypeError(`${n}`);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
async disposeInstance(
|
|
55
|
-
return this._univerInstanceService.disposeUnit(
|
|
54
|
+
async disposeInstance(r) {
|
|
55
|
+
return this._univerInstanceService.disposeUnit(r.unitID);
|
|
56
56
|
}
|
|
57
|
-
_applyMutation(
|
|
58
|
-
const { id: t, params: e } =
|
|
57
|
+
_applyMutation(r) {
|
|
58
|
+
const { id: t, params: e } = r;
|
|
59
59
|
return this._commandService.syncExecuteCommand(t, e, {
|
|
60
60
|
onlyLocal: !0,
|
|
61
61
|
fromSync: !0
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
|
-
q =
|
|
66
|
-
p(0,
|
|
65
|
+
q = G([
|
|
66
|
+
p(0, H),
|
|
67
67
|
p(1, b),
|
|
68
|
-
p(2,
|
|
68
|
+
p(2, ee)
|
|
69
69
|
], q);
|
|
70
|
-
function
|
|
71
|
-
const t =
|
|
70
|
+
function Y(r) {
|
|
71
|
+
const t = r;
|
|
72
72
|
return new class {
|
|
73
73
|
call(e, n) {
|
|
74
|
-
const
|
|
75
|
-
if (typeof
|
|
76
|
-
let i =
|
|
74
|
+
const s = t[e];
|
|
75
|
+
if (typeof s == "function") {
|
|
76
|
+
let i = n ? s.apply(t, n) : s.call(t);
|
|
77
77
|
return i instanceof Promise || (i = Promise.resolve(i)), i;
|
|
78
78
|
}
|
|
79
79
|
throw new Error(`[RPC]: method not found for ${e}!`);
|
|
80
80
|
}
|
|
81
81
|
subscribe(e, n) {
|
|
82
|
-
const
|
|
83
|
-
if (typeof
|
|
84
|
-
const i =
|
|
85
|
-
return
|
|
82
|
+
const s = t[e];
|
|
83
|
+
if (typeof s == "function") {
|
|
84
|
+
const i = n ? s.apply(t, n) : s.call(t);
|
|
85
|
+
return ie(i) ? i : ce(i);
|
|
86
86
|
}
|
|
87
87
|
throw new Error(`[RPC]: observable method not found for ${e}!`);
|
|
88
88
|
}
|
|
89
89
|
}();
|
|
90
90
|
}
|
|
91
|
-
function
|
|
91
|
+
function B(r) {
|
|
92
92
|
return new Proxy({}, {
|
|
93
93
|
get(t, e) {
|
|
94
94
|
if (e !== "dispose")
|
|
95
95
|
return function(...n) {
|
|
96
|
-
return
|
|
96
|
+
return de(e) ? r.subscribe(e, n) : r.call(e, n);
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
|
-
function
|
|
102
|
-
return
|
|
101
|
+
function de(r) {
|
|
102
|
+
return r.endsWith("$");
|
|
103
103
|
}
|
|
104
|
-
class
|
|
104
|
+
class pe extends j {
|
|
105
105
|
constructor(e) {
|
|
106
106
|
super();
|
|
107
|
-
a(this, "_initialized", new
|
|
107
|
+
a(this, "_initialized", new oe(!1));
|
|
108
108
|
a(this, "_lastRequestCounter", 0);
|
|
109
109
|
a(this, "_pendingRequests", /* @__PURE__ */ new Map());
|
|
110
110
|
this._protocol = e, this._protocol.send({
|
|
@@ -118,29 +118,29 @@ class de extends j {
|
|
|
118
118
|
getChannel(e) {
|
|
119
119
|
const n = this;
|
|
120
120
|
return {
|
|
121
|
-
call(
|
|
122
|
-
return n._disposed ? Promise.reject() : n._remoteCall(e,
|
|
121
|
+
call(s, i) {
|
|
122
|
+
return n._disposed ? Promise.reject() : n._remoteCall(e, s, i);
|
|
123
123
|
},
|
|
124
|
-
subscribe(
|
|
124
|
+
subscribe(s, i) {
|
|
125
125
|
if (n._disposed)
|
|
126
126
|
throw new Error("[ChannelClient]: client is disposed!");
|
|
127
|
-
return n._remoteSubscribe(e,
|
|
127
|
+
return n._remoteSubscribe(e, s, i);
|
|
128
128
|
}
|
|
129
129
|
};
|
|
130
130
|
}
|
|
131
131
|
_whenReady() {
|
|
132
|
-
return
|
|
132
|
+
return ae(
|
|
133
133
|
this._initialized.pipe(
|
|
134
|
-
|
|
135
|
-
|
|
134
|
+
se((e) => e),
|
|
135
|
+
re(1)
|
|
136
136
|
)
|
|
137
137
|
);
|
|
138
138
|
}
|
|
139
|
-
async _remoteCall(e, n,
|
|
139
|
+
async _remoteCall(e, n, s) {
|
|
140
140
|
await this._whenReady();
|
|
141
|
-
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args:
|
|
141
|
+
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args: s }, o = this;
|
|
142
142
|
return new Promise((w, _) => {
|
|
143
|
-
const
|
|
143
|
+
const Q = {
|
|
144
144
|
handle(P) {
|
|
145
145
|
switch (P.type) {
|
|
146
146
|
case 201:
|
|
@@ -154,15 +154,15 @@ class de extends j {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
};
|
|
157
|
-
this._pendingRequests.set(i,
|
|
157
|
+
this._pendingRequests.set(i, Q), this._sendRequest(h);
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
|
-
_remoteSubscribe(e, n,
|
|
160
|
+
_remoteSubscribe(e, n, s) {
|
|
161
161
|
return new $((i) => {
|
|
162
162
|
let c = -1;
|
|
163
163
|
return this._whenReady().then(() => {
|
|
164
164
|
c = ++this._lastRequestCounter;
|
|
165
|
-
const o = { seq: c, type: 101, channelName: e, method: n, args:
|
|
165
|
+
const o = { seq: c, type: 101, channelName: e, method: n, args: s }, w = {
|
|
166
166
|
handle(_) {
|
|
167
167
|
switch (_.type) {
|
|
168
168
|
case 300:
|
|
@@ -212,7 +212,7 @@ class de extends j {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
class
|
|
215
|
+
class ve extends j {
|
|
216
216
|
constructor(e) {
|
|
217
217
|
super();
|
|
218
218
|
a(this, "_channels", /* @__PURE__ */ new Map());
|
|
@@ -249,12 +249,12 @@ class pe extends j {
|
|
|
249
249
|
});
|
|
250
250
|
}
|
|
251
251
|
_onMethodCall(e) {
|
|
252
|
-
const { channelName: n, method:
|
|
252
|
+
const { channelName: n, method: s, args: i } = e, c = this._channels.get(n);
|
|
253
253
|
let h;
|
|
254
254
|
try {
|
|
255
255
|
if (!c)
|
|
256
256
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
257
|
-
h = c.call(
|
|
257
|
+
h = i ? c.call(s, i) : c.call(s);
|
|
258
258
|
} catch (o) {
|
|
259
259
|
h = Promise.reject(o);
|
|
260
260
|
}
|
|
@@ -265,24 +265,24 @@ class pe extends j {
|
|
|
265
265
|
});
|
|
266
266
|
}
|
|
267
267
|
_onSubscribe(e) {
|
|
268
|
-
const { channelName: n, seq:
|
|
268
|
+
const { channelName: n, seq: s } = e, i = this._channels.get(n);
|
|
269
269
|
try {
|
|
270
270
|
if (!i)
|
|
271
271
|
throw new Error(`[ChannelServer]: Channel ${n} not found!`);
|
|
272
272
|
const h = i.subscribe(e.method, e.args).subscribe({
|
|
273
273
|
next: (o) => {
|
|
274
|
-
this._sendResponse({ seq:
|
|
274
|
+
this._sendResponse({ seq: s, type: 300, data: o });
|
|
275
275
|
},
|
|
276
276
|
error: (o) => {
|
|
277
|
-
this._sendResponse({ seq:
|
|
278
|
-
seq:
|
|
277
|
+
this._sendResponse({ seq: s, type: 301, data: o.message }), this._sendResponse({
|
|
278
|
+
seq: s,
|
|
279
279
|
type: 302
|
|
280
280
|
/* SUBSCRIBE_COMPLETE */
|
|
281
281
|
});
|
|
282
282
|
},
|
|
283
283
|
complete: () => {
|
|
284
284
|
this._sendResponse({
|
|
285
|
-
seq:
|
|
285
|
+
seq: s,
|
|
286
286
|
type: 302
|
|
287
287
|
/* SUBSCRIBE_COMPLETE */
|
|
288
288
|
});
|
|
@@ -302,11 +302,11 @@ class pe extends j {
|
|
|
302
302
|
}
|
|
303
303
|
}
|
|
304
304
|
const I = O("IRPCChannelService");
|
|
305
|
-
class
|
|
305
|
+
class K {
|
|
306
306
|
constructor(t) {
|
|
307
307
|
a(this, "_client");
|
|
308
308
|
a(this, "_server");
|
|
309
|
-
this._client = new
|
|
309
|
+
this._client = new pe(t), this._server = new ve(t);
|
|
310
310
|
}
|
|
311
311
|
dispose() {
|
|
312
312
|
this._client.dispose(), this._server.dispose();
|
|
@@ -318,18 +318,18 @@ class G {
|
|
|
318
318
|
this._server.registerChannel(t, e);
|
|
319
319
|
}
|
|
320
320
|
}
|
|
321
|
-
var
|
|
322
|
-
for (var
|
|
323
|
-
(c =
|
|
324
|
-
return n &&
|
|
325
|
-
}, l = (
|
|
321
|
+
var fe = Object.defineProperty, me = Object.getOwnPropertyDescriptor, ge = (r, t, e, n) => {
|
|
322
|
+
for (var s = n > 1 ? void 0 : n ? me(t, e) : t, i = r.length - 1, c; i >= 0; i--)
|
|
323
|
+
(c = r[i]) && (s = (n ? c(t, e, s) : c(s)) || s);
|
|
324
|
+
return n && s && fe(t, e, s), s;
|
|
325
|
+
}, l = (r, t) => (e, n) => t(e, n, r);
|
|
326
326
|
let g = class extends j {
|
|
327
|
-
constructor(t, e, n,
|
|
327
|
+
constructor(t, e, n, s, i) {
|
|
328
328
|
super();
|
|
329
329
|
a(this, "_remoteInstanceService");
|
|
330
330
|
a(this, "_syncingUnits", /* @__PURE__ */ new Set());
|
|
331
331
|
a(this, "_syncingMutations", /* @__PURE__ */ new Set());
|
|
332
|
-
this._injector = t, this._commandService = e, this._univerInstanceService = n, this._rpcChannelService =
|
|
332
|
+
this._injector = t, this._commandService = e, this._univerInstanceService = n, this._rpcChannelService = s, this._remoteSyncService = i, this._initRPCChannels(), this._init();
|
|
333
333
|
}
|
|
334
334
|
registerSyncingMutations(t) {
|
|
335
335
|
this._syncingMutations.add(t.id);
|
|
@@ -339,12 +339,12 @@ let g = class extends j {
|
|
|
339
339
|
* sync other types of documents, you should manually call this method with that document's id.
|
|
340
340
|
*/
|
|
341
341
|
syncUnit(t) {
|
|
342
|
-
return this._syncingUnits.add(t),
|
|
342
|
+
return this._syncingUnits.add(t), te(() => this._syncingUnits.delete(t));
|
|
343
343
|
}
|
|
344
344
|
_initRPCChannels() {
|
|
345
|
-
this._rpcChannelService.registerChannel(T,
|
|
345
|
+
this._rpcChannelService.registerChannel(T, Y(this._remoteSyncService)), this._injector.add([
|
|
346
346
|
m,
|
|
347
|
-
{ useFactory: () =>
|
|
347
|
+
{ useFactory: () => B(this._rpcChannelService.requestChannel(W)) }
|
|
348
348
|
]), this._remoteInstanceService = this._injector.get(m);
|
|
349
349
|
}
|
|
350
350
|
_init() {
|
|
@@ -359,93 +359,97 @@ let g = class extends j {
|
|
|
359
359
|
unitID: t.getUnitId()
|
|
360
360
|
});
|
|
361
361
|
}), this.disposeWithMe(this._commandService.onCommandExecuted((t, e) => {
|
|
362
|
-
const { type: n, params:
|
|
363
|
-
n ===
|
|
362
|
+
const { type: n, params: s, id: i } = t, c = (s == null ? void 0 : s.unitId) || "";
|
|
363
|
+
n === k.MUTATION && // only sync mutations to the worker thread
|
|
364
364
|
(!c || this._syncingUnits.has(c)) && // do not sync mutations from the web worker back to the web worker
|
|
365
365
|
!(e != null && e.fromSync) && // do not sync mutations those are not meant to be synced
|
|
366
366
|
this._syncingMutations.has(i) && this._remoteInstanceService.syncMutation({ mutationInfo: t });
|
|
367
367
|
}));
|
|
368
368
|
}
|
|
369
369
|
};
|
|
370
|
-
g =
|
|
370
|
+
g = ge([
|
|
371
371
|
l(0, C(R)),
|
|
372
372
|
l(1, b),
|
|
373
|
-
l(2,
|
|
373
|
+
l(2, H),
|
|
374
374
|
l(3, I),
|
|
375
375
|
l(4, f)
|
|
376
376
|
], g);
|
|
377
|
-
var
|
|
378
|
-
for (var
|
|
379
|
-
(c =
|
|
380
|
-
return n &&
|
|
381
|
-
}, d = (
|
|
382
|
-
let y = class extends
|
|
383
|
-
constructor(t, e, n,
|
|
377
|
+
var ye = Object.defineProperty, Se = Object.getOwnPropertyDescriptor, be = (r, t, e, n) => {
|
|
378
|
+
for (var s = n > 1 ? void 0 : n ? Se(t, e) : t, i = r.length - 1, c; i >= 0; i--)
|
|
379
|
+
(c = r[i]) && (s = (n ? c(t, e, s) : c(s)) || s);
|
|
380
|
+
return n && s && ye(t, e, s), s;
|
|
381
|
+
}, d = (r, t) => (e, n) => t(e, n, r);
|
|
382
|
+
let y = class extends ne {
|
|
383
|
+
constructor(t, e, n, s) {
|
|
384
384
|
super();
|
|
385
385
|
a(this, "_remoteSyncService");
|
|
386
|
-
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService =
|
|
386
|
+
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = s, this._initRPCChannels(), this._init();
|
|
387
387
|
}
|
|
388
388
|
_initRPCChannels() {
|
|
389
|
-
this._rpcChannelService.registerChannel(W,
|
|
389
|
+
this._rpcChannelService.registerChannel(W, Y(this._remoteInstanceService)), this._injector.add([
|
|
390
390
|
f,
|
|
391
|
-
{ useFactory: () =>
|
|
391
|
+
{ useFactory: () => B(this._rpcChannelService.requestChannel(T)) }
|
|
392
392
|
]), this._remoteSyncService = this._injector.get(f);
|
|
393
393
|
}
|
|
394
394
|
_init() {
|
|
395
395
|
this.disposeWithMe(
|
|
396
396
|
// Mutations executed on the main thread should be synced to the worker thread.
|
|
397
397
|
this._commandService.onCommandExecuted((t, e) => {
|
|
398
|
-
t.type ===
|
|
398
|
+
t.type === k.MUTATION && !(e != null && e.fromSync) && this._remoteSyncService.syncMutation({
|
|
399
399
|
mutationInfo: t
|
|
400
400
|
});
|
|
401
401
|
})
|
|
402
402
|
);
|
|
403
403
|
}
|
|
404
404
|
};
|
|
405
|
-
y =
|
|
405
|
+
y = be([
|
|
406
406
|
d(0, C(R)),
|
|
407
407
|
d(1, m),
|
|
408
408
|
d(2, b),
|
|
409
409
|
d(3, I)
|
|
410
410
|
], y);
|
|
411
|
-
function
|
|
411
|
+
function Ce() {
|
|
412
412
|
return {
|
|
413
|
-
send(
|
|
414
|
-
postMessage(
|
|
413
|
+
send(r) {
|
|
414
|
+
postMessage(r);
|
|
415
415
|
},
|
|
416
|
-
onMessage: new $((
|
|
416
|
+
onMessage: new $((r) => {
|
|
417
417
|
const t = (e) => {
|
|
418
|
-
|
|
418
|
+
r.next(e.data);
|
|
419
419
|
};
|
|
420
420
|
return addEventListener("message", t), () => removeEventListener("message", t);
|
|
421
|
-
}).pipe(
|
|
421
|
+
}).pipe(z(1))
|
|
422
422
|
};
|
|
423
423
|
}
|
|
424
|
-
function
|
|
424
|
+
function Re(r) {
|
|
425
425
|
return {
|
|
426
426
|
send(t) {
|
|
427
|
-
|
|
427
|
+
r.postMessage(t);
|
|
428
428
|
},
|
|
429
429
|
onMessage: new $((t) => {
|
|
430
430
|
const e = (n) => {
|
|
431
431
|
t.next(n.data);
|
|
432
432
|
};
|
|
433
|
-
return
|
|
434
|
-
}).pipe(
|
|
433
|
+
return r.addEventListener("message", e), () => r.removeEventListener("message", e);
|
|
434
|
+
}).pipe(z(1))
|
|
435
435
|
};
|
|
436
436
|
}
|
|
437
|
-
var
|
|
438
|
-
for (var
|
|
439
|
-
(c =
|
|
440
|
-
return n &&
|
|
441
|
-
}, S = (
|
|
442
|
-
let
|
|
443
|
-
constructor(t =
|
|
437
|
+
var Ie = Object.defineProperty, we = Object.getOwnPropertyDescriptor, J = (r, t, e, n) => {
|
|
438
|
+
for (var s = n > 1 ? void 0 : n ? we(t, e) : t, i = r.length - 1, c; i >= 0; i--)
|
|
439
|
+
(c = r[i]) && (s = (n ? c(t, e, s) : c(s)) || s);
|
|
440
|
+
return n && s && Ie(t, e, s), s;
|
|
441
|
+
}, S = (r, t) => (e, n) => t(e, n, r), E;
|
|
442
|
+
let x = (E = class extends A {
|
|
443
|
+
constructor(t = D, e, n) {
|
|
444
444
|
super();
|
|
445
445
|
a(this, "_internalWorker", null);
|
|
446
446
|
this._config = t, this._injector = e, this._configService = n;
|
|
447
|
-
const { ...
|
|
448
|
-
|
|
447
|
+
const { ...s } = V(
|
|
448
|
+
{},
|
|
449
|
+
D,
|
|
450
|
+
this._config
|
|
451
|
+
);
|
|
452
|
+
this._configService.setConfig(he, s);
|
|
449
453
|
}
|
|
450
454
|
dispose() {
|
|
451
455
|
super.dispose(), this._internalWorker && (this._internalWorker.terminate(), this._internalWorker = null);
|
|
@@ -456,12 +460,12 @@ let D = (E = class extends H {
|
|
|
456
460
|
throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");
|
|
457
461
|
const e = t instanceof Worker ? t : new Worker(t);
|
|
458
462
|
this._internalWorker = t instanceof Worker ? null : e;
|
|
459
|
-
const n =
|
|
463
|
+
const n = Re(e);
|
|
460
464
|
[
|
|
461
465
|
[
|
|
462
466
|
I,
|
|
463
467
|
{
|
|
464
|
-
useFactory: () => new
|
|
468
|
+
useFactory: () => new K(n)
|
|
465
469
|
}
|
|
466
470
|
],
|
|
467
471
|
[g],
|
|
@@ -469,16 +473,20 @@ let D = (E = class extends H {
|
|
|
469
473
|
].forEach((i) => this._injector.add(i)), this._injector.get(g);
|
|
470
474
|
}
|
|
471
475
|
}, a(E, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), E);
|
|
472
|
-
|
|
476
|
+
x = J([
|
|
473
477
|
S(1, C(R)),
|
|
474
|
-
S(2,
|
|
475
|
-
],
|
|
478
|
+
S(2, F)
|
|
479
|
+
], x);
|
|
476
480
|
var U;
|
|
477
|
-
let
|
|
478
|
-
constructor(
|
|
479
|
-
super(), this._config =
|
|
480
|
-
const { ...n } =
|
|
481
|
-
|
|
481
|
+
let L = (U = class extends A {
|
|
482
|
+
constructor(r = N, t, e) {
|
|
483
|
+
super(), this._config = r, this._injector = t, this._configService = e;
|
|
484
|
+
const { ...n } = V(
|
|
485
|
+
{},
|
|
486
|
+
N,
|
|
487
|
+
this._config
|
|
488
|
+
);
|
|
489
|
+
this._configService.setConfig(_e, n);
|
|
482
490
|
}
|
|
483
491
|
onStarting() {
|
|
484
492
|
[
|
|
@@ -486,21 +494,21 @@ let N = (U = class extends H {
|
|
|
486
494
|
[
|
|
487
495
|
I,
|
|
488
496
|
{
|
|
489
|
-
useFactory: () => new
|
|
497
|
+
useFactory: () => new K(Ce())
|
|
490
498
|
}
|
|
491
499
|
],
|
|
492
500
|
[m, { useClass: q }]
|
|
493
|
-
].forEach((
|
|
501
|
+
].forEach((r) => this._injector.add(r)), this._injector.get(y);
|
|
494
502
|
}
|
|
495
503
|
}, a(U, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), U);
|
|
496
|
-
|
|
504
|
+
L = J([
|
|
497
505
|
S(1, C(R)),
|
|
498
|
-
S(2,
|
|
499
|
-
],
|
|
506
|
+
S(2, F)
|
|
507
|
+
], L);
|
|
500
508
|
export {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
509
|
+
pe as ChannelClient,
|
|
510
|
+
ve as ChannelServer,
|
|
511
|
+
K as ChannelService,
|
|
504
512
|
g as DataSyncPrimaryController,
|
|
505
513
|
y as DataSyncReplicaController,
|
|
506
514
|
I as IRPCChannelService,
|
|
@@ -509,9 +517,9 @@ export {
|
|
|
509
517
|
W as RemoteInstanceServiceName,
|
|
510
518
|
M as RemoteSyncPrimaryService,
|
|
511
519
|
T as RemoteSyncServiceName,
|
|
512
|
-
|
|
513
|
-
|
|
520
|
+
x as UniverRPCMainThreadPlugin,
|
|
521
|
+
L as UniverRPCWorkerThreadPlugin,
|
|
514
522
|
q as WebWorkerRemoteInstanceService,
|
|
515
|
-
|
|
516
|
-
|
|
523
|
+
Y as fromModule,
|
|
524
|
+
B as toModule
|
|
517
525
|
};
|
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 x=Object.defineProperty;var ee=(r,a,u)=>a in r?x(r,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[a]=u;var l=(r,a,u)=>ee(r,typeof a!="symbol"?a+"":a,u);var U,E;const O="rpc.main-thread.config",$={},N="rpc.worker-thread.config",k={};var L=Object.defineProperty,H=Object.getOwnPropertyDescriptor,q=(c,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&L(t,e,s),s},f=(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){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=q([f(0,a.ICommandService)],r.RemoteSyncPrimaryService);const R="univer.remote-instance-service",p=a.createIdentifier(R);r.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;default: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._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=q([f(0,a.IUniverInstanceService),f(1,a.ICommandService),f(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=s.apply(t,[n]);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=s.apply(t,n);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 A(e)?c.subscribe(e,n[0]):c.call(e,n[0])}}})}function A(c){return c.endsWith("$")}class W extends a.RxDisposable{constructor(e){super();l(this,"_initialized",new d.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,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},h=this;return new Promise((M,y)=>{const Z={handle(T){switch(T.type){case 201:h._pendingRequests.delete(i),M(T.data);break;case 202:h._pendingRequests.delete(i),y(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,Z),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const h={seq:o,type:101,channelName:e,method:n,args:s},M={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,M),this._sendRequest(h)}),()=>{if(o===-1)return;const _={type:102,seq:o,channelName:e,method:n};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class j extends a.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:i}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=o.call(s,i)}catch(h){_=Promise.reject(h)}_.then(h=>{this._sendResponse({seq:e.seq,type:201,data:h})}).catch(h=>{h instanceof Error?this._sendResponse({seq:e.seq,type:202,data:h.message}):this._sendResponse({seq:e.seq,type:202,data:String(h)})})}_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:h=>{this._sendResponse({seq:s,type:300,data:h})},error:h=>{this._sendResponse({seq:s,type:301,data:h.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 m=a.createIdentifier("IRPCChannelService");class w{constructor(t){l(this,"_client");l(this,"_server");this._client=new W(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 V=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(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=(n?o(t,e,s):o(s))||s);return n&&s&&V(t,e,s),s},S=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){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=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([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(R))}]),this._remoteInstanceService=this._injector.get(p)}_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})}))}},r.DataSyncPrimaryController=z([S(0,a.Inject(a.Injector)),S(1,a.ICommandService),S(2,a.IUniverInstanceService),S(3,m),S(4,v)],r.DataSyncPrimaryController);var G=Object.defineProperty,Y=Object.getOwnPropertyDescriptor,B=(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=(n?o(t,e,s):o(s))||s);return n&&s&&G(t,e,s),s},g=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,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})}))}},r.DataSyncReplicaController=B([g(0,a.Inject(a.Injector)),g(1,p),g(2,a.ICommandService),g(3,m)],r.DataSyncReplicaController);function K(){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 J(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 Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,D=(c,t,e,n)=>{for(var s=n>1?void 0:n?X(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&Q(t,e,s),s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=$,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=this._config;this._configService.setConfig(O,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=J(n);[[m,{useFactory:()=>new w(s)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>this._injector.add(o)),this._injector.get(r.DataSyncPrimaryController)}},l(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),r.UniverRPCMainThreadPlugin=D([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=k,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=this._config;this._configService.setConfig(N,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(K())}],[p,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(r.DataSyncReplicaController)}},l(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),r.UniverRPCWorkerThreadPlugin=D([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=W,r.ChannelServer=j,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=R,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 x=Object.defineProperty;var ee=(r,a,u)=>a in r?x(r,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[a]=u;var h=(r,a,u)=>ee(r,typeof a!="symbol"?a+"":a,u);var U,E;const N="rpc.main-thread.config",q={},k="rpc.worker-thread.config",W={};var L=Object.defineProperty,H=Object.getOwnPropertyDescriptor,j=(c,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&L(t,e,s),s},f=(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){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=j([f(0,a.ICommandService)],r.RemoteSyncPrimaryService);const R="univer.remote-instance-service",p=a.createIdentifier(R);r.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;default: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._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=j([f(0,a.IUniverInstanceService),f(1,a.ICommandService),f(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 A(e)?c.subscribe(e,n):c.call(e,n)}}})}function A(c){return c.endsWith("$")}class D 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,y)=>{const Z={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),y(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,Z),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(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,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 n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class O 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 m=a.createIdentifier("IRPCChannelService");class w{constructor(t){h(this,"_client");h(this,"_server");this._client=new D(t),this._server=new O(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var V=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(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=(n?o(t,e,s):o(s))||s);return n&&s&&V(t,e,s),s},S=(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([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(R))}]),this._remoteInstanceService=this._injector.get(p)}_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})}))}},r.DataSyncPrimaryController=z([S(0,a.Inject(a.Injector)),S(1,a.ICommandService),S(2,a.IUniverInstanceService),S(3,m),S(4,v)],r.DataSyncPrimaryController);var G=Object.defineProperty,Y=Object.getOwnPropertyDescriptor,B=(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=(n?o(t,e,s):o(s))||s);return n&&s&&G(t,e,s),s},g=(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(R,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})}))}},r.DataSyncReplicaController=B([g(0,a.Inject(a.Injector)),g(1,p),g(2,a.ICommandService),g(3,m)],r.DataSyncReplicaController);function K(){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 J(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 Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,$=(c,t,e,n)=>{for(var s=n>1?void 0:n?X(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&Q(t,e,s),s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=q,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},q,this._config);this._configService.setConfig(N,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=J(n);[[m,{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=W,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},W,this._config);this._configService.setConfig(k,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(K())}],[p,{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=D,r.ChannelServer=O,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=R,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.5.
|
|
3
|
+
"version": "0.5.2",
|
|
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.5.
|
|
50
|
+
"@univerjs/core": "0.5.2"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"rxjs": "^7.8.1",
|
|
54
54
|
"typescript": "^5.7.2",
|
|
55
|
-
"vite": "^6.0.
|
|
56
|
-
"vitest": "^2.1.
|
|
57
|
-
"@univerjs-infra/shared": "0.5.
|
|
55
|
+
"vite": "^6.0.3",
|
|
56
|
+
"vitest": "^2.1.8",
|
|
57
|
+
"@univerjs-infra/shared": "0.5.2"
|
|
58
58
|
},
|
|
59
59
|
"space": {
|
|
60
60
|
".": {
|
|
@@ -70,6 +70,9 @@
|
|
|
70
70
|
"./lib/*": "./lib/*"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
|
+
"test": "vitest run",
|
|
74
|
+
"test:watch": "vitest",
|
|
75
|
+
"coverage": "vitest run --coverage",
|
|
73
76
|
"lint:types": "tsc --noEmit",
|
|
74
77
|
"build": "univer-cli build"
|
|
75
78
|
},
|