@univerjs/rpc 0.1.7 → 0.1.8
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 T=Object.defineProperty;var x=(i,n,e)=>n in i?T(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),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),h=require("@wendellhu/redi"),d=require("rxjs/operators"),_=require("rxjs");var L=Object.defineProperty,k=Object.getOwnPropertyDescriptor,q=(i,n,e,t)=>{for(var r=t>1?void 0:t?k(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&L(n,e,r),r},b=(i,n)=>(e,t)=>n(e,t,i);const f="univer.remote-sync-service",m=h.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([b(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const R="univer.remote-instance-service",S=h.createIdentifier(R);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.SHEET:return!!this._univerInstanceService.createSheet(t);default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(r){throw r instanceof Error?r:new TypeError(`${r}`)}}async disposeInstance(n){return this._univerInstanceService.disposeDocument(n.unitID)}_applyMutation(n){const{id:e,params:t}=n;return this._commandService.hasCommand(e)?this._commandService.syncExecuteCommand(e,t,{onlyLocal:!0,fromSync:!0}):(this._logService.debug("[RemoteInstanceReplicaService]",`command "${e}" not found. Skip sync mutation.`),!0)}};exports.WebWorkerRemoteInstanceService=q([b(0,a.IUniverInstanceService),b(1,a.ICommandService),b(2,a.ILogService)],exports.WebWorkerRemoteInstanceService);function D(i){const n=i;return new class{call(e,t){const r=n[e];if(typeof r=="function"){let s=r.apply(n,[t]);return s instanceof Promise||(s=Promise.resolve(s)),s}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const r=n[e];if(typeof r=="function"){const s=r.apply(n,t);return _.isObservable(s)?s:_.of(s)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function M(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return N(e)?i.subscribe(e,t[0]):i.call(e,t[0])}}})}function N(i){return i.endsWith("$")}class E 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(d.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(r,s){return t._disposed?Promise.reject():t._remoteCall(e,r,s)},subscribe(r,s){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,r,s)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(d.filter(e=>e),d.take(1)))}async _remoteCall(e,t,r){await this._whenReady();const s=++this._lastRequestCounter,u={seq:s,type:100,channelName:e,method:t,args:r},o=this;return new Promise((g,p)=>{const $={handle(I){switch(I.type){case 201:o._pendingRequests.delete(s),g(I.data);break;case 202:o._pendingRequests.delete(s),p(I.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(s,$),this._sendRequest(u)})}_remoteSubscribe(e,t,r){return new _.Observable(s=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:r},g={handle(p){switch(p.type){case 300:s.next(p.data);break;case 301:s.error(p.data);break;case 302:s.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,g),this._sendRequest(o)}),()=>{if(c===-1)return;const u={type:102,seq:c,channelName:e,method:t};this._sendRequest(u)}})}_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})}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:r,args:s}=e,c=this._channels.get(t);let u;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);u=c.call(r,s)}catch(o){u=Promise.reject(o)}u.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:r}=e,s=this._channels.get(t);try{if(!s)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const u=s.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:r,type:300,data:o})},error:o=>{this._sendResponse({seq:r,type:301,data:o.message}),this._sendResponse({seq:r,type:302})},complete:()=>{this._sendResponse({seq:r,type:302})}});this._subscriptions.set(e.seq,u)}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 y=h.createIdentifier("IRPCChannelService");class U{constructor(n){l(this,"_client");l(this,"_server");this._client=new E(n),this._server=new O(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var A=Object.defineProperty,H=Object.getOwnPropertyDescriptor,F=(i,n,e,t)=>{for(var r=t>1?void 0:t?H(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&A(n,e,r),r},v=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,t,r,s,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);this._injector=e,this._commandService=t,this._univerInstanceService=r,this._rpcChannelService=s,this._remoteSyncService=c,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,D(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>M(this._rpcChannelService.requestChannel(R))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.sheetAdded$.pipe(d.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:a.UniverInstanceType.SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.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:r,params:s}=e,c=(s==null?void 0:s.unitId)||"";r===a.CommandType.MUTATION&&(!c||this._syncingUnits.has(c))&&!(t!=null&&t.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=F([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncPrimaryController),v(0,h.Inject(h.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,y),v(4,m)],exports.DataSyncPrimaryController);var z=Object.defineProperty,V=Object.getOwnPropertyDescriptor,G=(i,n,e,t)=>{for(var r=t>1?void 0:t?V(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&z(n,e,r),r},C=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends a.Disposable{constructor(e,t,r,s){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=r,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,D(this._remoteInstanceService)),this._injector.add([m,{useFactory:()=>M(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,h.Inject(h.Injector)),C(1,S),C(2,a.ICommandService),C(3,y)],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,j=(i,n,e,t)=>{for(var r=t>1?void 0:t?X(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&Q(n,e,r),r},W=(i,n)=>(e,t)=>n(e,t,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const{workerURL:e}=this._config,t=e instanceof Worker?e:new Worker(e),r=J(t);[[y,{useFactory:()=>new U(r)}],[exports.DataSyncPrimaryController],[m,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>n.add(c)),n.get(exports.DataSyncPrimaryController)}},l(P,"type",a.PluginType.Univer),P);exports.UniverRPCMainThreadPlugin=j([W(1,h.Inject(h.Injector))],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[exports.DataSyncReplicaController],[y,{useFactory:()=>new U(B())}],[S,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(e=>n.add(e)),n.get(exports.DataSyncReplicaController)}},l(w,"type",a.PluginType.Univer),w);exports.UniverRPCWorkerThreadPlugin=j([W(1,h.Inject(h.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=E;exports.ChannelServer=O;exports.ChannelService=U;exports.IRPCChannelService=y;exports.IRemoteInstanceService=S;exports.IRemoteSyncService=m;exports.RemoteInstanceServiceName=R;exports.RemoteSyncServiceName=f;
|
|
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),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),h=require("@wendellhu/redi"),p=require("rxjs/operators"),_=require("rxjs");var N=Object.defineProperty,L=Object.getOwnPropertyDescriptor,E=(i,n,e,t)=>{for(var r=t>1?void 0:t?L(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&N(n,e,r),r},m=(i,n)=>(e,t)=>n(e,t,i);const f="rpc.remote-sync.service",S=h.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=E([m(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const b="univer.remote-instance-service",y=h.createIdentifier(b);exports.WebWorkerRemoteInstanceService=class{constructor(n,e,t,r){this._injector=n,this._univerInstanceService=e,this._commandService=t,this._logService=r}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(r){throw r instanceof Error?r:new TypeError(`${r}`)}}async disposeInstance(n){return this._univerInstanceService.disposeUnit(n.unitID)}_applyMutation(n){const{id:e,params:t}=n;return this._commandService.hasCommand(e)?this._commandService.syncExecuteCommand(e,t,{onlyLocal:!0,fromSync:!0}):(this._logService.debug("[RemoteInstanceReplicaService]",`command "${e}" not found. Skip sync mutation.`),!0)}};exports.WebWorkerRemoteInstanceService=E([m(0,h.Inject(h.Injector)),m(1,a.IUniverInstanceService),m(2,a.ICommandService),m(3,a.ILogService)],exports.WebWorkerRemoteInstanceService);function q(i){const n=i;return new class{call(e,t){const r=n[e];if(typeof r=="function"){let s=r.apply(n,[t]);return s instanceof Promise||(s=Promise.resolve(s)),s}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const r=n[e];if(typeof r=="function"){const s=r.apply(n,t);return _.isObservable(s)?s:_.of(s)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function M(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return 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(r,s){return t._disposed?Promise.reject():t._remoteCall(e,r,s)},subscribe(r,s){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,r,s)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(p.filter(e=>e),p.take(1)))}async _remoteCall(e,t,r){await this._whenReady();const s=++this._lastRequestCounter,u={seq:s,type:100,channelName:e,method:t,args:r},o=this;return new Promise((I,d)=>{const W={handle(g){switch(g.type){case 201:o._pendingRequests.delete(s),I(g.data);break;case 202:o._pendingRequests.delete(s),d(g.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(s,W),this._sendRequest(u)})}_remoteSubscribe(e,t,r){return new _.Observable(s=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:r},I={handle(d){switch(d.type){case 300:s.next(d.data);break;case 301:s.error(d.data);break;case 302:s.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,I),this._sendRequest(o)}),()=>{if(c===-1)return;const u={type:102,seq:c,channelName:e,method:t};this._sendRequest(u)}})}_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:r,args:s}=e,c=this._channels.get(t);let u;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);u=c.call(r,s)}catch(o){u=Promise.reject(o)}u.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:r}=e,s=this._channels.get(t);try{if(!s)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const u=s.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:r,type:300,data:o})},error:o=>{this._sendResponse({seq:r,type:301,data:o.message}),this._sendResponse({seq:r,type:302})},complete:()=>{this._sendResponse({seq:r,type:302})}});this._subscriptions.set(e.seq,u)}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 C=h.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 r=t>1?void 0:t?V(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&H(n,e,r),r},v=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,t,r,s,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);this._injector=e,this._commandService=t,this._univerInstanceService=r,this._rpcChannelService=s,this._remoteSyncService=c,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,q(this._remoteSyncService)),this._injector.add([y,{useFactory:()=>M(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(y)}_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:r,params:s}=e,c=(s==null?void 0:s.unitId)||"";r===a.CommandType.MUTATION&&(!c||this._syncingUnits.has(c))&&!(t!=null&&t.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=A([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncPrimaryController),v(0,h.Inject(h.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,C),v(4,S)],exports.DataSyncPrimaryController);var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,G=(i,n,e,t)=>{for(var r=t>1?void 0:t?z(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&F(n,e,r),r},R=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends a.Disposable{constructor(e,t,r,s){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=r,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,q(this._remoteInstanceService)),this._injector.add([S,{useFactory:()=>M(this._rpcChannelService.requestChannel(f))}]),this._remoteSyncService=this._injector.get(S)}_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),R(0,h.Inject(h.Injector)),R(1,y),R(2,a.ICommandService),R(3,C)],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,j=(i,n,e,t)=>{for(var r=t>1?void 0:t?X(n,e):n,s=i.length-1,c;s>=0;s--)(c=i[s])&&(r=(t?c(n,e,r):c(r))||r);return t&&r&&Q(n,e,r),r},T=(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),r=J(t);[[C,{useFactory:()=>new U(r)}],[exports.DataSyncPrimaryController],[S,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>n.add(c)),n.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=j([T(1,h.Inject(h.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],[C,{useFactory:()=>new U(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=j([T(1,h.Inject(h.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=D;exports.ChannelServer=O;exports.ChannelService=U;exports.IRPCChannelService=C;exports.IRemoteInstanceService=y;exports.IRemoteSyncService=S;exports.RemoteInstanceServiceName=b;exports.RemoteSyncServiceName=f;
|
package/lib/es/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var a = (s, t, e) => (
|
|
4
|
-
import {
|
|
5
|
-
import { createIdentifier as O, Inject as
|
|
6
|
-
import { takeUntil as
|
|
7
|
-
import { BehaviorSubject as
|
|
8
|
-
var
|
|
9
|
-
for (var r = n > 1 ? void 0 : n ?
|
|
1
|
+
var X = Object.defineProperty;
|
|
2
|
+
var Y = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
|
|
3
|
+
var a = (s, t, e) => (Y(s, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
import { ICommandService as C, UniverInstanceType as p, IUniverInstanceService as N, ILogService as Z, RxDisposable as M, OnLifecycle as W, LifecycleStages as L, CommandType as H, Disposable as K, Plugin as V } from "@univerjs/core";
|
|
5
|
+
import { createIdentifier as O, Inject as m, Injector as f } from "@wendellhu/redi";
|
|
6
|
+
import { takeUntil as y, filter as ee, take as te } from "rxjs/operators";
|
|
7
|
+
import { BehaviorSubject as ne, firstValueFrom as se, Observable as $, isObservable as re, of as ie, shareReplay as A } from "rxjs";
|
|
8
|
+
var ce = Object.defineProperty, oe = Object.getOwnPropertyDescriptor, F = (s, t, e, n) => {
|
|
9
|
+
for (var r = n > 1 ? void 0 : n ? oe(t, e) : t, i = s.length - 1, c; i >= 0; i--)
|
|
10
10
|
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r);
|
|
11
|
-
return n && r &&
|
|
12
|
-
},
|
|
13
|
-
const
|
|
14
|
-
let
|
|
11
|
+
return n && r && ce(t, e, r), r;
|
|
12
|
+
}, u = (s, t) => (e, n) => t(e, n, s);
|
|
13
|
+
const j = "rpc.remote-sync.service", b = O(j);
|
|
14
|
+
let U = class {
|
|
15
15
|
constructor(s) {
|
|
16
16
|
this._commandService = s;
|
|
17
17
|
}
|
|
@@ -22,13 +22,13 @@ let E = class {
|
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
],
|
|
28
|
-
const D = "univer.remote-instance-service",
|
|
25
|
+
U = F([
|
|
26
|
+
u(0, C)
|
|
27
|
+
], U);
|
|
28
|
+
const D = "univer.remote-instance-service", g = O(D);
|
|
29
29
|
let q = class {
|
|
30
|
-
constructor(s, t, e) {
|
|
31
|
-
this.
|
|
30
|
+
constructor(s, t, e, n) {
|
|
31
|
+
this._injector = s, this._univerInstanceService = t, this._commandService = e, this._logService = n;
|
|
32
32
|
}
|
|
33
33
|
whenReady() {
|
|
34
34
|
return Promise.resolve(!0);
|
|
@@ -40,8 +40,8 @@ let q = class {
|
|
|
40
40
|
const { type: t, snapshot: e } = s;
|
|
41
41
|
try {
|
|
42
42
|
switch (t) {
|
|
43
|
-
case
|
|
44
|
-
return
|
|
43
|
+
case p.UNIVER_SHEET:
|
|
44
|
+
return this._univerInstanceService.createUnit(p.UNIVER_SHEET, e), !0;
|
|
45
45
|
default:
|
|
46
46
|
throw new Error(
|
|
47
47
|
`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`
|
|
@@ -52,7 +52,7 @@ let q = class {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
async disposeInstance(s) {
|
|
55
|
-
return this._univerInstanceService.
|
|
55
|
+
return this._univerInstanceService.disposeUnit(s.unitID);
|
|
56
56
|
}
|
|
57
57
|
_applyMutation(s) {
|
|
58
58
|
const { id: t, params: e } = s;
|
|
@@ -62,12 +62,13 @@ let q = class {
|
|
|
62
62
|
}) : (this._logService.debug("[RemoteInstanceReplicaService]", `command "${t}" not found. Skip sync mutation.`), !0);
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
|
-
q =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
q = F([
|
|
66
|
+
u(0, m(f)),
|
|
67
|
+
u(1, N),
|
|
68
|
+
u(2, C),
|
|
69
|
+
u(3, Z)
|
|
69
70
|
], q);
|
|
70
|
-
function
|
|
71
|
+
function z(s) {
|
|
71
72
|
const t = s;
|
|
72
73
|
return new class {
|
|
73
74
|
call(e, n) {
|
|
@@ -82,33 +83,33 @@ function G(s) {
|
|
|
82
83
|
const r = t[e];
|
|
83
84
|
if (typeof r == "function") {
|
|
84
85
|
const i = r.apply(t, n);
|
|
85
|
-
return
|
|
86
|
+
return re(i) ? i : ie(i);
|
|
86
87
|
}
|
|
87
88
|
throw new Error(`[RPC]: observable method not found for ${e}!`);
|
|
88
89
|
}
|
|
89
90
|
}();
|
|
90
91
|
}
|
|
91
|
-
function
|
|
92
|
+
function G(s) {
|
|
92
93
|
return new Proxy({}, {
|
|
93
94
|
get(t, e) {
|
|
94
95
|
if (e !== "dispose")
|
|
95
96
|
return function(...n) {
|
|
96
|
-
return
|
|
97
|
+
return ae(e) ? s.subscribe(e, n[0]) : s.call(e, n[0]);
|
|
97
98
|
};
|
|
98
99
|
}
|
|
99
100
|
});
|
|
100
101
|
}
|
|
101
|
-
function
|
|
102
|
+
function ae(s) {
|
|
102
103
|
return s.endsWith("$");
|
|
103
104
|
}
|
|
104
|
-
class
|
|
105
|
+
class he extends M {
|
|
105
106
|
constructor(e) {
|
|
106
107
|
super();
|
|
107
|
-
a(this, "_initialized", new
|
|
108
|
+
a(this, "_initialized", new ne(!1));
|
|
108
109
|
a(this, "_lastRequestCounter", 0);
|
|
109
110
|
a(this, "_pendingRequests", /* @__PURE__ */ new Map());
|
|
110
111
|
a(this, "_pendingSubscriptions", /* @__PURE__ */ new Map());
|
|
111
|
-
this._protocol = e, this._protocol.onMessage.pipe(
|
|
112
|
+
this._protocol = e, this._protocol.onMessage.pipe(y(this.dispose$)).subscribe((n) => this._onMessage(n));
|
|
112
113
|
}
|
|
113
114
|
getChannel(e) {
|
|
114
115
|
const n = this;
|
|
@@ -124,22 +125,22 @@ class le extends M {
|
|
|
124
125
|
};
|
|
125
126
|
}
|
|
126
127
|
_whenReady() {
|
|
127
|
-
return
|
|
128
|
+
return se(
|
|
128
129
|
this._initialized.pipe(
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
ee((e) => e),
|
|
131
|
+
te(1)
|
|
131
132
|
)
|
|
132
133
|
);
|
|
133
134
|
}
|
|
134
135
|
async _remoteCall(e, n, r) {
|
|
135
136
|
await this._whenReady();
|
|
136
137
|
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: n, args: r }, o = this;
|
|
137
|
-
return new Promise((
|
|
138
|
-
const
|
|
138
|
+
return new Promise((I, l) => {
|
|
139
|
+
const Q = {
|
|
139
140
|
handle(w) {
|
|
140
141
|
switch (w.type) {
|
|
141
142
|
case 201:
|
|
142
|
-
o._pendingRequests.delete(i),
|
|
143
|
+
o._pendingRequests.delete(i), I(w.data);
|
|
143
144
|
break;
|
|
144
145
|
case 202:
|
|
145
146
|
o._pendingRequests.delete(i), l(w.data);
|
|
@@ -149,7 +150,7 @@ class le extends M {
|
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
};
|
|
152
|
-
this._pendingRequests.set(i,
|
|
153
|
+
this._pendingRequests.set(i, Q), this._sendRequest(h);
|
|
153
154
|
});
|
|
154
155
|
}
|
|
155
156
|
_remoteSubscribe(e, n, r) {
|
|
@@ -157,7 +158,7 @@ class le extends M {
|
|
|
157
158
|
let c = -1;
|
|
158
159
|
return this._whenReady().then(() => {
|
|
159
160
|
c = ++this._lastRequestCounter;
|
|
160
|
-
const o = { seq: c, type: 101, channelName: e, method: n, args: r },
|
|
161
|
+
const o = { seq: c, type: 101, channelName: e, method: n, args: r }, I = {
|
|
161
162
|
handle(l) {
|
|
162
163
|
switch (l.type) {
|
|
163
164
|
case 300:
|
|
@@ -174,7 +175,7 @@ class le extends M {
|
|
|
174
175
|
}
|
|
175
176
|
}
|
|
176
177
|
};
|
|
177
|
-
this._pendingRequests.set(c,
|
|
178
|
+
this._pendingRequests.set(c, I), this._sendRequest(o);
|
|
178
179
|
}), () => {
|
|
179
180
|
if (c === -1)
|
|
180
181
|
return;
|
|
@@ -207,12 +208,12 @@ class le extends M {
|
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
|
-
class
|
|
211
|
+
class le extends M {
|
|
211
212
|
constructor(e) {
|
|
212
213
|
super();
|
|
213
214
|
a(this, "_channels", /* @__PURE__ */ new Map());
|
|
214
215
|
a(this, "_subscriptions", /* @__PURE__ */ new Map());
|
|
215
|
-
this._protocol = e, this._protocol.onMessage.pipe(
|
|
216
|
+
this._protocol = e, this._protocol.onMessage.pipe(y(this.dispose$)).subscribe((n) => this._onRequest(n)), this._sendResponse({
|
|
216
217
|
seq: -1,
|
|
217
218
|
type: 0
|
|
218
219
|
/* INITIALIZE */
|
|
@@ -287,12 +288,12 @@ class _e extends M {
|
|
|
287
288
|
this._protocol.send(e);
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
|
-
const
|
|
291
|
-
class
|
|
291
|
+
const R = O("IRPCChannelService");
|
|
292
|
+
class k {
|
|
292
293
|
constructor(t) {
|
|
293
294
|
a(this, "_client");
|
|
294
295
|
a(this, "_server");
|
|
295
|
-
this._client = new
|
|
296
|
+
this._client = new he(t), this._server = new le(t);
|
|
296
297
|
}
|
|
297
298
|
requestChannel(t) {
|
|
298
299
|
return this._client.getChannel(t);
|
|
@@ -301,12 +302,12 @@ class B {
|
|
|
301
302
|
this._server.registerChannel(t, e);
|
|
302
303
|
}
|
|
303
304
|
}
|
|
304
|
-
var
|
|
305
|
-
for (var r = n > 1 ? void 0 : n ?
|
|
305
|
+
var _e = Object.defineProperty, ue = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => {
|
|
306
|
+
for (var r = n > 1 ? void 0 : n ? ue(t, e) : t, i = s.length - 1, c; i >= 0; i--)
|
|
306
307
|
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r);
|
|
307
|
-
return n && r &&
|
|
308
|
+
return n && r && _e(t, e, r), r;
|
|
308
309
|
}, _ = (s, t) => (e, n) => t(e, n, s);
|
|
309
|
-
let
|
|
310
|
+
let d = class extends M {
|
|
310
311
|
constructor(t, e, n, r, i) {
|
|
311
312
|
super();
|
|
312
313
|
a(this, "_remoteInstanceService");
|
|
@@ -314,21 +315,21 @@ let u = class extends M {
|
|
|
314
315
|
this._injector = t, this._commandService = e, this._univerInstanceService = n, this._rpcChannelService = r, this._remoteSyncService = i, this._initRPCChannels(), this._init();
|
|
315
316
|
}
|
|
316
317
|
_initRPCChannels() {
|
|
317
|
-
this._rpcChannelService.registerChannel(
|
|
318
|
-
|
|
318
|
+
this._rpcChannelService.registerChannel(j, z(this._remoteSyncService)), this._injector.add([
|
|
319
|
+
g,
|
|
319
320
|
{
|
|
320
|
-
useFactory: () =>
|
|
321
|
+
useFactory: () => G(this._rpcChannelService.requestChannel(D))
|
|
321
322
|
}
|
|
322
|
-
]), this._remoteInstanceService = this._injector.get(
|
|
323
|
+
]), this._remoteInstanceService = this._injector.get(g);
|
|
323
324
|
}
|
|
324
325
|
_init() {
|
|
325
|
-
this._univerInstanceService.
|
|
326
|
+
this._univerInstanceService.getTypeOfUnitAdded$(p.UNIVER_SHEET).pipe(y(this.dispose$)).subscribe((t) => {
|
|
326
327
|
this._syncingUnits.add(t.getUnitId()), this._remoteInstanceService.createInstance({
|
|
327
328
|
unitID: t.getUnitId(),
|
|
328
|
-
type:
|
|
329
|
+
type: p.UNIVER_SHEET,
|
|
329
330
|
snapshot: t.getSnapshot()
|
|
330
331
|
});
|
|
331
|
-
}), this._univerInstanceService.
|
|
332
|
+
}), this._univerInstanceService.getTypeOfUnitDisposed$(p.UNIVER_SHEET).pipe(y(this.dispose$)).subscribe((t) => {
|
|
332
333
|
this._syncingUnits.delete(t.getUnitId()), this._remoteInstanceService.disposeInstance({
|
|
333
334
|
unitID: t.getUnitId()
|
|
334
335
|
});
|
|
@@ -337,58 +338,58 @@ let u = class extends M {
|
|
|
337
338
|
this._commandService.onCommandExecuted((t, e) => {
|
|
338
339
|
const { type: n, params: r } = t, i = (r == null ? void 0 : r.unitId) || "";
|
|
339
340
|
// only sync mutations to the worker thread
|
|
340
|
-
n ===
|
|
341
|
+
n === H.MUTATION && (!i || this._syncingUnits.has(i)) && // do not sync mutations from the web worker back to the web worker
|
|
341
342
|
!(e != null && e.fromSync) && this._remoteInstanceService.syncMutation({ mutationInfo: t });
|
|
342
343
|
})
|
|
343
344
|
);
|
|
344
345
|
}
|
|
345
346
|
};
|
|
346
|
-
|
|
347
|
-
L
|
|
348
|
-
_(0,
|
|
349
|
-
_(1,
|
|
350
|
-
_(2,
|
|
351
|
-
_(3,
|
|
352
|
-
_(4,
|
|
353
|
-
],
|
|
354
|
-
var
|
|
355
|
-
for (var r = n > 1 ? void 0 : n ?
|
|
347
|
+
d = pe([
|
|
348
|
+
W(L.Starting, d),
|
|
349
|
+
_(0, m(f)),
|
|
350
|
+
_(1, C),
|
|
351
|
+
_(2, N),
|
|
352
|
+
_(3, R),
|
|
353
|
+
_(4, b)
|
|
354
|
+
], d);
|
|
355
|
+
var de = Object.defineProperty, ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
|
|
356
|
+
for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
|
|
356
357
|
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r);
|
|
357
|
-
return n && r &&
|
|
358
|
-
},
|
|
359
|
-
let
|
|
358
|
+
return n && r && de(t, e, r), r;
|
|
359
|
+
}, S = (s, t) => (e, n) => t(e, n, s);
|
|
360
|
+
let v = class extends K {
|
|
360
361
|
constructor(t, e, n, r) {
|
|
361
362
|
super();
|
|
362
363
|
a(this, "_remoteSyncService");
|
|
363
364
|
this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init();
|
|
364
365
|
}
|
|
365
366
|
_initRPCChannels() {
|
|
366
|
-
this._rpcChannelService.registerChannel(D,
|
|
367
|
-
|
|
367
|
+
this._rpcChannelService.registerChannel(D, z(this._remoteInstanceService)), this._injector.add([
|
|
368
|
+
b,
|
|
368
369
|
{
|
|
369
|
-
useFactory: () =>
|
|
370
|
+
useFactory: () => G(this._rpcChannelService.requestChannel(j))
|
|
370
371
|
}
|
|
371
|
-
]), this._remoteSyncService = this._injector.get(
|
|
372
|
+
]), this._remoteSyncService = this._injector.get(b);
|
|
372
373
|
}
|
|
373
374
|
_init() {
|
|
374
375
|
this.disposeWithMe(
|
|
375
376
|
// Mutations executed on the main thread should be synced to the worker thread.
|
|
376
377
|
this._commandService.onCommandExecuted((t, e) => {
|
|
377
|
-
t.type ===
|
|
378
|
+
t.type === H.MUTATION && !(e != null && e.fromSync) && this._remoteSyncService.syncMutation({
|
|
378
379
|
mutationInfo: t
|
|
379
380
|
});
|
|
380
381
|
})
|
|
381
382
|
);
|
|
382
383
|
}
|
|
383
384
|
};
|
|
384
|
-
|
|
385
|
-
L
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
],
|
|
391
|
-
function
|
|
385
|
+
v = me([
|
|
386
|
+
W(L.Starting, v),
|
|
387
|
+
S(0, m(f)),
|
|
388
|
+
S(1, g),
|
|
389
|
+
S(2, C),
|
|
390
|
+
S(3, R)
|
|
391
|
+
], v);
|
|
392
|
+
function fe() {
|
|
392
393
|
return {
|
|
393
394
|
send(s) {
|
|
394
395
|
postMessage(s);
|
|
@@ -398,7 +399,7 @@ function ye() {
|
|
|
398
399
|
s.next(e.data);
|
|
399
400
|
};
|
|
400
401
|
return addEventListener("message", t), () => removeEventListener("message", t);
|
|
401
|
-
}).pipe(
|
|
402
|
+
}).pipe(A(1))
|
|
402
403
|
};
|
|
403
404
|
}
|
|
404
405
|
function Se(s) {
|
|
@@ -411,69 +412,69 @@ function Se(s) {
|
|
|
411
412
|
t.next(n.data);
|
|
412
413
|
};
|
|
413
414
|
return s.addEventListener("message", e), () => s.removeEventListener("message", e);
|
|
414
|
-
}).pipe(
|
|
415
|
+
}).pipe(A(1))
|
|
415
416
|
};
|
|
416
417
|
}
|
|
417
|
-
var
|
|
418
|
-
for (var r = n > 1 ? void 0 : n ?
|
|
418
|
+
var ye = Object.defineProperty, be = Object.getOwnPropertyDescriptor, B = (s, t, e, n) => {
|
|
419
|
+
for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, c; i >= 0; i--)
|
|
419
420
|
(c = s[i]) && (r = (n ? c(t, e, r) : c(r)) || r);
|
|
420
|
-
return n && r &&
|
|
421
|
-
},
|
|
422
|
-
let
|
|
421
|
+
return n && r && ye(t, e, r), r;
|
|
422
|
+
}, J = (s, t) => (e, n) => t(e, n, s), P;
|
|
423
|
+
let x = (P = class extends V {
|
|
423
424
|
constructor(s, t) {
|
|
424
|
-
super(
|
|
425
|
+
super(), this._config = s, this._injector = t;
|
|
425
426
|
}
|
|
426
427
|
async onStarting(s) {
|
|
427
428
|
const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t), n = Se(e);
|
|
428
429
|
[
|
|
429
430
|
[
|
|
430
|
-
|
|
431
|
+
R,
|
|
431
432
|
{
|
|
432
|
-
useFactory: () => new
|
|
433
|
+
useFactory: () => new k(n)
|
|
433
434
|
}
|
|
434
435
|
],
|
|
435
|
-
[
|
|
436
|
-
[
|
|
437
|
-
].forEach((i) => s.add(i)), s.get(
|
|
438
|
-
}
|
|
439
|
-
}, a(
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
],
|
|
443
|
-
var
|
|
444
|
-
let
|
|
436
|
+
[d],
|
|
437
|
+
[b, { useClass: U }]
|
|
438
|
+
].forEach((i) => s.add(i)), s.get(d);
|
|
439
|
+
}
|
|
440
|
+
}, a(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P);
|
|
441
|
+
x = B([
|
|
442
|
+
J(1, m(f))
|
|
443
|
+
], x);
|
|
444
|
+
var E;
|
|
445
|
+
let T = (E = class extends V {
|
|
445
446
|
constructor(s, t) {
|
|
446
|
-
super(
|
|
447
|
+
super(), this._config = s, this._injector = t;
|
|
447
448
|
}
|
|
448
449
|
onStarting(s) {
|
|
449
450
|
[
|
|
450
|
-
[
|
|
451
|
+
[v],
|
|
451
452
|
[
|
|
452
|
-
|
|
453
|
+
R,
|
|
453
454
|
{
|
|
454
|
-
useFactory: () => new
|
|
455
|
+
useFactory: () => new k(fe())
|
|
455
456
|
}
|
|
456
457
|
],
|
|
457
|
-
[
|
|
458
|
-
].forEach((t) => s.add(t)), s.get(
|
|
458
|
+
[g, { useClass: q }]
|
|
459
|
+
].forEach((t) => s.add(t)), s.get(v);
|
|
459
460
|
}
|
|
460
|
-
}, a(
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
],
|
|
461
|
+
}, a(E, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), E);
|
|
462
|
+
T = B([
|
|
463
|
+
J(1, m(f))
|
|
464
|
+
], T);
|
|
464
465
|
export {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
466
|
+
he as ChannelClient,
|
|
467
|
+
le as ChannelServer,
|
|
468
|
+
k as ChannelService,
|
|
469
|
+
d as DataSyncPrimaryController,
|
|
470
|
+
v as DataSyncReplicaController,
|
|
471
|
+
R as IRPCChannelService,
|
|
472
|
+
g as IRemoteInstanceService,
|
|
473
|
+
b as IRemoteSyncService,
|
|
473
474
|
D as RemoteInstanceServiceName,
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
475
|
+
U as RemoteSyncPrimaryService,
|
|
476
|
+
j as RemoteSyncServiceName,
|
|
477
|
+
x as UniverRPCMainThreadPlugin,
|
|
478
|
+
T as UniverRPCWorkerThreadPlugin,
|
|
478
479
|
q as WebWorkerRemoteInstanceService
|
|
479
480
|
};
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Injector } from '@wendellhu/redi';
|
|
2
|
-
import { Plugin
|
|
2
|
+
import { Plugin } from '@univerjs/core';
|
|
3
3
|
|
|
4
4
|
export interface IUniverRPCMainThreadConfig {
|
|
5
5
|
workerURL: string | URL | Worker;
|
|
@@ -11,7 +11,7 @@ export interface IUniverRPCMainThreadConfig {
|
|
|
11
11
|
export declare class UniverRPCMainThreadPlugin extends Plugin {
|
|
12
12
|
private readonly _config;
|
|
13
13
|
protected readonly _injector: Injector;
|
|
14
|
-
static
|
|
14
|
+
static pluginName: string;
|
|
15
15
|
constructor(_config: IUniverRPCMainThreadConfig, _injector: Injector);
|
|
16
16
|
onStarting(injector: Injector): Promise<void>;
|
|
17
17
|
}
|
|
@@ -23,7 +23,7 @@ export interface IUniverRPCWorkerThreadPluginConfig {
|
|
|
23
23
|
export declare class UniverRPCWorkerThreadPlugin extends Plugin {
|
|
24
24
|
private readonly _config;
|
|
25
25
|
protected readonly _injector: Injector;
|
|
26
|
-
static
|
|
26
|
+
static pluginName: string;
|
|
27
27
|
constructor(_config: IUniverRPCWorkerThreadPluginConfig, _injector: Injector);
|
|
28
28
|
onStarting(injector: Injector): void;
|
|
29
29
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { Injector } from '@wendellhu/redi';
|
|
1
2
|
import { IExecutionOptions, IMutationInfo, IWorkbookData, ICommandService, ILogService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
|
|
2
3
|
|
|
3
4
|
export interface IRemoteSyncMutationOptions extends IExecutionOptions {
|
|
4
5
|
/** If this mutation is executed after it was sent from the peer univer instance (e.g. in a web worker). */
|
|
5
6
|
fromSync?: boolean;
|
|
6
7
|
}
|
|
7
|
-
export declare const RemoteSyncServiceName = "
|
|
8
|
+
export declare const RemoteSyncServiceName = "rpc.remote-sync.service";
|
|
8
9
|
/**
|
|
9
10
|
* This service is provided by the primary Univer.
|
|
10
11
|
*
|
|
@@ -47,10 +48,11 @@ export interface IRemoteInstanceService {
|
|
|
47
48
|
}): Promise<boolean>;
|
|
48
49
|
}
|
|
49
50
|
export declare class WebWorkerRemoteInstanceService implements IRemoteInstanceService {
|
|
51
|
+
private readonly _injector;
|
|
50
52
|
protected readonly _univerInstanceService: IUniverInstanceService;
|
|
51
53
|
protected readonly _commandService: ICommandService;
|
|
52
54
|
protected readonly _logService: ILogService;
|
|
53
|
-
constructor(_univerInstanceService: IUniverInstanceService, _commandService: ICommandService, _logService: ILogService);
|
|
55
|
+
constructor(_injector: Injector, _univerInstanceService: IUniverInstanceService, _commandService: ICommandService, _logService: ILogService);
|
|
54
56
|
whenReady(): Promise<true>;
|
|
55
57
|
syncMutation(params: {
|
|
56
58
|
mutationInfo: IMutationInfo;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(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"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i.UniverRpc={},i.UniverCore,i["@wendellhu/redi"],i.rxjs.operators,i.rxjs))})(this,function(i,o,l,v,d){"use strict";var Y=Object.defineProperty;var Z=(i,o,l)=>o in i?Y(i,o,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[o]=l;var u=(i,o,l)=>(Z(i,typeof o!="symbol"?o+"":o,l),l);var P,w;var $=Object.defineProperty,L=Object.getOwnPropertyDescriptor,D=(a,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,r=a.length-1,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&$(t,e,s),s},C=(a,t)=>(e,n)=>t(e,n,a);const R="univer.remote-sync-service",m=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,o.ICommandService)],i.RemoteSyncPrimaryService);const b="univer.remote-instance-service",S=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 o.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(n);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.disposeDocument(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.hasCommand(e)?this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0}):(this._logService.debug("[RemoteInstanceReplicaService]",`command "${e}" not found. Skip sync mutation.`),!0)}},i.WebWorkerRemoteInstanceService=D([C(0,o.IUniverInstanceService),C(1,o.ICommandService),C(2,o.ILogService)],i.WebWorkerRemoteInstanceService);function M(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 E(a){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return k(e)?a.subscribe(e,n[0]):a.call(e,n[0])}}})}function k(a){return a.endsWith("$")}class O extends o.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(q){switch(q.type){case 201:h._pendingRequests.delete(r),U(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 c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const h={seq:c,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(c,U),this._sendRequest(h)}),()=>{if(c===-1)return;const _={type:102,seq:c,channelName:e,method:n};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class j extends o.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,c=this._channels.get(n);let _;try{if(!c)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=c.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(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const p=l.createIdentifier("IRPCChannelService");class I{constructor(t){u(this,"_client");u(this,"_server");this._client=new O(t),this._server=new j(t)}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var N=Object.defineProperty,A=Object.getOwnPropertyDescriptor,H=(a,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,r=a.length-1,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&N(t,e,s),s},y=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncPrimaryController=class extends o.RxDisposable{constructor(e,n,s,r,c){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,M(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>E(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.sheetAdded$.pipe(v.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:o.UniverInstanceType.SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.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}=e,c=(r==null?void 0:r.unitId)||"";s===o.CommandType.MUTATION&&(!c||this._syncingUnits.has(c))&&!(n!=null&&n.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},i.DataSyncPrimaryController=H([o.OnLifecycle(o.LifecycleStages.Starting,i.DataSyncPrimaryController),y(0,l.Inject(l.Injector)),y(1,o.ICommandService),y(2,o.IUniverInstanceService),y(3,p),y(4,m)],i.DataSyncPrimaryController);var F=Object.defineProperty,z=Object.getOwnPropertyDescriptor,V=(a,t,e,n)=>{for(var s=n>1?void 0:n?z(t,e):t,r=a.length-1,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&F(t,e,s),s},g=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncReplicaController=class extends o.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,M(this._remoteInstanceService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(m)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===o.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},i.DataSyncReplicaController=V([o.OnLifecycle(o.LifecycleStages.Starting,i.DataSyncReplicaController),g(0,l.Inject(l.Injector)),g(1,S),g(2,o.ICommandService),g(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,W=(a,t,e,n)=>{for(var s=n>1?void 0:n?Q(t,e):t,r=a.length-1,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&J(t,e,s),s},T=(a,t)=>(e,n)=>t(e,n,a);i.UniverRPCMainThreadPlugin=(P=class extends o.Plugin{constructor(t,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),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 I(s)}],[i.DataSyncPrimaryController],[m,{useClass:i.RemoteSyncPrimaryService}]].forEach(c=>t.add(c)),t.get(i.DataSyncPrimaryController)}},u(P,"type",o.PluginType.Univer),P),i.UniverRPCMainThreadPlugin=W([T(1,l.Inject(l.Injector))],i.UniverRPCMainThreadPlugin),i.UniverRPCWorkerThreadPlugin=(w=class extends o.Plugin{constructor(t,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=t,this._injector=e}onStarting(t){[[i.DataSyncReplicaController],[p,{useFactory:()=>new I(G())}],[S,{useClass:i.WebWorkerRemoteInstanceService}]].forEach(e=>t.add(e)),t.get(i.DataSyncReplicaController)}},u(w,"type",o.PluginType.Univer),w),i.UniverRPCWorkerThreadPlugin=W([T(1,l.Inject(l.Injector))],i.UniverRPCWorkerThreadPlugin),i.ChannelClient=O,i.ChannelServer=j,i.ChannelService=I,i.IRPCChannelService=p,i.IRemoteInstanceService=S,i.IRemoteSyncService=m,i.RemoteInstanceServiceName=b,i.RemoteSyncServiceName=R,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(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"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i.UniverRpc={},i.UniverCore,i["@wendellhu/redi"],i.rxjs.operators,i.rxjs))})(this,function(i,o,l,v,d){"use strict";var Y=Object.defineProperty;var Z=(i,o,l)=>o in i?Y(i,o,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[o]=l;var u=(i,o,l)=>(Z(i,typeof o!="symbol"?o+"":o,l),l);var P,w;var $=Object.defineProperty,N=Object.getOwnPropertyDescriptor,q=(a,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,r=a.length-1,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&$(t,e,s),s},m=(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=q([m(0,o.ICommandService)],i.RemoteSyncPrimaryService);const b="univer.remote-instance-service",S=l.createIdentifier(b);i.WebWorkerRemoteInstanceService=class{constructor(t,e,n,s){this._injector=t,this._univerInstanceService=e,this._commandService=n,this._logService=s}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case o.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(o.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.hasCommand(e)?this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0}):(this._logService.debug("[RemoteInstanceReplicaService]",`command "${e}" not found. Skip sync mutation.`),!0)}},i.WebWorkerRemoteInstanceService=q([m(0,l.Inject(l.Injector)),m(1,o.IUniverInstanceService),m(2,o.ICommandService),m(3,o.ILogService)],i.WebWorkerRemoteInstanceService);function M(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 o.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,C)=>{const X={handle(E){switch(E.type){case 201:h._pendingRequests.delete(r),U(E.data);break;case 202:h._pendingRequests.delete(r),C(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 c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const h={seq:c,type:101,channelName:e,method:n,args:s},U={handle(C){switch(C.type){case 300:r.next(C.data);break;case 301:r.error(C.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,U),this._sendRequest(h)}),()=>{if(c===-1)return;const _={type:102,seq:c,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 o.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,c=this._channels.get(n);let _;try{if(!c)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=c.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(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const y=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,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&k(t,e,s),s},f=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncPrimaryController=class extends o.RxDisposable{constructor(e,n,s,r,c){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,M(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>D(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(o.UniverInstanceType.UNIVER_SHEET).pipe(v.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:o.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(o.UniverInstanceType.UNIVER_SHEET).pipe(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}=e,c=(r==null?void 0:r.unitId)||"";s===o.CommandType.MUTATION&&(!c||this._syncingUnits.has(c))&&!(n!=null&&n.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},i.DataSyncPrimaryController=V([o.OnLifecycle(o.LifecycleStages.Starting,i.DataSyncPrimaryController),f(0,l.Inject(l.Injector)),f(1,o.ICommandService),f(2,o.IUniverInstanceService),f(3,y),f(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,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&A(t,e,s),s},I=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncReplicaController=class extends o.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,M(this._remoteInstanceService)),this._injector.add([p,{useFactory:()=>D(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(p)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===o.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},i.DataSyncReplicaController=z([o.OnLifecycle(o.LifecycleStages.Starting,i.DataSyncReplicaController),I(0,l.Inject(l.Injector)),I(1,S),I(2,o.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,c;r>=0;r--)(c=a[r])&&(s=(n?c(t,e,s):c(s))||s);return n&&s&&J(t,e,s),s},W=(a,t)=>(e,n)=>t(e,n,a);i.UniverRPCMainThreadPlugin=(P=class extends o.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 g(s)}],[i.DataSyncPrimaryController],[p,{useClass:i.RemoteSyncPrimaryService}]].forEach(c=>t.add(c)),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 o.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}onStarting(t){[[i.DataSyncReplicaController],[y,{useFactory:()=>new g(G())}],[S,{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=y,i.IRemoteInstanceService=S,i.IRemoteSyncService=p,i.RemoteInstanceServiceName=b,i.RemoteSyncServiceName=R,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/rpc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "DreamNum <developer@univer.ai>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -49,18 +49,18 @@
|
|
|
49
49
|
"npm": ">=8.0.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@wendellhu/redi": "0.13.
|
|
52
|
+
"@wendellhu/redi": "^0.13.3",
|
|
53
53
|
"rxjs": ">=7.0.0",
|
|
54
|
-
"@univerjs/core": "0.1.
|
|
54
|
+
"@univerjs/core": "0.1.8"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@wendellhu/redi": "^0.13.
|
|
57
|
+
"@wendellhu/redi": "^0.13.3",
|
|
58
58
|
"rxjs": "^7.8.1",
|
|
59
59
|
"typescript": "^5.4.5",
|
|
60
|
-
"vite": "^5.2.
|
|
61
|
-
"vitest": "^1.
|
|
62
|
-
"@univerjs/
|
|
63
|
-
"@univerjs/
|
|
60
|
+
"vite": "^5.2.10",
|
|
61
|
+
"vitest": "^1.5.0",
|
|
62
|
+
"@univerjs/core": "0.1.8",
|
|
63
|
+
"@univerjs/shared": "0.1.8"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"lint:types": "tsc --noEmit",
|