@univerjs/rpc 0.8.2-nightly.202506261607 → 0.8.3-nightly.202506271607

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 CHANGED
@@ -1 +1 @@
1
- "use strict";var N=Object.defineProperty;var $=(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)=>$(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs"),x="rpc.main-thread.config",k={},L="rpc.worker-thread.config",T={};var H=Object.getOwnPropertyDescriptor,q=(i,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},g=(i,t)=>(e,n)=>t(e,n,i);const R="rpc.remote-sync.service",v=c.createIdentifier(R);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=q([g(0,c.ICommandService)],exports.RemoteSyncPrimaryService);const f="univer.remote-instance-service",m=c.createIdentifier(f);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 c.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(c.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=q([g(0,c.IUniverInstanceService),g(1,c.ICommandService),g(2,c.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 A(e)?i.subscribe(e,n):i.call(e,n)}}})}function A(i){return i.endsWith("$")}class W extends c.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 O={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,O),this._sendRequest(h)})}_remoteSubscribe(e,n,s){return new _.Observable(r=>{let a=-1;return this._whenReady().then(()=>{a=++this._lastRequestCounter;const o={seq:a,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(a,I),this._sendRequest(o)}),()=>{if(a===-1)return;const h={type:102,seq:a,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 c.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,a=this._channels.get(n);let h;try{if(!a)throw new Error(`[ChannelServer]: Channel ${n} not found!`);h=r?a.call(s,r):a.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(a){a instanceof Error?this._sendResponse({seq:e.seq,type:301,data:a.message}):this._sendResponse({seq:e.seq,type:301,data:String(a)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const y=c.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new W(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 V=Object.getOwnPropertyDescriptor,F=(i,t,e,n)=>{for(var s=n>1?void 0:n?V(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,a){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=a,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),c.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:c.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(c.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:a}=e,h=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=F([p(0,c.Inject(c.Injector)),p(1,c.ICommandService),p(2,c.IUniverInstanceService),p(3,y),p(4,v)],exports.DataSyncPrimaryController);var z=Object.getOwnPropertyDescriptor,G=(i,t,e,n)=>{for(var s=n>1?void 0:n?z(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},S=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends c.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(f,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=G([S(0,c.Inject(c.Injector)),S(1,m),S(2,c.ICommandService),S(3,y)],exports.DataSyncReplicaController);function Y(){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 B(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 K=Object.getOwnPropertyDescriptor,j=(i,t,e,n)=>{for(var s=n>1?void 0:n?K(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends c.Plugin{constructor(e=k,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=c.merge({},k,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=B(n);[[y,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(a=>this._injector.add(a)),this._injector.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=j([C(1,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends c.Plugin{constructor(t=T,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=c.merge({},T,this._config);this._configService.setConfig(L,s)}onStarting(){[[exports.DataSyncReplicaController],[y,{useFactory:()=>new M(Y())}],[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,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=W;exports.ChannelServer=D;exports.ChannelService=M;exports.IRPCChannelService=y;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=f;exports.RemoteSyncServiceName=R;exports.fromModule=U;exports.toModule=E;
1
+ "use strict";var N=Object.defineProperty;var $=(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)=>$(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs");var x=Object.getOwnPropertyDescriptor,q=(i,t,e,n)=>{for(var s=n>1?void 0:n?x(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},g=(i,t)=>(e,n)=>t(e,n,i);const R="rpc.remote-sync.service",v=c.createIdentifier(R);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=q([g(0,c.ICommandService)],exports.RemoteSyncPrimaryService);const f="univer.remote-instance-service",m=c.createIdentifier(f);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 c.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(c.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=q([g(0,c.IUniverInstanceService),g(1,c.ICommandService),g(2,c.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 L(e)?i.subscribe(e,n):i.call(e,n)}}})}function L(i){return i.endsWith("$")}class W extends c.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 O={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,O),this._sendRequest(h)})}_remoteSubscribe(e,n,s){return new _.Observable(r=>{let a=-1;return this._whenReady().then(()=>{a=++this._lastRequestCounter;const o={seq:a,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(a,I),this._sendRequest(o)}),()=>{if(a===-1)return;const h={type:102,seq:a,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 c.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,a=this._channels.get(n);let h;try{if(!a)throw new Error(`[ChannelServer]: Channel ${n} not found!`);h=r?a.call(s,r):a.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(a){a instanceof Error?this._sendResponse({seq:e.seq,type:301,data:a.message}):this._sendResponse({seq:e.seq,type:301,data:String(a)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const y=c.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new W(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 H=Object.getOwnPropertyDescriptor,A=(i,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,a){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=a,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),c.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:c.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(c.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:a}=e,h=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=A([p(0,c.Inject(c.Injector)),p(1,c.ICommandService),p(2,c.IUniverInstanceService),p(3,y),p(4,v)],exports.DataSyncPrimaryController);var V=Object.getOwnPropertyDescriptor,F=(i,t,e,n)=>{for(var s=n>1?void 0:n?V(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},S=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends c.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(f,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=F([S(0,c.Inject(c.Injector)),S(1,m),S(2,c.ICommandService),S(3,y)],exports.DataSyncReplicaController);const z="rpc.main-thread.config",k={},G="rpc.worker-thread.config",T={};function Y(){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 B(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 K=Object.getOwnPropertyDescriptor,j=(i,t,e,n)=>{for(var s=n>1?void 0:n?K(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends c.Plugin{constructor(e=k,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=c.merge({},k,this._config);this._configService.setConfig(z,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=B(n);[[y,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(a=>this._injector.add(a)),this._injector.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=j([C(1,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends c.Plugin{constructor(t=T,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=c.merge({},T,this._config);this._configService.setConfig(G,s)}onStarting(){[[exports.DataSyncReplicaController],[y,{useFactory:()=>new M(Y())}],[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,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=W;exports.ChannelServer=D;exports.ChannelService=M;exports.IRPCChannelService=y;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=f;exports.RemoteSyncServiceName=R;exports.fromModule=U;exports.toModule=E;
package/lib/es/index.js CHANGED
@@ -1,12 +1,11 @@
1
1
  var X = Object.defineProperty;
2
2
  var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
3
  var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { createIdentifier as k, UniverInstanceType as u, ICommandService as C, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
4
+ import { createIdentifier as k, ICommandService as C, UniverInstanceType as u, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
5
5
  import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
6
6
  import { BehaviorSubject as ie, firstValueFrom as ce, Observable as T, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
- const he = "rpc.main-thread.config", j = {}, le = "rpc.worker-thread.config", D = {};
8
- var _e = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
9
- for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, c; i >= 0; i--)
7
+ var he = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
8
+ for (var r = n > 1 ? void 0 : n ? he(t, e) : t, i = s.length - 1, c; i >= 0; i--)
10
9
  (c = s[i]) && (r = c(r) || r);
11
10
  return r;
12
11
  }, p = (s, t) => (e, n) => t(e, n, s);
@@ -93,15 +92,15 @@ function B(s) {
93
92
  get(t, e) {
94
93
  if (e !== "dispose")
95
94
  return function(...n) {
96
- return ue(e) ? s.subscribe(e, n) : s.call(e, n);
95
+ return le(e) ? s.subscribe(e, n) : s.call(e, n);
97
96
  };
98
97
  }
99
98
  });
100
99
  }
101
- function ue(s) {
100
+ function le(s) {
102
101
  return s.endsWith("$");
103
102
  }
104
- class de extends O {
103
+ class _e extends O {
105
104
  constructor(e) {
106
105
  super();
107
106
  a(this, "_initialized", new ie(!1));
@@ -212,7 +211,7 @@ class de extends O {
212
211
  }
213
212
  }
214
213
  }
215
- class pe extends O {
214
+ class ue extends O {
216
215
  constructor(e) {
217
216
  super();
218
217
  a(this, "_channels", /* @__PURE__ */ new Map());
@@ -306,7 +305,7 @@ class K {
306
305
  constructor(t) {
307
306
  a(this, "_client");
308
307
  a(this, "_server");
309
- this._client = new de(t), this._server = new pe(t);
308
+ this._client = new _e(t), this._server = new ue(t);
310
309
  }
311
310
  dispose() {
312
311
  this._client.dispose(), this._server.dispose();
@@ -318,8 +317,8 @@ class K {
318
317
  this._server.registerChannel(t, e);
319
318
  }
320
319
  }
321
- var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
322
- for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
320
+ var de = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => {
321
+ for (var r = n > 1 ? void 0 : n ? de(t, e) : t, i = s.length - 1, c; i >= 0; i--)
323
322
  (c = s[i]) && (r = c(r) || r);
324
323
  return r;
325
324
  }, _ = (s, t) => (e, n) => t(e, n, s);
@@ -367,15 +366,15 @@ let y = class extends O {
367
366
  }));
368
367
  }
369
368
  };
370
- y = me([
369
+ y = pe([
371
370
  _(0, b(R)),
372
371
  _(1, C),
373
372
  _(2, L),
374
373
  _(3, I),
375
374
  _(4, m)
376
375
  ], y);
377
- var ge = Object.getOwnPropertyDescriptor, ye = (s, t, e, n) => {
378
- for (var r = n > 1 ? void 0 : n ? ge(t, e) : t, i = s.length - 1, c; i >= 0; i--)
376
+ var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
377
+ for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
379
378
  (c = s[i]) && (r = c(r) || r);
380
379
  return r;
381
380
  }, d = (s, t) => (e, n) => t(e, n, s);
@@ -402,12 +401,13 @@ let f = class extends ne {
402
401
  );
403
402
  }
404
403
  };
405
- f = ye([
404
+ f = me([
406
405
  d(0, b(R)),
407
406
  d(1, g),
408
407
  d(2, C),
409
408
  d(3, I)
410
409
  ], f);
410
+ const ge = "rpc.main-thread.config", j = {}, ye = "rpc.worker-thread.config", D = {};
411
411
  function fe() {
412
412
  return {
413
413
  send(s) {
@@ -449,7 +449,7 @@ let N = (P = class extends V {
449
449
  j,
450
450
  this._config
451
451
  );
452
- this._configService.setConfig(he, r);
452
+ this._configService.setConfig(ge, r);
453
453
  }
454
454
  dispose() {
455
455
  super.dispose(), this._internalWorker && (this._internalWorker.terminate(), this._internalWorker = null);
@@ -486,7 +486,7 @@ let x = (U = class extends V {
486
486
  D,
487
487
  this._config
488
488
  );
489
- this._configService.setConfig(le, n);
489
+ this._configService.setConfig(ye, n);
490
490
  }
491
491
  onStarting() {
492
492
  [
@@ -506,8 +506,8 @@ x = J([
506
506
  S(2, A)
507
507
  ], x);
508
508
  export {
509
- de as ChannelClient,
510
- pe as ChannelServer,
509
+ _e as ChannelClient,
510
+ ue as ChannelServer,
511
511
  K as ChannelService,
512
512
  y as DataSyncPrimaryController,
513
513
  f as DataSyncReplicaController,
package/lib/index.js CHANGED
@@ -1,12 +1,11 @@
1
1
  var X = Object.defineProperty;
2
2
  var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
3
  var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { createIdentifier as k, UniverInstanceType as u, ICommandService as C, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
4
+ import { createIdentifier as k, ICommandService as C, UniverInstanceType as u, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
5
5
  import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
6
6
  import { BehaviorSubject as ie, firstValueFrom as ce, Observable as T, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
- const he = "rpc.main-thread.config", j = {}, le = "rpc.worker-thread.config", D = {};
8
- var _e = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
9
- for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, c; i >= 0; i--)
7
+ var he = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
8
+ for (var r = n > 1 ? void 0 : n ? he(t, e) : t, i = s.length - 1, c; i >= 0; i--)
10
9
  (c = s[i]) && (r = c(r) || r);
11
10
  return r;
12
11
  }, p = (s, t) => (e, n) => t(e, n, s);
@@ -93,15 +92,15 @@ function B(s) {
93
92
  get(t, e) {
94
93
  if (e !== "dispose")
95
94
  return function(...n) {
96
- return ue(e) ? s.subscribe(e, n) : s.call(e, n);
95
+ return le(e) ? s.subscribe(e, n) : s.call(e, n);
97
96
  };
98
97
  }
99
98
  });
100
99
  }
101
- function ue(s) {
100
+ function le(s) {
102
101
  return s.endsWith("$");
103
102
  }
104
- class de extends O {
103
+ class _e extends O {
105
104
  constructor(e) {
106
105
  super();
107
106
  a(this, "_initialized", new ie(!1));
@@ -212,7 +211,7 @@ class de extends O {
212
211
  }
213
212
  }
214
213
  }
215
- class pe extends O {
214
+ class ue extends O {
216
215
  constructor(e) {
217
216
  super();
218
217
  a(this, "_channels", /* @__PURE__ */ new Map());
@@ -306,7 +305,7 @@ class K {
306
305
  constructor(t) {
307
306
  a(this, "_client");
308
307
  a(this, "_server");
309
- this._client = new de(t), this._server = new pe(t);
308
+ this._client = new _e(t), this._server = new ue(t);
310
309
  }
311
310
  dispose() {
312
311
  this._client.dispose(), this._server.dispose();
@@ -318,8 +317,8 @@ class K {
318
317
  this._server.registerChannel(t, e);
319
318
  }
320
319
  }
321
- var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
322
- for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
320
+ var de = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => {
321
+ for (var r = n > 1 ? void 0 : n ? de(t, e) : t, i = s.length - 1, c; i >= 0; i--)
323
322
  (c = s[i]) && (r = c(r) || r);
324
323
  return r;
325
324
  }, _ = (s, t) => (e, n) => t(e, n, s);
@@ -367,15 +366,15 @@ let y = class extends O {
367
366
  }));
368
367
  }
369
368
  };
370
- y = me([
369
+ y = pe([
371
370
  _(0, b(R)),
372
371
  _(1, C),
373
372
  _(2, L),
374
373
  _(3, I),
375
374
  _(4, m)
376
375
  ], y);
377
- var ge = Object.getOwnPropertyDescriptor, ye = (s, t, e, n) => {
378
- for (var r = n > 1 ? void 0 : n ? ge(t, e) : t, i = s.length - 1, c; i >= 0; i--)
376
+ var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
377
+ for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
379
378
  (c = s[i]) && (r = c(r) || r);
380
379
  return r;
381
380
  }, d = (s, t) => (e, n) => t(e, n, s);
@@ -402,12 +401,13 @@ let f = class extends ne {
402
401
  );
403
402
  }
404
403
  };
405
- f = ye([
404
+ f = me([
406
405
  d(0, b(R)),
407
406
  d(1, g),
408
407
  d(2, C),
409
408
  d(3, I)
410
409
  ], f);
410
+ const ge = "rpc.main-thread.config", j = {}, ye = "rpc.worker-thread.config", D = {};
411
411
  function fe() {
412
412
  return {
413
413
  send(s) {
@@ -449,7 +449,7 @@ let N = (P = class extends V {
449
449
  j,
450
450
  this._config
451
451
  );
452
- this._configService.setConfig(he, r);
452
+ this._configService.setConfig(ge, r);
453
453
  }
454
454
  dispose() {
455
455
  super.dispose(), this._internalWorker && (this._internalWorker.terminate(), this._internalWorker = null);
@@ -486,7 +486,7 @@ let x = (U = class extends V {
486
486
  D,
487
487
  this._config
488
488
  );
489
- this._configService.setConfig(le, n);
489
+ this._configService.setConfig(ye, n);
490
490
  }
491
491
  onStarting() {
492
492
  [
@@ -506,8 +506,8 @@ x = J([
506
506
  S(2, A)
507
507
  ], x);
508
508
  export {
509
- de as ChannelClient,
510
- pe as ChannelServer,
509
+ _e as ChannelClient,
510
+ ue as ChannelServer,
511
511
  K as ChannelService,
512
512
  y as DataSyncPrimaryController,
513
513
  f as DataSyncReplicaController,
@@ -13,11 +13,11 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export { UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
17
- export { DataSyncReplicaController } from './controllers/data-sync/data-sync-replica.controller';
16
+ export type { IUniverRPCMainThreadConfig, IUniverRPCWorkerThreadConfig } from './controllers/config.schema';
18
17
  export { DataSyncPrimaryController } from './controllers/data-sync/data-sync-primary.controller';
18
+ export { DataSyncReplicaController } from './controllers/data-sync/data-sync-replica.controller';
19
+ export { UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
19
20
  export { IRemoteInstanceService, RemoteInstanceServiceName, RemoteSyncPrimaryService, RemoteSyncServiceName, WebWorkerRemoteInstanceService, } from './services/remote-instance/remote-instance.service';
21
+ export { IRemoteSyncService } from './services/remote-instance/remote-instance.service';
20
22
  export { ChannelService, IRPCChannelService } from './services/rpc/channel.service';
21
23
  export { ChannelClient, ChannelServer, fromModule, type IMessageProtocol, toModule } from './services/rpc/rpc.service';
22
- export { IRemoteSyncService } from './services/remote-instance/remote-instance.service';
23
- export type { IUniverRPCMainThreadConfig, IUniverRPCWorkerThreadConfig } from './controllers/config.schema';
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,_,d){"use strict";var J=Object.defineProperty;var Q=(r,a,_)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:_}):r[a]=_;var h=(r,a,_)=>Q(r,typeof a!="symbol"?a+"":a,_);var U,E;const N="rpc.main-thread.config",k={},$="rpc.worker-thread.config",q={};var L=Object.getOwnPropertyDescriptor,W=(c,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},g=(c,t)=>(e,n)=>t(e,n,c);const f="rpc.remote-sync.service",v=a.createIdentifier(f);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=W([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const C="univer.remote-instance-service",p=a.createIdentifier(C);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=W([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=n?s.apply(t,n):s.call(t);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=n?s.apply(t,n):s.call(t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return H(e)?c.subscribe(e,n):c.call(e,n)}}})}function H(c){return c.endsWith("$")}class 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(_.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(_.filter(e=>e),_.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((M,S)=>{const K={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),S(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,K),this._sendRequest(u)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},M={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,M),this._sendRequest(l)}),()=>{if(o===-1)return;const u={type:102,seq:o,channelName:e,method:n};this._sendRequest(u)}})}_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();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.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 u;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);u=i?o.call(s,i):o.call(s)}catch(l){u=Promise.reject(l)}u.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 u=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,u)}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 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 A=Object.getOwnPropertyDescriptor,V=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},y=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(f,b(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.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(_.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,u=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!u||this._syncingUnits.has(u))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},r.DataSyncPrimaryController=V([y(0,a.Inject(a.Injector)),y(1,a.ICommandService),y(2,a.IUniverInstanceService),y(3,m),y(4,v)],r.DataSyncPrimaryController);var 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=o(s)||s);return s},R=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(f))}]),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=z([R(0,a.Inject(a.Injector)),R(1,p),R(2,a.ICommandService),R(3,m)],r.DataSyncReplicaController);function G(){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 Y(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 B=Object.getOwnPropertyDescriptor,O=(c,t,e,n)=>{for(var s=n>1?void 0:n?B(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=k,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},k,this._config);this._configService.setConfig(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=Y(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=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=q,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},q,this._config);this._configService.setConfig($,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(G())}],[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=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=D,r.ChannelServer=j,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=C,r.RemoteSyncServiceName=f,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,_,d){"use strict";var J=Object.defineProperty;var Q=(r,a,_)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:_}):r[a]=_;var h=(r,a,_)=>Q(r,typeof a!="symbol"?a+"":a,_);var U,E;var N=Object.getOwnPropertyDescriptor,k=(c,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},g=(c,t)=>(e,n)=>t(e,n,c);const f="rpc.remote-sync.service",v=a.createIdentifier(f);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=k([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const C="univer.remote-instance-service",p=a.createIdentifier(C);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=k([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=n?s.apply(t,n):s.call(t);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=n?s.apply(t,n):s.call(t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return $(e)?c.subscribe(e,n):c.call(e,n)}}})}function $(c){return c.endsWith("$")}class q 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(_.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(_.filter(e=>e),_.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((M,S)=>{const K={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),S(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,K),this._sendRequest(u)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},M={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,M),this._sendRequest(l)}),()=>{if(o===-1)return;const u={type:102,seq:o,channelName:e,method:n};this._sendRequest(u)}})}_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 a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.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 u;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);u=i?o.call(s,i):o.call(s)}catch(l){u=Promise.reject(l)}u.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 u=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,u)}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 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 L=Object.getOwnPropertyDescriptor,H=(c,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},y=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(f,b(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.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(_.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,u=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!u||this._syncingUnits.has(u))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},r.DataSyncPrimaryController=H([y(0,a.Inject(a.Injector)),y(1,a.ICommandService),y(2,a.IUniverInstanceService),y(3,m),y(4,v)],r.DataSyncPrimaryController);var A=Object.getOwnPropertyDescriptor,V=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},R=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(f))}]),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=V([R(0,a.Inject(a.Injector)),R(1,p),R(2,a.ICommandService),R(3,m)],r.DataSyncReplicaController);const F="rpc.main-thread.config",D={},z="rpc.worker-thread.config",j={};function G(){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 Y(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 B=Object.getOwnPropertyDescriptor,O=(c,t,e,n)=>{for(var s=n>1?void 0:n?B(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=D,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},D,this._config);this._configService.setConfig(F,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=Y(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=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=j,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},j,this._config);this._configService.setConfig(z,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(G())}],[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=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=q,r.ChannelServer=W,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=C,r.RemoteSyncServiceName=f,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.8.2-nightly.202506261607",
3
+ "version": "0.8.3-nightly.202506271607",
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.8.2-nightly.202506261607"
50
+ "@univerjs/core": "0.8.3-nightly.202506271607"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.2",
54
54
  "typescript": "^5.8.3",
55
55
  "vite": "^6.3.5",
56
56
  "vitest": "^3.2.4",
57
- "@univerjs-infra/shared": "0.8.2"
57
+ "@univerjs-infra/shared": "0.8.3"
58
58
  },
59
59
  "scripts": {
60
60
  "test": "vitest run",