@univerjs/rpc
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,1 +0,1 @@ | ||
"use strict";var $=Object.defineProperty;var x=(i,n,e)=>n in i?$(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var l=(i,n,e)=>x(i,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),u=require("@wendellhu/redi"),p=require("rxjs/operators"),_=require("rxjs");var N=Object.defineProperty,L=Object.getOwnPropertyDescriptor,E=(i,n,e,t)=>{for(var s=t>1?void 0:t?L(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&N(n,e,s),s},R=(i,n)=>(e,t)=>n(e,t,i);const b="rpc.remote-sync.service",y=u.createIdentifier(b);exports.RemoteSyncPrimaryService=class{constructor(n){this._commandService=n}async syncMutation(n){return this._commandService.syncExecuteCommand(n.mutationInfo.id,n.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=E([R(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const f="univer.remote-instance-service",S=u.createIdentifier(f);exports.WebWorkerRemoteInstanceService=class{constructor(n,e,t){this._univerInstanceService=n,this._commandService=e,this._logService=t}whenReady(){return Promise.resolve(!0)}async syncMutation(n){return this._applyMutation(n.mutationInfo)}async createInstance(n){const{type:e,snapshot:t}=n;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,t),!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(n){return this._univerInstanceService.disposeUnit(n.unitID)}_applyMutation(n){const{id:e,params:t}=n;return this._commandService.syncExecuteCommand(e,t,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=E([R(0,a.IUniverInstanceService),R(1,a.ICommandService),R(2,a.ILogService)],exports.WebWorkerRemoteInstanceService);function M(i){const n=i;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let r=s.apply(n,[t]);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const r=s.apply(n,t);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function q(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return k(e)?i.subscribe(e,t[0]):i.call(e,t[0])}}})}function k(i){return i.endsWith("$")}class D extends a.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);l(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,r){return t._disposed?Promise.reject():t._remoteCall(e,s,r)},subscribe(s,r){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(p.filter(e=>e),p.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:t,args:s},o=this;return new Promise((g,d)=>{const W={handle(I){switch(I.type){case 201:o._pendingRequests.delete(r),g(I.data);break;case 202:o._pendingRequests.delete(r),d(I.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,W),this._sendRequest(h)})}_remoteSubscribe(e,t,s){return new _.Observable(r=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},g={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,g),this._sendRequest(o)}),()=>{if(c===-1)return;const h={type:102,seq:c,channelName:e,method:t};this._sendRequest(h)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class O extends a.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:r}=e,c=this._channels.get(t);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);h=c.call(s,r)}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:t,seq:s}=e,r=this._channels.get(t);try{if(!r)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=u.createIdentifier("IRPCChannelService");class U{constructor(n){l(this,"_client");l(this,"_server");this._client=new D(n),this._server=new O(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var H=Object.defineProperty,V=Object.getOwnPropertyDescriptor,A=(i,n,e,t)=>{for(var s=t>1?void 0:t?V(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&H(n,e,s),s},v=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,t,s,r,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=t,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}_initRPCChannels(){this._rpcChannelService.registerChannel(b,M(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>q(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(p.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(p.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{const{type:s,params:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===a.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(t!=null&&t.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=A([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncPrimaryController),v(0,u.Inject(u.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,m),v(4,y)],exports.DataSyncPrimaryController);var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,G=(i,n,e,t)=>{for(var s=t>1?void 0:t?z(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&F(n,e,s),s},C=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends a.Disposable{constructor(e,t,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,M(this._remoteInstanceService)),this._injector.add([y,{useFactory:()=>q(this._rpcChannelService.requestChannel(b))}]),this._remoteSyncService=this._injector.get(y)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{e.type===a.CommandType.MUTATION&&!(t!=null&&t.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=G([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncReplicaController),C(0,u.Inject(u.Injector)),C(1,S),C(2,a.ICommandService),C(3,m)],exports.DataSyncReplicaController);function B(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const n=e=>{i.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function J(i){return{send(n){i.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,T=(i,n,e,t)=>{for(var s=t>1?void 0:t?X(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&Q(n,e,s),s},j=(i,n)=>(e,t)=>n(e,t,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super(),this._config=n,this._injector=e}async onStarting(n){const{workerURL:e}=this._config,t=e instanceof Worker?e:new Worker(e),s=J(t);[[m,{useFactory:()=>new U(s)}],[exports.DataSyncPrimaryController],[y,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>n.add(c)),n.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=T([j(1,u.Inject(u.Injector))],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends a.Plugin{constructor(n,e){super(),this._config=n,this._injector=e}onStarting(n){[[exports.DataSyncReplicaController],[m,{useFactory:()=>new U(B())}],[S,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(e=>n.add(e)),n.get(exports.DataSyncReplicaController)}},l(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w);exports.UniverRPCWorkerThreadPlugin=T([j(1,u.Inject(u.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=D;exports.ChannelServer=O;exports.ChannelService=U;exports.IRPCChannelService=m;exports.IRemoteInstanceService=S;exports.IRemoteSyncService=y;exports.RemoteInstanceServiceName=f;exports.RemoteSyncServiceName=b; | ||
"use strict";var $=Object.defineProperty;var x=(i,n,e)=>n in i?$(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var l=(i,n,e)=>x(i,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),u=require("@wendellhu/redi"),d=require("rxjs/operators"),_=require("rxjs");var N=Object.defineProperty,L=Object.getOwnPropertyDescriptor,q=(i,n,e,t)=>{for(var s=t>1?void 0:t?L(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&N(n,e,s),s},R=(i,n)=>(e,t)=>n(e,t,i);const f="rpc.remote-sync.service",m=u.createIdentifier(f);exports.RemoteSyncPrimaryService=class{constructor(n){this._commandService=n}async syncMutation(n){return this._commandService.syncExecuteCommand(n.mutationInfo.id,n.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=q([R(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const b="univer.remote-instance-service",y=u.createIdentifier(b);exports.WebWorkerRemoteInstanceService=class{constructor(n,e,t){this._univerInstanceService=n,this._commandService=e,this._logService=t}whenReady(){return Promise.resolve(!0)}async syncMutation(n){return this._applyMutation(n.mutationInfo)}async createInstance(n){const{type:e,snapshot:t}=n;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,t),!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(n){return this._univerInstanceService.disposeUnit(n.unitID)}_applyMutation(n){const{id:e,params:t}=n;return this._commandService.syncExecuteCommand(e,t,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=q([R(0,a.IUniverInstanceService),R(1,a.ICommandService),R(2,a.ILogService)],exports.WebWorkerRemoteInstanceService);function U(i){const n=i;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let r=s.apply(n,[t]);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const r=s.apply(n,t);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function E(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return k(e)?i.subscribe(e,t[0]):i.call(e,t[0])}}})}function k(i){return i.endsWith("$")}class D extends a.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}dispose(){this._pendingRequests.clear()}getChannel(e){const t=this;return{call(s,r){return t._disposed?Promise.reject():t._remoteCall(e,s,r)},subscribe(s,r){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(d.filter(e=>e),d.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:t,args:s},o=this;return new Promise((g,p)=>{const W={handle(I){switch(I.type){case 201:o._pendingRequests.delete(r),g(I.data);break;case 202:o._pendingRequests.delete(r),p(I.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,W),this._sendRequest(h)})}_remoteSubscribe(e,t,s){return new _.Observable(r=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},g={handle(p){switch(p.type){case 300:r.next(p.data);break;case 301:r.error(p.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,g),this._sendRequest(o)}),()=>{if(c===-1)return;const h={type:102,seq:c,channelName:e,method:t};this._sendRequest(h)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class O extends a.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:r}=e,c=this._channels.get(t);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);h=c.call(s,r)}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:t,seq:s}=e,r=this._channels.get(t);try{if(!r)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const S=u.createIdentifier("IRPCChannelService");class M{constructor(n){l(this,"_client");l(this,"_server");this._client=new D(n),this._server=new O(n)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var H=Object.defineProperty,V=Object.getOwnPropertyDescriptor,A=(i,n,e,t)=>{for(var s=t>1?void 0:t?V(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&H(n,e,s),s},v=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,t,s,r,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=t,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}_initRPCChannels(){this._rpcChannelService.registerChannel(f,U(this._remoteSyncService)),this._injector.add([y,{useFactory:()=>E(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(y)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(d.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(d.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{const{type:s,params:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===a.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(t!=null&&t.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=A([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncPrimaryController),v(0,u.Inject(u.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,S),v(4,m)],exports.DataSyncPrimaryController);var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,G=(i,n,e,t)=>{for(var s=t>1?void 0:t?z(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&F(n,e,s),s},C=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends a.Disposable{constructor(e,t,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,U(this._remoteInstanceService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(f))}]),this._remoteSyncService=this._injector.get(m)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{e.type===a.CommandType.MUTATION&&!(t!=null&&t.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=G([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncReplicaController),C(0,u.Inject(u.Injector)),C(1,y),C(2,a.ICommandService),C(3,S)],exports.DataSyncReplicaController);function B(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const n=e=>{i.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function J(i){return{send(n){i.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,T=(i,n,e,t)=>{for(var s=t>1?void 0:t?X(n,e):n,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&Q(n,e,s),s},j=(i,n)=>(e,t)=>n(e,t,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super(),this._config=n,this._injector=e}async onStarting(n){const{workerURL:e}=this._config,t=e instanceof Worker?e:new Worker(e),s=J(t);[[S,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[m,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>n.add(c)),n.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=T([j(1,u.Inject(u.Injector))],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends a.Plugin{constructor(n,e){super(),this._config=n,this._injector=e}onStarting(n){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new M(B())}],[y,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(e=>n.add(e)),n.get(exports.DataSyncReplicaController)}},l(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w);exports.UniverRPCWorkerThreadPlugin=T([j(1,u.Inject(u.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=D;exports.ChannelServer=O;exports.ChannelService=M;exports.IRPCChannelService=S;exports.IRemoteInstanceService=y;exports.IRemoteSyncService=m;exports.RemoteInstanceServiceName=b;exports.RemoteSyncServiceName=f;exports.fromModule=U;exports.toModule=E; |
var X = Object.defineProperty; | ||
var Y = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e; | ||
var o = (s, t, e) => Y(s, typeof t != "symbol" ? t + "" : t, e); | ||
import { UniverInstanceType as u, ICommandService as b, IUniverInstanceService as N, ILogService as Z, RxDisposable as q, OnLifecycle as W, LifecycleStages as L, CommandType as H, Disposable as K, Plugin as V } from "@univerjs/core"; | ||
var Y = (n, t, e) => t in n ? X(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e; | ||
var a = (n, t, e) => Y(n, typeof t != "symbol" ? t + "" : t, e); | ||
import { UniverInstanceType as u, ICommandService as b, IUniverInstanceService as N, ILogService as Z, RxDisposable as U, OnLifecycle as W, LifecycleStages as L, CommandType as H, Disposable as K, Plugin as V } from "@univerjs/core"; | ||
import { createIdentifier as O, Inject as g, Injector as C } from "@wendellhu/redi"; | ||
import { takeUntil as f, filter as ee, take as te } from "rxjs/operators"; | ||
import { BehaviorSubject as ne, firstValueFrom as se, Observable as $, isObservable as re, of as ie, shareReplay as A } from "rxjs"; | ||
var ce = Object.defineProperty, ae = Object.getOwnPropertyDescriptor, F = (s, t, e, n) => { | ||
for (var r = n > 1 ? void 0 : n ? ae(t, e) : t, i = s.length - 1, c; i >= 0; i--) | ||
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r); | ||
return n && r && ce(t, e, r), r; | ||
}, m = (s, t) => (e, n) => t(e, n, s); | ||
import { isObservable as se, of as ne, BehaviorSubject as re, firstValueFrom as ie, Observable as $, shareReplay as A } from "rxjs"; | ||
var ce = Object.defineProperty, oe = Object.getOwnPropertyDescriptor, F = (n, t, e, s) => { | ||
for (var r = s > 1 ? void 0 : s ? oe(t, e) : t, i = n.length - 1, c; i >= 0; i--) | ||
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r); | ||
return s && r && ce(t, e, r), r; | ||
}, m = (n, t) => (e, s) => t(e, s, n); | ||
const j = "rpc.remote-sync.service", y = O(j); | ||
let M = class { | ||
constructor(s) { | ||
this._commandService = s; | ||
constructor(n) { | ||
this._commandService = n; | ||
} | ||
async syncMutation(s) { | ||
return this._commandService.syncExecuteCommand(s.mutationInfo.id, s.mutationInfo.params, { | ||
async syncMutation(n) { | ||
return this._commandService.syncExecuteCommand(n.mutationInfo.id, n.mutationInfo.params, { | ||
onlyLocal: !0, | ||
@@ -29,5 +29,5 @@ fromSync: !0 | ||
const x = "univer.remote-instance-service", S = O(x); | ||
let U = class { | ||
constructor(s, t, e) { | ||
this._univerInstanceService = s, this._commandService = t, this._logService = e; | ||
let q = class { | ||
constructor(n, t, e) { | ||
this._univerInstanceService = n, this._commandService = t, this._logService = e; | ||
} | ||
@@ -37,7 +37,7 @@ whenReady() { | ||
} | ||
async syncMutation(s) { | ||
return this._applyMutation(s.mutationInfo); | ||
async syncMutation(n) { | ||
return this._applyMutation(n.mutationInfo); | ||
} | ||
async createInstance(s) { | ||
const { type: t, snapshot: e } = s; | ||
async createInstance(n) { | ||
const { type: t, snapshot: e } = n; | ||
try { | ||
@@ -52,11 +52,11 @@ switch (t) { | ||
} | ||
} catch (n) { | ||
throw n instanceof Error ? n : new TypeError(`${n}`); | ||
} catch (s) { | ||
throw s instanceof Error ? s : new TypeError(`${s}`); | ||
} | ||
} | ||
async disposeInstance(s) { | ||
return this._univerInstanceService.disposeUnit(s.unitID); | ||
async disposeInstance(n) { | ||
return this._univerInstanceService.disposeUnit(n.unitID); | ||
} | ||
_applyMutation(s) { | ||
const { id: t, params: e } = s; | ||
_applyMutation(n) { | ||
const { id: t, params: e } = n; | ||
return this._commandService.syncExecuteCommand(t, e, { | ||
@@ -68,14 +68,14 @@ onlyLocal: !0, | ||
}; | ||
U = F([ | ||
q = F([ | ||
m(0, N), | ||
m(1, b), | ||
m(2, Z) | ||
], U); | ||
function z(s) { | ||
const t = s; | ||
], q); | ||
function z(n) { | ||
const t = n; | ||
return new class { | ||
call(e, n) { | ||
call(e, s) { | ||
const r = t[e]; | ||
if (typeof r == "function") { | ||
let i = r.apply(t, [n]); | ||
let i = r.apply(t, [s]); | ||
return i instanceof Promise || (i = Promise.resolve(i)), i; | ||
@@ -85,7 +85,7 @@ } | ||
} | ||
subscribe(e, n) { | ||
subscribe(e, s) { | ||
const r = t[e]; | ||
if (typeof r == "function") { | ||
const i = r.apply(t, n); | ||
return re(i) ? i : ie(i); | ||
const i = r.apply(t, s); | ||
return se(i) ? i : ne(i); | ||
} | ||
@@ -96,8 +96,8 @@ throw new Error(`[RPC]: observable method not found for ${e}!`); | ||
} | ||
function G(s) { | ||
function G(n) { | ||
return new Proxy({}, { | ||
get(t, e) { | ||
if (e !== "dispose") | ||
return function(...n) { | ||
return oe(e) ? s.subscribe(e, n[0]) : s.call(e, n[0]); | ||
return function(...s) { | ||
return ae(e) ? n.subscribe(e, s[0]) : n.call(e, s[0]); | ||
}; | ||
@@ -107,24 +107,26 @@ } | ||
} | ||
function oe(s) { | ||
return s.endsWith("$"); | ||
function ae(n) { | ||
return n.endsWith("$"); | ||
} | ||
class he extends q { | ||
class he extends U { | ||
constructor(e) { | ||
super(); | ||
o(this, "_initialized", new ne(!1)); | ||
o(this, "_lastRequestCounter", 0); | ||
o(this, "_pendingRequests", /* @__PURE__ */ new Map()); | ||
o(this, "_pendingSubscriptions", /* @__PURE__ */ new Map()); | ||
this._protocol = e, this._protocol.onMessage.pipe(f(this.dispose$)).subscribe((n) => this._onMessage(n)); | ||
a(this, "_initialized", new re(!1)); | ||
a(this, "_lastRequestCounter", 0); | ||
a(this, "_pendingRequests", /* @__PURE__ */ new Map()); | ||
this._protocol = e, this._protocol.onMessage.pipe(f(this.dispose$)).subscribe((s) => this._onMessage(s)); | ||
} | ||
dispose() { | ||
this._pendingRequests.clear(); | ||
} | ||
getChannel(e) { | ||
const n = this; | ||
const s = this; | ||
return { | ||
call(r, i) { | ||
return n._disposed ? Promise.reject() : n._remoteCall(e, r, i); | ||
return s._disposed ? Promise.reject() : s._remoteCall(e, r, i); | ||
}, | ||
subscribe(r, i) { | ||
if (n._disposed) | ||
if (s._disposed) | ||
throw new Error("[ChannelClient]: client is disposed!"); | ||
return n._remoteSubscribe(e, r, i); | ||
return s._remoteSubscribe(e, r, i); | ||
} | ||
@@ -134,3 +136,3 @@ }; | ||
_whenReady() { | ||
return se( | ||
return ie( | ||
this._initialized.pipe( | ||
@@ -142,5 +144,5 @@ ee((e) => e), | ||
} | ||
async _remoteCall(e, n, r) { | ||
async _remoteCall(e, s, r) { | ||
await this._whenReady(); | ||
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args: r }, a = this; | ||
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: s, args: r }, o = this; | ||
return new Promise((I, _) => { | ||
@@ -151,6 +153,6 @@ const Q = { | ||
case 201: | ||
a._pendingRequests.delete(i), I(w.data); | ||
o._pendingRequests.delete(i), I(w.data); | ||
break; | ||
case 202: | ||
a._pendingRequests.delete(i), _(w.data); | ||
o._pendingRequests.delete(i), _(w.data); | ||
break; | ||
@@ -165,3 +167,3 @@ default: | ||
} | ||
_remoteSubscribe(e, n, r) { | ||
_remoteSubscribe(e, s, r) { | ||
return new $((i) => { | ||
@@ -171,3 +173,3 @@ let c = -1; | ||
c = ++this._lastRequestCounter; | ||
const a = { seq: c, type: 101, channelName: e, method: n, args: r }, I = { | ||
const o = { seq: c, type: 101, channelName: e, method: s, args: r }, I = { | ||
handle(_) { | ||
@@ -189,3 +191,3 @@ switch (_.type) { | ||
}; | ||
this._pendingRequests.set(c, I), this._sendRequest(a); | ||
this._pendingRequests.set(c, I), this._sendRequest(o); | ||
}), () => { | ||
@@ -198,3 +200,3 @@ if (c === -1) | ||
channelName: e, | ||
method: n | ||
method: s | ||
}; | ||
@@ -209,3 +211,3 @@ this._sendRequest(h); | ||
_onMessage(e) { | ||
var n; | ||
var s; | ||
switch (e.type) { | ||
@@ -220,3 +222,3 @@ case 0: | ||
case 301: | ||
(n = this._pendingRequests.get(e.seq)) == null || n.handle(e); | ||
(s = this._pendingRequests.get(e.seq)) == null || s.handle(e); | ||
break; | ||
@@ -226,8 +228,8 @@ } | ||
} | ||
class _e extends q { | ||
class _e extends U { | ||
constructor(e) { | ||
super(); | ||
o(this, "_channels", /* @__PURE__ */ new Map()); | ||
o(this, "_subscriptions", /* @__PURE__ */ new Map()); | ||
this._protocol = e, this._protocol.onMessage.pipe(f(this.dispose$)).subscribe((n) => this._onRequest(n)), this._sendResponse({ | ||
a(this, "_channels", /* @__PURE__ */ new Map()); | ||
a(this, "_subscriptions", /* @__PURE__ */ new Map()); | ||
this._protocol = e, this._protocol.onMessage.pipe(f(this.dispose$)).subscribe((s) => this._onRequest(s)), this._sendResponse({ | ||
seq: -1, | ||
@@ -238,5 +240,8 @@ type: 0 | ||
} | ||
registerChannel(e, n) { | ||
this._channels.set(e, n); | ||
dispose() { | ||
super.dispose(), this._subscriptions.clear(), this._channels.clear(); | ||
} | ||
registerChannel(e, s) { | ||
this._channels.set(e, s); | ||
} | ||
_onRequest(e) { | ||
@@ -256,28 +261,28 @@ switch (e.type) { | ||
_onMethodCall(e) { | ||
const { channelName: n, method: r, args: i } = e, c = this._channels.get(n); | ||
const { channelName: s, method: r, args: i } = e, c = this._channels.get(s); | ||
let h; | ||
try { | ||
if (!c) | ||
throw new Error(`[ChannelServer]: Channel ${n} not found!`); | ||
throw new Error(`[ChannelServer]: Channel ${s} not found!`); | ||
h = c.call(r, i); | ||
} catch (a) { | ||
h = Promise.reject(a); | ||
} catch (o) { | ||
h = Promise.reject(o); | ||
} | ||
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) }); | ||
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: r } = e, i = this._channels.get(n); | ||
const { channelName: s, seq: r } = e, i = this._channels.get(s); | ||
try { | ||
if (!i) | ||
throw new Error(`[ChannelServer]: Channel ${n} not found!`); | ||
throw new Error(`[ChannelServer]: Channel ${s} not found!`); | ||
const h = i.subscribe(e.method, e.args).subscribe({ | ||
next: (a) => { | ||
this._sendResponse({ seq: r, type: 300, data: a }); | ||
next: (o) => { | ||
this._sendResponse({ seq: r, type: 300, data: o }); | ||
}, | ||
error: (a) => { | ||
this._sendResponse({ seq: r, type: 301, data: a.message }), this._sendResponse({ | ||
error: (o) => { | ||
this._sendResponse({ seq: r, type: 301, data: o.message }), this._sendResponse({ | ||
seq: r, | ||
@@ -302,4 +307,4 @@ type: 302 | ||
_onUnsubscribe(e) { | ||
const n = this._subscriptions.get(e.seq); | ||
n && (n.unsubscribe(), this._subscriptions.delete(e.seq)); | ||
const s = this._subscriptions.get(e.seq); | ||
s && (s.unsubscribe(), this._subscriptions.delete(e.seq)); | ||
} | ||
@@ -313,6 +318,9 @@ _sendResponse(e) { | ||
constructor(t) { | ||
o(this, "_client"); | ||
o(this, "_server"); | ||
a(this, "_client"); | ||
a(this, "_server"); | ||
this._client = new he(t), this._server = new _e(t); | ||
} | ||
dispose() { | ||
this._client.dispose(), this._server.dispose(); | ||
} | ||
requestChannel(t) { | ||
@@ -325,14 +333,14 @@ return this._client.getChannel(t); | ||
} | ||
var le = Object.defineProperty, ue = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => { | ||
for (var r = n > 1 ? void 0 : n ? ue(t, e) : t, i = s.length - 1, c; i >= 0; i--) | ||
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r); | ||
return n && r && le(t, e, r), r; | ||
}, l = (s, t) => (e, n) => t(e, n, s); | ||
let p = class extends q { | ||
constructor(t, e, n, r, i) { | ||
var le = Object.defineProperty, ue = Object.getOwnPropertyDescriptor, pe = (n, t, e, s) => { | ||
for (var r = s > 1 ? void 0 : s ? ue(t, e) : t, i = n.length - 1, c; i >= 0; i--) | ||
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r); | ||
return s && r && le(t, e, r), r; | ||
}, l = (n, t) => (e, s) => t(e, s, n); | ||
let p = class extends U { | ||
constructor(t, e, s, r, i) { | ||
super(); | ||
o(this, "_remoteInstanceService"); | ||
o(this, "_syncingUnits", /* @__PURE__ */ new Set()); | ||
o(this, "_syncingMutations", /* @__PURE__ */ new Set()); | ||
this._injector = t, this._commandService = e, this._univerInstanceService = n, this._rpcChannelService = r, this._remoteSyncService = i, this._initRPCChannels(), this._init(); | ||
a(this, "_remoteInstanceService"); | ||
a(this, "_syncingUnits", /* @__PURE__ */ new Set()); | ||
a(this, "_syncingMutations", /* @__PURE__ */ new Set()); | ||
this._injector = t, this._commandService = e, this._univerInstanceService = s, this._rpcChannelService = r, this._remoteSyncService = i, this._initRPCChannels(), this._init(); | ||
} | ||
@@ -360,4 +368,4 @@ registerSyncingMutations(t) { | ||
}), this.disposeWithMe(this._commandService.onCommandExecuted((t, e) => { | ||
const { type: n, params: r, id: i } = t, c = (r == null ? void 0 : r.unitId) || ""; | ||
n === H.MUTATION && // only sync mutations to the worker thread | ||
const { type: s, params: r, id: i } = t, c = (r == null ? void 0 : r.unitId) || ""; | ||
s === H.MUTATION && // only sync mutations to the worker thread | ||
(!c || this._syncingUnits.has(c)) && // do not sync mutations from the web worker back to the web worker | ||
@@ -377,12 +385,12 @@ !(e != null && e.fromSync) && // do not sync mutations those are not meant to be synced | ||
], p); | ||
var de = Object.defineProperty, ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => { | ||
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--) | ||
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r); | ||
return n && r && de(t, e, r), r; | ||
}, v = (s, t) => (e, n) => t(e, n, s); | ||
var de = Object.defineProperty, ve = Object.getOwnPropertyDescriptor, me = (n, t, e, s) => { | ||
for (var r = s > 1 ? void 0 : s ? ve(t, e) : t, i = n.length - 1, c; i >= 0; i--) | ||
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r); | ||
return s && r && de(t, e, r), r; | ||
}, v = (n, t) => (e, s) => t(e, s, n); | ||
let d = class extends K { | ||
constructor(t, e, n, r) { | ||
constructor(t, e, s, r) { | ||
super(); | ||
o(this, "_remoteSyncService"); | ||
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init(); | ||
a(this, "_remoteSyncService"); | ||
this._injector = t, this._remoteInstanceService = e, this._commandService = s, this._rpcChannelService = r, this._initRPCChannels(), this._init(); | ||
} | ||
@@ -415,8 +423,8 @@ _initRPCChannels() { | ||
return { | ||
send(s) { | ||
postMessage(s); | ||
send(n) { | ||
postMessage(n); | ||
}, | ||
onMessage: new $((s) => { | ||
onMessage: new $((n) => { | ||
const t = (e) => { | ||
s.next(e.data); | ||
n.next(e.data); | ||
}; | ||
@@ -427,26 +435,26 @@ return addEventListener("message", t), () => removeEventListener("message", t); | ||
} | ||
function ye(s) { | ||
function ye(n) { | ||
return { | ||
send(t) { | ||
s.postMessage(t); | ||
n.postMessage(t); | ||
}, | ||
onMessage: new $((t) => { | ||
const e = (n) => { | ||
t.next(n.data); | ||
const e = (s) => { | ||
t.next(s.data); | ||
}; | ||
return s.addEventListener("message", e), () => s.removeEventListener("message", e); | ||
return n.addEventListener("message", e), () => n.removeEventListener("message", e); | ||
}).pipe(A(1)) | ||
}; | ||
} | ||
var Se = Object.defineProperty, be = Object.getOwnPropertyDescriptor, k = (s, t, e, n) => { | ||
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, c; i >= 0; i--) | ||
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r); | ||
return n && r && Se(t, e, r), r; | ||
}, J = (s, t) => (e, n) => t(e, n, s), P; | ||
var Se = Object.defineProperty, be = Object.getOwnPropertyDescriptor, k = (n, t, e, s) => { | ||
for (var r = s > 1 ? void 0 : s ? be(t, e) : t, i = n.length - 1, c; i >= 0; i--) | ||
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r); | ||
return s && r && Se(t, e, r), r; | ||
}, J = (n, t) => (e, s) => t(e, s, n), P; | ||
let D = (P = class extends V { | ||
constructor(s, t) { | ||
super(), this._config = s, this._injector = t; | ||
constructor(n, t) { | ||
super(), this._config = n, this._injector = t; | ||
} | ||
async onStarting(s) { | ||
const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t), n = ye(e); | ||
async onStarting(n) { | ||
const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t), s = ye(e); | ||
[ | ||
@@ -456,3 +464,3 @@ [ | ||
{ | ||
useFactory: () => new B(n) | ||
useFactory: () => new B(s) | ||
} | ||
@@ -462,5 +470,5 @@ ], | ||
[y, { useClass: M }] | ||
].forEach((i) => s.add(i)), s.get(p); | ||
].forEach((i) => n.add(i)), n.get(p); | ||
} | ||
}, o(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P); | ||
}, a(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P); | ||
D = k([ | ||
@@ -471,6 +479,6 @@ J(1, g(C)) | ||
let T = (E = class extends V { | ||
constructor(s, t) { | ||
super(), this._config = s, this._injector = t; | ||
constructor(n, t) { | ||
super(), this._config = n, this._injector = t; | ||
} | ||
onStarting(s) { | ||
onStarting(n) { | ||
[ | ||
@@ -484,6 +492,6 @@ [d], | ||
], | ||
[S, { useClass: U }] | ||
].forEach((t) => s.add(t)), s.get(d); | ||
[S, { useClass: q }] | ||
].forEach((t) => n.add(t)), n.get(d); | ||
} | ||
}, o(E, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), E); | ||
}, a(E, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), E); | ||
T = k([ | ||
@@ -506,3 +514,5 @@ J(1, g(C)) | ||
T as UniverRPCWorkerThreadPlugin, | ||
U as WebWorkerRemoteInstanceService | ||
q as WebWorkerRemoteInstanceService, | ||
z as fromModule, | ||
G as toModule | ||
}; |
@@ -21,3 +21,3 @@ /** | ||
export { ChannelService, IRPCChannelService } from './services/rpc/channel.service'; | ||
export { ChannelClient, ChannelServer, type IMessageProtocol } from './services/rpc/rpc.service'; | ||
export { ChannelClient, ChannelServer, type IMessageProtocol, fromModule, toModule } from './services/rpc/rpc.service'; | ||
export { IRemoteSyncService } from './services/remote-instance/remote-instance.service'; |
@@ -0,1 +1,2 @@ | ||
import { IDisposable } from '@wendellhu/redi'; | ||
import { IChannel, IMessageProtocol } from './rpc.service'; | ||
@@ -11,8 +12,9 @@ | ||
*/ | ||
export declare class ChannelService { | ||
export declare class ChannelService implements IDisposable { | ||
private readonly _client; | ||
private readonly _server; | ||
constructor(_messageProtocol: IMessageProtocol); | ||
dispose(): void; | ||
requestChannel(name: string): IChannel; | ||
registerChannel(name: string, channel: IChannel): void; | ||
} |
@@ -53,4 +53,4 @@ import { RxDisposable } from '@univerjs/core'; | ||
private _pendingRequests; | ||
private _pendingSubscriptions; | ||
constructor(_protocol: IMessageProtocol); | ||
dispose(): void; | ||
getChannel<T extends IChannel>(channelName: string): T; | ||
@@ -68,2 +68,3 @@ private _whenReady; | ||
constructor(_protocol: IMessageProtocol); | ||
dispose(): void; | ||
registerChannel(channelName: string, channel: IChannel): void; | ||
@@ -70,0 +71,0 @@ private _onRequest; |
@@ -1,1 +0,1 @@ | ||
(function(i,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs/operators","rxjs"],c):(i=typeof globalThis<"u"?globalThis:i||self,c(i.UniverRpc={},i.UniverCore,i["@wendellhu/redi"],i.rxjs.operators,i.rxjs))})(this,function(i,c,l,v,d){"use strict";var Y=Object.defineProperty;var Z=(i,c,l)=>c in i?Y(i,c,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[c]=l;var u=(i,c,l)=>Z(i,typeof c!="symbol"?c+"":c,l);var P,w;var $=Object.defineProperty,N=Object.getOwnPropertyDescriptor,M=(a,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&$(t,e,s),s},C=(a,t)=>(e,n)=>t(e,n,a);const R="rpc.remote-sync.service",y=l.createIdentifier(R);i.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},i.RemoteSyncPrimaryService=M([C(0,c.ICommandService)],i.RemoteSyncPrimaryService);const b="univer.remote-instance-service",m=l.createIdentifier(b);i.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})}},i.WebWorkerRemoteInstanceService=M([C(0,c.IUniverInstanceService),C(1,c.ICommandService),C(2,c.ILogService)],i.WebWorkerRemoteInstanceService);function q(a){const t=a;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 d.isObservable(r)?r:d.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function D(a){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return L(e)?a.subscribe(e,n[0]):a.call(e,n[0])}}})}function L(a){return a.endsWith("$")}class O extends c.RxDisposable{constructor(e){super();u(this,"_initialized",new d.BehaviorSubject(!1));u(this,"_lastRequestCounter",0);u(this,"_pendingRequests",new Map);u(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}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 d.firstValueFrom(this._initialized.pipe(v.filter(e=>e),v.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,_={seq:r,type:100,channelName:e,method:n,args:s},h=this;return new Promise((U,f)=>{const X={handle(E){switch(E.type){case 201:h._pendingRequests.delete(r),U(E.data);break;case 202:h._pendingRequests.delete(r),f(E.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,X),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(r=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const h={seq:o,type:101,channelName:e,method:n,args:s},U={handle(f){switch(f.type){case 300:r.next(f.data);break;case 301:r.error(f.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,U),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 T extends c.RxDisposable{constructor(e){super();u(this,"_channels",new Map);u(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendResponse({seq:-1,type:0})}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:n,method:s,args:r}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=o.call(s,r)}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,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=r.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 p=l.createIdentifier("IRPCChannelService");class g{constructor(t){u(this,"_client");u(this,"_server");this._client=new O(t),this._server=new T(t)}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var k=Object.defineProperty,H=Object.getOwnPropertyDescriptor,V=(a,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&k(t,e,s),s},S=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,o){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);u(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}_initRPCChannels(){this._rpcChannelService.registerChannel(R,q(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>D(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(v.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(v.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:o}=e,_=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},i.DataSyncPrimaryController=V([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncPrimaryController),S(0,l.Inject(l.Injector)),S(1,c.ICommandService),S(2,c.IUniverInstanceService),S(3,p),S(4,y)],i.DataSyncPrimaryController);var A=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(a,t,e,n)=>{for(var s=n>1?void 0:n?F(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&A(t,e,s),s},I=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncReplicaController=class extends c.Disposable{constructor(e,n,s,r){super();u(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,q(this._remoteInstanceService)),this._injector.add([y,{useFactory:()=>D(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(y)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},i.DataSyncReplicaController=z([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncReplicaController),I(0,l.Inject(l.Injector)),I(1,m),I(2,c.ICommandService),I(3,p)],i.DataSyncReplicaController);function G(){return{send(a){postMessage(a)},onMessage:new d.Observable(a=>{const t=e=>{a.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function B(a){return{send(t){a.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return a.addEventListener("message",e),()=>a.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,j=(a,t,e,n)=>{for(var s=n>1?void 0:n?Q(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&J(t,e,s),s},W=(a,t)=>(e,n)=>t(e,n,a);i.UniverRPCMainThreadPlugin=(P=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}async onStarting(t){const{workerURL:e}=this._config,n=e instanceof Worker?e:new Worker(e),s=B(n);[[p,{useFactory:()=>new g(s)}],[i.DataSyncPrimaryController],[y,{useClass:i.RemoteSyncPrimaryService}]].forEach(o=>t.add(o)),t.get(i.DataSyncPrimaryController)}},u(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P),i.UniverRPCMainThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCMainThreadPlugin),i.UniverRPCWorkerThreadPlugin=(w=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}onStarting(t){[[i.DataSyncReplicaController],[p,{useFactory:()=>new g(G())}],[m,{useClass:i.WebWorkerRemoteInstanceService}]].forEach(e=>t.add(e)),t.get(i.DataSyncReplicaController)}},u(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w),i.UniverRPCWorkerThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCWorkerThreadPlugin),i.ChannelClient=O,i.ChannelServer=T,i.ChannelService=g,i.IRPCChannelService=p,i.IRemoteInstanceService=m,i.IRemoteSyncService=y,i.RemoteInstanceServiceName=b,i.RemoteSyncServiceName=R,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); | ||
(function(i,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs/operators","rxjs"],c):(i=typeof globalThis<"u"?globalThis:i||self,c(i.UniverRpc={},i.UniverCore,i["@wendellhu/redi"],i.rxjs.operators,i.rxjs))})(this,function(i,c,l,v,d){"use strict";var Y=Object.defineProperty;var Z=(i,c,l)=>c in i?Y(i,c,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[c]=l;var u=(i,c,l)=>Z(i,typeof c!="symbol"?c+"":c,l);var U,E;var $=Object.defineProperty,N=Object.getOwnPropertyDescriptor,D=(a,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&$(t,e,s),s},C=(a,t)=>(e,n)=>t(e,n,a);const R="rpc.remote-sync.service",p=l.createIdentifier(R);i.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},i.RemoteSyncPrimaryService=D([C(0,c.ICommandService)],i.RemoteSyncPrimaryService);const b="univer.remote-instance-service",m=l.createIdentifier(b);i.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})}},i.WebWorkerRemoteInstanceService=D([C(0,c.IUniverInstanceService),C(1,c.ICommandService),C(2,c.ILogService)],i.WebWorkerRemoteInstanceService);function g(a){const t=a;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 d.isObservable(r)?r:d.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(a){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return L(e)?a.subscribe(e,n[0]):a.call(e,n[0])}}})}function L(a){return a.endsWith("$")}class O extends c.RxDisposable{constructor(e){super();u(this,"_initialized",new d.BehaviorSubject(!1));u(this,"_lastRequestCounter",0);u(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.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 d.firstValueFrom(this._initialized.pipe(v.filter(e=>e),v.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,_={seq:r,type:100,channelName:e,method:n,args:s},h=this;return new Promise((M,f)=>{const X={handle(q){switch(q.type){case 201:h._pendingRequests.delete(r),M(q.data);break;case 202:h._pendingRequests.delete(r),f(q.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,X),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(r=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const h={seq:o,type:101,channelName:e,method:n,args:s},M={handle(f){switch(f.type){case 300:r.next(f.data);break;case 301:r.error(f.data);break;case 302:r.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 T extends c.RxDisposable{constructor(e){super();u(this,"_channels",new Map);u(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.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,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=o.call(s,r)}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,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=r.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 y=l.createIdentifier("IRPCChannelService");class w{constructor(t){u(this,"_client");u(this,"_server");this._client=new O(t),this._server=new T(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var k=Object.defineProperty,H=Object.getOwnPropertyDescriptor,V=(a,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&k(t,e,s),s},S=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,o){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);u(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}_initRPCChannels(){this._rpcChannelService.registerChannel(R,g(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>P(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(v.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(v.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:o}=e,_=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},i.DataSyncPrimaryController=V([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncPrimaryController),S(0,l.Inject(l.Injector)),S(1,c.ICommandService),S(2,c.IUniverInstanceService),S(3,y),S(4,p)],i.DataSyncPrimaryController);var A=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(a,t,e,n)=>{for(var s=n>1?void 0:n?F(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&A(t,e,s),s},I=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncReplicaController=class extends c.Disposable{constructor(e,n,s,r){super();u(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,g(this._remoteInstanceService)),this._injector.add([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(p)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},i.DataSyncReplicaController=z([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncReplicaController),I(0,l.Inject(l.Injector)),I(1,m),I(2,c.ICommandService),I(3,y)],i.DataSyncReplicaController);function G(){return{send(a){postMessage(a)},onMessage:new d.Observable(a=>{const t=e=>{a.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function B(a){return{send(t){a.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return a.addEventListener("message",e),()=>a.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,j=(a,t,e,n)=>{for(var s=n>1?void 0:n?Q(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&J(t,e,s),s},W=(a,t)=>(e,n)=>t(e,n,a);i.UniverRPCMainThreadPlugin=(U=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}async onStarting(t){const{workerURL:e}=this._config,n=e instanceof Worker?e:new Worker(e),s=B(n);[[y,{useFactory:()=>new w(s)}],[i.DataSyncPrimaryController],[p,{useClass:i.RemoteSyncPrimaryService}]].forEach(o=>t.add(o)),t.get(i.DataSyncPrimaryController)}},u(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),i.UniverRPCMainThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCMainThreadPlugin),i.UniverRPCWorkerThreadPlugin=(E=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}onStarting(t){[[i.DataSyncReplicaController],[y,{useFactory:()=>new w(G())}],[m,{useClass:i.WebWorkerRemoteInstanceService}]].forEach(e=>t.add(e)),t.get(i.DataSyncReplicaController)}},u(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),i.UniverRPCWorkerThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCWorkerThreadPlugin),i.ChannelClient=O,i.ChannelServer=T,i.ChannelService=w,i.IRPCChannelService=y,i.IRemoteInstanceService=m,i.IRemoteSyncService=p,i.RemoteInstanceServiceName=b,i.RemoteSyncServiceName=R,i.fromModule=g,i.toModule=P,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); |
{ | ||
"name": "@univerjs/rpc", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"private": false, | ||
@@ -50,3 +50,3 @@ "author": "DreamNum <developer@univer.ai>", | ||
"rxjs": ">=7.0.0", | ||
"@univerjs/core": "0.2.2" | ||
"@univerjs/core": "0.2.3" | ||
}, | ||
@@ -59,4 +59,4 @@ "devDependencies": { | ||
"vitest": "^1.6.0", | ||
"@univerjs/core": "0.2.2", | ||
"@univerjs/shared": "0.2.2" | ||
"@univerjs/core": "0.2.3", | ||
"@univerjs/shared": "0.2.3" | ||
}, | ||
@@ -63,0 +63,0 @@ "univerSpace": { |
# @univerjs/rpc | ||
[](https://npmjs.org/package/@univerjs/rpc) | ||
[](https://img.shields.io/npm/l/@univerjs/rpc) | ||
## Package Overview | ||
| Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales | | ||
| --- | --- | --- | --- | --- | :---: | :---: | | ||
| `@univerjs/rpc` | `UniverRpc` | [![][npm-version-shield]][npm-version-link] | ![][npm-license-shield] | ![][npm-downloads-shield] | ❌ | ❌ | | ||
## Introduction | ||
@@ -23,1 +26,7 @@ | ||
``` | ||
<!-- Links --> | ||
[npm-version-shield]: https://img.shields.io/npm/v/@univerjs/rpc?style=flat-square | ||
[npm-version-link]: https://npmjs.com/package/@univerjs/rpc | ||
[npm-license-shield]: https://img.shields.io/npm/l/@univerjs/rpc?style=flat-square | ||
[npm-downloads-shield]: https://img.shields.io/npm/dm/@univerjs/rpc?style=flat-square |
61892
849
32