@univerjs/rpc 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`@univerjs/core`),require(`rxjs/operators`),require(`rxjs`)):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs/operators`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverRpc={},e.UniverCore,e.rxjs.operators,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`rpc.main-thread.config`;Symbol(i);let a={},o=`rpc.worker-thread.config`;Symbol(o);let s={};function c(e,t){return function(n,r){t(n,r,e)}}function l(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let u=`rpc.remote-sync.service`,d=(0,t.createIdentifier)(u),f=class{constructor(e){this._commandService=e}async syncMutation(e,t){let{fromCollab:n,...r}=t||{};return this._commandService.syncExecuteCommand(e.mutationInfo.id,e.mutationInfo.params,{...r,onlyLocal:!0,fromSync:!0})}};f=l([c(0,t.ICommandService)],f);let p=`univer.remote-instance-service`,m=(0,t.createIdentifier)(p),h=class{constructor(e,t,n){this._univerInstanceService=e,this._commandService=t,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(e,t){return this._applyMutation(e.mutationInfo,t)}async createInstance(e){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${e.unitID}`);let{type:n,snapshot:r}=e;try{switch(n){case t.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(t.UniverInstanceType.UNIVER_SHEET,r),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${n}.`)}}catch(e){throw e instanceof Error?e:TypeError(`${e}`)}}async disposeInstance(e){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${e.unitID}`),this._univerInstanceService.disposeUnit(e.unitID)}_applyMutation(e,t){let{id:n,params:r}=e,{fromCollab:i,...a}=t||{};return this._commandService.syncExecuteCommand(n,r,{...a,onlyLocal:!0,fromSync:!0})}};h=l([c(0,t.IUniverInstanceService),c(1,t.ICommandService),c(2,t.ILogService)],h);function g(e){"@babel/helpers - typeof";return g=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function v(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function y(e,t,n){return(t=v(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function b(e){let t=e;return new class{call(e,n){let r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return e instanceof Promise||(e=Promise.resolve(e)),e}throw Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){let i=t[e];if(typeof i==`function`){let e=n?i.apply(t,n):i.call(t);return(0,r.isObservable)(e)?e:(0,r.of)(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function x(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return S(n)?e.subscribe(n,t):e.call(n,t)}}})}function S(e){return e.endsWith(`$`)}var C=function(e){return e[e.REQUEST_INITIALIZATION=50]=`REQUEST_INITIALIZATION`,e[e.CALL=100]=`CALL`,e[e.SUBSCRIBE=101]=`SUBSCRIBE`,e[e.UNSUBSCRIBE=102]=`UNSUBSCRIBE`,e}(C||{}),w=function(e){return e[e.INITIALIZE=0]=`INITIALIZE`,e[e.CALL_SUCCESS=201]=`CALL_SUCCESS`,e[e.CALL_FAILURE=202]=`CALL_FAILURE`,e[e.SUBSCRIBE_NEXT=300]=`SUBSCRIBE_NEXT`,e[e.SUBSCRIBE_ERROR=301]=`SUBSCRIBE_ERROR`,e[e.SUBSCRIBE_COMPLETE=302]=`SUBSCRIBE_COMPLETE`,e}(w||{}),T=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_initialized`,new r.BehaviorSubject(!1)),y(this,`_lastRequestCounter`,0),y(this,`_pendingRequests`,new Map),this._protocol.send({type:C.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onMessage(e))}dispose(){this._pendingRequests.clear()}getChannel(e){return{call:(t,n)=>this._disposed?Promise.reject(Error(`[ChannelClient]: client is disposed!`)):this._remoteCall(e,t,n),subscribe:(t,n)=>{if(this._disposed)throw Error(`[ChannelClient]: client is disposed!`);return this._remoteSubscribe(e,t,n)}}}_whenReady(){return(0,r.firstValueFrom)(this._initialized.pipe((0,n.filter)(e=>e),(0,n.take)(1)))}_remoteCall(e,t,n){return this._initialized.getValue()?this._doRemoteCall(e,t,n):this._whenReady().then(()=>this._doRemoteCall(e,t,n))}_doRemoteCall(e,t,n){let r=++this._lastRequestCounter,i={seq:r,type:C.CALL,channelName:e,method:t,args:n},a=this._pendingRequests;return new Promise((e,t)=>{this._pendingRequests.set(r,{handle(n){switch(n.type){case w.CALL_SUCCESS:a.delete(r),e(n.data);break;case w.CALL_FAILURE:a.delete(r),t(n.data);break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(i)})}_remoteSubscribe(e,t,n){return new r.Observable(r=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=C.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case w.SUBSCRIBE_NEXT:r.next(e.data);break;case w.SUBSCRIBE_ERROR:r.error(e.data);break;case w.SUBSCRIBE_COMPLETE:r.complete();break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(o)};return this._initialized.getValue()?a():this._whenReady().then(a),()=>{if(i===-1)return;let n={type:C.UNSUBSCRIBE,seq:i,channelName:e,method:t};this._sendRequest(n)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){let{type:t,seq:n}=e;switch(t){case w.INITIALIZE:this._initialized.next(!0);break;case w.CALL_SUCCESS:case w.CALL_FAILURE:case w.SUBSCRIBE_NEXT:case w.SUBSCRIBE_COMPLETE:case w.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==w.SUBSCRIBE_NEXT&&i.delete(n);break}}}},E=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_channels`,new Map),y(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onRequest(e)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case C.REQUEST_INITIALIZATION:this._sendInitialize();break;case C.CALL:this._onMethodCall(e);break;case C.SUBSCRIBE:this._onSubscribe(e);break;case C.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:w.INITIALIZE})}_onMethodCall(e){let{channelName:t,method:n,args:r}=e,i=this._channels.get(t),a;try{if(!i)throw Error(`[ChannelServer]: Channel ${t} not found!`);a=r?i.call(n,r):i.call(n)}catch(e){a=Promise.reject(e instanceof Error?e:Error(String(e)))}a.then(t=>{this._sendResponse({seq:e.seq,type:w.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:w.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:w.CALL_FAILURE,data:String(t)})})}_onSubscribe(e){let{channelName:t,seq:n}=e,r=this._channels.get(t);try{if(!r)throw Error(`[ChannelServer]: Channel ${t} not found!`);let i=r.subscribe(e.method,e.args).subscribe({next:e=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:w.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:w.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:w.SUBSCRIBE_ERROR,data:String(t)})}}_onUnsubscribe(e){let t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}};let D=(0,t.createIdentifier)(`IRPCChannelService`);var O=class{constructor(e){y(this,`_client`,void 0),y(this,`_server`,void 0),this._client=new T(e),this._server=new E(e)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(e){return this._client.getChannel(e)}registerChannel(e,t){this._server.registerChannel(e,t)}};let k=class extends t.RxDisposable{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,y(this,`_remoteInstanceService`,void 0),y(this,`_syncingUnits`,new Set),y(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),(0,t.toDisposable)(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(u,b(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>x(this._rpcChannelService.requestChannel(p))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:t.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{let{type:r,params:i,id:a}=e,o=(i==null?void 0:i.unitId)||``;r===t.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}};k=l([c(0,(0,t.Inject)(t.Injector)),c(1,t.ICommandService),c(2,t.IUniverInstanceService),c(3,D),c(4,d)],k);let A=class extends t.Disposable{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,y(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(p,b(this._remoteInstanceService)),this._injector.add([d,{useFactory:()=>x(this._rpcChannelService.requestChannel(u))}]),this._remoteSyncService=this._injector.get(d)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===t.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}};A=l([c(0,(0,t.Inject)(t.Injector)),c(1,m),c(2,t.ICommandService),c(3,D)],A);var j=`@univerjs/rpc`,M=`0.20.1`;function N(){return{send(e){postMessage(e)},onMessage:new r.Observable(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe((0,r.shareReplay)(1))}}function P(e){return{send(t){e.postMessage(t)},onMessage:new r.Observable(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe((0,r.shareReplay)(1))}}let F=class extends t.Plugin{constructor(e=a,n,r){super(),this._config=e,this._injector=n,this._configService=r,y(this,`_internalWorker`,null);let{...o}=(0,t.merge)({},a,this._config);this._configService.setConfig(i,o)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){let{workerURL:e}=this._config;if(!e)throw Error(`[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.`);let t=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:t;let n=P(t);[[D,{useFactory:()=>new O(n)}],[k],[d,{useClass:f}]].forEach(e=>this._injector.add(e)),this._injector.get(k)}};y(F,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),y(F,`packageName`,j),y(F,`version`,M),F=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],F);let I=class extends t.Plugin{constructor(e=s,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},s,this._config);this._configService.setConfig(o,i)}onStarting(){[[A],[D,{useFactory:()=>new O(N())}],[m,{useClass:h}]].forEach(e=>this._injector.add(e)),this._injector.get(A)}};y(I,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),y(I,`packageName`,j),y(I,`version`,M),I=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],I),e.ChannelClient=T,e.ChannelServer=E,e.ChannelService=O,Object.defineProperty(e,`DataSyncPrimaryController`,{enumerable:!0,get:function(){return k}}),Object.defineProperty(e,`DataSyncReplicaController`,{enumerable:!0,get:function(){return A}}),e.IRPCChannelService=D,e.IRemoteInstanceService=m,e.IRemoteSyncService=d,e.PLUGIN_CONFIG_KEY_MAIN_THREAD=i,e.PLUGIN_CONFIG_KEY_WORKER_THREAD=o,e.RemoteInstanceServiceName=p,Object.defineProperty(e,`RemoteSyncPrimaryService`,{enumerable:!0,get:function(){return f}}),e.RemoteSyncServiceName=u,Object.defineProperty(e,`UniverRPCMainThreadPlugin`,{enumerable:!0,get:function(){return F}}),Object.defineProperty(e,`UniverRPCWorkerThreadPlugin`,{enumerable:!0,get:function(){return I}}),Object.defineProperty(e,`WebWorkerRemoteInstanceService`,{enumerable:!0,get:function(){return h}}),e.fromModule=b,e.toModule=x});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`@univerjs/core`),require(`rxjs/operators`),require(`rxjs`)):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs/operators`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverRpc={},e.UniverCore,e.rxjs.operators,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`rpc.main-thread.config`;Symbol(i);let a={},o=`rpc.worker-thread.config`;Symbol(o);let s={};function c(e,t){return function(n,r){t(n,r,e)}}function l(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let u=`rpc.remote-sync.service`,d=(0,t.createIdentifier)(u),f=class{constructor(e){this._commandService=e}async syncMutation(e,t){let{fromCollab:n,...r}=t||{};return this._commandService.syncExecuteCommand(e.mutationInfo.id,e.mutationInfo.params,{...r,onlyLocal:!0,fromSync:!0})}};f=l([c(0,t.ICommandService)],f);let p=`univer.remote-instance-service`,m=(0,t.createIdentifier)(p),h=class{constructor(e,t,n){this._univerInstanceService=e,this._commandService=t,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(e,t){return this._applyMutation(e.mutationInfo,t)}async createInstance(e){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${e.unitID}`);let{type:n,snapshot:r}=e;try{switch(n){case t.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(t.UniverInstanceType.UNIVER_SHEET,r),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${n}.`)}}catch(e){throw e instanceof Error?e:TypeError(`${e}`)}}async disposeInstance(e){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${e.unitID}`),this._univerInstanceService.disposeUnit(e.unitID)}_applyMutation(e,t){let{id:n,params:r}=e,{fromCollab:i,...a}=t||{};return this._commandService.syncExecuteCommand(n,r,{...a,onlyLocal:!0,fromSync:!0})}};h=l([c(0,t.IUniverInstanceService),c(1,t.ICommandService),c(2,t.ILogService)],h);function g(e){"@babel/helpers - typeof";return g=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function v(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function y(e,t,n){return(t=v(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function b(e){let t=e;return new class{call(e,n){let r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return e instanceof Promise||(e=Promise.resolve(e)),e}throw Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){let i=t[e];if(typeof i==`function`){let e=n?i.apply(t,n):i.call(t);return(0,r.isObservable)(e)?e:(0,r.of)(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function x(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return S(n)?e.subscribe(n,t):e.call(n,t)}}})}function S(e){return e.endsWith(`$`)}var C=function(e){return e[e.REQUEST_INITIALIZATION=50]=`REQUEST_INITIALIZATION`,e[e.CALL=100]=`CALL`,e[e.SUBSCRIBE=101]=`SUBSCRIBE`,e[e.UNSUBSCRIBE=102]=`UNSUBSCRIBE`,e}(C||{}),w=function(e){return e[e.INITIALIZE=0]=`INITIALIZE`,e[e.CALL_SUCCESS=201]=`CALL_SUCCESS`,e[e.CALL_FAILURE=202]=`CALL_FAILURE`,e[e.SUBSCRIBE_NEXT=300]=`SUBSCRIBE_NEXT`,e[e.SUBSCRIBE_ERROR=301]=`SUBSCRIBE_ERROR`,e[e.SUBSCRIBE_COMPLETE=302]=`SUBSCRIBE_COMPLETE`,e}(w||{}),T=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_initialized`,new r.BehaviorSubject(!1)),y(this,`_lastRequestCounter`,0),y(this,`_pendingRequests`,new Map),this._protocol.send({type:C.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onMessage(e))}dispose(){this._pendingRequests.clear()}getChannel(e){return{call:(t,n)=>this._disposed?Promise.reject(Error(`[ChannelClient]: client is disposed!`)):this._remoteCall(e,t,n),subscribe:(t,n)=>{if(this._disposed)throw Error(`[ChannelClient]: client is disposed!`);return this._remoteSubscribe(e,t,n)}}}_whenReady(){return(0,r.firstValueFrom)(this._initialized.pipe((0,n.filter)(e=>e),(0,n.take)(1)))}_remoteCall(e,t,n){return this._initialized.getValue()?this._doRemoteCall(e,t,n):this._whenReady().then(()=>this._doRemoteCall(e,t,n))}_doRemoteCall(e,t,n){let r=++this._lastRequestCounter,i={seq:r,type:C.CALL,channelName:e,method:t,args:n},a=this._pendingRequests;return new Promise((e,t)=>{this._pendingRequests.set(r,{handle(n){switch(n.type){case w.CALL_SUCCESS:a.delete(r),e(n.data);break;case w.CALL_FAILURE:a.delete(r),t(n.data);break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(i)})}_remoteSubscribe(e,t,n){return new r.Observable(r=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=C.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case w.SUBSCRIBE_NEXT:r.next(e.data);break;case w.SUBSCRIBE_ERROR:r.error(e.data);break;case w.SUBSCRIBE_COMPLETE:r.complete();break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(o)};return this._initialized.getValue()?a():this._whenReady().then(a),()=>{if(i===-1)return;let n={type:C.UNSUBSCRIBE,seq:i,channelName:e,method:t};this._sendRequest(n)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){let{type:t,seq:n}=e;switch(t){case w.INITIALIZE:this._initialized.next(!0);break;case w.CALL_SUCCESS:case w.CALL_FAILURE:case w.SUBSCRIBE_NEXT:case w.SUBSCRIBE_COMPLETE:case w.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==w.SUBSCRIBE_NEXT&&i.delete(n);break}}}},E=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_channels`,new Map),y(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onRequest(e)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case C.REQUEST_INITIALIZATION:this._sendInitialize();break;case C.CALL:this._onMethodCall(e);break;case C.SUBSCRIBE:this._onSubscribe(e);break;case C.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:w.INITIALIZE})}_onMethodCall(e){let{channelName:t,method:n,args:r}=e,i=this._channels.get(t),a;try{if(!i)throw Error(`[ChannelServer]: Channel ${t} not found!`);a=r?i.call(n,r):i.call(n)}catch(e){a=Promise.reject(e instanceof Error?e:Error(String(e)))}a.then(t=>{this._sendResponse({seq:e.seq,type:w.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:w.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:w.CALL_FAILURE,data:String(t)})})}_onSubscribe(e){let{channelName:t,seq:n}=e,r=this._channels.get(t);try{if(!r)throw Error(`[ChannelServer]: Channel ${t} not found!`);let i=r.subscribe(e.method,e.args).subscribe({next:e=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:w.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:w.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:w.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:w.SUBSCRIBE_ERROR,data:String(t)})}}_onUnsubscribe(e){let t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}};let D=(0,t.createIdentifier)(`IRPCChannelService`);var O=class{constructor(e){y(this,`_client`,void 0),y(this,`_server`,void 0),this._client=new T(e),this._server=new E(e)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(e){return this._client.getChannel(e)}registerChannel(e,t){this._server.registerChannel(e,t)}};let k=class extends t.RxDisposable{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,y(this,`_remoteInstanceService`,void 0),y(this,`_syncingUnits`,new Set),y(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),(0,t.toDisposable)(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(u,b(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>x(this._rpcChannelService.requestChannel(p))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:t.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{let{type:r,params:i,id:a}=e,o=(i==null?void 0:i.unitId)||``;r===t.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}};k=l([c(0,(0,t.Inject)(t.Injector)),c(1,t.ICommandService),c(2,t.IUniverInstanceService),c(3,D),c(4,d)],k);let A=class extends t.Disposable{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,y(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(p,b(this._remoteInstanceService)),this._injector.add([d,{useFactory:()=>x(this._rpcChannelService.requestChannel(u))}]),this._remoteSyncService=this._injector.get(d)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===t.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}};A=l([c(0,(0,t.Inject)(t.Injector)),c(1,m),c(2,t.ICommandService),c(3,D)],A);var j=`@univerjs/rpc`,M=`0.21.0`;function N(){return{send(e){postMessage(e)},onMessage:new r.Observable(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe((0,r.shareReplay)(1))}}function P(e){return{send(t){e.postMessage(t)},onMessage:new r.Observable(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe((0,r.shareReplay)(1))}}let F=class extends t.Plugin{constructor(e=a,n,r){super(),this._config=e,this._injector=n,this._configService=r,y(this,`_internalWorker`,null);let{...o}=(0,t.merge)({},a,this._config);this._configService.setConfig(i,o)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){let{workerURL:e}=this._config;if(!e)throw Error(`[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.`);let t=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:t;let n=P(t);[[D,{useFactory:()=>new O(n)}],[k],[d,{useClass:f}]].forEach(e=>this._injector.add(e)),this._injector.get(k)}};y(F,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),y(F,`packageName`,j),y(F,`version`,M),F=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],F);let I=class extends t.Plugin{constructor(e=s,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},s,this._config);this._configService.setConfig(o,i)}onStarting(){[[A],[D,{useFactory:()=>new O(N())}],[m,{useClass:h}]].forEach(e=>this._injector.add(e)),this._injector.get(A)}};y(I,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),y(I,`packageName`,j),y(I,`version`,M),I=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],I),e.ChannelClient=T,e.ChannelServer=E,e.ChannelService=O,Object.defineProperty(e,`DataSyncPrimaryController`,{enumerable:!0,get:function(){return k}}),Object.defineProperty(e,`DataSyncReplicaController`,{enumerable:!0,get:function(){return A}}),e.IRPCChannelService=D,e.IRemoteInstanceService=m,e.IRemoteSyncService=d,e.PLUGIN_CONFIG_KEY_MAIN_THREAD=i,e.PLUGIN_CONFIG_KEY_WORKER_THREAD=o,e.RemoteInstanceServiceName=p,Object.defineProperty(e,`RemoteSyncPrimaryService`,{enumerable:!0,get:function(){return f}}),e.RemoteSyncServiceName=u,Object.defineProperty(e,`UniverRPCMainThreadPlugin`,{enumerable:!0,get:function(){return F}}),Object.defineProperty(e,`UniverRPCWorkerThreadPlugin`,{enumerable:!0,get:function(){return I}}),Object.defineProperty(e,`WebWorkerRemoteInstanceService`,{enumerable:!0,get:function(){return h}}),e.fromModule=b,e.toModule=x});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/rpc",
3
- "version": "0.20.1",
3
+ "version": "0.21.0",
4
4
  "private": false,
5
5
  "author": "DreamNum <developer@univer.ai>",
6
6
  "license": "Apache-2.0",
@@ -47,13 +47,13 @@
47
47
  "rxjs": ">=7.0.0"
48
48
  },
49
49
  "dependencies": {
50
- "@univerjs/core": "0.20.1"
50
+ "@univerjs/core": "0.21.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.2",
54
54
  "typescript": "^6.0.2",
55
- "vitest": "^4.1.1",
56
- "@univerjs-infra/shared": "0.20.1"
55
+ "vitest": "^4.1.4",
56
+ "@univerjs-infra/shared": "0.21.0"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "vitest run",