@univerjs/rpc 0.12.4 → 0.13.0-insiders.20251216-1519cb7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var N=Object.defineProperty;var $=(i,t,e)=>t in i?N(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>$(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs");var x=Object.getOwnPropertyDescriptor,q=(i,t,e,n)=>{for(var s=n>1?void 0:n?x(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},g=(i,t)=>(e,n)=>t(e,n,i);const R="rpc.remote-sync.service",v=c.createIdentifier(R);exports.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=q([g(0,c.ICommandService)],exports.RemoteSyncPrimaryService);const f="univer.remote-instance-service",m=c.createIdentifier(f);exports.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case c.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(c.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=q([g(0,c.IUniverInstanceService),g(1,c.ICommandService),g(2,c.ILogService)],exports.WebWorkerRemoteInstanceService);function U(i){const t=i;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let r=n?s.apply(t,n):s.call(t);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const r=n?s.apply(t,n):s.call(t);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function E(i){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return L(e)?i.subscribe(e,n):i.call(e,n)}}})}function L(i){return i.endsWith("$")}class W extends c.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,r){return n._disposed?Promise.reject():n._remoteCall(e,s,r)},subscribe(s,r){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:n,args:s},o=this;return new Promise((I,d)=>{const O={handle(b){switch(b.type){case 201:o._pendingRequests.delete(r),I(b.data);break;case 202:o._pendingRequests.delete(r),d(b.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,O),this._sendRequest(h)})}_remoteSubscribe(e,n,s){return new _.Observable(r=>{let a=-1;return this._whenReady().then(()=>{a=++this._lastRequestCounter;const o={seq:a,type:101,channelName:e,method:n,args:s},I={handle(d){switch(d.type){case 300:r.next(d.data);break;case 301:r.error(d.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(a,I),this._sendRequest(o)}),()=>{if(a===-1)return;const h={type:102,seq:a,channelName:e,method:n};this._sendRequest(h)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class D extends c.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:r}=e,a=this._channels.get(n);let h;try{if(!a)throw new Error(`[ChannelServer]: Channel ${n} not found!`);h=r?a.call(s,r):a.call(s)}catch(o){h=Promise.reject(o)}h.then(o=>{this._sendResponse({seq:e.seq,type:201,data:o})}).catch(o=>{o instanceof Error?this._sendResponse({seq:e.seq,type:202,data:o.message}):this._sendResponse({seq:e.seq,type:202,data:String(o)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:s,type:300,data:o})},error:o=>{this._sendResponse({seq:s,type:301,data:o.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}catch(a){a instanceof Error?this._sendResponse({seq:e.seq,type:301,data:a.message}):this._sendResponse({seq:e.seq,type:301,data:String(a)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const y=c.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new W(t),this._server=new D(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var H=Object.getOwnPropertyDescriptor,A=(i,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends c.RxDisposable{constructor(e,n,s,r,a){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=a,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),c.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,U(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>E(this._rpcChannelService.requestChannel(f))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:c.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(c.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:r,id:a}=e,h=(r==null?void 0:r.unitId)||"";s===c.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=A([p(0,c.Inject(c.Injector)),p(1,c.ICommandService),p(2,c.IUniverInstanceService),p(3,y),p(4,v)],exports.DataSyncPrimaryController);var V=Object.getOwnPropertyDescriptor,F=(i,t,e,n)=>{for(var s=n>1?void 0:n?V(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},S=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends c.Disposable{constructor(e,n,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(f,U(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>E(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===c.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=F([S(0,c.Inject(c.Injector)),S(1,m),S(2,c.ICommandService),S(3,y)],exports.DataSyncReplicaController);const z="rpc.main-thread.config",k={},G="rpc.worker-thread.config",T={};function Y(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const t=e=>{i.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(_.shareReplay(1))}}function B(i){return{send(t){i.postMessage(t)},onMessage:new _.Observable(t=>{const e=n=>{t.next(n.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var K=Object.getOwnPropertyDescriptor,j=(i,t,e,n)=>{for(var s=n>1?void 0:n?K(t,e):t,r=i.length-1,a;r>=0;r--)(a=i[r])&&(s=a(s)||s);return s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends c.Plugin{constructor(e=k,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=c.merge({},k,this._config);this._configService.setConfig(z,r)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=B(n);[[y,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(a=>this._injector.add(a)),this._injector.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=j([C(1,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends c.Plugin{constructor(t=T,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=c.merge({},T,this._config);this._configService.setConfig(G,s)}onStarting(){[[exports.DataSyncReplicaController],[y,{useFactory:()=>new M(Y())}],[m,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(exports.DataSyncReplicaController)}},l(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w);exports.UniverRPCWorkerThreadPlugin=j([C(1,c.Inject(c.Injector)),C(2,c.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=W;exports.ChannelServer=D;exports.ChannelService=M;exports.IRPCChannelService=y;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.RemoteInstanceServiceName=f;exports.RemoteSyncServiceName=R;exports.fromModule=U;exports.toModule=E;
1
+ "use strict";var x=Object.defineProperty;var L=(i,t,e)=>t in i?x(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var l=(i,t,e)=>L(i,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@univerjs/core"),u=require("rxjs/operators"),_=require("rxjs"),k="rpc.main-thread.config",W={},D="rpc.worker-thread.config",T={};var H=Object.getOwnPropertyDescriptor,q=(i,t,e,n)=>{for(var s=n>1?void 0:n?H(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},g=(i,t)=>(e,n)=>t(e,n,i);const R="rpc.remote-sync.service",v=o.createIdentifier(R);exports.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}};exports.RemoteSyncPrimaryService=q([g(0,o.ICommandService)],exports.RemoteSyncPrimaryService);const I="univer.remote-instance-service",m=o.createIdentifier(I);exports.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);const{type:e,snapshot:n}=t;try{switch(e){case o.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(o.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${t.unitID}`),this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}};exports.WebWorkerRemoteInstanceService=q([g(0,o.IUniverInstanceService),g(1,o.ICommandService),g(2,o.ILogService)],exports.WebWorkerRemoteInstanceService);function E(i){const t=i;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let r=n?s.apply(t,n):s.call(t);return r instanceof Promise||(r=Promise.resolve(r)),r}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const r=n?s.apply(t,n):s.call(t);return _.isObservable(r)?r:_.of(r)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function U(i){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return A(e)?i.subscribe(e,n):i.call(e,n)}}})}function A(i){return i.endsWith("$")}class O extends o.RxDisposable{constructor(e){super();l(this,"_initialized",new _.BehaviorSubject(!1));l(this,"_lastRequestCounter",0);l(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,r){return n._disposed?Promise.reject():n._remoteCall(e,s,r)},subscribe(s,r){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,r)}}}_whenReady(){return _.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const r=++this._lastRequestCounter,h={seq:r,type:100,channelName:e,method:n,args:s},a=this;return new Promise((f,d)=>{const $={handle(b){switch(b.type){case 201:a._pendingRequests.delete(r),f(b.data);break;case 202:a._pendingRequests.delete(r),d(b.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(r,$),this._sendRequest(h)})}_remoteSubscribe(e,n,s){return new _.Observable(r=>{let c=-1;return this._whenReady().then(()=>{c=++this._lastRequestCounter;const a={seq:c,type:101,channelName:e,method:n,args:s},f={handle(d){switch(d.type){case 300:r.next(d.data);break;case 301:r.error(d.data);break;case 302:r.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(c,f),this._sendRequest(a)}),()=>{if(c===-1)return;const h={type:102,seq:c,channelName:e,method:n};this._sendRequest(h)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var r;const{type:n,seq:s}=e;switch(n){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:c}=this;(r=c.get(s))==null||r.handle(e),n!==300&&c.delete(s);break}}}}class j extends o.RxDisposable{constructor(e){super();l(this,"_channels",new Map);l(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:r}=e,c=this._channels.get(n);let h;try{if(!c)throw new Error(`[ChannelServer]: Channel ${n} not found!`);h=r?c.call(s,r):c.call(s)}catch(a){h=Promise.reject(a)}h.then(a=>{this._sendResponse({seq:e.seq,type:201,data:a})}).catch(a=>{a instanceof Error?this._sendResponse({seq:e.seq,type:202,data:a.message}):this._sendResponse({seq:e.seq,type:202,data:String(a)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,r=this._channels.get(n);try{if(!r)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const h=r.subscribe(e.method,e.args).subscribe({next:a=>{this._sendResponse({seq:s,type:300,data:a})},error:a=>{this._sendResponse({seq:s,type:301,data:a.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,h)}catch(c){c instanceof Error?this._sendResponse({seq:e.seq,type:301,data:c.message}):this._sendResponse({seq:e.seq,type:301,data:String(c)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const S=o.createIdentifier("IRPCChannelService");class M{constructor(t){l(this,"_client");l(this,"_server");this._client=new O(t),this._server=new j(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var G=Object.getOwnPropertyDescriptor,F=(i,t,e,n)=>{for(var s=n>1?void 0:n?G(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},p=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncPrimaryController=class extends o.RxDisposable{constructor(e,n,s,r,c){super();l(this,"_remoteInstanceService");l(this,"_syncingUnits",new Set);l(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=r,this._remoteSyncService=c,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),o.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(R,E(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>U(this._rpcChannelService.requestChannel(I))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:o.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(o.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:r,id:c}=e,h=(r==null?void 0:r.unitId)||"";s===o.CommandType.MUTATION&&(!h||this._syncingUnits.has(h))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(c)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}};exports.DataSyncPrimaryController=F([p(0,o.Inject(o.Injector)),p(1,o.ICommandService),p(2,o.IUniverInstanceService),p(3,S),p(4,v)],exports.DataSyncPrimaryController);var V=Object.getOwnPropertyDescriptor,z=(i,t,e,n)=>{for(var s=n>1?void 0:n?V(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},y=(i,t)=>(e,n)=>t(e,n,i);exports.DataSyncReplicaController=class extends o.Disposable{constructor(e,n,s,r){super();l(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=r,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(I,E(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>U(this._rpcChannelService.requestChannel(R))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===o.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}};exports.DataSyncReplicaController=z([y(0,o.Inject(o.Injector)),y(1,m),y(2,o.ICommandService),y(3,S)],exports.DataSyncReplicaController);function K(){return{send(i){postMessage(i)},onMessage:new _.Observable(i=>{const t=e=>{i.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(_.shareReplay(1))}}function Y(i){return{send(t){i.postMessage(t)},onMessage:new _.Observable(t=>{const e=n=>{t.next(n.data)};return i.addEventListener("message",e),()=>i.removeEventListener("message",e)}).pipe(_.shareReplay(1))}}var B=Object.getOwnPropertyDescriptor,N=(i,t,e,n)=>{for(var s=n>1?void 0:n?B(t,e):t,r=i.length-1,c;r>=0;r--)(c=i[r])&&(s=c(s)||s);return s},C=(i,t)=>(e,n)=>t(e,n,i),P;exports.UniverRPCMainThreadPlugin=(P=class extends o.Plugin{constructor(e=W,n,s){super();l(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...r}=o.merge({},W,this._config);this._configService.setConfig(k,r)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=Y(n);[[S,{useFactory:()=>new M(s)}],[exports.DataSyncPrimaryController],[v,{useClass:exports.RemoteSyncPrimaryService}]].forEach(c=>this._injector.add(c)),this._injector.get(exports.DataSyncPrimaryController)}},l(P,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),P);exports.UniverRPCMainThreadPlugin=N([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCMainThreadPlugin);var w;exports.UniverRPCWorkerThreadPlugin=(w=class extends o.Plugin{constructor(t=T,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=o.merge({},T,this._config);this._configService.setConfig(D,s)}onStarting(){[[exports.DataSyncReplicaController],[S,{useFactory:()=>new M(K())}],[m,{useClass:exports.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(exports.DataSyncReplicaController)}},l(w,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),w);exports.UniverRPCWorkerThreadPlugin=N([C(1,o.Inject(o.Injector)),C(2,o.IConfigService)],exports.UniverRPCWorkerThreadPlugin);exports.ChannelClient=O;exports.ChannelServer=j;exports.ChannelService=M;exports.IRPCChannelService=S;exports.IRemoteInstanceService=m;exports.IRemoteSyncService=v;exports.PLUGIN_CONFIG_KEY_MAIN_THREAD=k;exports.PLUGIN_CONFIG_KEY_WORKER_THREAD=D;exports.RemoteInstanceServiceName=I;exports.RemoteSyncServiceName=R;exports.fromModule=E;exports.toModule=U;
package/lib/es/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  var X = Object.defineProperty;
2
2
  var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
3
  var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { createIdentifier as k, ICommandService as C, UniverInstanceType as u, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
4
+ import { createIdentifier as k, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as L, ILogService as ee, RxDisposable as W, Inject as C, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
5
5
  import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
6
- import { BehaviorSubject as ie, firstValueFrom as ce, Observable as T, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
- var he = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
8
- for (var r = n > 1 ? void 0 : n ? he(t, e) : t, i = s.length - 1, c; i >= 0; i--)
6
+ import { BehaviorSubject as ie, firstValueFrom as ce, Observable as O, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
+ const he = "rpc.main-thread.config", $ = {}, le = "rpc.worker-thread.config", j = {};
8
+ var _e = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
9
+ for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, c; i >= 0; i--)
9
10
  (c = s[i]) && (r = c(r) || r);
10
11
  return r;
11
12
  }, p = (s, t) => (e, n) => t(e, n, s);
12
- const W = "rpc.remote-sync.service", m = k(W);
13
+ const T = "rpc.remote-sync.service", g = k(T);
13
14
  let M = class {
14
15
  constructor(s) {
15
16
  this._commandService = s;
@@ -22,9 +23,9 @@ let M = class {
22
23
  }
23
24
  };
24
25
  M = G([
25
- p(0, C)
26
+ p(0, b)
26
27
  ], M);
27
- const $ = "univer.remote-instance-service", g = k($);
28
+ const D = "univer.remote-instance-service", m = k(D);
28
29
  let q = class {
29
30
  constructor(s, t, e) {
30
31
  this._univerInstanceService = s, this._commandService = t, this._logService = e;
@@ -36,6 +37,7 @@ let q = class {
36
37
  return this._applyMutation(s.mutationInfo);
37
38
  }
38
39
  async createInstance(s) {
40
+ this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${s.unitID}`);
39
41
  const { type: t, snapshot: e } = s;
40
42
  try {
41
43
  switch (t) {
@@ -51,7 +53,7 @@ let q = class {
51
53
  }
52
54
  }
53
55
  async disposeInstance(s) {
54
- return this._univerInstanceService.disposeUnit(s.unitID);
56
+ return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${s.unitID}`), this._univerInstanceService.disposeUnit(s.unitID);
55
57
  }
56
58
  _applyMutation(s) {
57
59
  const { id: t, params: e } = s;
@@ -63,7 +65,7 @@ let q = class {
63
65
  };
64
66
  q = G([
65
67
  p(0, L),
66
- p(1, C),
68
+ p(1, b),
67
69
  p(2, ee)
68
70
  ], q);
69
71
  function Y(s) {
@@ -92,15 +94,15 @@ function B(s) {
92
94
  get(t, e) {
93
95
  if (e !== "dispose")
94
96
  return function(...n) {
95
- return le(e) ? s.subscribe(e, n) : s.call(e, n);
97
+ return ue(e) ? s.subscribe(e, n) : s.call(e, n);
96
98
  };
97
99
  }
98
100
  });
99
101
  }
100
- function le(s) {
102
+ function ue(s) {
101
103
  return s.endsWith("$");
102
104
  }
103
- class _e extends O {
105
+ class de extends W {
104
106
  constructor(e) {
105
107
  super();
106
108
  a(this, "_initialized", new ie(!1));
@@ -157,7 +159,7 @@ class _e extends O {
157
159
  });
158
160
  }
159
161
  _remoteSubscribe(e, n, r) {
160
- return new T((i) => {
162
+ return new O((i) => {
161
163
  let c = -1;
162
164
  return this._whenReady().then(() => {
163
165
  c = ++this._lastRequestCounter;
@@ -196,8 +198,9 @@ class _e extends O {
196
198
  this._protocol.send(e);
197
199
  }
198
200
  _onMessage(e) {
199
- var n;
200
- switch (e.type) {
201
+ var i;
202
+ const { type: n, seq: r } = e;
203
+ switch (n) {
201
204
  case 0:
202
205
  this._initialized.next(!0);
203
206
  break;
@@ -205,13 +208,15 @@ class _e extends O {
205
208
  case 202:
206
209
  case 300:
207
210
  case 302:
208
- case 301:
209
- (n = this._pendingRequests.get(e.seq)) == null || n.handle(e);
211
+ case 301: {
212
+ const { _pendingRequests: c } = this;
213
+ (i = c.get(r)) == null || i.handle(e), n !== 300 && c.delete(r);
210
214
  break;
215
+ }
211
216
  }
212
217
  }
213
218
  }
214
- class ue extends O {
219
+ class pe extends W {
215
220
  constructor(e) {
216
221
  super();
217
222
  a(this, "_channels", /* @__PURE__ */ new Map());
@@ -305,7 +310,7 @@ class K {
305
310
  constructor(t) {
306
311
  a(this, "_client");
307
312
  a(this, "_server");
308
- this._client = new _e(t), this._server = new ue(t);
313
+ this._client = new de(t), this._server = new pe(t);
309
314
  }
310
315
  dispose() {
311
316
  this._client.dispose(), this._server.dispose();
@@ -317,12 +322,12 @@ class K {
317
322
  this._server.registerChannel(t, e);
318
323
  }
319
324
  }
320
- var de = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => {
321
- for (var r = n > 1 ? void 0 : n ? de(t, e) : t, i = s.length - 1, c; i >= 0; i--)
325
+ var ve = Object.getOwnPropertyDescriptor, ge = (s, t, e, n) => {
326
+ for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
322
327
  (c = s[i]) && (r = c(r) || r);
323
328
  return r;
324
329
  }, _ = (s, t) => (e, n) => t(e, n, s);
325
- let y = class extends O {
330
+ let y = class extends W {
326
331
  constructor(t, e, n, r, i) {
327
332
  super();
328
333
  a(this, "_remoteInstanceService");
@@ -341,10 +346,10 @@ let y = class extends O {
341
346
  return this._syncingUnits.add(t), te(() => this._syncingUnits.delete(t));
342
347
  }
343
348
  _initRPCChannels() {
344
- this._rpcChannelService.registerChannel(W, Y(this._remoteSyncService)), this._injector.add([
345
- g,
346
- { useFactory: () => B(this._rpcChannelService.requestChannel($)) }
347
- ]), this._remoteInstanceService = this._injector.get(g);
349
+ this._rpcChannelService.registerChannel(T, Y(this._remoteSyncService)), this._injector.add([
350
+ m,
351
+ { useFactory: () => B(this._rpcChannelService.requestChannel(D)) }
352
+ ]), this._remoteInstanceService = this._injector.get(m);
348
353
  }
349
354
  _init() {
350
355
  this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(v(this.dispose$)).subscribe((t) => {
@@ -366,15 +371,15 @@ let y = class extends O {
366
371
  }));
367
372
  }
368
373
  };
369
- y = pe([
370
- _(0, b(R)),
371
- _(1, C),
374
+ y = ge([
375
+ _(0, C(R)),
376
+ _(1, b),
372
377
  _(2, L),
373
378
  _(3, I),
374
- _(4, m)
379
+ _(4, g)
375
380
  ], y);
376
- var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
377
- for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
381
+ var me = Object.getOwnPropertyDescriptor, ye = (s, t, e, n) => {
382
+ for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1, c; i >= 0; i--)
378
383
  (c = s[i]) && (r = c(r) || r);
379
384
  return r;
380
385
  }, d = (s, t) => (e, n) => t(e, n, s);
@@ -385,10 +390,10 @@ let f = class extends ne {
385
390
  this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init();
386
391
  }
387
392
  _initRPCChannels() {
388
- this._rpcChannelService.registerChannel($, Y(this._remoteInstanceService)), this._injector.add([
389
- m,
390
- { useFactory: () => B(this._rpcChannelService.requestChannel(W)) }
391
- ]), this._remoteSyncService = this._injector.get(m);
393
+ this._rpcChannelService.registerChannel(D, Y(this._remoteInstanceService)), this._injector.add([
394
+ g,
395
+ { useFactory: () => B(this._rpcChannelService.requestChannel(T)) }
396
+ ]), this._remoteSyncService = this._injector.get(g);
392
397
  }
393
398
  _init() {
394
399
  this.disposeWithMe(
@@ -401,19 +406,18 @@ let f = class extends ne {
401
406
  );
402
407
  }
403
408
  };
404
- f = me([
405
- d(0, b(R)),
406
- d(1, g),
407
- d(2, C),
409
+ f = ye([
410
+ d(0, C(R)),
411
+ d(1, m),
412
+ d(2, b),
408
413
  d(3, I)
409
414
  ], f);
410
- const ge = "rpc.main-thread.config", j = {}, ye = "rpc.worker-thread.config", D = {};
411
415
  function fe() {
412
416
  return {
413
417
  send(s) {
414
418
  postMessage(s);
415
419
  },
416
- onMessage: new T((s) => {
420
+ onMessage: new O((s) => {
417
421
  const t = (e) => {
418
422
  s.next(e.data);
419
423
  };
@@ -426,7 +430,7 @@ function Se(s) {
426
430
  send(t) {
427
431
  s.postMessage(t);
428
432
  },
429
- onMessage: new T((t) => {
433
+ onMessage: new O((t) => {
430
434
  const e = (n) => {
431
435
  t.next(n.data);
432
436
  };
@@ -434,22 +438,22 @@ function Se(s) {
434
438
  }).pipe(z(1))
435
439
  };
436
440
  }
437
- var Ce = Object.getOwnPropertyDescriptor, J = (s, t, e, n) => {
438
- for (var r = n > 1 ? void 0 : n ? Ce(t, e) : t, i = s.length - 1, c; i >= 0; i--)
441
+ var be = Object.getOwnPropertyDescriptor, J = (s, t, e, n) => {
442
+ for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, c; i >= 0; i--)
439
443
  (c = s[i]) && (r = c(r) || r);
440
444
  return r;
441
445
  }, S = (s, t) => (e, n) => t(e, n, s), P;
442
446
  let N = (P = class extends V {
443
- constructor(t = j, e, n) {
447
+ constructor(t = $, e, n) {
444
448
  super();
445
449
  a(this, "_internalWorker", null);
446
450
  this._config = t, this._injector = e, this._configService = n;
447
451
  const { ...r } = F(
448
452
  {},
449
- j,
453
+ $,
450
454
  this._config
451
455
  );
452
- this._configService.setConfig(ge, r);
456
+ this._configService.setConfig(he, r);
453
457
  }
454
458
  dispose() {
455
459
  super.dispose(), this._internalWorker && (this._internalWorker.terminate(), this._internalWorker = null);
@@ -469,24 +473,24 @@ let N = (P = class extends V {
469
473
  }
470
474
  ],
471
475
  [y],
472
- [m, { useClass: M }]
476
+ [g, { useClass: M }]
473
477
  ].forEach((i) => this._injector.add(i)), this._injector.get(y);
474
478
  }
475
479
  }, a(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P);
476
480
  N = J([
477
- S(1, b(R)),
481
+ S(1, C(R)),
478
482
  S(2, A)
479
483
  ], N);
480
484
  var U;
481
485
  let x = (U = class extends V {
482
- constructor(s = D, t, e) {
486
+ constructor(s = j, t, e) {
483
487
  super(), this._config = s, this._injector = t, this._configService = e;
484
488
  const { ...n } = F(
485
489
  {},
486
- D,
490
+ j,
487
491
  this._config
488
492
  );
489
- this._configService.setConfig(ye, n);
493
+ this._configService.setConfig(le, n);
490
494
  }
491
495
  onStarting() {
492
496
  [
@@ -497,26 +501,28 @@ let x = (U = class extends V {
497
501
  useFactory: () => new K(fe())
498
502
  }
499
503
  ],
500
- [g, { useClass: q }]
504
+ [m, { useClass: q }]
501
505
  ].forEach((s) => this._injector.add(s)), this._injector.get(f);
502
506
  }
503
507
  }, a(U, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), U);
504
508
  x = J([
505
- S(1, b(R)),
509
+ S(1, C(R)),
506
510
  S(2, A)
507
511
  ], x);
508
512
  export {
509
- _e as ChannelClient,
510
- ue as ChannelServer,
513
+ de as ChannelClient,
514
+ pe as ChannelServer,
511
515
  K as ChannelService,
512
516
  y as DataSyncPrimaryController,
513
517
  f as DataSyncReplicaController,
514
518
  I as IRPCChannelService,
515
- g as IRemoteInstanceService,
516
- m as IRemoteSyncService,
517
- $ as RemoteInstanceServiceName,
519
+ m as IRemoteInstanceService,
520
+ g as IRemoteSyncService,
521
+ he as PLUGIN_CONFIG_KEY_MAIN_THREAD,
522
+ le as PLUGIN_CONFIG_KEY_WORKER_THREAD,
523
+ D as RemoteInstanceServiceName,
518
524
  M as RemoteSyncPrimaryService,
519
- W as RemoteSyncServiceName,
525
+ T as RemoteSyncServiceName,
520
526
  N as UniverRPCMainThreadPlugin,
521
527
  x as UniverRPCWorkerThreadPlugin,
522
528
  q as WebWorkerRemoteInstanceService,
package/lib/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  var X = Object.defineProperty;
2
2
  var Z = (s, t, e) => t in s ? X(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
3
  var a = (s, t, e) => Z(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { createIdentifier as k, ICommandService as C, UniverInstanceType as u, IUniverInstanceService as L, ILogService as ee, RxDisposable as O, Inject as b, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
4
+ import { createIdentifier as k, UniverInstanceType as u, ICommandService as b, IUniverInstanceService as L, ILogService as ee, RxDisposable as W, Inject as C, Injector as R, toDisposable as te, CommandType as H, Disposable as ne, IConfigService as A, Plugin as V, merge as F } from "@univerjs/core";
5
5
  import { takeUntil as v, filter as se, take as re } from "rxjs/operators";
6
- import { BehaviorSubject as ie, firstValueFrom as ce, Observable as T, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
- var he = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
8
- for (var r = n > 1 ? void 0 : n ? he(t, e) : t, i = s.length - 1, c; i >= 0; i--)
6
+ import { BehaviorSubject as ie, firstValueFrom as ce, Observable as O, isObservable as oe, of as ae, shareReplay as z } from "rxjs";
7
+ const he = "rpc.main-thread.config", $ = {}, le = "rpc.worker-thread.config", j = {};
8
+ var _e = Object.getOwnPropertyDescriptor, G = (s, t, e, n) => {
9
+ for (var r = n > 1 ? void 0 : n ? _e(t, e) : t, i = s.length - 1, c; i >= 0; i--)
9
10
  (c = s[i]) && (r = c(r) || r);
10
11
  return r;
11
12
  }, p = (s, t) => (e, n) => t(e, n, s);
12
- const W = "rpc.remote-sync.service", m = k(W);
13
+ const T = "rpc.remote-sync.service", g = k(T);
13
14
  let M = class {
14
15
  constructor(s) {
15
16
  this._commandService = s;
@@ -22,9 +23,9 @@ let M = class {
22
23
  }
23
24
  };
24
25
  M = G([
25
- p(0, C)
26
+ p(0, b)
26
27
  ], M);
27
- const $ = "univer.remote-instance-service", g = k($);
28
+ const D = "univer.remote-instance-service", m = k(D);
28
29
  let q = class {
29
30
  constructor(s, t, e) {
30
31
  this._univerInstanceService = s, this._commandService = t, this._logService = e;
@@ -36,6 +37,7 @@ let q = class {
36
37
  return this._applyMutation(s.mutationInfo);
37
38
  }
38
39
  async createInstance(s) {
40
+ this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${s.unitID}`);
39
41
  const { type: t, snapshot: e } = s;
40
42
  try {
41
43
  switch (t) {
@@ -51,7 +53,7 @@ let q = class {
51
53
  }
52
54
  }
53
55
  async disposeInstance(s) {
54
- return this._univerInstanceService.disposeUnit(s.unitID);
56
+ return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${s.unitID}`), this._univerInstanceService.disposeUnit(s.unitID);
55
57
  }
56
58
  _applyMutation(s) {
57
59
  const { id: t, params: e } = s;
@@ -63,7 +65,7 @@ let q = class {
63
65
  };
64
66
  q = G([
65
67
  p(0, L),
66
- p(1, C),
68
+ p(1, b),
67
69
  p(2, ee)
68
70
  ], q);
69
71
  function Y(s) {
@@ -92,15 +94,15 @@ function B(s) {
92
94
  get(t, e) {
93
95
  if (e !== "dispose")
94
96
  return function(...n) {
95
- return le(e) ? s.subscribe(e, n) : s.call(e, n);
97
+ return ue(e) ? s.subscribe(e, n) : s.call(e, n);
96
98
  };
97
99
  }
98
100
  });
99
101
  }
100
- function le(s) {
102
+ function ue(s) {
101
103
  return s.endsWith("$");
102
104
  }
103
- class _e extends O {
105
+ class de extends W {
104
106
  constructor(e) {
105
107
  super();
106
108
  a(this, "_initialized", new ie(!1));
@@ -157,7 +159,7 @@ class _e extends O {
157
159
  });
158
160
  }
159
161
  _remoteSubscribe(e, n, r) {
160
- return new T((i) => {
162
+ return new O((i) => {
161
163
  let c = -1;
162
164
  return this._whenReady().then(() => {
163
165
  c = ++this._lastRequestCounter;
@@ -196,8 +198,9 @@ class _e extends O {
196
198
  this._protocol.send(e);
197
199
  }
198
200
  _onMessage(e) {
199
- var n;
200
- switch (e.type) {
201
+ var i;
202
+ const { type: n, seq: r } = e;
203
+ switch (n) {
201
204
  case 0:
202
205
  this._initialized.next(!0);
203
206
  break;
@@ -205,13 +208,15 @@ class _e extends O {
205
208
  case 202:
206
209
  case 300:
207
210
  case 302:
208
- case 301:
209
- (n = this._pendingRequests.get(e.seq)) == null || n.handle(e);
211
+ case 301: {
212
+ const { _pendingRequests: c } = this;
213
+ (i = c.get(r)) == null || i.handle(e), n !== 300 && c.delete(r);
210
214
  break;
215
+ }
211
216
  }
212
217
  }
213
218
  }
214
- class ue extends O {
219
+ class pe extends W {
215
220
  constructor(e) {
216
221
  super();
217
222
  a(this, "_channels", /* @__PURE__ */ new Map());
@@ -305,7 +310,7 @@ class K {
305
310
  constructor(t) {
306
311
  a(this, "_client");
307
312
  a(this, "_server");
308
- this._client = new _e(t), this._server = new ue(t);
313
+ this._client = new de(t), this._server = new pe(t);
309
314
  }
310
315
  dispose() {
311
316
  this._client.dispose(), this._server.dispose();
@@ -317,12 +322,12 @@ class K {
317
322
  this._server.registerChannel(t, e);
318
323
  }
319
324
  }
320
- var de = Object.getOwnPropertyDescriptor, pe = (s, t, e, n) => {
321
- for (var r = n > 1 ? void 0 : n ? de(t, e) : t, i = s.length - 1, c; i >= 0; i--)
325
+ var ve = Object.getOwnPropertyDescriptor, ge = (s, t, e, n) => {
326
+ for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
322
327
  (c = s[i]) && (r = c(r) || r);
323
328
  return r;
324
329
  }, _ = (s, t) => (e, n) => t(e, n, s);
325
- let y = class extends O {
330
+ let y = class extends W {
326
331
  constructor(t, e, n, r, i) {
327
332
  super();
328
333
  a(this, "_remoteInstanceService");
@@ -341,10 +346,10 @@ let y = class extends O {
341
346
  return this._syncingUnits.add(t), te(() => this._syncingUnits.delete(t));
342
347
  }
343
348
  _initRPCChannels() {
344
- this._rpcChannelService.registerChannel(W, Y(this._remoteSyncService)), this._injector.add([
345
- g,
346
- { useFactory: () => B(this._rpcChannelService.requestChannel($)) }
347
- ]), this._remoteInstanceService = this._injector.get(g);
349
+ this._rpcChannelService.registerChannel(T, Y(this._remoteSyncService)), this._injector.add([
350
+ m,
351
+ { useFactory: () => B(this._rpcChannelService.requestChannel(D)) }
352
+ ]), this._remoteInstanceService = this._injector.get(m);
348
353
  }
349
354
  _init() {
350
355
  this._univerInstanceService.getTypeOfUnitAdded$(u.UNIVER_SHEET).pipe(v(this.dispose$)).subscribe((t) => {
@@ -366,15 +371,15 @@ let y = class extends O {
366
371
  }));
367
372
  }
368
373
  };
369
- y = pe([
370
- _(0, b(R)),
371
- _(1, C),
374
+ y = ge([
375
+ _(0, C(R)),
376
+ _(1, b),
372
377
  _(2, L),
373
378
  _(3, I),
374
- _(4, m)
379
+ _(4, g)
375
380
  ], y);
376
- var ve = Object.getOwnPropertyDescriptor, me = (s, t, e, n) => {
377
- for (var r = n > 1 ? void 0 : n ? ve(t, e) : t, i = s.length - 1, c; i >= 0; i--)
381
+ var me = Object.getOwnPropertyDescriptor, ye = (s, t, e, n) => {
382
+ for (var r = n > 1 ? void 0 : n ? me(t, e) : t, i = s.length - 1, c; i >= 0; i--)
378
383
  (c = s[i]) && (r = c(r) || r);
379
384
  return r;
380
385
  }, d = (s, t) => (e, n) => t(e, n, s);
@@ -385,10 +390,10 @@ let f = class extends ne {
385
390
  this._injector = t, this._remoteInstanceService = e, this._commandService = n, this._rpcChannelService = r, this._initRPCChannels(), this._init();
386
391
  }
387
392
  _initRPCChannels() {
388
- this._rpcChannelService.registerChannel($, Y(this._remoteInstanceService)), this._injector.add([
389
- m,
390
- { useFactory: () => B(this._rpcChannelService.requestChannel(W)) }
391
- ]), this._remoteSyncService = this._injector.get(m);
393
+ this._rpcChannelService.registerChannel(D, Y(this._remoteInstanceService)), this._injector.add([
394
+ g,
395
+ { useFactory: () => B(this._rpcChannelService.requestChannel(T)) }
396
+ ]), this._remoteSyncService = this._injector.get(g);
392
397
  }
393
398
  _init() {
394
399
  this.disposeWithMe(
@@ -401,19 +406,18 @@ let f = class extends ne {
401
406
  );
402
407
  }
403
408
  };
404
- f = me([
405
- d(0, b(R)),
406
- d(1, g),
407
- d(2, C),
409
+ f = ye([
410
+ d(0, C(R)),
411
+ d(1, m),
412
+ d(2, b),
408
413
  d(3, I)
409
414
  ], f);
410
- const ge = "rpc.main-thread.config", j = {}, ye = "rpc.worker-thread.config", D = {};
411
415
  function fe() {
412
416
  return {
413
417
  send(s) {
414
418
  postMessage(s);
415
419
  },
416
- onMessage: new T((s) => {
420
+ onMessage: new O((s) => {
417
421
  const t = (e) => {
418
422
  s.next(e.data);
419
423
  };
@@ -426,7 +430,7 @@ function Se(s) {
426
430
  send(t) {
427
431
  s.postMessage(t);
428
432
  },
429
- onMessage: new T((t) => {
433
+ onMessage: new O((t) => {
430
434
  const e = (n) => {
431
435
  t.next(n.data);
432
436
  };
@@ -434,22 +438,22 @@ function Se(s) {
434
438
  }).pipe(z(1))
435
439
  };
436
440
  }
437
- var Ce = Object.getOwnPropertyDescriptor, J = (s, t, e, n) => {
438
- for (var r = n > 1 ? void 0 : n ? Ce(t, e) : t, i = s.length - 1, c; i >= 0; i--)
441
+ var be = Object.getOwnPropertyDescriptor, J = (s, t, e, n) => {
442
+ for (var r = n > 1 ? void 0 : n ? be(t, e) : t, i = s.length - 1, c; i >= 0; i--)
439
443
  (c = s[i]) && (r = c(r) || r);
440
444
  return r;
441
445
  }, S = (s, t) => (e, n) => t(e, n, s), P;
442
446
  let N = (P = class extends V {
443
- constructor(t = j, e, n) {
447
+ constructor(t = $, e, n) {
444
448
  super();
445
449
  a(this, "_internalWorker", null);
446
450
  this._config = t, this._injector = e, this._configService = n;
447
451
  const { ...r } = F(
448
452
  {},
449
- j,
453
+ $,
450
454
  this._config
451
455
  );
452
- this._configService.setConfig(ge, r);
456
+ this._configService.setConfig(he, r);
453
457
  }
454
458
  dispose() {
455
459
  super.dispose(), this._internalWorker && (this._internalWorker.terminate(), this._internalWorker = null);
@@ -469,24 +473,24 @@ let N = (P = class extends V {
469
473
  }
470
474
  ],
471
475
  [y],
472
- [m, { useClass: M }]
476
+ [g, { useClass: M }]
473
477
  ].forEach((i) => this._injector.add(i)), this._injector.get(y);
474
478
  }
475
479
  }, a(P, "pluginName", "UNIVER_RPC_MAIN_THREAD_PLUGIN"), P);
476
480
  N = J([
477
- S(1, b(R)),
481
+ S(1, C(R)),
478
482
  S(2, A)
479
483
  ], N);
480
484
  var U;
481
485
  let x = (U = class extends V {
482
- constructor(s = D, t, e) {
486
+ constructor(s = j, t, e) {
483
487
  super(), this._config = s, this._injector = t, this._configService = e;
484
488
  const { ...n } = F(
485
489
  {},
486
- D,
490
+ j,
487
491
  this._config
488
492
  );
489
- this._configService.setConfig(ye, n);
493
+ this._configService.setConfig(le, n);
490
494
  }
491
495
  onStarting() {
492
496
  [
@@ -497,26 +501,28 @@ let x = (U = class extends V {
497
501
  useFactory: () => new K(fe())
498
502
  }
499
503
  ],
500
- [g, { useClass: q }]
504
+ [m, { useClass: q }]
501
505
  ].forEach((s) => this._injector.add(s)), this._injector.get(f);
502
506
  }
503
507
  }, a(U, "pluginName", "UNIVER_RPC_WORKER_THREAD_PLUGIN"), U);
504
508
  x = J([
505
- S(1, b(R)),
509
+ S(1, C(R)),
506
510
  S(2, A)
507
511
  ], x);
508
512
  export {
509
- _e as ChannelClient,
510
- ue as ChannelServer,
513
+ de as ChannelClient,
514
+ pe as ChannelServer,
511
515
  K as ChannelService,
512
516
  y as DataSyncPrimaryController,
513
517
  f as DataSyncReplicaController,
514
518
  I as IRPCChannelService,
515
- g as IRemoteInstanceService,
516
- m as IRemoteSyncService,
517
- $ as RemoteInstanceServiceName,
519
+ m as IRemoteInstanceService,
520
+ g as IRemoteSyncService,
521
+ he as PLUGIN_CONFIG_KEY_MAIN_THREAD,
522
+ le as PLUGIN_CONFIG_KEY_WORKER_THREAD,
523
+ D as RemoteInstanceServiceName,
518
524
  M as RemoteSyncPrimaryService,
519
- W as RemoteSyncServiceName,
525
+ T as RemoteSyncServiceName,
520
526
  N as UniverRPCMainThreadPlugin,
521
527
  x as UniverRPCWorkerThreadPlugin,
522
528
  q as WebWorkerRemoteInstanceService,
@@ -14,6 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  export type { IUniverRPCMainThreadConfig, IUniverRPCWorkerThreadConfig } from './controllers/config.schema';
17
+ export { PLUGIN_CONFIG_KEY_MAIN_THREAD, PLUGIN_CONFIG_KEY_WORKER_THREAD } from './controllers/config.schema';
17
18
  export { DataSyncPrimaryController } from './controllers/data-sync/data-sync-primary.controller';
18
19
  export { DataSyncReplicaController } from './controllers/data-sync/data-sync-replica.controller';
19
20
  export { UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs/operators","rxjs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.UniverRpc={},r.UniverCore,r.rxjs.operators,r.rxjs))})(this,(function(r,a,_,d){"use strict";var J=Object.defineProperty;var Q=(r,a,_)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:_}):r[a]=_;var h=(r,a,_)=>Q(r,typeof a!="symbol"?a+"":a,_);var U,E;var N=Object.getOwnPropertyDescriptor,k=(c,t,e,n)=>{for(var s=n>1?void 0:n?N(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},g=(c,t)=>(e,n)=>t(e,n,c);const f="rpc.remote-sync.service",v=a.createIdentifier(f);r.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=k([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const C="univer.remote-instance-service",p=a.createIdentifier(C);r.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){const{type:e,snapshot:n}=t;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=k([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=n?s.apply(t,n):s.call(t);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=n?s.apply(t,n):s.call(t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return $(e)?c.subscribe(e,n):c.call(e,n)}}})}function $(c){return c.endsWith("$")}class q extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new d.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,i){return n._disposed?Promise.reject():n._remoteCall(e,s,i)},subscribe(s,i){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(_.filter(e=>e),_.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,u={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((M,S)=>{const K={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),S(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,K),this._sendRequest(u)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},M={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,M),this._sendRequest(l)}),()=>{if(o===-1)return;const u={type:102,seq:o,channelName:e,method:n};this._sendRequest(u)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var n;switch(e.type){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:(n=this._pendingRequests.get(e.seq))==null||n.handle(e);break}}}class W extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(_.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:i}=e,o=this._channels.get(n);let u;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);u=i?o.call(s,i):o.call(s)}catch(l){u=Promise.reject(l)}u.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,i=this._channels.get(n);try{if(!i)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const u=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:s,type:300,data:l})},error:l=>{this._sendResponse({seq:s,type:301,data:l.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,u)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=a.createIdentifier("IRPCChannelService");class w{constructor(t){h(this,"_client");h(this,"_server");this._client=new q(t),this._server=new W(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var L=Object.getOwnPropertyDescriptor,H=(c,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},y=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(f,b(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:a.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(_.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:i,id:o}=e,u=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!u||this._syncingUnits.has(u))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},r.DataSyncPrimaryController=H([y(0,a.Inject(a.Injector)),y(1,a.ICommandService),y(2,a.IUniverInstanceService),y(3,m),y(4,v)],r.DataSyncPrimaryController);var A=Object.getOwnPropertyDescriptor,V=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},R=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(f))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===a.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},r.DataSyncReplicaController=V([R(0,a.Inject(a.Injector)),R(1,p),R(2,a.ICommandService),R(3,m)],r.DataSyncReplicaController);const F="rpc.main-thread.config",D={},z="rpc.worker-thread.config",j={};function G(){return{send(c){postMessage(c)},onMessage:new d.Observable(c=>{const t=e=>{c.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function Y(c){return{send(t){c.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return c.addEventListener("message",e),()=>c.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var B=Object.getOwnPropertyDescriptor,O=(c,t,e,n)=>{for(var s=n>1?void 0:n?B(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=D,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},D,this._config);this._configService.setConfig(F,i)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=Y(n);[[m,{useFactory:()=>new w(s)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>this._injector.add(o)),this._injector.get(r.DataSyncPrimaryController)}},h(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),r.UniverRPCMainThreadPlugin=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=j,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},j,this._config);this._configService.setConfig(z,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(G())}],[p,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(r.DataSyncReplicaController)}},h(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),r.UniverRPCWorkerThreadPlugin=O([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=q,r.ChannelServer=W,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.RemoteInstanceServiceName=C,r.RemoteSyncServiceName=f,r.fromModule=b,r.toModule=P,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs/operators","rxjs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.UniverRpc={},r.UniverCore,r.rxjs.operators,r.rxjs))})(this,(function(r,a,u,d){"use strict";var J=Object.defineProperty;var Q=(r,a,u)=>a in r?J(r,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[a]=u;var h=(r,a,u)=>Q(r,typeof a!="symbol"?a+"":a,u);var U,E;const W="rpc.main-thread.config",k={},D="rpc.worker-thread.config",q={};var L=Object.getOwnPropertyDescriptor,O=(c,t,e,n)=>{for(var s=n>1?void 0:n?L(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},g=(c,t)=>(e,n)=>t(e,n,c);const C="rpc.remote-sync.service",v=a.createIdentifier(C);r.RemoteSyncPrimaryService=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{onlyLocal:!0,fromSync:!0})}},r.RemoteSyncPrimaryService=O([g(0,a.ICommandService)],r.RemoteSyncPrimaryService);const R="univer.remote-instance-service",p=a.createIdentifier(R);r.WebWorkerRemoteInstanceService=class{constructor(t,e,n){this._univerInstanceService=t,this._commandService=e,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(t){return this._applyMutation(t.mutationInfo)}async createInstance(t){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${t.unitID}`);const{type:e,snapshot:n}=t;try{switch(e){case a.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(a.UniverInstanceType.UNIVER_SHEET,n),!0;default:throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${e}.`)}}catch(s){throw s instanceof Error?s:new TypeError(`${s}`)}}async disposeInstance(t){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${t.unitID}`),this._univerInstanceService.disposeUnit(t.unitID)}_applyMutation(t){const{id:e,params:n}=t;return this._commandService.syncExecuteCommand(e,n,{onlyLocal:!0,fromSync:!0})}},r.WebWorkerRemoteInstanceService=O([g(0,a.IUniverInstanceService),g(1,a.ICommandService),g(2,a.ILogService)],r.WebWorkerRemoteInstanceService);function b(c){const t=c;return new class{call(e,n){const s=t[e];if(typeof s=="function"){let i=n?s.apply(t,n):s.call(t);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){const s=t[e];if(typeof s=="function"){const i=n?s.apply(t,n):s.call(t);return d.isObservable(i)?i:d.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function P(c){return new Proxy({},{get(t,e){if(e!=="dispose")return function(...n){return H(e)?c.subscribe(e,n):c.call(e,n)}}})}function H(c){return c.endsWith("$")}class j extends a.RxDisposable{constructor(e){super();h(this,"_initialized",new d.BehaviorSubject(!1));h(this,"_lastRequestCounter",0);h(this,"_pendingRequests",new Map);this._protocol=e,this._protocol.send({type:50}),this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onMessage(n))}dispose(){this._pendingRequests.clear()}getChannel(e){const n=this;return{call(s,i){return n._disposed?Promise.reject():n._remoteCall(e,s,i)},subscribe(s,i){if(n._disposed)throw new Error("[ChannelClient]: client is disposed!");return n._remoteSubscribe(e,s,i)}}}_whenReady(){return d.firstValueFrom(this._initialized.pipe(u.filter(e=>e),u.take(1)))}async _remoteCall(e,n,s){await this._whenReady();const i=++this._lastRequestCounter,_={seq:i,type:100,channelName:e,method:n,args:s},l=this;return new Promise((M,S)=>{const B={handle(T){switch(T.type){case 201:l._pendingRequests.delete(i),M(T.data);break;case 202:l._pendingRequests.delete(i),S(T.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,B),this._sendRequest(_)})}_remoteSubscribe(e,n,s){return new d.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const l={seq:o,type:101,channelName:e,method:n,args:s},M={handle(S){switch(S.type){case 300:i.next(S.data);break;case 301:i.error(S.data);break;case 302:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,M),this._sendRequest(l)}),()=>{if(o===-1)return;const _={type:102,seq:o,channelName:e,method:n};this._sendRequest(_)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){var i;const{type:n,seq:s}=e;switch(n){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{const{_pendingRequests:o}=this;(i=o.get(s))==null||i.handle(e),n!==300&&o.delete(s);break}}}}class N extends a.RxDisposable{constructor(e){super();h(this,"_channels",new Map);h(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(u.takeUntil(this.dispose$)).subscribe(n=>this._onRequest(n)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,n){this._channels.set(e,n)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){const{channelName:n,method:s,args:i}=e,o=this._channels.get(n);let _;try{if(!o)throw new Error(`[ChannelServer]: Channel ${n} not found!`);_=i?o.call(s,i):o.call(s)}catch(l){_=Promise.reject(l)}_.then(l=>{this._sendResponse({seq:e.seq,type:201,data:l})}).catch(l=>{l instanceof Error?this._sendResponse({seq:e.seq,type:202,data:l.message}):this._sendResponse({seq:e.seq,type:202,data:String(l)})})}_onSubscribe(e){const{channelName:n,seq:s}=e,i=this._channels.get(n);try{if(!i)throw new Error(`[ChannelServer]: Channel ${n} not found!`);const _=i.subscribe(e.method,e.args).subscribe({next:l=>{this._sendResponse({seq:s,type:300,data:l})},error:l=>{this._sendResponse({seq:s,type:301,data:l.message}),this._sendResponse({seq:s,type:302})},complete:()=>{this._sendResponse({seq:s,type:302})}});this._subscriptions.set(e.seq,_)}catch(o){o instanceof Error?this._sendResponse({seq:e.seq,type:301,data:o.message}):this._sendResponse({seq:e.seq,type:301,data:String(o)})}}_onUnsubscribe(e){const n=this._subscriptions.get(e.seq);n&&(n.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const m=a.createIdentifier("IRPCChannelService");class w{constructor(t){h(this,"_client");h(this,"_server");this._client=new j(t),this._server=new N(t)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(t){return this._client.getChannel(t)}registerChannel(t,e){this._server.registerChannel(t,e)}}var A=Object.getOwnPropertyDescriptor,G=(c,t,e,n)=>{for(var s=n>1?void 0:n?A(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},y=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncPrimaryController=class extends a.RxDisposable{constructor(e,n,s,i,o){super();h(this,"_remoteInstanceService");h(this,"_syncingUnits",new Set);h(this,"_syncingMutations",new Set);this._injector=e,this._commandService=n,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),a.toDisposable(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(C,b(this._remoteSyncService)),this._injector.add([p,{useFactory:()=>P(this._rpcChannelService.requestChannel(R))}]),this._remoteInstanceService=this._injector.get(p)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.add(e.getUnitId()),this._remoteInstanceService.createInstance({unitID:e.getUnitId(),type:a.UniverInstanceType.UNIVER_SHEET,snapshot:e.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(u.takeUntil(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{const{type:s,params:i,id:o}=e,_=(i==null?void 0:i.unitId)||"";s===a.CommandType.MUTATION&&(!_||this._syncingUnits.has(_))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(o)&&this._remoteInstanceService.syncMutation({mutationInfo:e})}))}},r.DataSyncPrimaryController=G([y(0,a.Inject(a.Injector)),y(1,a.ICommandService),y(2,a.IUniverInstanceService),y(3,m),y(4,v)],r.DataSyncPrimaryController);var F=Object.getOwnPropertyDescriptor,V=(c,t,e,n)=>{for(var s=n>1?void 0:n?F(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},f=(c,t)=>(e,n)=>t(e,n,c);r.DataSyncReplicaController=class extends a.Disposable{constructor(e,n,s,i){super();h(this,"_remoteSyncService");this._injector=e,this._remoteInstanceService=n,this._commandService=s,this._rpcChannelService=i,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(R,b(this._remoteInstanceService)),this._injector.add([v,{useFactory:()=>P(this._rpcChannelService.requestChannel(C))}]),this._remoteSyncService=this._injector.get(v)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===a.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e})}))}},r.DataSyncReplicaController=V([f(0,a.Inject(a.Injector)),f(1,p),f(2,a.ICommandService),f(3,m)],r.DataSyncReplicaController);function z(){return{send(c){postMessage(c)},onMessage:new d.Observable(c=>{const t=e=>{c.next(e.data)};return addEventListener("message",t),()=>removeEventListener("message",t)}).pipe(d.shareReplay(1))}}function K(c){return{send(t){c.postMessage(t)},onMessage:new d.Observable(t=>{const e=n=>{t.next(n.data)};return c.addEventListener("message",e),()=>c.removeEventListener("message",e)}).pipe(d.shareReplay(1))}}var Y=Object.getOwnPropertyDescriptor,$=(c,t,e,n)=>{for(var s=n>1?void 0:n?Y(t,e):t,i=c.length-1,o;i>=0;i--)(o=c[i])&&(s=o(s)||s);return s},I=(c,t)=>(e,n)=>t(e,n,c);r.UniverRPCMainThreadPlugin=(U=class extends a.Plugin{constructor(e=k,n,s){super();h(this,"_internalWorker",null);this._config=e,this._injector=n,this._configService=s;const{...i}=a.merge({},k,this._config);this._configService.setConfig(W,i)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){const{workerURL:e}=this._config;if(!e)throw new Error("[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.");const n=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:n;const s=K(n);[[m,{useFactory:()=>new w(s)}],[r.DataSyncPrimaryController],[v,{useClass:r.RemoteSyncPrimaryService}]].forEach(o=>this._injector.add(o)),this._injector.get(r.DataSyncPrimaryController)}},h(U,"pluginName","UNIVER_RPC_MAIN_THREAD_PLUGIN"),U),r.UniverRPCMainThreadPlugin=$([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCMainThreadPlugin),r.UniverRPCWorkerThreadPlugin=(E=class extends a.Plugin{constructor(t=q,e,n){super(),this._config=t,this._injector=e,this._configService=n;const{...s}=a.merge({},q,this._config);this._configService.setConfig(D,s)}onStarting(){[[r.DataSyncReplicaController],[m,{useFactory:()=>new w(z())}],[p,{useClass:r.WebWorkerRemoteInstanceService}]].forEach(t=>this._injector.add(t)),this._injector.get(r.DataSyncReplicaController)}},h(E,"pluginName","UNIVER_RPC_WORKER_THREAD_PLUGIN"),E),r.UniverRPCWorkerThreadPlugin=$([I(1,a.Inject(a.Injector)),I(2,a.IConfigService)],r.UniverRPCWorkerThreadPlugin),r.ChannelClient=j,r.ChannelServer=N,r.ChannelService=w,r.IRPCChannelService=m,r.IRemoteInstanceService=p,r.IRemoteSyncService=v,r.PLUGIN_CONFIG_KEY_MAIN_THREAD=W,r.PLUGIN_CONFIG_KEY_WORKER_THREAD=D,r.RemoteInstanceServiceName=R,r.RemoteSyncServiceName=C,r.fromModule=b,r.toModule=P,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/rpc",
3
- "version": "0.12.4",
3
+ "version": "0.13.0-insiders.20251216-1519cb7",
4
4
  "private": false,
5
5
  "author": "DreamNum <developer@univer.ai>",
6
6
  "license": "Apache-2.0",
@@ -47,14 +47,14 @@
47
47
  "rxjs": ">=7.0.0"
48
48
  },
49
49
  "dependencies": {
50
- "@univerjs/core": "0.12.4"
50
+ "@univerjs/core": "0.13.0-insiders.20251216-1519cb7"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.2",
54
54
  "typescript": "^5.9.3",
55
- "vite": "^7.2.6",
55
+ "vite": "^7.2.7",
56
56
  "vitest": "^4.0.15",
57
- "@univerjs-infra/shared": "0.12.4"
57
+ "@univerjs-infra/shared": "0.13.0"
58
58
  },
59
59
  "scripts": {
60
60
  "test": "vitest run",
package/LICENSE DELETED
@@ -1,176 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS