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