@univerjs/rpc 0.5.0 → 0.5.1
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 +23 -14
- package/lib/types/services/rpc/rpc.service.d.ts +1 -3
- package/lib/umd/index.js +1 -1
- package/package.json +6 -6
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",x={},k="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.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((b,d)=>{const D={handle(I){switch(I.type){case 201:a._pendingRequests.delete(r),b(I.data);break;case 202:a._pendingRequests.delete(r),d(I.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},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(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._sendResponse({seq:-1,type:0})}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{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,G=Object.getOwnPropertyDescriptor,z=(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=(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=z([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=x,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(k,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 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;
|
package/lib/es/index.js
CHANGED
|
@@ -88,7 +88,7 @@ function F(s) {
|
|
|
88
88
|
}
|
|
89
89
|
}();
|
|
90
90
|
}
|
|
91
|
-
function
|
|
91
|
+
function z(s) {
|
|
92
92
|
return new Proxy({}, {
|
|
93
93
|
get(t, e) {
|
|
94
94
|
if (e !== "dispose")
|
|
@@ -107,7 +107,10 @@ class de extends j {
|
|
|
107
107
|
a(this, "_initialized", new re(!1));
|
|
108
108
|
a(this, "_lastRequestCounter", 0);
|
|
109
109
|
a(this, "_pendingRequests", /* @__PURE__ */ new Map());
|
|
110
|
-
this._protocol = e, this._protocol.
|
|
110
|
+
this._protocol = e, this._protocol.send({
|
|
111
|
+
type: 50
|
|
112
|
+
/* REQUEST_INITIALIZATION */
|
|
113
|
+
}), this._protocol.onMessage.pipe(v(this.dispose$)).subscribe((n) => this._onMessage(n));
|
|
111
114
|
}
|
|
112
115
|
dispose() {
|
|
113
116
|
this._pendingRequests.clear();
|
|
@@ -214,11 +217,7 @@ class pe extends j {
|
|
|
214
217
|
super();
|
|
215
218
|
a(this, "_channels", /* @__PURE__ */ new Map());
|
|
216
219
|
a(this, "_subscriptions", /* @__PURE__ */ new Map());
|
|
217
|
-
this._protocol = e, this._protocol.onMessage.pipe(v(this.dispose$)).subscribe((n) => this._onRequest(n)), this.
|
|
218
|
-
seq: -1,
|
|
219
|
-
type: 0
|
|
220
|
-
/* INITIALIZE */
|
|
221
|
-
});
|
|
220
|
+
this._protocol = e, this._protocol.onMessage.pipe(v(this.dispose$)).subscribe((n) => this._onRequest(n)), this._sendInitialize();
|
|
222
221
|
}
|
|
223
222
|
dispose() {
|
|
224
223
|
super.dispose(), this._subscriptions.clear(), this._channels.clear();
|
|
@@ -228,6 +227,9 @@ class pe extends j {
|
|
|
228
227
|
}
|
|
229
228
|
_onRequest(e) {
|
|
230
229
|
switch (e.type) {
|
|
230
|
+
case 50:
|
|
231
|
+
this._sendInitialize();
|
|
232
|
+
break;
|
|
231
233
|
case 100:
|
|
232
234
|
this._onMethodCall(e);
|
|
233
235
|
break;
|
|
@@ -239,6 +241,13 @@ class pe extends j {
|
|
|
239
241
|
break;
|
|
240
242
|
}
|
|
241
243
|
}
|
|
244
|
+
_sendInitialize() {
|
|
245
|
+
this._sendResponse({
|
|
246
|
+
seq: -1,
|
|
247
|
+
type: 0
|
|
248
|
+
/* INITIALIZE */
|
|
249
|
+
});
|
|
250
|
+
}
|
|
242
251
|
_onMethodCall(e) {
|
|
243
252
|
const { channelName: n, method: r, args: i } = e, c = this._channels.get(n);
|
|
244
253
|
let h;
|
|
@@ -293,7 +302,7 @@ class pe extends j {
|
|
|
293
302
|
}
|
|
294
303
|
}
|
|
295
304
|
const I = O("IRPCChannelService");
|
|
296
|
-
class
|
|
305
|
+
class G {
|
|
297
306
|
constructor(t) {
|
|
298
307
|
a(this, "_client");
|
|
299
308
|
a(this, "_server");
|
|
@@ -335,7 +344,7 @@ let g = class extends j {
|
|
|
335
344
|
_initRPCChannels() {
|
|
336
345
|
this._rpcChannelService.registerChannel(T, F(this._remoteSyncService)), this._injector.add([
|
|
337
346
|
m,
|
|
338
|
-
{ useFactory: () =>
|
|
347
|
+
{ useFactory: () => z(this._rpcChannelService.requestChannel(W)) }
|
|
339
348
|
]), this._remoteInstanceService = this._injector.get(m);
|
|
340
349
|
}
|
|
341
350
|
_init() {
|
|
@@ -379,7 +388,7 @@ let y = class extends Z {
|
|
|
379
388
|
_initRPCChannels() {
|
|
380
389
|
this._rpcChannelService.registerChannel(W, F(this._remoteInstanceService)), this._injector.add([
|
|
381
390
|
f,
|
|
382
|
-
{ useFactory: () =>
|
|
391
|
+
{ useFactory: () => z(this._rpcChannelService.requestChannel(T)) }
|
|
383
392
|
]), this._remoteSyncService = this._injector.get(f);
|
|
384
393
|
}
|
|
385
394
|
_init() {
|
|
@@ -452,7 +461,7 @@ let D = (E = class extends H {
|
|
|
452
461
|
[
|
|
453
462
|
I,
|
|
454
463
|
{
|
|
455
|
-
useFactory: () => new
|
|
464
|
+
useFactory: () => new G(n)
|
|
456
465
|
}
|
|
457
466
|
],
|
|
458
467
|
[g],
|
|
@@ -477,7 +486,7 @@ let N = (U = class extends H {
|
|
|
477
486
|
[
|
|
478
487
|
I,
|
|
479
488
|
{
|
|
480
|
-
useFactory: () => new
|
|
489
|
+
useFactory: () => new G(be())
|
|
481
490
|
}
|
|
482
491
|
],
|
|
483
492
|
[m, { useClass: q }]
|
|
@@ -491,7 +500,7 @@ N = Y([
|
|
|
491
500
|
export {
|
|
492
501
|
de as ChannelClient,
|
|
493
502
|
pe as ChannelServer,
|
|
494
|
-
|
|
503
|
+
G as ChannelService,
|
|
495
504
|
g as DataSyncPrimaryController,
|
|
496
505
|
y as DataSyncReplicaController,
|
|
497
506
|
I as IRPCChannelService,
|
|
@@ -504,5 +513,5 @@ export {
|
|
|
504
513
|
N as UniverRPCWorkerThreadPlugin,
|
|
505
514
|
q as WebWorkerRemoteInstanceService,
|
|
506
515
|
F as fromModule,
|
|
507
|
-
|
|
516
|
+
z as toModule
|
|
508
517
|
};
|
|
@@ -34,9 +34,6 @@ export declare function toModule<T extends object>(channel: IChannel): T;
|
|
|
34
34
|
export interface IChannelClient {
|
|
35
35
|
getChannel<T extends IChannel>(channelName: string): T;
|
|
36
36
|
}
|
|
37
|
-
/**
|
|
38
|
-
*
|
|
39
|
-
*/
|
|
40
37
|
export interface IChannelServer {
|
|
41
38
|
registerChannel<T extends IChannel>(channelName: string, channel: T): void;
|
|
42
39
|
}
|
|
@@ -66,6 +63,7 @@ export declare class ChannelServer extends RxDisposable implements IChannelServe
|
|
|
66
63
|
dispose(): void;
|
|
67
64
|
registerChannel(channelName: string, channel: IChannel): void;
|
|
68
65
|
private _onRequest;
|
|
66
|
+
private _sendInitialize;
|
|
69
67
|
private _onMethodCall;
|
|
70
68
|
private _onSubscribe;
|
|
71
69
|
private _onUnsubscribe;
|
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.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._sendResponse({seq:-1,type:0})}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{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,G=(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=G([S(0,a.Inject(a.Injector)),S(1,a.ICommandService),S(2,a.IUniverInstanceService),S(3,m),S(4,v)],r.DataSyncPrimaryController);var z=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&&z(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 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"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/rpc",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
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.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"rxjs": "^7.8.1",
|
|
54
|
-
"typescript": "^5.
|
|
55
|
-
"vite": "^
|
|
56
|
-
"vitest": "^2.1.
|
|
57
|
-
"@univerjs-infra/shared": "0.5.
|
|
54
|
+
"typescript": "^5.7.2",
|
|
55
|
+
"vite": "^6.0.1",
|
|
56
|
+
"vitest": "^2.1.6",
|
|
57
|
+
"@univerjs-infra/shared": "0.5.1"
|
|
58
58
|
},
|
|
59
59
|
"space": {
|
|
60
60
|
".": {
|