@univerjs/rpc 0.19.0 → 0.20.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/cjs/index.js +1 -1
- package/lib/es/index.js +1 -1
- package/lib/index.js +1 -1
- package/lib/umd/index.js +1 -1
- package/package.json +3 -3
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@univerjs/core`),t=require(`rxjs/operators`),n=require(`rxjs`);const r=`rpc.main-thread.config`;Symbol(r);const i={},a=`rpc.worker-thread.config`;Symbol(a);const o={};function s(e,t){return function(n,r){t(n,r,e)}}function c(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}const l=`rpc.remote-sync.service`,u=(0,e.createIdentifier)(l);let d=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})}};d=c([s(0,e.ICommandService)],d);const f=`univer.remote-instance-service`,p=(0,e.createIdentifier)(f);let m=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(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);let{type:n,snapshot:r}=t;try{switch(n){case e.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(e.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})}};m=c([s(0,e.IUniverInstanceService),s(1,e.ICommandService),s(2,e.ILogService)],m);function h(e){"@babel/helpers - typeof";return h=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},h(e)}function g(e,t){if(h(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(h(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function _(e){var t=g(e,`string`);return h(t)==`symbol`?t:t+``}function v(e,t,n){return(t=_(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function y(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,r){let i=t[e];if(typeof i==`function`){let e=r?i.apply(t,r):i.call(t);return(0,n.isObservable)(e)?e:(0,n.of)(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function b(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return x(n)?e.subscribe(n,t):e.call(n,t)}}})}function x(e){return e.endsWith(`$`)}var S=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}(S||{}),C=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}(C||{}),w=class extends e.RxDisposable{constructor(e){super(),this._protocol=e,v(this,`_initialized`,new n.BehaviorSubject(!1)),v(this,`_lastRequestCounter`,0),v(this,`_pendingRequests`,new Map),this._protocol.send({type:S.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe((0,t.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,n.firstValueFrom)(this._initialized.pipe((0,t.filter)(e=>e),(0,t.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:S.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 C.CALL_SUCCESS:a.delete(r),e(n.data);break;case C.CALL_FAILURE:a.delete(r),t(n.data);break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(i)})}_remoteSubscribe(e,t,r){return new n.Observable(n=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=S.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:r};this._pendingRequests.set(i,{handle(e){switch(e.type){case C.SUBSCRIBE_NEXT:n.next(e.data);break;case C.SUBSCRIBE_ERROR:n.error(e.data);break;case C.SUBSCRIBE_COMPLETE:n.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:S.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 C.INITIALIZE:this._initialized.next(!0);break;case C.CALL_SUCCESS:case C.CALL_FAILURE:case C.SUBSCRIBE_NEXT:case C.SUBSCRIBE_COMPLETE:case C.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==C.SUBSCRIBE_NEXT&&i.delete(n);break}}}},T=class extends e.RxDisposable{constructor(e){super(),this._protocol=e,v(this,`_channels`,new Map),v(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe((0,t.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 S.REQUEST_INITIALIZATION:this._sendInitialize();break;case S.CALL:this._onMethodCall(e);break;case S.SUBSCRIBE:this._onSubscribe(e);break;case S.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:C.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:C.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:C.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:C.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:C.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:C.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:C.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:C.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:C.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:C.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)}};const E=(0,e.createIdentifier)(`IRPCChannelService`);var D=class{constructor(e){v(this,`_client`,void 0),v(this,`_server`,void 0),this._client=new w(e),this._server=new T(e)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(e){return this._client.getChannel(e)}registerChannel(e,t){this._server.registerChannel(e,t)}};let O=class extends e.RxDisposable{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,v(this,`_remoteInstanceService`,void 0),v(this,`_syncingUnits`,new Set),v(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(t){return this._syncingUnits.add(t),(0,e.toDisposable)(()=>this._syncingUnits.delete(t))}_initRPCChannels(){this._rpcChannelService.registerChannel(l,y(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>b(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(e.UniverInstanceType.UNIVER_SHEET).pipe((0,t.takeUntil)(this.dispose$)).subscribe(t=>{this._syncingUnits.add(t.getUnitId()),this._remoteInstanceService.createInstance({unitID:t.getUnitId(),type:e.UniverInstanceType.UNIVER_SHEET,snapshot:t.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(e.UniverInstanceType.UNIVER_SHEET).pipe((0,t.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};O=c([s(0,(0,e.Inject)(e.Injector)),s(1,e.ICommandService),s(2,e.IUniverInstanceService),s(3,E),s(4,u)],O);let k=class extends e.Disposable{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,v(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,y(this._remoteInstanceService)),this._injector.add([u,{useFactory:()=>b(this._rpcChannelService.requestChannel(l))}]),this._remoteSyncService=this._injector.get(u)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};k=c([s(0,(0,e.Inject)(e.Injector)),s(1,p),s(2,e.ICommandService),s(3,E)],k);var A=`@univerjs/rpc`,j=`0.19.0`;function M(){return{send(e){postMessage(e)},onMessage:new n.Observable(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe((0,n.shareReplay)(1))}}function N(e){return{send(t){e.postMessage(t)},onMessage:new n.Observable(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe((0,n.shareReplay)(1))}}let P=class extends e.Plugin{constructor(t=i,n,a){super(),this._config=t,this._injector=n,this._configService=a,v(this,`_internalWorker`,null);let{...o}=(0,e.merge)({},i,this._config);this._configService.setConfig(r,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=N(t);[[E,{useFactory:()=>new D(n)}],[O],[u,{useClass:d}]].forEach(e=>this._injector.add(e)),this._injector.get(O)}};v(P,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),v(P,`packageName`,A),v(P,`version`,j),P=c([s(1,(0,e.Inject)(e.Injector)),s(2,e.IConfigService)],P);let F=class extends e.Plugin{constructor(t=o,n,r){super(),this._config=t,this._injector=n,this._configService=r;let{...i}=(0,e.merge)({},o,this._config);this._configService.setConfig(a,i)}onStarting(){[[k],[E,{useFactory:()=>new D(M())}],[p,{useClass:m}]].forEach(e=>this._injector.add(e)),this._injector.get(k)}};v(F,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),v(F,`packageName`,A),v(F,`version`,j),F=c([s(1,(0,e.Inject)(e.Injector)),s(2,e.IConfigService)],F),exports.ChannelClient=w,exports.ChannelServer=T,exports.ChannelService=D,Object.defineProperty(exports,`DataSyncPrimaryController`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`DataSyncReplicaController`,{enumerable:!0,get:function(){return k}}),exports.IRPCChannelService=E,exports.IRemoteInstanceService=p,exports.IRemoteSyncService=u,exports.PLUGIN_CONFIG_KEY_MAIN_THREAD=r,exports.PLUGIN_CONFIG_KEY_WORKER_THREAD=a,exports.RemoteInstanceServiceName=f,Object.defineProperty(exports,`RemoteSyncPrimaryService`,{enumerable:!0,get:function(){return d}}),exports.RemoteSyncServiceName=l,Object.defineProperty(exports,`UniverRPCMainThreadPlugin`,{enumerable:!0,get:function(){return P}}),Object.defineProperty(exports,`UniverRPCWorkerThreadPlugin`,{enumerable:!0,get:function(){return F}}),Object.defineProperty(exports,`WebWorkerRemoteInstanceService`,{enumerable:!0,get:function(){return m}}),exports.fromModule=y,exports.toModule=b;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@univerjs/core`),t=require(`rxjs/operators`),n=require(`rxjs`);const r=`rpc.main-thread.config`;Symbol(r);const i={},a=`rpc.worker-thread.config`;Symbol(a);const o={};function s(e,t){return function(n,r){t(n,r,e)}}function c(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}const l=`rpc.remote-sync.service`,u=(0,e.createIdentifier)(l);let d=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})}};d=c([s(0,e.ICommandService)],d);const f=`univer.remote-instance-service`,p=(0,e.createIdentifier)(f);let m=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(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);let{type:n,snapshot:r}=t;try{switch(n){case e.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(e.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})}};m=c([s(0,e.IUniverInstanceService),s(1,e.ICommandService),s(2,e.ILogService)],m);function h(e){"@babel/helpers - typeof";return h=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},h(e)}function g(e,t){if(h(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(h(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function _(e){var t=g(e,`string`);return h(t)==`symbol`?t:t+``}function v(e,t,n){return(t=_(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function y(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,r){let i=t[e];if(typeof i==`function`){let e=r?i.apply(t,r):i.call(t);return(0,n.isObservable)(e)?e:(0,n.of)(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function b(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return x(n)?e.subscribe(n,t):e.call(n,t)}}})}function x(e){return e.endsWith(`$`)}var S=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}(S||{}),C=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}(C||{}),w=class extends e.RxDisposable{constructor(e){super(),this._protocol=e,v(this,`_initialized`,new n.BehaviorSubject(!1)),v(this,`_lastRequestCounter`,0),v(this,`_pendingRequests`,new Map),this._protocol.send({type:S.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe((0,t.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,n.firstValueFrom)(this._initialized.pipe((0,t.filter)(e=>e),(0,t.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:S.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 C.CALL_SUCCESS:a.delete(r),e(n.data);break;case C.CALL_FAILURE:a.delete(r),t(n.data);break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(i)})}_remoteSubscribe(e,t,r){return new n.Observable(n=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=S.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:r};this._pendingRequests.set(i,{handle(e){switch(e.type){case C.SUBSCRIBE_NEXT:n.next(e.data);break;case C.SUBSCRIBE_ERROR:n.error(e.data);break;case C.SUBSCRIBE_COMPLETE:n.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:S.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 C.INITIALIZE:this._initialized.next(!0);break;case C.CALL_SUCCESS:case C.CALL_FAILURE:case C.SUBSCRIBE_NEXT:case C.SUBSCRIBE_COMPLETE:case C.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==C.SUBSCRIBE_NEXT&&i.delete(n);break}}}},T=class extends e.RxDisposable{constructor(e){super(),this._protocol=e,v(this,`_channels`,new Map),v(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe((0,t.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 S.REQUEST_INITIALIZATION:this._sendInitialize();break;case S.CALL:this._onMethodCall(e);break;case S.SUBSCRIBE:this._onSubscribe(e);break;case S.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:C.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:C.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:C.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:C.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:C.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:C.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:C.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:C.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:C.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:C.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)}};const E=(0,e.createIdentifier)(`IRPCChannelService`);var D=class{constructor(e){v(this,`_client`,void 0),v(this,`_server`,void 0),this._client=new w(e),this._server=new T(e)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(e){return this._client.getChannel(e)}registerChannel(e,t){this._server.registerChannel(e,t)}};let O=class extends e.RxDisposable{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,v(this,`_remoteInstanceService`,void 0),v(this,`_syncingUnits`,new Set),v(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(t){return this._syncingUnits.add(t),(0,e.toDisposable)(()=>this._syncingUnits.delete(t))}_initRPCChannels(){this._rpcChannelService.registerChannel(l,y(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>b(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(e.UniverInstanceType.UNIVER_SHEET).pipe((0,t.takeUntil)(this.dispose$)).subscribe(t=>{this._syncingUnits.add(t.getUnitId()),this._remoteInstanceService.createInstance({unitID:t.getUnitId(),type:e.UniverInstanceType.UNIVER_SHEET,snapshot:t.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(e.UniverInstanceType.UNIVER_SHEET).pipe((0,t.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};O=c([s(0,(0,e.Inject)(e.Injector)),s(1,e.ICommandService),s(2,e.IUniverInstanceService),s(3,E),s(4,u)],O);let k=class extends e.Disposable{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,v(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,y(this._remoteInstanceService)),this._injector.add([u,{useFactory:()=>b(this._rpcChannelService.requestChannel(l))}]),this._remoteSyncService=this._injector.get(u)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};k=c([s(0,(0,e.Inject)(e.Injector)),s(1,p),s(2,e.ICommandService),s(3,E)],k);var A=`@univerjs/rpc`,j=`0.20.0`;function M(){return{send(e){postMessage(e)},onMessage:new n.Observable(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe((0,n.shareReplay)(1))}}function N(e){return{send(t){e.postMessage(t)},onMessage:new n.Observable(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe((0,n.shareReplay)(1))}}let P=class extends e.Plugin{constructor(t=i,n,a){super(),this._config=t,this._injector=n,this._configService=a,v(this,`_internalWorker`,null);let{...o}=(0,e.merge)({},i,this._config);this._configService.setConfig(r,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=N(t);[[E,{useFactory:()=>new D(n)}],[O],[u,{useClass:d}]].forEach(e=>this._injector.add(e)),this._injector.get(O)}};v(P,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),v(P,`packageName`,A),v(P,`version`,j),P=c([s(1,(0,e.Inject)(e.Injector)),s(2,e.IConfigService)],P);let F=class extends e.Plugin{constructor(t=o,n,r){super(),this._config=t,this._injector=n,this._configService=r;let{...i}=(0,e.merge)({},o,this._config);this._configService.setConfig(a,i)}onStarting(){[[k],[E,{useFactory:()=>new D(M())}],[p,{useClass:m}]].forEach(e=>this._injector.add(e)),this._injector.get(k)}};v(F,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),v(F,`packageName`,A),v(F,`version`,j),F=c([s(1,(0,e.Inject)(e.Injector)),s(2,e.IConfigService)],F),exports.ChannelClient=w,exports.ChannelServer=T,exports.ChannelService=D,Object.defineProperty(exports,`DataSyncPrimaryController`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`DataSyncReplicaController`,{enumerable:!0,get:function(){return k}}),exports.IRPCChannelService=E,exports.IRemoteInstanceService=p,exports.IRemoteSyncService=u,exports.PLUGIN_CONFIG_KEY_MAIN_THREAD=r,exports.PLUGIN_CONFIG_KEY_WORKER_THREAD=a,exports.RemoteInstanceServiceName=f,Object.defineProperty(exports,`RemoteSyncPrimaryService`,{enumerable:!0,get:function(){return d}}),exports.RemoteSyncServiceName=l,Object.defineProperty(exports,`UniverRPCMainThreadPlugin`,{enumerable:!0,get:function(){return P}}),Object.defineProperty(exports,`UniverRPCWorkerThreadPlugin`,{enumerable:!0,get:function(){return F}}),Object.defineProperty(exports,`WebWorkerRemoteInstanceService`,{enumerable:!0,get:function(){return m}}),exports.fromModule=y,exports.toModule=b;
|
package/lib/es/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{CommandType as e,Disposable as t,ICommandService as n,IConfigService as r,ILogService as i,IUniverInstanceService as a,Inject as o,Injector as s,Plugin as c,RxDisposable as l,UniverInstanceType as u,createIdentifier as d,merge as f,toDisposable as p}from"@univerjs/core";import{filter as m,take as ee,takeUntil as h}from"rxjs/operators";import{BehaviorSubject as g,Observable as _,firstValueFrom as v,isObservable as te,of as y,shareReplay as b}from"rxjs";const x=`rpc.main-thread.config`;Symbol(x);const S={},C=`rpc.worker-thread.config`;Symbol(C);const w={};function T(e,t){return function(n,r){t(n,r,e)}}function E(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}const D=`rpc.remote-sync.service`,O=d(D);let k=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})}};k=E([T(0,n)],k);const A=`univer.remote-instance-service`,j=d(A);let M=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:t,snapshot:n}=e;try{switch(t){case u.UNIVER_SHEET:return this._univerInstanceService.createUnit(u.UNIVER_SHEET,n),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`)}}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})}};M=E([T(0,a),T(1,n),T(2,i)],M);function N(e){"@babel/helpers - typeof";return N=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},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(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 r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return te(e)?e:y(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function R(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return z(n)?e.subscribe(n,t):e.call(n,t)}}})}function z(e){return e.endsWith(`$`)}var B=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}(B||{}),V=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}(V||{}),H=class extends l{constructor(e){super(),this._protocol=e,I(this,`_initialized`,new g(!1)),I(this,`_lastRequestCounter`,0),I(this,`_pendingRequests`,new Map),this._protocol.send({type:B.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe(h(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 v(this._initialized.pipe(m(e=>e),ee(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:B.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 V.CALL_SUCCESS:a.delete(r),e(n.data);break;case V.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=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=B.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case V.SUBSCRIBE_NEXT:r.next(e.data);break;case V.SUBSCRIBE_ERROR:r.error(e.data);break;case V.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:B.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 V.INITIALIZE:this._initialized.next(!0);break;case V.CALL_SUCCESS:case V.CALL_FAILURE:case V.SUBSCRIBE_NEXT:case V.SUBSCRIBE_COMPLETE:case V.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==V.SUBSCRIBE_NEXT&&i.delete(n);break}}}},U=class extends l{constructor(e){super(),this._protocol=e,I(this,`_channels`,new Map),I(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe(h(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 B.REQUEST_INITIALIZATION:this._sendInitialize();break;case B.CALL:this._onMethodCall(e);break;case B.SUBSCRIBE:this._onSubscribe(e);break;case B.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:V.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:V.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:V.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:V.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:V.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:V.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:V.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)}};const W=d(`IRPCChannelService`);var G=class{constructor(e){I(this,`_client`,void 0),I(this,`_server`,void 0),this._client=new H(e),this._server=new U(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 l{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,I(this,`_remoteInstanceService`,void 0),I(this,`_syncingUnits`,new Set),I(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),p(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(D,L(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>R(this._rpcChannelService.requestChannel(A))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:u.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};K=E([T(0,o(s)),T(1,n),T(2,a),T(3,W),T(4,O)],K);let q=class extends t{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,I(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(A,L(this._remoteInstanceService)),this._injector.add([O,{useFactory:()=>R(this._rpcChannelService.requestChannel(D))}]),this._remoteSyncService=this._injector.get(O)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};q=E([T(0,o(s)),T(1,j),T(2,n),T(3,W)],q);var J=`@univerjs/rpc`,Y=`0.19.0`;function X(){return{send(e){postMessage(e)},onMessage:new _(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe(b(1))}}function Z(e){return{send(t){e.postMessage(t)},onMessage:new _(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe(b(1))}}let Q=class extends c{constructor(e=S,t,n){super(),this._config=e,this._injector=t,this._configService=n,I(this,`_internalWorker`,null);let{...r}=f({},S,this._config);this._configService.setConfig(x,r)}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=Z(t);[[W,{useFactory:()=>new G(n)}],[K],[O,{useClass:k}]].forEach(e=>this._injector.add(e)),this._injector.get(K)}};I(Q,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),I(Q,`packageName`,J),I(Q,`version`,Y),Q=E([T(1,o(s)),T(2,r)],Q);let $=class extends c{constructor(e=w,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=f({},w,this._config);this._configService.setConfig(C,r)}onStarting(){[[q],[W,{useFactory:()=>new G(X())}],[j,{useClass:M}]].forEach(e=>this._injector.add(e)),this._injector.get(q)}};I($,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),I($,`packageName`,J),I($,`version`,Y),$=E([T(1,o(s)),T(2,r)],$);export{H as ChannelClient,U as ChannelServer,G as ChannelService,K as DataSyncPrimaryController,q as DataSyncReplicaController,W as IRPCChannelService,j as IRemoteInstanceService,O as IRemoteSyncService,x as PLUGIN_CONFIG_KEY_MAIN_THREAD,C as PLUGIN_CONFIG_KEY_WORKER_THREAD,A as RemoteInstanceServiceName,k as RemoteSyncPrimaryService,D as RemoteSyncServiceName,Q as UniverRPCMainThreadPlugin,$ as UniverRPCWorkerThreadPlugin,M as WebWorkerRemoteInstanceService,L as fromModule,R as toModule};
|
|
1
|
+
import{CommandType as e,Disposable as t,ICommandService as n,IConfigService as r,ILogService as i,IUniverInstanceService as a,Inject as o,Injector as s,Plugin as c,RxDisposable as l,UniverInstanceType as u,createIdentifier as d,merge as f,toDisposable as p}from"@univerjs/core";import{filter as m,take as ee,takeUntil as h}from"rxjs/operators";import{BehaviorSubject as g,Observable as _,firstValueFrom as v,isObservable as te,of as y,shareReplay as b}from"rxjs";const x=`rpc.main-thread.config`;Symbol(x);const S={},C=`rpc.worker-thread.config`;Symbol(C);const w={};function T(e,t){return function(n,r){t(n,r,e)}}function E(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}const D=`rpc.remote-sync.service`,O=d(D);let k=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})}};k=E([T(0,n)],k);const A=`univer.remote-instance-service`,j=d(A);let M=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:t,snapshot:n}=e;try{switch(t){case u.UNIVER_SHEET:return this._univerInstanceService.createUnit(u.UNIVER_SHEET,n),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`)}}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})}};M=E([T(0,a),T(1,n),T(2,i)],M);function N(e){"@babel/helpers - typeof";return N=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},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(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 r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return te(e)?e:y(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function R(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return z(n)?e.subscribe(n,t):e.call(n,t)}}})}function z(e){return e.endsWith(`$`)}var B=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}(B||{}),V=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}(V||{}),H=class extends l{constructor(e){super(),this._protocol=e,I(this,`_initialized`,new g(!1)),I(this,`_lastRequestCounter`,0),I(this,`_pendingRequests`,new Map),this._protocol.send({type:B.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe(h(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 v(this._initialized.pipe(m(e=>e),ee(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:B.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 V.CALL_SUCCESS:a.delete(r),e(n.data);break;case V.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=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=B.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case V.SUBSCRIBE_NEXT:r.next(e.data);break;case V.SUBSCRIBE_ERROR:r.error(e.data);break;case V.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:B.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 V.INITIALIZE:this._initialized.next(!0);break;case V.CALL_SUCCESS:case V.CALL_FAILURE:case V.SUBSCRIBE_NEXT:case V.SUBSCRIBE_COMPLETE:case V.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==V.SUBSCRIBE_NEXT&&i.delete(n);break}}}},U=class extends l{constructor(e){super(),this._protocol=e,I(this,`_channels`,new Map),I(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe(h(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 B.REQUEST_INITIALIZATION:this._sendInitialize();break;case B.CALL:this._onMethodCall(e);break;case B.SUBSCRIBE:this._onSubscribe(e);break;case B.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:V.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:V.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:V.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:V.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:V.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:V.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:V.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)}};const W=d(`IRPCChannelService`);var G=class{constructor(e){I(this,`_client`,void 0),I(this,`_server`,void 0),this._client=new H(e),this._server=new U(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 l{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,I(this,`_remoteInstanceService`,void 0),I(this,`_syncingUnits`,new Set),I(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),p(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(D,L(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>R(this._rpcChannelService.requestChannel(A))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:u.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};K=E([T(0,o(s)),T(1,n),T(2,a),T(3,W),T(4,O)],K);let q=class extends t{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,I(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(A,L(this._remoteInstanceService)),this._injector.add([O,{useFactory:()=>R(this._rpcChannelService.requestChannel(D))}]),this._remoteSyncService=this._injector.get(O)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};q=E([T(0,o(s)),T(1,j),T(2,n),T(3,W)],q);var J=`@univerjs/rpc`,Y=`0.20.0`;function X(){return{send(e){postMessage(e)},onMessage:new _(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe(b(1))}}function Z(e){return{send(t){e.postMessage(t)},onMessage:new _(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe(b(1))}}let Q=class extends c{constructor(e=S,t,n){super(),this._config=e,this._injector=t,this._configService=n,I(this,`_internalWorker`,null);let{...r}=f({},S,this._config);this._configService.setConfig(x,r)}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=Z(t);[[W,{useFactory:()=>new G(n)}],[K],[O,{useClass:k}]].forEach(e=>this._injector.add(e)),this._injector.get(K)}};I(Q,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),I(Q,`packageName`,J),I(Q,`version`,Y),Q=E([T(1,o(s)),T(2,r)],Q);let $=class extends c{constructor(e=w,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=f({},w,this._config);this._configService.setConfig(C,r)}onStarting(){[[q],[W,{useFactory:()=>new G(X())}],[j,{useClass:M}]].forEach(e=>this._injector.add(e)),this._injector.get(q)}};I($,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),I($,`packageName`,J),I($,`version`,Y),$=E([T(1,o(s)),T(2,r)],$);export{H as ChannelClient,U as ChannelServer,G as ChannelService,K as DataSyncPrimaryController,q as DataSyncReplicaController,W as IRPCChannelService,j as IRemoteInstanceService,O as IRemoteSyncService,x as PLUGIN_CONFIG_KEY_MAIN_THREAD,C as PLUGIN_CONFIG_KEY_WORKER_THREAD,A as RemoteInstanceServiceName,k as RemoteSyncPrimaryService,D as RemoteSyncServiceName,Q as UniverRPCMainThreadPlugin,$ as UniverRPCWorkerThreadPlugin,M as WebWorkerRemoteInstanceService,L as fromModule,R as toModule};
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{CommandType as e,Disposable as t,ICommandService as n,IConfigService as r,ILogService as i,IUniverInstanceService as a,Inject as o,Injector as s,Plugin as c,RxDisposable as l,UniverInstanceType as u,createIdentifier as d,merge as f,toDisposable as p}from"@univerjs/core";import{filter as m,take as ee,takeUntil as h}from"rxjs/operators";import{BehaviorSubject as g,Observable as _,firstValueFrom as v,isObservable as te,of as y,shareReplay as b}from"rxjs";const x=`rpc.main-thread.config`;Symbol(x);const S={},C=`rpc.worker-thread.config`;Symbol(C);const w={};function T(e,t){return function(n,r){t(n,r,e)}}function E(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}const D=`rpc.remote-sync.service`,O=d(D);let k=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})}};k=E([T(0,n)],k);const A=`univer.remote-instance-service`,j=d(A);let M=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:t,snapshot:n}=e;try{switch(t){case u.UNIVER_SHEET:return this._univerInstanceService.createUnit(u.UNIVER_SHEET,n),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`)}}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})}};M=E([T(0,a),T(1,n),T(2,i)],M);function N(e){"@babel/helpers - typeof";return N=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},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(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 r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return te(e)?e:y(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function R(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return z(n)?e.subscribe(n,t):e.call(n,t)}}})}function z(e){return e.endsWith(`$`)}var B=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}(B||{}),V=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}(V||{}),H=class extends l{constructor(e){super(),this._protocol=e,I(this,`_initialized`,new g(!1)),I(this,`_lastRequestCounter`,0),I(this,`_pendingRequests`,new Map),this._protocol.send({type:B.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe(h(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 v(this._initialized.pipe(m(e=>e),ee(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:B.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 V.CALL_SUCCESS:a.delete(r),e(n.data);break;case V.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=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=B.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case V.SUBSCRIBE_NEXT:r.next(e.data);break;case V.SUBSCRIBE_ERROR:r.error(e.data);break;case V.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:B.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 V.INITIALIZE:this._initialized.next(!0);break;case V.CALL_SUCCESS:case V.CALL_FAILURE:case V.SUBSCRIBE_NEXT:case V.SUBSCRIBE_COMPLETE:case V.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==V.SUBSCRIBE_NEXT&&i.delete(n);break}}}},U=class extends l{constructor(e){super(),this._protocol=e,I(this,`_channels`,new Map),I(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe(h(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 B.REQUEST_INITIALIZATION:this._sendInitialize();break;case B.CALL:this._onMethodCall(e);break;case B.SUBSCRIBE:this._onSubscribe(e);break;case B.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:V.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:V.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:V.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:V.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:V.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:V.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:V.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)}};const W=d(`IRPCChannelService`);var G=class{constructor(e){I(this,`_client`,void 0),I(this,`_server`,void 0),this._client=new H(e),this._server=new U(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 l{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,I(this,`_remoteInstanceService`,void 0),I(this,`_syncingUnits`,new Set),I(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),p(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(D,L(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>R(this._rpcChannelService.requestChannel(A))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:u.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};K=E([T(0,o(s)),T(1,n),T(2,a),T(3,W),T(4,O)],K);let q=class extends t{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,I(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(A,L(this._remoteInstanceService)),this._injector.add([O,{useFactory:()=>R(this._rpcChannelService.requestChannel(D))}]),this._remoteSyncService=this._injector.get(O)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};q=E([T(0,o(s)),T(1,j),T(2,n),T(3,W)],q);var J=`@univerjs/rpc`,Y=`0.19.0`;function X(){return{send(e){postMessage(e)},onMessage:new _(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe(b(1))}}function Z(e){return{send(t){e.postMessage(t)},onMessage:new _(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe(b(1))}}let Q=class extends c{constructor(e=S,t,n){super(),this._config=e,this._injector=t,this._configService=n,I(this,`_internalWorker`,null);let{...r}=f({},S,this._config);this._configService.setConfig(x,r)}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=Z(t);[[W,{useFactory:()=>new G(n)}],[K],[O,{useClass:k}]].forEach(e=>this._injector.add(e)),this._injector.get(K)}};I(Q,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),I(Q,`packageName`,J),I(Q,`version`,Y),Q=E([T(1,o(s)),T(2,r)],Q);let $=class extends c{constructor(e=w,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=f({},w,this._config);this._configService.setConfig(C,r)}onStarting(){[[q],[W,{useFactory:()=>new G(X())}],[j,{useClass:M}]].forEach(e=>this._injector.add(e)),this._injector.get(q)}};I($,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),I($,`packageName`,J),I($,`version`,Y),$=E([T(1,o(s)),T(2,r)],$);export{H as ChannelClient,U as ChannelServer,G as ChannelService,K as DataSyncPrimaryController,q as DataSyncReplicaController,W as IRPCChannelService,j as IRemoteInstanceService,O as IRemoteSyncService,x as PLUGIN_CONFIG_KEY_MAIN_THREAD,C as PLUGIN_CONFIG_KEY_WORKER_THREAD,A as RemoteInstanceServiceName,k as RemoteSyncPrimaryService,D as RemoteSyncServiceName,Q as UniverRPCMainThreadPlugin,$ as UniverRPCWorkerThreadPlugin,M as WebWorkerRemoteInstanceService,L as fromModule,R as toModule};
|
|
1
|
+
import{CommandType as e,Disposable as t,ICommandService as n,IConfigService as r,ILogService as i,IUniverInstanceService as a,Inject as o,Injector as s,Plugin as c,RxDisposable as l,UniverInstanceType as u,createIdentifier as d,merge as f,toDisposable as p}from"@univerjs/core";import{filter as m,take as ee,takeUntil as h}from"rxjs/operators";import{BehaviorSubject as g,Observable as _,firstValueFrom as v,isObservable as te,of as y,shareReplay as b}from"rxjs";const x=`rpc.main-thread.config`;Symbol(x);const S={},C=`rpc.worker-thread.config`;Symbol(C);const w={};function T(e,t){return function(n,r){t(n,r,e)}}function E(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}const D=`rpc.remote-sync.service`,O=d(D);let k=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})}};k=E([T(0,n)],k);const A=`univer.remote-instance-service`,j=d(A);let M=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:t,snapshot:n}=e;try{switch(t){case u.UNIVER_SHEET:return this._univerInstanceService.createUnit(u.UNIVER_SHEET,n),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${t}.`)}}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})}};M=E([T(0,a),T(1,n),T(2,i)],M);function N(e){"@babel/helpers - typeof";return N=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},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(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 r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return te(e)?e:y(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function R(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return z(n)?e.subscribe(n,t):e.call(n,t)}}})}function z(e){return e.endsWith(`$`)}var B=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}(B||{}),V=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}(V||{}),H=class extends l{constructor(e){super(),this._protocol=e,I(this,`_initialized`,new g(!1)),I(this,`_lastRequestCounter`,0),I(this,`_pendingRequests`,new Map),this._protocol.send({type:B.REQUEST_INITIALIZATION}),this._protocol.onMessage.pipe(h(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 v(this._initialized.pipe(m(e=>e),ee(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:B.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 V.CALL_SUCCESS:a.delete(r),e(n.data);break;case V.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=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a=B.SUBSCRIBE,o={seq:i,type:a,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case V.SUBSCRIBE_NEXT:r.next(e.data);break;case V.SUBSCRIBE_ERROR:r.error(e.data);break;case V.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:B.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 V.INITIALIZE:this._initialized.next(!0);break;case V.CALL_SUCCESS:case V.CALL_FAILURE:case V.SUBSCRIBE_NEXT:case V.SUBSCRIBE_COMPLETE:case V.SUBSCRIBE_ERROR:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==V.SUBSCRIBE_NEXT&&i.delete(n);break}}}},U=class extends l{constructor(e){super(),this._protocol=e,I(this,`_channels`,new Map),I(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe(h(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 B.REQUEST_INITIALIZATION:this._sendInitialize();break;case B.CALL:this._onMethodCall(e);break;case B.SUBSCRIBE:this._onSubscribe(e);break;case B.UNSUBSCRIBE:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:V.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:V.CALL_SUCCESS,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:V.CALL_FAILURE,data:t.message}):this._sendResponse({seq:e.seq,type:V.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:V.SUBSCRIBE_NEXT,data:e})},error:e=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_ERROR,data:e.message}),this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})},complete:()=>{this._sendResponse({seq:n,type:V.SUBSCRIBE_COMPLETE})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:V.SUBSCRIBE_ERROR,data:t.message}):this._sendResponse({seq:e.seq,type:V.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)}};const W=d(`IRPCChannelService`);var G=class{constructor(e){I(this,`_client`,void 0),I(this,`_server`,void 0),this._client=new H(e),this._server=new U(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 l{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,I(this,`_remoteInstanceService`,void 0),I(this,`_syncingUnits`,new Set),I(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),p(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(D,L(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>R(this._rpcChannelService.requestChannel(A))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:u.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(u.UNIVER_SHEET).pipe(h(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{let{type:r,params:i,id:a}=t,o=(i==null?void 0:i.unitId)||``;r===e.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:t},n)}))}};K=E([T(0,o(s)),T(1,n),T(2,a),T(3,W),T(4,O)],K);let q=class extends t{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,I(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(A,L(this._remoteInstanceService)),this._injector.add([O,{useFactory:()=>R(this._rpcChannelService.requestChannel(D))}]),this._remoteSyncService=this._injector.get(O)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{t.type===e.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:t},n)}))}};q=E([T(0,o(s)),T(1,j),T(2,n),T(3,W)],q);var J=`@univerjs/rpc`,Y=`0.20.0`;function X(){return{send(e){postMessage(e)},onMessage:new _(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe(b(1))}}function Z(e){return{send(t){e.postMessage(t)},onMessage:new _(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe(b(1))}}let Q=class extends c{constructor(e=S,t,n){super(),this._config=e,this._injector=t,this._configService=n,I(this,`_internalWorker`,null);let{...r}=f({},S,this._config);this._configService.setConfig(x,r)}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=Z(t);[[W,{useFactory:()=>new G(n)}],[K],[O,{useClass:k}]].forEach(e=>this._injector.add(e)),this._injector.get(K)}};I(Q,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),I(Q,`packageName`,J),I(Q,`version`,Y),Q=E([T(1,o(s)),T(2,r)],Q);let $=class extends c{constructor(e=w,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=f({},w,this._config);this._configService.setConfig(C,r)}onStarting(){[[q],[W,{useFactory:()=>new G(X())}],[j,{useClass:M}]].forEach(e=>this._injector.add(e)),this._injector.get(q)}};I($,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),I($,`packageName`,J),I($,`version`,Y),$=E([T(1,o(s)),T(2,r)],$);export{H as ChannelClient,U as ChannelServer,G as ChannelService,K as DataSyncPrimaryController,q as DataSyncReplicaController,W as IRPCChannelService,j as IRemoteInstanceService,O as IRemoteSyncService,x as PLUGIN_CONFIG_KEY_MAIN_THREAD,C as PLUGIN_CONFIG_KEY_WORKER_THREAD,A as RemoteInstanceServiceName,k as RemoteSyncPrimaryService,D as RemoteSyncServiceName,Q as UniverRPCMainThreadPlugin,$ as UniverRPCWorkerThreadPlugin,M as WebWorkerRemoteInstanceService,L as fromModule,R as toModule};
|
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.19.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});
|
|
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.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.
|
|
3
|
+
"version": "0.20.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.
|
|
50
|
+
"@univerjs/core": "0.20.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"rxjs": "^7.8.2",
|
|
54
54
|
"typescript": "^6.0.2",
|
|
55
55
|
"vitest": "^4.1.1",
|
|
56
|
-
"@univerjs-infra/shared": "0.
|
|
56
|
+
"@univerjs-infra/shared": "0.20.0"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"test": "vitest run",
|