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