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