@univerjs/rpc 0.2.2 → 0.2.4-alpha.0
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/README.md +11 -2
- package/lib/cjs/index.js +1 -1
- package/lib/es/index.js +157 -141
- package/lib/types/controllers/data-sync/data-sync-primary.controller.d.ts +8 -2
- package/lib/types/controllers/data-sync/data-sync-replica.controller.d.ts +1 -2
- package/lib/types/index.d.ts +1 -1
- package/lib/types/plugin.d.ts +1 -2
- package/lib/types/services/remote-instance/remote-instance.service.d.ts +2 -2
- package/lib/types/services/rpc/channel.service.d.ts +4 -2
- package/lib/types/services/rpc/rpc.service.d.ts +2 -1
- package/lib/umd/index.js +1 -1
- package/package.json +6 -8
package/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
# @univerjs/rpc
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
## Package Overview
|
|
4
|
+
|
|
5
|
+
| Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales |
|
|
6
|
+
| --- | --- | --- | --- | --- | :---: | :---: |
|
|
7
|
+
| `@univerjs/rpc` | `UniverRpc` | [![][npm-version-shield]][npm-version-link] | ![][npm-license-shield] | ![][npm-downloads-shield] | ❌ | ❌ |
|
|
5
8
|
|
|
6
9
|
## Introduction
|
|
7
10
|
|
|
@@ -20,3 +23,9 @@ npm install @univerjs/rpc
|
|
|
20
23
|
# Using pnpm
|
|
21
24
|
pnpm add @univerjs/rpc
|
|
22
25
|
```
|
|
26
|
+
|
|
27
|
+
<!-- Links -->
|
|
28
|
+
[npm-version-shield]: https://img.shields.io/npm/v/@univerjs/rpc?style=flat-square
|
|
29
|
+
[npm-version-link]: https://npmjs.com/package/@univerjs/rpc
|
|
30
|
+
[npm-license-shield]: https://img.shields.io/npm/l/@univerjs/rpc?style=flat-square
|
|
31
|
+
[npm-downloads-shield]: https://img.shields.io/npm/dm/@univerjs/rpc?style=flat-square
|
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);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;
|
|
1
|
+
"use strict";var W=Object.defineProperty;var $=(i,n,e)=>n in i?W(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var l=(i,n,e)=>$(i,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),_=require("rxjs/operators"),u=require("rxjs");var x=Object.defineProperty,N=Object.getOwnPropertyDescriptor,M=(i,n,e,t)=>{for(var s=t>1?void 0:t?N(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&&x(n,e,s),s},C=(i,n)=>(e,t)=>n(e,t,i);const R="rpc.remote-sync.service",v=a.createIdentifier(R);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=M([C(0,a.ICommandService)],exports.RemoteSyncPrimaryService);const b="univer.remote-instance-service",y=a.createIdentifier(b);exports.WebWorkerRemoteInstanceService=class{constructor(n,e,t){this._univerInstanceService=n,this._commandService=e,this._logService=t}whenReady(){return Promise.resolve(!0)}async syncMutation(n){return this._applyMutation(n.mutationInfo)}async createInstance(n){const{type:e,snapshot:t}=n;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,t),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(n){return this._univerInstanceService.disposeUnit(n.unitID)}_applyMutation(n){const{id:e,params:t}=n;return this._commandService.syncExecuteCommand(e,t,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=M([C(0,a.IUniverInstanceService),C(1,a.ICommandService),C(2,a.ILogService)],exports.WebWorkerRemoteInstanceService);function w(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 u.isObservable(r)?r:u.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function U(i){return new Proxy({},{get(n,e){if(e!=="dispose")return function(...t){return L(e)?i.subscribe(e,t[0]):i.call(e,t[0])}}})}function L(i){return i.endsWith("$")}class q extends a.RxDisposable{constructor(e){super();l(this,"_initialized",new u.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}dispose(){this._pendingRequests.clear()}getChannel(e){const t=this;return{call(s,r){return t._disposed?Promise.reject():t._remoteCall(e,s,r)},subscribe(s,r){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,r)}}}_whenReady(){return u.firstValueFrom(this._initialized.pipe(_.filter(e=>e),_.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((f,d)=>{const j={handle(g){switch(g.type){case 201:o._pendingRequests.delete(r),f(g.data);break;case 202:o._pendingRequests.delete(r),d(g.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,j),this._sendRequest(h)})}_remoteSubscribe(e,t,s){return new u.Observable(r=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},f={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,f),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 D extends a.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:r}=e,c=this._channels.get(t);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);h=c.call(s,r)}catch(o){h=Promise.reject(o)}h.then(o=>{this._sendResponse({seq:e.seq,type:201,data:o})}).catch(o=>{o instanceof Error?this._sendResponse({seq:e.seq,type:202,data:o.message}):this._sendResponse({seq:e.seq,type:202,data:String(o)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,r=this._channels.get(t);try{if(!r)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=a.createIdentifier("IRPCChannelService");class E{constructor(n){l(this,"_client");l(this,"_server");this._client=new q(n),this._server=new D(n)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var k=Object.defineProperty,H=Object.getOwnPropertyDescriptor,V=(i,n,e,t)=>{for(var s=t>1?void 0:t?H(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&&k(n,e,s),s},p=(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)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,w(this._remoteSyncService)),this._injector.add([y,{useFactory:()=>U(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(y)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.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(_.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=V([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncPrimaryController),p(0,a.Inject(a.Injector)),p(1,a.ICommandService),p(2,a.IUniverInstanceService),p(3,m),p(4,v)],exports.DataSyncPrimaryController);var A=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(i,n,e,t)=>{for(var s=t>1?void 0:t?F(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&&A(n,e,s),s},S=(i,n)=>(e,t)=>n(e,t,i);exports.DataSyncReplicaController=class extends a.Disposable{constructor(e,t,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=t,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,w(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>U(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{e.type===a.CommandType.MUTATION&&!(t!=null&&t.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=z([a.OnLifecycle(a.LifecycleStages.Starting,exports.DataSyncReplicaController),S(0,a.Inject(a.Injector)),S(1,y),S(2,a.ICommandService),S(3,m)],exports.DataSyncReplicaController);function G(){return{send(i){postMessage(i)},onMessage:new u.Observable(i=>{const n=e=>{i.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(u.shareReplay(1))}}function B(i){return{send(n){i.postMessage(n)},onMessage:new u.Observable(n=>{const e=t=>{n.next(t.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(u.shareReplay(1))}}var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,O=(i,n,e,t)=>{for(var s=t>1?void 0:t?Q(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&&J(n,e,s),s},T=(i,n)=>(e,t)=>n(e,t,i),I;exports.UniverRPCMainThreadPlugin=(I=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=B(t);[[m,{useFactory:()=>new E(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>n.add(c)),n.get(exports.DataSyncPrimaryController)}},l(I,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),I);exports.UniverRPCMainThreadPlugin=O([T(1,a.Inject(a.Injector))],exports.UniverRPCMainThreadPlugin);var P;exports.UniverRPCWorkerThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super(),this._config=n,this._injector=e}onStarting(n){[[exports.DataSyncReplicaController],[m,{useFactory:()=>new E(G())}],[y,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(e=>n.add(e)),n.get(exports.DataSyncReplicaController)}},l(P,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),P);exports.UniverRPCWorkerThreadPlugin=O([T(1,a.Inject(a.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=q;exports.ChannelServer=D;exports.ChannelService=E;exports.IRPCChannelService=m;exports.IRemoteInstanceService=y;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=b;exports.RemoteSyncServiceName=R;exports.fromModule=w;exports.toModule=U;
|
package/lib/es/index.js
CHANGED
|
@@ -1,43 +1,42 @@
|
|
|
1
1
|
var X = Object.defineProperty;
|
|
2
|
-
var Y = (
|
|
3
|
-
var o = (
|
|
4
|
-
import { UniverInstanceType as u, ICommandService as b, IUniverInstanceService as N, ILogService as Z, RxDisposable as
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this._commandService = s;
|
|
2
|
+
var Y = (n, t, e) => t in n ? X(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var o = (n, t, e) => Y(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { createIdentifier as q, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as N, ILogService as Z, RxDisposable as O, OnLifecycle as W, LifecycleStages as L, Inject as g, Injector as C, toDisposable as K, CommandType as H, Disposable as ee, Plugin as V } from "@univerjs/core";
|
|
5
|
+
import { takeUntil as y, filter as te, take as se } from "rxjs/operators";
|
|
6
|
+
import { isObservable as ne, of as re, BehaviorSubject as ie, firstValueFrom as ce, Observable as $, shareReplay as A } from "rxjs";
|
|
7
|
+
var ae = Object.defineProperty, oe = Object.getOwnPropertyDescriptor, F = (n, t, e, s) => {
|
|
8
|
+
for (var r = s > 1 ? void 0 : s ? oe(t, e) : t, i = n.length - 1, c; i >= 0; i--)
|
|
9
|
+
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r);
|
|
10
|
+
return s && r && ae(t, e, r), r;
|
|
11
|
+
}, m = (n, t) => (e, s) => t(e, s, n);
|
|
12
|
+
const j = "rpc.remote-sync.service", f = q(j);
|
|
13
|
+
let U = class {
|
|
14
|
+
constructor(n) {
|
|
15
|
+
this._commandService = n;
|
|
17
16
|
}
|
|
18
|
-
async syncMutation(
|
|
19
|
-
return this._commandService.syncExecuteCommand(
|
|
17
|
+
async syncMutation(n) {
|
|
18
|
+
return this._commandService.syncExecuteCommand(n.mutationInfo.id, n.mutationInfo.params, {
|
|
20
19
|
onlyLocal: !0,
|
|
21
20
|
fromSync: !0
|
|
22
21
|
});
|
|
23
22
|
}
|
|
24
23
|
};
|
|
25
|
-
|
|
24
|
+
U = F([
|
|
26
25
|
m(0, b)
|
|
27
|
-
],
|
|
28
|
-
const
|
|
29
|
-
let
|
|
30
|
-
constructor(
|
|
31
|
-
this._univerInstanceService =
|
|
26
|
+
], U);
|
|
27
|
+
const D = "univer.remote-instance-service", S = q(D);
|
|
28
|
+
let M = class {
|
|
29
|
+
constructor(n, t, e) {
|
|
30
|
+
this._univerInstanceService = n, this._commandService = t, this._logService = e;
|
|
32
31
|
}
|
|
33
32
|
whenReady() {
|
|
34
33
|
return Promise.resolve(!0);
|
|
35
34
|
}
|
|
36
|
-
async syncMutation(
|
|
37
|
-
return this._applyMutation(
|
|
35
|
+
async syncMutation(n) {
|
|
36
|
+
return this._applyMutation(n.mutationInfo);
|
|
38
37
|
}
|
|
39
|
-
async createInstance(
|
|
40
|
-
const { type: t, snapshot: e } =
|
|
38
|
+
async createInstance(n) {
|
|
39
|
+
const { type: t, snapshot: e } = n;
|
|
41
40
|
try {
|
|
42
41
|
switch (t) {
|
|
43
42
|
case u.UNIVER_SHEET:
|
|
@@ -47,93 +46,95 @@ let U = class {
|
|
|
47
46
|
`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`
|
|
48
47
|
);
|
|
49
48
|
}
|
|
50
|
-
} catch (
|
|
51
|
-
throw
|
|
49
|
+
} catch (s) {
|
|
50
|
+
throw s instanceof Error ? s : new TypeError(`${s}`);
|
|
52
51
|
}
|
|
53
52
|
}
|
|
54
|
-
async disposeInstance(
|
|
55
|
-
return this._univerInstanceService.disposeUnit(
|
|
53
|
+
async disposeInstance(n) {
|
|
54
|
+
return this._univerInstanceService.disposeUnit(n.unitID);
|
|
56
55
|
}
|
|
57
|
-
_applyMutation(
|
|
58
|
-
const { id: t, params: e } =
|
|
56
|
+
_applyMutation(n) {
|
|
57
|
+
const { id: t, params: e } = n;
|
|
59
58
|
return this._commandService.syncExecuteCommand(t, e, {
|
|
60
59
|
onlyLocal: !0,
|
|
61
60
|
fromSync: !0
|
|
62
61
|
});
|
|
63
62
|
}
|
|
64
63
|
};
|
|
65
|
-
|
|
64
|
+
M = F([
|
|
66
65
|
m(0, N),
|
|
67
66
|
m(1, b),
|
|
68
67
|
m(2, Z)
|
|
69
|
-
],
|
|
70
|
-
function z(
|
|
71
|
-
const t =
|
|
68
|
+
], M);
|
|
69
|
+
function z(n) {
|
|
70
|
+
const t = n;
|
|
72
71
|
return new class {
|
|
73
|
-
call(e,
|
|
72
|
+
call(e, s) {
|
|
74
73
|
const r = t[e];
|
|
75
74
|
if (typeof r == "function") {
|
|
76
|
-
let i = r.apply(t, [
|
|
75
|
+
let i = r.apply(t, [s]);
|
|
77
76
|
return i instanceof Promise || (i = Promise.resolve(i)), i;
|
|
78
77
|
}
|
|
79
78
|
throw new Error(`[RPC]: method not found for ${e}!`);
|
|
80
79
|
}
|
|
81
|
-
subscribe(e,
|
|
80
|
+
subscribe(e, s) {
|
|
82
81
|
const r = t[e];
|
|
83
82
|
if (typeof r == "function") {
|
|
84
|
-
const i = r.apply(t,
|
|
85
|
-
return
|
|
83
|
+
const i = r.apply(t, s);
|
|
84
|
+
return ne(i) ? i : re(i);
|
|
86
85
|
}
|
|
87
86
|
throw new Error(`[RPC]: observable method not found for ${e}!`);
|
|
88
87
|
}
|
|
89
88
|
}();
|
|
90
89
|
}
|
|
91
|
-
function G(
|
|
90
|
+
function G(n) {
|
|
92
91
|
return new Proxy({}, {
|
|
93
92
|
get(t, e) {
|
|
94
93
|
if (e !== "dispose")
|
|
95
|
-
return function(...
|
|
96
|
-
return
|
|
94
|
+
return function(...s) {
|
|
95
|
+
return he(e) ? n.subscribe(e, s[0]) : n.call(e, s[0]);
|
|
97
96
|
};
|
|
98
97
|
}
|
|
99
98
|
});
|
|
100
99
|
}
|
|
101
|
-
function
|
|
102
|
-
return
|
|
100
|
+
function he(n) {
|
|
101
|
+
return n.endsWith("$");
|
|
103
102
|
}
|
|
104
|
-
class
|
|
103
|
+
class _e extends O {
|
|
105
104
|
constructor(e) {
|
|
106
105
|
super();
|
|
107
|
-
o(this, "_initialized", new
|
|
106
|
+
o(this, "_initialized", new ie(!1));
|
|
108
107
|
o(this, "_lastRequestCounter", 0);
|
|
109
108
|
o(this, "_pendingRequests", /* @__PURE__ */ new Map());
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
this._protocol = e, this._protocol.onMessage.pipe(y(this.dispose$)).subscribe((s) => this._onMessage(s));
|
|
110
|
+
}
|
|
111
|
+
dispose() {
|
|
112
|
+
this._pendingRequests.clear();
|
|
112
113
|
}
|
|
113
114
|
getChannel(e) {
|
|
114
|
-
const
|
|
115
|
+
const s = this;
|
|
115
116
|
return {
|
|
116
117
|
call(r, i) {
|
|
117
|
-
return
|
|
118
|
+
return s._disposed ? Promise.reject() : s._remoteCall(e, r, i);
|
|
118
119
|
},
|
|
119
120
|
subscribe(r, i) {
|
|
120
|
-
if (
|
|
121
|
+
if (s._disposed)
|
|
121
122
|
throw new Error("[ChannelClient]: client is disposed!");
|
|
122
|
-
return
|
|
123
|
+
return s._remoteSubscribe(e, r, i);
|
|
123
124
|
}
|
|
124
125
|
};
|
|
125
126
|
}
|
|
126
127
|
_whenReady() {
|
|
127
|
-
return
|
|
128
|
+
return ce(
|
|
128
129
|
this._initialized.pipe(
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
te((e) => e),
|
|
131
|
+
se(1)
|
|
131
132
|
)
|
|
132
133
|
);
|
|
133
134
|
}
|
|
134
|
-
async _remoteCall(e,
|
|
135
|
+
async _remoteCall(e, s, r) {
|
|
135
136
|
await this._whenReady();
|
|
136
|
-
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method:
|
|
137
|
+
const i = ++this._lastRequestCounter, h = { seq: i, type: 100, channelName: e, method: s, args: r }, a = this;
|
|
137
138
|
return new Promise((I, _) => {
|
|
138
139
|
const Q = {
|
|
139
140
|
handle(w) {
|
|
@@ -152,12 +153,12 @@ class he extends q {
|
|
|
152
153
|
this._pendingRequests.set(i, Q), this._sendRequest(h);
|
|
153
154
|
});
|
|
154
155
|
}
|
|
155
|
-
_remoteSubscribe(e,
|
|
156
|
+
_remoteSubscribe(e, s, r) {
|
|
156
157
|
return new $((i) => {
|
|
157
158
|
let c = -1;
|
|
158
159
|
return this._whenReady().then(() => {
|
|
159
160
|
c = ++this._lastRequestCounter;
|
|
160
|
-
const a = { seq: c, type: 101, channelName: e, method:
|
|
161
|
+
const a = { seq: c, type: 101, channelName: e, method: s, args: r }, I = {
|
|
161
162
|
handle(_) {
|
|
162
163
|
switch (_.type) {
|
|
163
164
|
case 300:
|
|
@@ -182,7 +183,7 @@ class he extends q {
|
|
|
182
183
|
type: 102,
|
|
183
184
|
seq: c,
|
|
184
185
|
channelName: e,
|
|
185
|
-
method:
|
|
186
|
+
method: s
|
|
186
187
|
};
|
|
187
188
|
this._sendRequest(h);
|
|
188
189
|
};
|
|
@@ -192,7 +193,7 @@ class he extends q {
|
|
|
192
193
|
this._protocol.send(e);
|
|
193
194
|
}
|
|
194
195
|
_onMessage(e) {
|
|
195
|
-
var
|
|
196
|
+
var s;
|
|
196
197
|
switch (e.type) {
|
|
197
198
|
case 0:
|
|
198
199
|
this._initialized.next(!0);
|
|
@@ -202,24 +203,27 @@ class he extends q {
|
|
|
202
203
|
case 300:
|
|
203
204
|
case 302:
|
|
204
205
|
case 301:
|
|
205
|
-
(
|
|
206
|
+
(s = this._pendingRequests.get(e.seq)) == null || s.handle(e);
|
|
206
207
|
break;
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
|
-
class
|
|
211
|
+
class le extends O {
|
|
211
212
|
constructor(e) {
|
|
212
213
|
super();
|
|
213
214
|
o(this, "_channels", /* @__PURE__ */ new Map());
|
|
214
215
|
o(this, "_subscriptions", /* @__PURE__ */ new Map());
|
|
215
|
-
this._protocol = e, this._protocol.onMessage.pipe(
|
|
216
|
+
this._protocol = e, this._protocol.onMessage.pipe(y(this.dispose$)).subscribe((s) => this._onRequest(s)), this._sendResponse({
|
|
216
217
|
seq: -1,
|
|
217
218
|
type: 0
|
|
218
219
|
/* INITIALIZE */
|
|
219
220
|
});
|
|
220
221
|
}
|
|
221
|
-
|
|
222
|
-
this.
|
|
222
|
+
dispose() {
|
|
223
|
+
super.dispose(), this._subscriptions.clear(), this._channels.clear();
|
|
224
|
+
}
|
|
225
|
+
registerChannel(e, s) {
|
|
226
|
+
this._channels.set(e, s);
|
|
223
227
|
}
|
|
224
228
|
_onRequest(e) {
|
|
225
229
|
switch (e.type) {
|
|
@@ -235,11 +239,11 @@ class _e extends q {
|
|
|
235
239
|
}
|
|
236
240
|
}
|
|
237
241
|
_onMethodCall(e) {
|
|
238
|
-
const { channelName:
|
|
242
|
+
const { channelName: s, method: r, args: i } = e, c = this._channels.get(s);
|
|
239
243
|
let h;
|
|
240
244
|
try {
|
|
241
245
|
if (!c)
|
|
242
|
-
throw new Error(`[ChannelServer]: Channel ${
|
|
246
|
+
throw new Error(`[ChannelServer]: Channel ${s} not found!`);
|
|
243
247
|
h = c.call(r, i);
|
|
244
248
|
} catch (a) {
|
|
245
249
|
h = Promise.reject(a);
|
|
@@ -251,10 +255,10 @@ class _e extends q {
|
|
|
251
255
|
});
|
|
252
256
|
}
|
|
253
257
|
_onSubscribe(e) {
|
|
254
|
-
const { channelName:
|
|
258
|
+
const { channelName: s, seq: r } = e, i = this._channels.get(s);
|
|
255
259
|
try {
|
|
256
260
|
if (!i)
|
|
257
|
-
throw new Error(`[ChannelServer]: Channel ${
|
|
261
|
+
throw new Error(`[ChannelServer]: Channel ${s} not found!`);
|
|
258
262
|
const h = i.subscribe(e.method, e.args).subscribe({
|
|
259
263
|
next: (a) => {
|
|
260
264
|
this._sendResponse({ seq: r, type: 300, data: a });
|
|
@@ -280,19 +284,22 @@ class _e extends q {
|
|
|
280
284
|
}
|
|
281
285
|
}
|
|
282
286
|
_onUnsubscribe(e) {
|
|
283
|
-
const
|
|
284
|
-
|
|
287
|
+
const s = this._subscriptions.get(e.seq);
|
|
288
|
+
s && (s.unsubscribe(), this._subscriptions.delete(e.seq));
|
|
285
289
|
}
|
|
286
290
|
_sendResponse(e) {
|
|
287
291
|
this._protocol.send(e);
|
|
288
292
|
}
|
|
289
293
|
}
|
|
290
|
-
const R =
|
|
294
|
+
const R = q("IRPCChannelService");
|
|
291
295
|
class B {
|
|
292
296
|
constructor(t) {
|
|
293
297
|
o(this, "_client");
|
|
294
298
|
o(this, "_server");
|
|
295
|
-
this._client = new
|
|
299
|
+
this._client = new _e(t), this._server = new le(t);
|
|
300
|
+
}
|
|
301
|
+
dispose() {
|
|
302
|
+
this._client.dispose(), this._server.dispose();
|
|
296
303
|
}
|
|
297
304
|
requestChannel(t) {
|
|
298
305
|
return this._client.getChannel(t);
|
|
@@ -301,72 +308,79 @@ class B {
|
|
|
301
308
|
this._server.registerChannel(t, e);
|
|
302
309
|
}
|
|
303
310
|
}
|
|
304
|
-
var
|
|
305
|
-
for (var r =
|
|
306
|
-
(c =
|
|
307
|
-
return
|
|
308
|
-
}, l = (
|
|
309
|
-
let p = class extends
|
|
310
|
-
constructor(t, e,
|
|
311
|
+
var ue = Object.defineProperty, pe = Object.getOwnPropertyDescriptor, de = (n, t, e, s) => {
|
|
312
|
+
for (var r = s > 1 ? void 0 : s ? pe(t, e) : t, i = n.length - 1, c; i >= 0; i--)
|
|
313
|
+
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r);
|
|
314
|
+
return s && r && ue(t, e, r), r;
|
|
315
|
+
}, l = (n, t) => (e, s) => t(e, s, n);
|
|
316
|
+
let p = class extends O {
|
|
317
|
+
constructor(t, e, s, r, i) {
|
|
311
318
|
super();
|
|
312
319
|
o(this, "_remoteInstanceService");
|
|
313
320
|
o(this, "_syncingUnits", /* @__PURE__ */ new Set());
|
|
314
321
|
o(this, "_syncingMutations", /* @__PURE__ */ new Set());
|
|
315
|
-
this._injector = t, this._commandService = e, this._univerInstanceService =
|
|
322
|
+
this._injector = t, this._commandService = e, this._univerInstanceService = s, this._rpcChannelService = r, this._remoteSyncService = i, this._initRPCChannels(), this._init();
|
|
316
323
|
}
|
|
317
324
|
registerSyncingMutations(t) {
|
|
318
325
|
this._syncingMutations.add(t.id);
|
|
319
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Only spreadsheets would be synced to the web worker in normal situations. If you would like to
|
|
329
|
+
* sync other types of documents, you should manually call this method with that document's id.
|
|
330
|
+
*/
|
|
331
|
+
syncUnit(t) {
|
|
332
|
+
return this._syncingUnits.add(t), K(() => this._syncingUnits.delete(t));
|
|
333
|
+
}
|
|
320
334
|
_initRPCChannels() {
|
|
321
335
|
this._rpcChannelService.registerChannel(j, z(this._remoteSyncService)), this._injector.add([
|
|
322
336
|
S,
|
|
323
|
-
{ useFactory: () => G(this._rpcChannelService.requestChannel(
|
|
337
|
+
{ useFactory: () => G(this._rpcChannelService.requestChannel(D)) }
|
|
324
338
|
]), this._remoteInstanceService = this._injector.get(S);
|
|
325
339
|
}
|
|
326
340
|
_init() {
|
|
327
|
-
this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(
|
|
341
|
+
this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(y(this.dispose$)).subscribe((t) => {
|
|
328
342
|
this._syncingUnits.add(t.getUnitId()), this._remoteInstanceService.createInstance({
|
|
329
343
|
unitID: t.getUnitId(),
|
|
330
344
|
type: u.UNIVER_SHEET,
|
|
331
345
|
snapshot: t.getSnapshot()
|
|
332
346
|
});
|
|
333
|
-
}), this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(
|
|
347
|
+
}), this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(y(this.dispose$)).subscribe((t) => {
|
|
334
348
|
this._syncingUnits.delete(t.getUnitId()), this._remoteInstanceService.disposeInstance({
|
|
335
349
|
unitID: t.getUnitId()
|
|
336
350
|
});
|
|
337
351
|
}), this.disposeWithMe(this._commandService.onCommandExecuted((t, e) => {
|
|
338
|
-
const { type:
|
|
339
|
-
|
|
352
|
+
const { type: s, params: r, id: i } = t, c = (r == null ? void 0 : r.unitId) || "";
|
|
353
|
+
s === H.MUTATION && // only sync mutations to the worker thread
|
|
340
354
|
(!c || this._syncingUnits.has(c)) && // do not sync mutations from the web worker back to the web worker
|
|
341
355
|
!(e != null && e.fromSync) && // do not sync mutations those are not meant to be synced
|
|
342
356
|
this._syncingMutations.has(i) && this._remoteInstanceService.syncMutation({ mutationInfo: t });
|
|
343
357
|
}));
|
|
344
358
|
}
|
|
345
359
|
};
|
|
346
|
-
p =
|
|
360
|
+
p = de([
|
|
347
361
|
W(L.Starting, p),
|
|
348
362
|
l(0, g(C)),
|
|
349
363
|
l(1, b),
|
|
350
364
|
l(2, N),
|
|
351
365
|
l(3, R),
|
|
352
|
-
l(4,
|
|
366
|
+
l(4, f)
|
|
353
367
|
], p);
|
|
354
|
-
var
|
|
355
|
-
for (var r =
|
|
356
|
-
(c =
|
|
357
|
-
return
|
|
358
|
-
}, v = (
|
|
359
|
-
let d = class extends
|
|
360
|
-
constructor(t, e,
|
|
368
|
+
var ve = Object.defineProperty, me = Object.getOwnPropertyDescriptor, ye = (n, t, e, s) => {
|
|
369
|
+
for (var r = s > 1 ? void 0 : s ? me(t, e) : t, i = n.length - 1, c; i >= 0; i--)
|
|
370
|
+
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r);
|
|
371
|
+
return s && r && ve(t, e, r), r;
|
|
372
|
+
}, v = (n, t) => (e, s) => t(e, s, n);
|
|
373
|
+
let d = class extends ee {
|
|
374
|
+
constructor(t, e, s, r) {
|
|
361
375
|
super();
|
|
362
376
|
o(this, "_remoteSyncService");
|
|
363
|
-
this._injector = t, this._remoteInstanceService = e, this._commandService =
|
|
377
|
+
this._injector = t, this._remoteInstanceService = e, this._commandService = s, this._rpcChannelService = r, this._initRPCChannels(), this._init();
|
|
364
378
|
}
|
|
365
379
|
_initRPCChannels() {
|
|
366
|
-
this._rpcChannelService.registerChannel(
|
|
367
|
-
|
|
380
|
+
this._rpcChannelService.registerChannel(D, z(this._remoteInstanceService)), this._injector.add([
|
|
381
|
+
f,
|
|
368
382
|
{ useFactory: () => G(this._rpcChannelService.requestChannel(j)) }
|
|
369
|
-
]), this._remoteSyncService = this._injector.get(
|
|
383
|
+
]), this._remoteSyncService = this._injector.get(f);
|
|
370
384
|
}
|
|
371
385
|
_init() {
|
|
372
386
|
this.disposeWithMe(
|
|
@@ -379,7 +393,7 @@ let d = class extends K {
|
|
|
379
393
|
);
|
|
380
394
|
}
|
|
381
395
|
};
|
|
382
|
-
d =
|
|
396
|
+
d = ye([
|
|
383
397
|
W(L.Starting, d),
|
|
384
398
|
v(0, g(C)),
|
|
385
399
|
v(1, S),
|
|
@@ -388,62 +402,62 @@ d = me([
|
|
|
388
402
|
], d);
|
|
389
403
|
function fe() {
|
|
390
404
|
return {
|
|
391
|
-
send(
|
|
392
|
-
postMessage(
|
|
405
|
+
send(n) {
|
|
406
|
+
postMessage(n);
|
|
393
407
|
},
|
|
394
|
-
onMessage: new $((
|
|
408
|
+
onMessage: new $((n) => {
|
|
395
409
|
const t = (e) => {
|
|
396
|
-
|
|
410
|
+
n.next(e.data);
|
|
397
411
|
};
|
|
398
412
|
return addEventListener("message", t), () => removeEventListener("message", t);
|
|
399
413
|
}).pipe(A(1))
|
|
400
414
|
};
|
|
401
415
|
}
|
|
402
|
-
function
|
|
416
|
+
function Se(n) {
|
|
403
417
|
return {
|
|
404
418
|
send(t) {
|
|
405
|
-
|
|
419
|
+
n.postMessage(t);
|
|
406
420
|
},
|
|
407
421
|
onMessage: new $((t) => {
|
|
408
|
-
const e = (
|
|
409
|
-
t.next(
|
|
422
|
+
const e = (s) => {
|
|
423
|
+
t.next(s.data);
|
|
410
424
|
};
|
|
411
|
-
return
|
|
425
|
+
return n.addEventListener("message", e), () => n.removeEventListener("message", e);
|
|
412
426
|
}).pipe(A(1))
|
|
413
427
|
};
|
|
414
428
|
}
|
|
415
|
-
var
|
|
416
|
-
for (var r =
|
|
417
|
-
(c =
|
|
418
|
-
return
|
|
419
|
-
}, J = (
|
|
420
|
-
let
|
|
421
|
-
constructor(
|
|
422
|
-
super(), this._config =
|
|
423
|
-
}
|
|
424
|
-
async onStarting(
|
|
425
|
-
const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t),
|
|
429
|
+
var be = Object.defineProperty, ge = Object.getOwnPropertyDescriptor, k = (n, t, e, s) => {
|
|
430
|
+
for (var r = s > 1 ? void 0 : s ? ge(t, e) : t, i = n.length - 1, c; i >= 0; i--)
|
|
431
|
+
(c = n[i]) && (r = (s ? c(t, e, r) : c(r)) || r);
|
|
432
|
+
return s && r && be(t, e, r), r;
|
|
433
|
+
}, J = (n, t) => (e, s) => t(e, s, n), P;
|
|
434
|
+
let x = (P = class extends V {
|
|
435
|
+
constructor(n, t) {
|
|
436
|
+
super(), this._config = n, this._injector = t;
|
|
437
|
+
}
|
|
438
|
+
async onStarting(n) {
|
|
439
|
+
const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t), s = Se(e);
|
|
426
440
|
[
|
|
427
441
|
[
|
|
428
442
|
R,
|
|
429
443
|
{
|
|
430
|
-
useFactory: () => new B(
|
|
444
|
+
useFactory: () => new B(s)
|
|
431
445
|
}
|
|
432
446
|
],
|
|
433
447
|
[p],
|
|
434
|
-
[
|
|
435
|
-
].forEach((i) =>
|
|
448
|
+
[f, { useClass: U }]
|
|
449
|
+
].forEach((i) => n.add(i)), n.get(p);
|
|
436
450
|
}
|
|
437
451
|
}, o(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P);
|
|
438
|
-
|
|
452
|
+
x = k([
|
|
439
453
|
J(1, g(C))
|
|
440
|
-
],
|
|
454
|
+
], x);
|
|
441
455
|
var E;
|
|
442
456
|
let T = (E = class extends V {
|
|
443
|
-
constructor(
|
|
444
|
-
super(), this._config =
|
|
457
|
+
constructor(n, t) {
|
|
458
|
+
super(), this._config = n, this._injector = t;
|
|
445
459
|
}
|
|
446
|
-
onStarting(
|
|
460
|
+
onStarting(n) {
|
|
447
461
|
[
|
|
448
462
|
[d],
|
|
449
463
|
[
|
|
@@ -452,26 +466,28 @@ let T = (E = class extends V {
|
|
|
452
466
|
useFactory: () => new B(fe())
|
|
453
467
|
}
|
|
454
468
|
],
|
|
455
|
-
[S, { useClass:
|
|
456
|
-
].forEach((t) =>
|
|
469
|
+
[S, { useClass: M }]
|
|
470
|
+
].forEach((t) => n.add(t)), n.get(d);
|
|
457
471
|
}
|
|
458
472
|
}, o(E, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), E);
|
|
459
473
|
T = k([
|
|
460
474
|
J(1, g(C))
|
|
461
475
|
], T);
|
|
462
476
|
export {
|
|
463
|
-
|
|
464
|
-
|
|
477
|
+
_e as ChannelClient,
|
|
478
|
+
le as ChannelServer,
|
|
465
479
|
B as ChannelService,
|
|
466
480
|
p as DataSyncPrimaryController,
|
|
467
481
|
d as DataSyncReplicaController,
|
|
468
482
|
R as IRPCChannelService,
|
|
469
483
|
S as IRemoteInstanceService,
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
484
|
+
f as IRemoteSyncService,
|
|
485
|
+
D as RemoteInstanceServiceName,
|
|
486
|
+
U as RemoteSyncPrimaryService,
|
|
473
487
|
j as RemoteSyncServiceName,
|
|
474
|
-
|
|
488
|
+
x as UniverRPCMainThreadPlugin,
|
|
475
489
|
T as UniverRPCWorkerThreadPlugin,
|
|
476
|
-
|
|
490
|
+
M as WebWorkerRemoteInstanceService,
|
|
491
|
+
z as fromModule,
|
|
492
|
+
G as toModule
|
|
477
493
|
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { IMutation, ICommandService, IUniverInstanceService, RxDisposable } from '@univerjs/core';
|
|
2
|
-
import { Injector } from '@wendellhu/redi';
|
|
1
|
+
import { IDisposable, IMutation, ICommandService, Injector, IUniverInstanceService, RxDisposable } from '@univerjs/core';
|
|
3
2
|
import { IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';
|
|
4
3
|
import { IRPCChannelService } from '../../services/rpc/channel.service';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* This controller is responsible for syncing data from the primary thread to
|
|
8
7
|
* the worker thread.
|
|
8
|
+
*
|
|
9
|
+
* Note that only spreadsheets will be synced to the remote calculation instance by default.
|
|
9
10
|
*/
|
|
10
11
|
export declare class DataSyncPrimaryController extends RxDisposable {
|
|
11
12
|
private readonly _injector;
|
|
@@ -18,6 +19,11 @@ export declare class DataSyncPrimaryController extends RxDisposable {
|
|
|
18
19
|
private readonly _syncingMutations;
|
|
19
20
|
constructor(_injector: Injector, _commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _rpcChannelService: IRPCChannelService, _remoteSyncService: IRemoteSyncService);
|
|
20
21
|
registerSyncingMutations(mutation: IMutation<object>): void;
|
|
22
|
+
/**
|
|
23
|
+
* Only spreadsheets would be synced to the web worker in normal situations. If you would like to
|
|
24
|
+
* sync other types of documents, you should manually call this method with that document's id.
|
|
25
|
+
*/
|
|
26
|
+
syncUnit(unitId: string): IDisposable;
|
|
21
27
|
private _initRPCChannels;
|
|
22
28
|
private _init;
|
|
23
29
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Disposable, ICommandService } from '@univerjs/core';
|
|
2
|
-
import { Injector } from '@wendellhu/redi';
|
|
1
|
+
import { Disposable, ICommandService, Injector } from '@univerjs/core';
|
|
3
2
|
import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';
|
|
4
3
|
import { IRPCChannelService } from '../../services/rpc/channel.service';
|
|
5
4
|
|
package/lib/types/index.d.ts
CHANGED
|
@@ -18,5 +18,5 @@ export { DataSyncReplicaController } from './controllers/data-sync/data-sync-rep
|
|
|
18
18
|
export { DataSyncPrimaryController } from './controllers/data-sync/data-sync-primary.controller';
|
|
19
19
|
export { IRemoteInstanceService, WebWorkerRemoteInstanceService, RemoteInstanceServiceName, RemoteSyncPrimaryService, RemoteSyncServiceName, } from './services/remote-instance/remote-instance.service';
|
|
20
20
|
export { ChannelService, IRPCChannelService } from './services/rpc/channel.service';
|
|
21
|
-
export { ChannelClient, ChannelServer, type IMessageProtocol } from './services/rpc/rpc.service';
|
|
21
|
+
export { ChannelClient, ChannelServer, type IMessageProtocol, fromModule, toModule } from './services/rpc/rpc.service';
|
|
22
22
|
export { IRemoteSyncService } from './services/remote-instance/remote-instance.service';
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare const RemoteSyncServiceName = "rpc.remote-sync.service";
|
|
|
10
10
|
*
|
|
11
11
|
* Replica Univer could call this service to update mutations back to the primary Univer.
|
|
12
12
|
*/
|
|
13
|
-
export declare const IRemoteSyncService: import('@
|
|
13
|
+
export declare const IRemoteSyncService: import('@univerjs/core').IdentifierDecorator<IRemoteSyncService>;
|
|
14
14
|
export interface IRemoteSyncService {
|
|
15
15
|
syncMutation(params: {
|
|
16
16
|
mutationInfo: IMutationInfo;
|
|
@@ -30,7 +30,7 @@ export declare const RemoteInstanceServiceName = "univer.remote-instance-service
|
|
|
30
30
|
* Primary univer could call this service to init and dispose univer business instances
|
|
31
31
|
* and sync mutations to replica univer.
|
|
32
32
|
*/
|
|
33
|
-
export declare const IRemoteInstanceService: import('@
|
|
33
|
+
export declare const IRemoteInstanceService: import('@univerjs/core').IdentifierDecorator<IRemoteInstanceService>;
|
|
34
34
|
export interface IRemoteInstanceService {
|
|
35
35
|
/** Tell other modules if the `IRemoteInstanceService` is ready to load files. */
|
|
36
36
|
whenReady(): Promise<true>;
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { IDisposable } from '@univerjs/core';
|
|
1
2
|
import { IChannel, IMessageProtocol } from './rpc.service';
|
|
2
3
|
|
|
3
4
|
export interface IRPCChannelService {
|
|
4
5
|
requestChannel(name: string): IChannel;
|
|
5
6
|
registerChannel(name: string, channel: IChannel): void;
|
|
6
7
|
}
|
|
7
|
-
export declare const IRPCChannelService: import('@
|
|
8
|
+
export declare const IRPCChannelService: import('@univerjs/core').IdentifierDecorator<IRPCChannelService>;
|
|
8
9
|
/**
|
|
9
10
|
* This service is responsible for managing the RPC channels.
|
|
10
11
|
*/
|
|
11
|
-
export declare class ChannelService {
|
|
12
|
+
export declare class ChannelService implements IDisposable {
|
|
12
13
|
private readonly _client;
|
|
13
14
|
private readonly _server;
|
|
14
15
|
constructor(_messageProtocol: IMessageProtocol);
|
|
16
|
+
dispose(): void;
|
|
15
17
|
requestChannel(name: string): IChannel;
|
|
16
18
|
registerChannel(name: string, channel: IChannel): void;
|
|
17
19
|
}
|
|
@@ -50,8 +50,8 @@ export declare class ChannelClient extends RxDisposable implements IChannelClien
|
|
|
50
50
|
private _initialized;
|
|
51
51
|
private _lastRequestCounter;
|
|
52
52
|
private _pendingRequests;
|
|
53
|
-
private _pendingSubscriptions;
|
|
54
53
|
constructor(_protocol: IMessageProtocol);
|
|
54
|
+
dispose(): void;
|
|
55
55
|
getChannel<T extends IChannel>(channelName: string): T;
|
|
56
56
|
private _whenReady;
|
|
57
57
|
private _remoteCall;
|
|
@@ -64,6 +64,7 @@ export declare class ChannelServer extends RxDisposable implements IChannelServe
|
|
|
64
64
|
private _channels;
|
|
65
65
|
private _subscriptions;
|
|
66
66
|
constructor(_protocol: IMessageProtocol);
|
|
67
|
+
dispose(): void;
|
|
67
68
|
registerChannel(channelName: string, channel: IChannel): void;
|
|
68
69
|
private _onRequest;
|
|
69
70
|
private _onMethodCall;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs/operators","rxjs"],c):(i=typeof globalThis<"u"?globalThis:i||self,c(i.UniverRpc={},i.UniverCore,i["@wendellhu/redi"],i.rxjs.operators,i.rxjs))})(this,function(i,c,l,v,d){"use strict";var Y=Object.defineProperty;var Z=(i,c,l)=>c in i?Y(i,c,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[c]=l;var u=(i,c,l)=>Z(i,typeof c!="symbol"?c+"":c,l);var P,w;var $=Object.defineProperty,N=Object.getOwnPropertyDescriptor,M=(a,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&$(t,e,s),s},C=(a,t)=>(e,n)=>t(e,n,a);const R="rpc.remote-sync.service",y=l.createIdentifier(R);i.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},i.RemoteSyncPrimaryService=M([C(0,c.ICommandService)],i.RemoteSyncPrimaryService);const b="univer.remote-instance-service",m=l.createIdentifier(b);i.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case c.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(c.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}},i.WebWorkerRemoteInstanceService=M([C(0,c.IUniverInstanceService),C(1,c.ICommandService),C(2,c.ILogService)],i.WebWorkerRemoteInstanceService);function q(a){const t=a;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let r=s.apply(t,[n]);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const r=s.apply(t,n);return d.isObservable(r)?r:d.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function D(a){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return L(e)?a.subscribe(e,n[0]):a.call(e,n[0])}}})}function L(a){return a.endsWith("$")}class O extends c.RxDisposable{constructor(e){super();u(this,"_initialized",new d.BehaviorSubject(!1));u(this,"_lastRequestCounter",0);u(this,"_pendingRequests",new Map);u(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}getChannel(e){const n=this;return{call(s,r){return n._disposed?Promise.reject():n._remoteCall(e,s,r)},subscribe(s,r){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,r)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(v.filter(e=>e),v.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,_={seq:r,type:100,channelName:e,method:n,args:s},h=this;return new Promise((U,f)=>{const X={handle(E){switch(E.type){case 201:h._pendingRequests.delete(r),U(E.data);break;case 202:h._pendingRequests.delete(r),f(E.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,X),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(r=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const h={seq:o,type:101,channelName:e,method:n,args:s},U={handle(f){switch(f.type){case 300:r.next(f.data);break;case 301:r.error(f.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,U),this._sendRequest(h)}),()=>{if(o===-1)return;const _={type:102,seq:o,channelName:e,method:n};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class T extends c.RxDisposable{constructor(e){super();u(this,"_channels",new Map);u(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(v.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendResponse({seq:-1,type:0})}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:n,method:s,args:r}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=o.call(s,r)}catch(h){_=Promise.reject(h)}_.then(h=>{this._sendResponse({seq:e.seq,type:201,data:h})}).catch(h=>{h instanceof Error?this._sendResponse({seq:e.seq,type:202,data:h.message}):this._sendResponse({seq:e.seq,type:202,data:String(h)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=r.subscribe(e.method,e.args).subscribe({next:h=>{this._sendResponse({seq:s,type:300,data:h})},error:h=>{this._sendResponse({seq:s,type:301,data:h.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,_)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const p=l.createIdentifier("IRPCChannelService");class g{constructor(t){u(this,"_client");u(this,"_server");this._client=new O(t),this._server=new T(t)}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var k=Object.defineProperty,H=Object.getOwnPropertyDescriptor,V=(a,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&k(t,e,s),s},S=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,o){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);u(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}_initRPCChannels(){this._rpcChannelService.registerChannel(R,q(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>D(this._rpcChannelService.requestChannel(b))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(v.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:c.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(c.UniverInstanceType.UNIVER_SHEET).pipe(v.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:r,id:o}=e,_=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},i.DataSyncPrimaryController=V([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncPrimaryController),S(0,l.Inject(l.Injector)),S(1,c.ICommandService),S(2,c.IUniverInstanceService),S(3,p),S(4,y)],i.DataSyncPrimaryController);var A=Object.defineProperty,F=Object.getOwnPropertyDescriptor,z=(a,t,e,n)=>{for(var s=n>1?void 0:n?F(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&A(t,e,s),s},I=(a,t)=>(e,n)=>t(e,n,a);i.DataSyncReplicaController=class extends c.Disposable{constructor(e,n,s,r){super();u(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(b,q(this._remoteInstanceService)),this._injector.add([y,{useFactory:()=>D(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(y)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},i.DataSyncReplicaController=z([c.OnLifecycle(c.LifecycleStages.Starting,i.DataSyncReplicaController),I(0,l.Inject(l.Injector)),I(1,m),I(2,c.ICommandService),I(3,p)],i.DataSyncReplicaController);function G(){return{send(a){postMessage(a)},onMessage:new d.Observable(a=>{const t=e=>{a.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function B(a){return{send(t){a.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return a.addEventListener("message",e),()=>a.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,j=(a,t,e,n)=>{for(var s=n>1?void 0:n?Q(t,e):t,r=a.length-1,o;r>=0;r--)(o=a[r])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&J(t,e,s),s},W=(a,t)=>(e,n)=>t(e,n,a);i.UniverRPCMainThreadPlugin=(P=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}async onStarting(t){const{workerURL:e}=this._config,n=e instanceof Worker?e:new Worker(e),s=B(n);[[p,{useFactory:()=>new g(s)}],[i.DataSyncPrimaryController],[y,{useClass:i.RemoteSyncPrimaryService}]].forEach(o=>t.add(o)),t.get(i.DataSyncPrimaryController)}},u(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P),i.UniverRPCMainThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCMainThreadPlugin),i.UniverRPCWorkerThreadPlugin=(w=class extends c.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}onStarting(t){[[i.DataSyncReplicaController],[p,{useFactory:()=>new g(G())}],[m,{useClass:i.WebWorkerRemoteInstanceService}]].forEach(e=>t.add(e)),t.get(i.DataSyncReplicaController)}},u(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w),i.UniverRPCWorkerThreadPlugin=j([W(1,l.Inject(l.Injector))],i.UniverRPCWorkerThreadPlugin),i.ChannelClient=O,i.ChannelServer=T,i.ChannelService=g,i.IRPCChannelService=p,i.IRemoteInstanceService=m,i.IRemoteSyncService=y,i.RemoteInstanceServiceName=b,i.RemoteSyncServiceName=R,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs/operators","rxjs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.UniverRpc={},r.UniverCore,r.rxjs.operators,r.rxjs))})(this,function(r,a,_,d){"use strict";var X=Object.defineProperty;var Y=(r,a,_)=>a in r?X(r,a,{enumerable:!0,configurable:!0,writable:!0,value:_}):r[a]=_;var h=(r,a,_)=>Y(r,typeof a!="symbol"?a+"":a,_);var w,U;var W=Object.defineProperty,$=Object.getOwnPropertyDescriptor,q=(c,t,e,n)=>{for(var s=n>1?void 0:n?$(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&W(t,e,s),s},f=(c,t)=>(e,n)=>t(e,n,c);const C="rpc.remote-sync.service",v=a.createIdentifier(C);r.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=q([f(0,a.ICommandService)],r.RemoteSyncPrimaryService);const R="univer.remote-instance-service",p=a.createIdentifier(R);r.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 a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.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})}},r.WebWorkerRemoteInstanceService=q([f(0,a.IUniverInstanceService),f(1,a.ICommandService),f(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function g(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=s.apply(t,[n]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=s.apply(t,n);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function I(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return N(e)?c.subscribe(e,n[0]):c.call(e,n[0])}}})}function N(c){return c.endsWith("$")}class D extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new d.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,i){return n._disposed?Promise.reject():n._remoteCall(e,s,i)},subscribe(s,i){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(_.filter(e=>e),_.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((E,S)=>{const Q={handle(M){switch(M.type){case 201:l._pendingRequests.delete(i),E(M.data);break;case 202:l._pendingRequests.delete(i),S(M.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,Q),this._sendRequest(u)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},E={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,E),this._sendRequest(l)}),()=>{if(o===-1)return;const u={type:102,seq:o,channelName:e,method:n};this._sendRequest(u)}})}_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 O extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendResponse({seq:-1,type:0})}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:n,method:s,args:i}=e,o=this._channels.get(n);let u;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);u=o.call(s,i)}catch(l){u=Promise.reject(l)}u.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:n,seq:s}=e,i=this._channels.get(n);try{if(!i)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const u=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,u)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const y=a.createIdentifier("IRPCChannelService");class P{constructor(t){h(this,"_client");h(this,"_server");this._client=new D(t),this._server=new O(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var L=Object.defineProperty,k=Object.getOwnPropertyDescriptor,H=(c,t,e,n)=>{for(var s=n>1?void 0:n?k(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&L(t,e,s),s},m=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(C,g(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>I(this._rpcChannelService.requestChannel(R))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.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(_.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:i,id:o}=e,u=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!u||this._syncingUnits.has(u))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},r.DataSyncPrimaryController=H([a.OnLifecycle(a.LifecycleStages.Starting,r.DataSyncPrimaryController),m(0,a.Inject(a.Injector)),m(1,a.ICommandService),m(2,a.IUniverInstanceService),m(3,y),m(4,v)],r.DataSyncPrimaryController);var V=Object.defineProperty,A=Object.getOwnPropertyDescriptor,F=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&V(t,e,s),s},b=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,g(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>I(this._rpcChannelService.requestChannel(C))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===a.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},r.DataSyncReplicaController=F([a.OnLifecycle(a.LifecycleStages.Starting,r.DataSyncReplicaController),b(0,a.Inject(a.Injector)),b(1,p),b(2,a.ICommandService),b(3,y)],r.DataSyncReplicaController);function z(){return{send(c){postMessage(c)},onMessage:new d.Observable(c=>{const t=e=>{c.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function G(c){return{send(t){c.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return c.addEventListener("message",e),()=>c.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var B=Object.defineProperty,J=Object.getOwnPropertyDescriptor,T=(c,t,e,n)=>{for(var s=n>1?void 0:n?J(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=(n?o(t,e,s):o(s))||s);return n&&s&&B(t,e,s),s},j=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(w=class extends a.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=G(n);[[y,{useFactory:()=>new P(s)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>t.add(o)),t.get(r.DataSyncPrimaryController)}},h(w,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),w),r.UniverRPCMainThreadPlugin=T([j(1,a.Inject(a.Injector))],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(U=class extends a.Plugin{constructor(t,e){super(),this._config=t,this._injector=e}onStarting(t){[[r.DataSyncReplicaController],[y,{useFactory:()=>new P(z())}],[p,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(e=>t.add(e)),t.get(r.DataSyncReplicaController)}},h(U,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),U),r.UniverRPCWorkerThreadPlugin=T([j(1,a.Inject(a.Injector))],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=D,r.ChannelServer=O,r.ChannelService=P,r.IRPCChannelService=y,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=R,r.RemoteSyncServiceName=C,r.fromModule=g,r.toModule=I,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/rpc",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4-alpha.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "DreamNum <developer@univer.ai>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -45,18 +45,16 @@
|
|
|
45
45
|
"lib"
|
|
46
46
|
],
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@wendellhu/redi": "0.15.5",
|
|
49
48
|
"rxjs": ">=7.0.0",
|
|
50
|
-
"@univerjs/core": "0.2.
|
|
49
|
+
"@univerjs/core": "0.2.4-alpha.0"
|
|
51
50
|
},
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"@wendellhu/redi": "0.15.5",
|
|
54
52
|
"rxjs": "^7.8.1",
|
|
55
53
|
"typescript": "^5.5.3",
|
|
56
|
-
"vite": "^5.3.
|
|
57
|
-
"vitest": "^
|
|
58
|
-
"@univerjs/core": "0.2.
|
|
59
|
-
"@univerjs/shared": "0.2.
|
|
54
|
+
"vite": "^5.3.4",
|
|
55
|
+
"vitest": "^2.0.3",
|
|
56
|
+
"@univerjs/core": "0.2.4-alpha.0",
|
|
57
|
+
"@univerjs/shared": "0.2.4-alpha.0"
|
|
60
58
|
},
|
|
61
59
|
"univerSpace": {
|
|
62
60
|
".": {
|