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