@univerjs/rpc 0.1.0-beta.0 → 0.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var A=Object.defineProperty;var H=(r,n,e)=>n in r?A(r,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[n]=e;var h=(r,n,e)=>(H(r,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),l=require("@wendellhu/redi"),d=require("rxjs/operators"),_=require("rxjs");var F=Object.defineProperty,k=Object.getOwnPropertyDescriptor,O=(r,n,e,t)=>{for(var s=t>1?void 0:t?k(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&F(n,e,s),s},w=(r,n)=>(e,t)=>n(e,t,r);const E="univer.remote-sync-service",C=l.createIdentifier(E);let U=class{constructor(r){this._commandService=r}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};U=O([w(0,a.ICommandService)],U);const M="univer.remote-instance-service",S=l.createIdentifier(M);let q=class{constructor(r,n){this._univerInstanceService=r,this._commandService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}async createInstance(r){const{type:n,snapshot:e}=r;try{switch(n){case a.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(t){throw t instanceof Error?t:new Error(`${t}`)}}async disposeInstance(r){return this._univerInstanceService.disposeDocument(r.unitID)}};q=O([w(0,a.IUniverInstanceService),w(1,a.ICommandService)],q);function j(r){const n=r;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[t]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const i=s.apply(n,t);return _.isObservable(i)?i:_.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function $(r){return new Proxy({},{get(n,e){return function(...t){return z(e)?r.subscribe(e,t[0]):r.call(e,t[0])}}})}function z(r){return r.endsWith("$")}class T extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new _.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);h(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,i){return t._disposed?Promise.reject():t._remoteCall(e,s,i)},subscribe(s,i){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,i)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(d.filter(e=>e),d.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:t,args:s},o=this;return new Promise((g,p)=>{const N={handle(R){switch(R.type){case 201:o._pendingRequests.delete(i),g(R.data);break;case 202:o._pendingRequests.delete(i),p(R.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,N),this._sendRequest(u)})}_remoteSubscribe(e,t,s){return new _.Observable(i=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},g={handle(p){switch(p.type){case 300:i.next(p.data);break;case 301:i.error(p.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,g),this._sendRequest(o)}),()=>{if(c===-1)return;const u={type:102,seq:c,channelName:e,method:t};this._sendRequest(u)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class D extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:i}=e,c=this._channels.get(t);let u;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);u=c.call(s,i)}catch(o){u=Promise.reject(o)}u.then(o=>{this._sendResponse({seq:e.seq,type:201,data:o})}).catch(o=>{o instanceof Error?this._sendResponse({seq:e.seq,type:202,data:o.message}):this._sendResponse({seq:e.seq,type:202,data:String(o)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,i=this._channels.get(t);try{if(!i)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const u=i.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,u)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const f=l.createIdentifier("IRPChannelService");class x{constructor(n){h(this,"_client");h(this,"_server");this._client=new T(n),this._server=new D(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var V=Object.defineProperty,G=Object.getOwnPropertyDescriptor,B=(r,n,e,t)=>{for(var s=t>1?void 0:t?G(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&V(n,e,s),s},v=(r,n)=>(e,t)=>n(e,t,r);let m=class extends a.RxDisposable{constructor(n,e,t,s,i){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);this._injector=n,this._commandService=e,this._univerInstanceService=t,this._rpcChannelService=s,this._remoteSyncService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(E,j(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>$(this._rpcChannelService.requestChannel(M))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.sheetAdded$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:a.UniverInstanceType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.delete(n.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{const{type:t,params:s}=n,i=(s==null?void 0:s.unitId)||"";t===a.CommandType.MUTATION&&(!i||this._syncingUnits.has(i))&&!(e!=null&&e.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};m=B([a.OnLifecycle(a.LifecycleStages.Starting,m),v(0,l.Inject(l.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,f),v(4,C)],m);var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,X=(r,n,e,t)=>{for(var s=t>1?void 0:t?Q(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&J(n,e,s),s},b=(r,n)=>(e,t)=>n(e,t,r);let y=class extends a.Disposable{constructor(n,e,t,s){super();h(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=t,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(M,j(this._remoteInstanceService)),this._injector.add([C,{useFactory:()=>$(this._rpcChannelService.requestChannel(E))}]),this._remoteSyncService=this._injector.get(C)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===a.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};y=X([a.OnLifecycle(a.LifecycleStages.Starting,y),b(0,l.Inject(l.Injector)),b(1,S),b(2,a.ICommandService),b(3,f)],y);function Y(){return{send(r){postMessage(r)},onMessage:new _.Observable(r=>{const n=e=>{r.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function Z(r){return{send(n){r.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return r.addEventListener("message",e),()=>r.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var K=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,W=(r,n,e,t)=>{for(var s=t>1?void 0:t?ee(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&K(n,e,s),s},L=(r,n)=>(e,t)=>n(e,t,r),I;exports.UniverRPCMainThreadPlugin=(I=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const e=new Worker(this._config.workerURL),t=Z(e);[[f,{useFactory:()=>new x(t)}],[m],[C,{useClass:U}]].forEach(i=>n.add(i)),n.get(m)}},h(I,"type",a.PluginType.Univer),I);exports.UniverRPCMainThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCMainThreadPlugin);var P;exports.UniverRPCWorkerThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[y],[f,{useFactory:()=>new x(Y())}],[S,{useClass:q}]].forEach(e=>n.add(e)),n.get(y)}},h(P,"type",a.PluginType.Univer),P);exports.UniverRPCWorkerThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=T;exports.ChannelServer=D;exports.IRPChannelService=f;exports.IRemoteInstanceService=S;
1
+ "use strict";var A=Object.defineProperty;var H=(r,n,e)=>n in r?A(r,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[n]=e;var h=(r,n,e)=>(H(r,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),l=require("@wendellhu/redi"),d=require("rxjs/operators"),_=require("rxjs");var F=Object.defineProperty,k=Object.getOwnPropertyDescriptor,O=(r,n,e,t)=>{for(var s=t>1?void 0:t?k(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&F(n,e,s),s},w=(r,n)=>(e,t)=>n(e,t,r);const E="univer.remote-sync-service",C=l.createIdentifier(E);let U=class{constructor(r){this._commandService=r}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};U=O([w(0,a.ICommandService)],U);const M="univer.remote-instance-service",S=l.createIdentifier(M);let q=class{constructor(r,n){this._univerInstanceService=r,this._commandService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}async createInstance(r){const{type:n,snapshot:e}=r;try{switch(n){case a.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(t){throw t instanceof Error?t:new Error(`${t}`)}}async disposeInstance(r){return this._univerInstanceService.disposeDocument(r.unitID)}};q=O([w(0,a.IUniverInstanceService),w(1,a.ICommandService)],q);function j(r){const n=r;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[t]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const i=s.apply(n,t);return _.isObservable(i)?i:_.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function $(r){return new Proxy({},{get(n,e){return function(...t){return z(e)?r.subscribe(e,t[0]):r.call(e,t[0])}}})}function z(r){return r.endsWith("$")}class T extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new _.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);h(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,i){return t._disposed?Promise.reject():t._remoteCall(e,s,i)},subscribe(s,i){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,i)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(d.filter(e=>e),d.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:t,args:s},o=this;return new Promise((g,p)=>{const N={handle(R){switch(R.type){case 201:o._pendingRequests.delete(i),g(R.data);break;case 202:o._pendingRequests.delete(i),p(R.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,N),this._sendRequest(u)})}_remoteSubscribe(e,t,s){return new _.Observable(i=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const o={seq:c,type:101,channelName:e,method:t,args:s},g={handle(p){switch(p.type){case 300:i.next(p.data);break;case 301:i.error(p.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,g),this._sendRequest(o)}),()=>{if(c===-1)return;const u={type:102,seq:c,channelName:e,method:t};this._sendRequest(u)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class D extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(d.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:i}=e,c=this._channels.get(t);let u;try{if(!c)throw new Error(`[ChannelServer]: Channel ${t} not found!`);u=c.call(s,i)}catch(o){u=Promise.reject(o)}u.then(o=>{this._sendResponse({seq:e.seq,type:201,data:o})}).catch(o=>{o instanceof Error?this._sendResponse({seq:e.seq,type:202,data:o.message}):this._sendResponse({seq:e.seq,type:202,data:String(o)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,i=this._channels.get(t);try{if(!i)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const u=i.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,u)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const f=l.createIdentifier("IRPChannelService");class x{constructor(n){h(this,"_client");h(this,"_server");this._client=new T(n),this._server=new D(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var V=Object.defineProperty,G=Object.getOwnPropertyDescriptor,B=(r,n,e,t)=>{for(var s=t>1?void 0:t?G(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&V(n,e,s),s},v=(r,n)=>(e,t)=>n(e,t,r);let m=class extends a.RxDisposable{constructor(n,e,t,s,i){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);this._injector=n,this._commandService=e,this._univerInstanceService=t,this._rpcChannelService=s,this._remoteSyncService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(E,j(this._remoteSyncService)),this._injector.add([S,{useFactory:()=>$(this._rpcChannelService.requestChannel(M))}]),this._remoteInstanceService=this._injector.get(S)}_init(){this._univerInstanceService.sheetAdded$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:a.UniverInstanceType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(d.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.delete(n.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{const{type:t,params:s}=n,i=(s==null?void 0:s.unitId)||"";t===a.CommandType.MUTATION&&(!i||this._syncingUnits.has(i))&&!(e!=null&&e.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};m=B([a.OnLifecycle(a.LifecycleStages.Starting,m),v(0,l.Inject(l.Injector)),v(1,a.ICommandService),v(2,a.IUniverInstanceService),v(3,f),v(4,C)],m);var J=Object.defineProperty,Q=Object.getOwnPropertyDescriptor,X=(r,n,e,t)=>{for(var s=t>1?void 0:t?Q(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&J(n,e,s),s},b=(r,n)=>(e,t)=>n(e,t,r);let y=class extends a.Disposable{constructor(n,e,t,s){super();h(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=t,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(M,j(this._remoteInstanceService)),this._injector.add([C,{useFactory:()=>$(this._rpcChannelService.requestChannel(E))}]),this._remoteSyncService=this._injector.get(C)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===a.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};y=X([a.OnLifecycle(a.LifecycleStages.Starting,y),b(0,l.Inject(l.Injector)),b(1,S),b(2,a.ICommandService),b(3,f)],y);function Y(){return{send(r){postMessage(r)},onMessage:new _.Observable(r=>{const n=e=>{r.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(_.shareReplay(1))}}function Z(r){return{send(n){r.postMessage(n)},onMessage:new _.Observable(n=>{const e=t=>{n.next(t.data)};return r.addEventListener("message",e),()=>r.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var K=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,W=(r,n,e,t)=>{for(var s=t>1?void 0:t?ee(n,e):n,i=r.length-1,c;i>=0;i--)(c=r[i])&&(s=(t?c(n,e,s):c(s))||s);return t&&s&&K(n,e,s),s},L=(r,n)=>(e,t)=>n(e,t,r),I;exports.UniverRPCMainThreadPlugin=(I=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const{workerURL:e}=this._config,t=e instanceof Worker?e:new Worker(e),s=Z(t);[[f,{useFactory:()=>new x(s)}],[m],[C,{useClass:U}]].forEach(c=>n.add(c)),n.get(m)}},h(I,"type",a.PluginType.Univer),I);exports.UniverRPCMainThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCMainThreadPlugin);var P;exports.UniverRPCWorkerThreadPlugin=(P=class extends a.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[y],[f,{useFactory:()=>new x(Y())}],[S,{useClass:q}]].forEach(e=>n.add(e)),n.get(y)}},h(P,"type",a.PluginType.Univer),P);exports.UniverRPCWorkerThreadPlugin=W([L(1,l.Inject(l.Injector))],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=T;exports.ChannelServer=D;exports.IRPChannelService=f;exports.IRemoteInstanceService=S;
package/lib/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var Z = Object.defineProperty;
2
2
  var K = (s, t, e) => t in s ? Z(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
3
  var a = (s, t, e) => (K(s, typeof t != "symbol" ? t + "" : t, e), e);
4
- import { UniverInstanceType as T, ICommandService as y, IUniverInstanceService as L, RxDisposable as M, OnLifecycle as W, LifecycleStages as N, CommandType as A, Disposable as k, Plugin as H, PluginType as F } from "@univerjs/core";
4
+ import { UniverInstanceType as T, ICommandService as y, IUniverInstanceService as W, RxDisposable as M, OnLifecycle as L, LifecycleStages as N, CommandType as A, Disposable as k, Plugin as H, PluginType as F } from "@univerjs/core";
5
5
  import { createIdentifier as O, Inject as S, Injector as b } from "@wendellhu/redi";
6
6
  import { takeUntil as v, filter as ee, take as te } from "rxjs/operators";
7
7
  import { BehaviorSubject as ne, firstValueFrom as se, Observable as $, isObservable as re, of as ie, shareReplay as z } from "rxjs";
@@ -59,7 +59,7 @@ let q = class {
59
59
  }
60
60
  };
61
61
  q = V([
62
- P(0, L),
62
+ P(0, W),
63
63
  P(1, y)
64
64
  ], q);
65
65
  function G(s) {
@@ -338,10 +338,10 @@ let u = class extends M {
338
338
  }
339
339
  };
340
340
  u = pe([
341
- W(N.Starting, u),
341
+ L(N.Starting, u),
342
342
  _(0, S(b)),
343
343
  _(1, y),
344
- _(2, L),
344
+ _(2, W),
345
345
  _(3, C),
346
346
  _(4, m)
347
347
  ], u);
@@ -376,7 +376,7 @@ let p = class extends k {
376
376
  }
377
377
  };
378
378
  p = me([
379
- W(N.Starting, p),
379
+ L(N.Starting, p),
380
380
  d(0, S(b)),
381
381
  d(1, f),
382
382
  d(2, y),
@@ -418,17 +418,17 @@ let j = (w = class extends H {
418
418
  super("UNIVER_RPC_MAIN_THREAD_PLUGIN"), this._config = s, this._injector = t;
419
419
  }
420
420
  async onStarting(s) {
421
- const t = new Worker(this._config.workerURL), e = ye(t);
421
+ const { workerURL: t } = this._config, e = t instanceof Worker ? t : new Worker(t), n = ye(e);
422
422
  [
423
423
  [
424
424
  C,
425
425
  {
426
- useFactory: () => new J(e)
426
+ useFactory: () => new J(n)
427
427
  }
428
428
  ],
429
429
  [u],
430
430
  [m, { useClass: E }]
431
- ].forEach((r) => s.add(r)), s.get(u);
431
+ ].forEach((i) => s.add(i)), s.get(u);
432
432
  }
433
433
  }, a(w, "type", F.Univer), w);
434
434
  j = Q([
@@ -16,7 +16,7 @@
16
16
  import { Plugin, PluginType } from '@univerjs/core';
17
17
  import { Injector } from '@wendellhu/redi';
18
18
  export interface IUniverRPCMainThreadConfig {
19
- workerURL: string | URL;
19
+ workerURL: string | URL | Worker;
20
20
  }
21
21
  /**
22
22
  * This plugin is used to register the RPC services on the main thread. It
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(o,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs/operators","rxjs"],c):(o=typeof globalThis<"u"?globalThis:o||self,c(o.UniverRpc={},o.UniverCore,o["@wendellhu/redi"],o.rxjs.operators,o.rxjs))})(this,function(o,c,h,p,d){"use strict";var ee=Object.defineProperty;var ne=(o,c,h)=>c in o?ee(o,c,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[c]=h;var u=(o,c,h)=>(ne(o,typeof c!="symbol"?c+"":c,h),h);var q,E;var A=Object.defineProperty,H=Object.getOwnPropertyDescriptor,j=(r,n,e,t)=>{for(var s=t>1?void 0:t?H(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&A(n,e,s),s},g=(r,n)=>(e,t)=>n(e,t,r);const I="univer.remote-sync-service",b=h.createIdentifier(I);let P=class{constructor(r){this._commandService=r}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};P=j([g(0,c.ICommandService)],P);const w="univer.remote-instance-service",v=h.createIdentifier(w);let U=class{constructor(r,n){this._univerInstanceService=r,this._commandService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}async createInstance(r){const{type:n,snapshot:e}=r;try{switch(n){case c.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(t){throw t instanceof Error?t:new Error(`${t}`)}}async disposeInstance(r){return this._univerInstanceService.disposeDocument(r.unitID)}};U=j([g(0,c.IUniverInstanceService),g(1,c.ICommandService)],U);function T(r){const n=r;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[t]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const i=s.apply(n,t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function $(r){return new Proxy({},{get(n,e){return function(...t){return F(e)?r.subscribe(e,t[0]):r.call(e,t[0])}}})}function F(r){return r.endsWith("$")}class D extends c.RxDisposable{constructor(e){super();u(this,"_initialized",new d.BehaviorSubject(!1));u(this,"_lastRequestCounter",0);u(this,"_pendingRequests",new Map);u(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,i){return t._disposed?Promise.reject():t._remoteCall(e,s,i)},subscribe(s,i){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(p.filter(e=>e),p.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const i=++this._lastRequestCounter,_={seq:i,type:100,channelName:e,method:t,args:s},l=this;return new Promise((M,C)=>{const K={handle(O){switch(O.type){case 201:l._pendingRequests.delete(i),M(O.data);break;case 202:l._pendingRequests.delete(i),C(O.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,K),this._sendRequest(_)})}_remoteSubscribe(e,t,s){return new d.Observable(i=>{let a=-1;return this._whenReady().then(()=>{a=++this._lastRequestCounter;const l={seq:a,type:101,channelName:e,method:t,args:s},M={handle(C){switch(C.type){case 300:i.next(C.data);break;case 301:i.error(C.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(a,M),this._sendRequest(l)}),()=>{if(a===-1)return;const _={type:102,seq:a,channelName:e,method:t};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class W extends c.RxDisposable{constructor(e){super();u(this,"_channels",new Map);u(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:i}=e,a=this._channels.get(t);let _;try{if(!a)throw new Error(`[ChannelServer]: Channel ${t} not found!`);_=a.call(s,i)}catch(l){_=Promise.reject(l)}_.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,i=this._channels.get(t);try{if(!i)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const _=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:s,type:300,data:l})},error:l=>{this._sendResponse({seq:s,type:301,data:l.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,_)}catch(a){a instanceof Error?this._sendResponse({seq:e.seq,type:301,data:a.message}):this._sendResponse({seq:e.seq,type:301,data:String(a)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=h.createIdentifier("IRPChannelService");class L{constructor(n){u(this,"_client");u(this,"_server");this._client=new D(n),this._server=new W(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var k=Object.defineProperty,z=Object.getOwnPropertyDescriptor,V=(r,n,e,t)=>{for(var s=t>1?void 0:t?z(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&k(n,e,s),s},f=(r,n)=>(e,t)=>n(e,t,r);let y=class extends c.RxDisposable{constructor(n,e,t,s,i){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);this._injector=n,this._commandService=e,this._univerInstanceService=t,this._rpcChannelService=s,this._remoteSyncService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(I,T(this._remoteSyncService)),this._injector.add([v,{useFactory:()=>$(this._rpcChannelService.requestChannel(w))}]),this._remoteInstanceService=this._injector.get(v)}_init(){this._univerInstanceService.sheetAdded$.pipe(p.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:c.UniverInstanceType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(p.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.delete(n.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{const{type:t,params:s}=n,i=(s==null?void 0:s.unitId)||"";t===c.CommandType.MUTATION&&(!i||this._syncingUnits.has(i))&&!(e!=null&&e.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};y=V([c.OnLifecycle(c.LifecycleStages.Starting,y),f(0,h.Inject(h.Injector)),f(1,c.ICommandService),f(2,c.IUniverInstanceService),f(3,m),f(4,b)],y);var G=Object.defineProperty,B=Object.getOwnPropertyDescriptor,J=(r,n,e,t)=>{for(var s=t>1?void 0:t?B(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&G(n,e,s),s},R=(r,n)=>(e,t)=>n(e,t,r);let S=class extends c.Disposable{constructor(n,e,t,s){super();u(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=t,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(w,T(this._remoteInstanceService)),this._injector.add([b,{useFactory:()=>$(this._rpcChannelService.requestChannel(I))}]),this._remoteSyncService=this._injector.get(b)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===c.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};S=J([c.OnLifecycle(c.LifecycleStages.Starting,S),R(0,h.Inject(h.Injector)),R(1,v),R(2,c.ICommandService),R(3,m)],S);function Q(){return{send(r){postMessage(r)},onMessage:new d.Observable(r=>{const n=e=>{r.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(d.shareReplay(1))}}function X(r){return{send(n){r.postMessage(n)},onMessage:new d.Observable(n=>{const e=t=>{n.next(t.data)};return r.addEventListener("message",e),()=>r.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var Y=Object.defineProperty,Z=Object.getOwnPropertyDescriptor,x=(r,n,e,t)=>{for(var s=t>1?void 0:t?Z(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&Y(n,e,s),s},N=(r,n)=>(e,t)=>n(e,t,r);o.UniverRPCMainThreadPlugin=(q=class extends c.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const e=new Worker(this._config.workerURL),t=X(e);[[m,{useFactory:()=>new L(t)}],[y],[b,{useClass:P}]].forEach(i=>n.add(i)),n.get(y)}},u(q,"type",c.PluginType.Univer),q),o.UniverRPCMainThreadPlugin=x([N(1,h.Inject(h.Injector))],o.UniverRPCMainThreadPlugin),o.UniverRPCWorkerThreadPlugin=(E=class extends c.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[S],[m,{useFactory:()=>new L(Q())}],[v,{useClass:U}]].forEach(e=>n.add(e)),n.get(S)}},u(E,"type",c.PluginType.Univer),E),o.UniverRPCWorkerThreadPlugin=x([N(1,h.Inject(h.Injector))],o.UniverRPCWorkerThreadPlugin),o.ChannelClient=D,o.ChannelServer=W,o.IRPChannelService=m,o.IRemoteInstanceService=v,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs/operators","rxjs"],c):(o=typeof globalThis<"u"?globalThis:o||self,c(o.UniverRpc={},o.UniverCore,o["@wendellhu/redi"],o.rxjs.operators,o.rxjs))})(this,function(o,c,h,p,d){"use strict";var ee=Object.defineProperty;var ne=(o,c,h)=>c in o?ee(o,c,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[c]=h;var u=(o,c,h)=>(ne(o,typeof c!="symbol"?c+"":c,h),h);var q,E;var A=Object.defineProperty,H=Object.getOwnPropertyDescriptor,j=(r,n,e,t)=>{for(var s=t>1?void 0:t?H(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&A(n,e,s),s},g=(r,n)=>(e,t)=>n(e,t,r);const I="univer.remote-sync-service",b=h.createIdentifier(I);let P=class{constructor(r){this._commandService=r}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};P=j([g(0,c.ICommandService)],P);const w="univer.remote-instance-service",v=h.createIdentifier(w);let U=class{constructor(r,n){this._univerInstanceService=r,this._commandService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(r){return this._commandService.syncExecuteCommand(r.mutationInfo.id,r.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}async createInstance(r){const{type:n,snapshot:e}=r;try{switch(n){case c.UniverInstanceType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(t){throw t instanceof Error?t:new Error(`${t}`)}}async disposeInstance(r){return this._univerInstanceService.disposeDocument(r.unitID)}};U=j([g(0,c.IUniverInstanceService),g(1,c.ICommandService)],U);function T(r){const n=r;return new class{call(e,t){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[t]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,t){const s=n[e];if(typeof s=="function"){const i=s.apply(n,t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function $(r){return new Proxy({},{get(n,e){return function(...t){return F(e)?r.subscribe(e,t[0]):r.call(e,t[0])}}})}function F(r){return r.endsWith("$")}class D extends c.RxDisposable{constructor(e){super();u(this,"_initialized",new d.BehaviorSubject(!1));u(this,"_lastRequestCounter",0);u(this,"_pendingRequests",new Map);u(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onMessage(t))}getChannel(e){const t=this;return{call(s,i){return t._disposed?Promise.reject():t._remoteCall(e,s,i)},subscribe(s,i){if(t._disposed)throw new Error("[ChannelClient]: client is disposed!");return t._remoteSubscribe(e,s,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(p.filter(e=>e),p.take(1)))}async _remoteCall(e,t,s){await this._whenReady();const i=++this._lastRequestCounter,_={seq:i,type:100,channelName:e,method:t,args:s},l=this;return new Promise((M,C)=>{const K={handle(O){switch(O.type){case 201:l._pendingRequests.delete(i),M(O.data);break;case 202:l._pendingRequests.delete(i),C(O.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,K),this._sendRequest(_)})}_remoteSubscribe(e,t,s){return new d.Observable(i=>{let a=-1;return this._whenReady().then(()=>{a=++this._lastRequestCounter;const l={seq:a,type:101,channelName:e,method:t,args:s},M={handle(C){switch(C.type){case 300:i.next(C.data);break;case 301:i.error(C.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(a,M),this._sendRequest(l)}),()=>{if(a===-1)return;const _={type:102,seq:a,channelName:e,method:t};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var t;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(t=this._pendingRequests.get(e.seq))==null||t.handle(e);break}}}class W extends c.RxDisposable{constructor(e){super();u(this,"_channels",new Map);u(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(p.takeUntil(this.dispose$)).subscribe(t=>this._onRequest(t)),this._sendResponse({seq:-1,type:0})}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const{channelName:t,method:s,args:i}=e,a=this._channels.get(t);let _;try{if(!a)throw new Error(`[ChannelServer]: Channel ${t} not found!`);_=a.call(s,i)}catch(l){_=Promise.reject(l)}_.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:t,seq:s}=e,i=this._channels.get(t);try{if(!i)throw new Error(`[ChannelServer]: Channel ${t} not found!`);const _=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:s,type:300,data:l})},error:l=>{this._sendResponse({seq:s,type:301,data:l.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,_)}catch(a){a instanceof Error?this._sendResponse({seq:e.seq,type:301,data:a.message}):this._sendResponse({seq:e.seq,type:301,data:String(a)})}}_onUnsubscribe(e){const t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=h.createIdentifier("IRPChannelService");class L{constructor(n){u(this,"_client");u(this,"_server");this._client=new D(n),this._server=new W(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var k=Object.defineProperty,z=Object.getOwnPropertyDescriptor,V=(r,n,e,t)=>{for(var s=t>1?void 0:t?z(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&k(n,e,s),s},f=(r,n)=>(e,t)=>n(e,t,r);let y=class extends c.RxDisposable{constructor(n,e,t,s,i){super();u(this,"_remoteInstanceService");u(this,"_syncingUnits",new Set);this._injector=n,this._commandService=e,this._univerInstanceService=t,this._rpcChannelService=s,this._remoteSyncService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(I,T(this._remoteSyncService)),this._injector.add([v,{useFactory:()=>$(this._rpcChannelService.requestChannel(w))}]),this._remoteInstanceService=this._injector.get(v)}_init(){this._univerInstanceService.sheetAdded$.pipe(p.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:c.UniverInstanceType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(p.takeUntil(this.dispose$)).subscribe(n=>{this._syncingUnits.delete(n.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{const{type:t,params:s}=n,i=(s==null?void 0:s.unitId)||"";t===c.CommandType.MUTATION&&(!i||this._syncingUnits.has(i))&&!(e!=null&&e.fromSync)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};y=V([c.OnLifecycle(c.LifecycleStages.Starting,y),f(0,h.Inject(h.Injector)),f(1,c.ICommandService),f(2,c.IUniverInstanceService),f(3,m),f(4,b)],y);var G=Object.defineProperty,B=Object.getOwnPropertyDescriptor,J=(r,n,e,t)=>{for(var s=t>1?void 0:t?B(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&G(n,e,s),s},R=(r,n)=>(e,t)=>n(e,t,r);let S=class extends c.Disposable{constructor(n,e,t,s){super();u(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=t,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(w,T(this._remoteInstanceService)),this._injector.add([b,{useFactory:()=>$(this._rpcChannelService.requestChannel(I))}]),this._remoteSyncService=this._injector.get(b)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===c.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};S=J([c.OnLifecycle(c.LifecycleStages.Starting,S),R(0,h.Inject(h.Injector)),R(1,v),R(2,c.ICommandService),R(3,m)],S);function Q(){return{send(r){postMessage(r)},onMessage:new d.Observable(r=>{const n=e=>{r.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(d.shareReplay(1))}}function X(r){return{send(n){r.postMessage(n)},onMessage:new d.Observable(n=>{const e=t=>{n.next(t.data)};return r.addEventListener("message",e),()=>r.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var Y=Object.defineProperty,Z=Object.getOwnPropertyDescriptor,x=(r,n,e,t)=>{for(var s=t>1?void 0:t?Z(n,e):n,i=r.length-1,a;i>=0;i--)(a=r[i])&&(s=(t?a(n,e,s):a(s))||s);return t&&s&&Y(n,e,s),s},N=(r,n)=>(e,t)=>n(e,t,r);o.UniverRPCMainThreadPlugin=(q=class extends c.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const{workerURL:e}=this._config,t=e instanceof Worker?e:new Worker(e),s=X(t);[[m,{useFactory:()=>new L(s)}],[y],[b,{useClass:P}]].forEach(a=>n.add(a)),n.get(y)}},u(q,"type",c.PluginType.Univer),q),o.UniverRPCMainThreadPlugin=x([N(1,h.Inject(h.Injector))],o.UniverRPCMainThreadPlugin),o.UniverRPCWorkerThreadPlugin=(E=class extends c.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[S],[m,{useFactory:()=>new L(Q())}],[v,{useClass:U}]].forEach(e=>n.add(e)),n.get(S)}},u(E,"type",c.PluginType.Univer),E),o.UniverRPCWorkerThreadPlugin=x([N(1,h.Inject(h.Injector))],o.UniverRPCWorkerThreadPlugin),o.ChannelClient=D,o.ChannelServer=W,o.IRPChannelService=m,o.IRemoteInstanceService=v,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/rpc",
3
- "version": "0.1.0-beta.0",
3
+ "version": "0.1.0-beta.2",
4
4
  "keywords": [],
5
5
  "author": "DreamNum <developer@univer.ai>",
6
6
  "license": "Apache-2.0",
@@ -29,22 +29,23 @@
29
29
  "lib"
30
30
  ],
31
31
  "private": false,
32
- "devDependencies": {
33
- "@types/numeral": "^2.0.5",
34
- "@vitest/coverage-istanbul": "^1.0.4",
35
- "@wendellhu/redi": "^0.12.12",
32
+ "dependencies": {
33
+ "@wendellhu/redi": "^0.12.13",
36
34
  "rxjs": "^7.8.1",
35
+ "@univerjs/core": "0.1.0-beta.2"
36
+ },
37
+ "devDependencies": {
38
+ "@vitest/coverage-istanbul": "^1.1.1",
37
39
  "typescript": "^5.3.3",
38
- "vite": "^5.0.8",
39
- "vite-plugin-dts": "^3.6.4",
40
+ "vite": "^5.0.10",
41
+ "vite-plugin-dts": "^3.7.0",
40
42
  "vite-plugin-externals": "^0.6.2",
41
- "vitest": "^1.0.4",
42
- "@univerjs/core": "0.1.0-beta.0"
43
+ "vitest": "^1.1.1"
43
44
  },
44
45
  "peerDependencies": {
45
46
  "@wendellhu/redi": ">=0.12.12",
46
47
  "rxjs": ">=7.0.0",
47
- "@univerjs/core": "0.1.0-beta.0"
48
+ "@univerjs/core": "0.1.0-beta.2"
48
49
  },
49
50
  "scripts": {
50
51
  "build": "tsc && vite build"