@univerjs/rpc 0.1.0-alpha.1 → 0.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,35 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
  import { ICommandService, IUniverInstanceService, RxDisposable } from '@univerjs/core';
2
- import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';
17
+ import { Injector } from '@wendellhu/redi';
18
+ import { IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';
19
+ import { IRPChannelService } from '../../services/rpc/channel.service';
3
20
  /**
4
21
  * This controller is responsible for syncing data from the primary thread to
5
22
  * the worker thread.
6
23
  */
7
24
  export declare class DataSyncPrimaryController extends RxDisposable {
8
25
  private readonly _unsyncMutations;
9
- private readonly _univerInstanceService;
10
- private readonly _remoteInstanceService;
26
+ private readonly _injector;
11
27
  private readonly _commandService;
12
- constructor(_unsyncMutations: Set<string>, _univerInstanceService: IUniverInstanceService, _remoteInstanceService: IRemoteInstanceService, _commandService: ICommandService);
28
+ private readonly _univerInstanceService;
29
+ private readonly _rpcChannelService;
30
+ private readonly _remoteSyncService;
31
+ private _remoteInstanceService;
32
+ constructor(_unsyncMutations: Set<string>, _injector: Injector, _commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _rpcChannelService: IRPChannelService, _remoteSyncService: IRemoteSyncService);
33
+ private _initRPCChannels;
13
34
  private _init;
14
35
  }
15
- //# sourceMappingURL=data-sync-primary.controller.d.ts.map
@@ -1,14 +1,33 @@
1
- import { Disposable, ICommandService, IUniverInstanceService } from '@univerjs/core';
2
- import { IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { Disposable, ICommandService } from '@univerjs/core';
17
+ import { Injector } from '@wendellhu/redi';
18
+ import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';
19
+ import { IRPChannelService } from '../../services/rpc/channel.service';
3
20
  /**
4
21
  * This controller is responsible for syncing data from the worker thread to
5
22
  * the primary thread.
6
23
  */
7
24
  export declare class DataSyncReplicaController extends Disposable {
8
- private readonly _commandService;
25
+ private readonly _injector;
9
26
  private readonly _remoteInstanceService;
10
- private readonly _univerInstanceService;
11
- constructor(_commandService: ICommandService, _remoteInstanceService: IRemoteSyncService, _univerInstanceService: IUniverInstanceService);
27
+ private readonly _commandService;
28
+ private readonly _rpcChannelService;
29
+ private _remoteSyncService;
30
+ constructor(_injector: Injector, _remoteInstanceService: IRemoteInstanceService, _commandService: ICommandService, _rpcChannelService: IRPChannelService);
31
+ private _initRPCChannels;
12
32
  private _init;
13
33
  }
14
- //# sourceMappingURL=data-sync-replica.controller.d.ts.map
@@ -1,2 +1,18 @@
1
- export { type IUniverRPCMainThreadPluginConfig, type IUniverRPCWorkerThreadPluginConfig, UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
2
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export { type IUniverRPCMainThreadConfig, type IUniverRPCWorkerThreadPluginConfig, UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
17
+ export { IRPChannelService } from './services/rpc/channel.service';
18
+ export { ChannelClient, ChannelServer, type IMessageProtocol } from './services/rpc/rpc.service';
@@ -1,6 +1,21 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
  import { Plugin, PluginType } from '@univerjs/core';
2
17
  import { Injector } from '@wendellhu/redi';
3
- export interface IUniverRPCMainThreadPluginConfig {
18
+ export interface IUniverRPCMainThreadConfig {
4
19
  workerURL: string | URL;
5
20
  unsyncMutations?: Set<string>;
6
21
  }
@@ -12,7 +27,7 @@ export declare class UniverRPCMainThreadPlugin extends Plugin {
12
27
  private readonly _config;
13
28
  protected readonly _injector: Injector;
14
29
  static type: PluginType;
15
- constructor(_config: IUniverRPCMainThreadPluginConfig, _injector: Injector);
30
+ constructor(_config: IUniverRPCMainThreadConfig, _injector: Injector);
16
31
  onStarting(injector: Injector): Promise<void>;
17
32
  }
18
33
  export interface IUniverRPCWorkerThreadPluginConfig {
@@ -27,4 +42,3 @@ export declare class UniverRPCWorkerThreadPlugin extends Plugin {
27
42
  constructor(_config: UniverRPCWorkerThreadPlugin, _injector: Injector);
28
43
  onStarting(injector: Injector): void;
29
44
  }
30
- //# sourceMappingURL=plugin.d.ts.map
@@ -1,3 +1,18 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
  import type { IExecutionOptions, IMutationInfo, IWorkbookData } from '@univerjs/core';
2
17
  import { DocumentType, ICommandService, IUniverInstanceService } from '@univerjs/core';
3
18
  export interface IRemoteSyncMutationOptions extends IExecutionOptions {
@@ -60,4 +75,3 @@ export declare class RemoteInstanceReplicaService implements IRemoteInstanceServ
60
75
  unitID: string;
61
76
  }): Promise<boolean>;
62
77
  }
63
- //# sourceMappingURL=remote-instance.service.d.ts.map
@@ -1,2 +1,16 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
  export {};
2
- //# sourceMappingURL=rpc.service.spec.d.ts.map
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { IChannel, IMessageProtocol } from './rpc.service';
17
+ export interface IRPChannelService {
18
+ requestChannel(name: string): IChannel;
19
+ registerChannel(name: string, channel: IChannel): void;
20
+ }
21
+ export declare const IRPChannelService: import("@wendellhu/redi").IdentifierDecorator<IRPChannelService>;
22
+ /**
23
+ * This service is responsible for managing the RPC channels.
24
+ */
25
+ export declare class ChannelService {
26
+ private readonly _client;
27
+ private readonly _server;
28
+ constructor(_messageProtocol: IMessageProtocol);
29
+ requestChannel(name: string): IChannel;
30
+ registerChannel(name: string, channel: IChannel): void;
31
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { IMessageProtocol } from '../rpc.service';
17
+ /**
18
+ * Generate an `IMessageProtocol` on the web worker.
19
+ */
20
+ export declare function createWebWorkerMessagePortOnWorker(): IMessageProtocol;
21
+ /**
22
+ * Generate an `IMessageProtocol` on the main thread side.
23
+ * @param worker The Web Worker object
24
+ * @returns
25
+ */
26
+ export declare function createWebWorkerMessagePortOnMain(worker: Worker): IMessageProtocol;
@@ -1,3 +1,18 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
  import { RxDisposable } from '@univerjs/core';
2
17
  import { Observable } from 'rxjs';
3
18
  /** This protocol is for transferring data from the two peer univer instance running in different locations. */
@@ -70,10 +85,3 @@ export declare class ChannelServer extends RxDisposable implements IChannelServe
70
85
  private _onUnsubscribe;
71
86
  private _sendResponse;
72
87
  }
73
- /**
74
- * This service is for other service to register channels to the RPC framework.
75
- */
76
- export interface IRPCService {
77
- }
78
- export declare const IRPCService: import("@wendellhu/redi").IdentifierDecorator<IRPCService>;
79
- //# sourceMappingURL=rpc.service.d.ts.map
@@ -0,0 +1,3 @@
1
+ (function(p,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs"],l):(p=typeof globalThis<"u"?globalThis:p||self,l(p.UniverRpc={},p.UniverCore,p["@wendellhu/redi"],p.rxjs))})(this,function(p,l,d,S){"use strict";var ft=Object.defineProperty;var ht=(p,l,d)=>l in p?ft(p,l,{enumerable:!0,configurable:!0,writable:!0,value:d}):p[l]=d;var b=(p,l,d)=>(ht(p,typeof l!="symbol"?l+"":l,d),d);var N,ee;function v(t){return typeof t=="function"}function be(t){return v(t==null?void 0:t.lift)}function D(t){return function(n){if(be(n))return n.lift(function(e){try{return t(e,this)}catch(r){this.error(r)}});throw new TypeError("Unable to lift unknown Observable type")}}var $=function(t,n){return $=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s])},$(t,n)};function L(t,n){if(typeof n!="function"&&n!==null)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");$(t,n);function e(){this.constructor=t}t.prototype=n===null?Object.create(n):(e.prototype=n.prototype,new e)}function me(t,n,e,r){function s(i){return i instanceof e?i:new e(function(o){o(i)})}return new(e||(e=Promise))(function(i,o){function a(f){try{c(r.next(f))}catch(_){o(_)}}function u(f){try{c(r.throw(f))}catch(_){o(_)}}function c(f){f.done?i(f.value):s(f.value).then(a,u)}c((r=r.apply(t,n||[])).next())})}function te(t,n){var e={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},r,s,i,o;return o={next:a(0),throw:a(1),return:a(2)},typeof Symbol=="function"&&(o[Symbol.iterator]=function(){return this}),o;function a(c){return function(f){return u([c,f])}}function u(c){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(e=0)),e;)try{if(r=1,s&&(i=c[0]&2?s.return:c[0]?s.throw||((i=s.return)&&i.call(s),0):s.next)&&!(i=i.call(s,c[1])).done)return i;switch(s=0,i&&(c=[c[0]&2,i.value]),c[0]){case 0:case 1:i=c;break;case 4:return e.label++,{value:c[1],done:!1};case 5:e.label++,s=c[1],c=[0];continue;case 7:c=e.ops.pop(),e.trys.pop();continue;default:if(i=e.trys,!(i=i.length>0&&i[i.length-1])&&(c[0]===6||c[0]===2)){e=0;continue}if(c[0]===3&&(!i||c[1]>i[0]&&c[1]<i[3])){e.label=c[1];break}if(c[0]===6&&e.label<i[1]){e.label=i[1],i=c;break}if(i&&e.label<i[2]){e.label=i[2],e.ops.push(c);break}i[2]&&e.ops.pop(),e.trys.pop();continue}c=n.call(t,e)}catch(f){c=[6,f],s=0}finally{r=i=0}if(c[0]&5)throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}}function P(t){var n=typeof Symbol=="function"&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function O(t,n){var e=typeof Symbol=="function"&&t[Symbol.iterator];if(!e)return t;var r=e.call(t),s,i=[],o;try{for(;(n===void 0||n-- >0)&&!(s=r.next()).done;)i.push(s.value)}catch(a){o={error:a}}finally{try{s&&!s.done&&(e=r.return)&&e.call(r)}finally{if(o)throw o.error}}return i}function x(t,n,e){if(e||arguments.length===2)for(var r=0,s=n.length,i;r<s;r++)(i||!(r in n))&&(i||(i=Array.prototype.slice.call(n,0,r)),i[r]=n[r]);return t.concat(i||Array.prototype.slice.call(n))}function g(t){return this instanceof g?(this.v=t,this):new g(t)}function _e(t,n,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r=e.apply(t,n||[]),s,i=[];return s={},o("next"),o("throw"),o("return"),s[Symbol.asyncIterator]=function(){return this},s;function o(h){r[h]&&(s[h]=function(m){return new Promise(function(T,y){i.push([h,m,T,y])>1||a(h,m)})})}function a(h,m){try{u(r[h](m))}catch(T){_(i[0][3],T)}}function u(h){h.value instanceof g?Promise.resolve(h.value.v).then(c,f):_(i[0][2],h)}function c(h){a("next",h)}function f(h){a("throw",h)}function _(h,m){h(m),i.shift(),i.length&&a(i[0][0],i[0][1])}}function Se(t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n=t[Symbol.asyncIterator],e;return n?n.call(t):(t=typeof P=="function"?P(t):t[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(i){e[i]=t[i]&&function(o){return new Promise(function(a,u){o=t[i](o),s(a,u,o.done,o.value)})}}function s(i,o,a,u){Promise.resolve(u).then(function(c){i({value:c,done:a})},o)}}typeof SuppressedError=="function"&&SuppressedError;var we=function(t){return t&&typeof t.length=="number"&&typeof t!="function"};function ge(t){return v(t==null?void 0:t.then)}function Pe(t){var n=function(r){Error.call(r),r.stack=new Error().stack},e=t(n);return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}var k=Pe(function(t){return function(e){t(this),this.message=e?e.length+` errors occurred during unsubscription:
2
+ `+e.map(function(r,s){return s+1+") "+r.toString()}).join(`
3
+ `):"",this.name="UnsubscriptionError",this.errors=e}});function ne(t,n){if(t){var e=t.indexOf(n);0<=e&&t.splice(e,1)}}var W=function(){function t(n){this.initialTeardown=n,this.closed=!1,this._parentage=null,this._finalizers=null}return t.prototype.unsubscribe=function(){var n,e,r,s,i;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var a=P(o),u=a.next();!u.done;u=a.next()){var c=u.value;c.remove(this)}}catch(y){n={error:y}}finally{try{u&&!u.done&&(e=a.return)&&e.call(a)}finally{if(n)throw n.error}}else o.remove(this);var f=this.initialTeardown;if(v(f))try{f()}catch(y){i=y instanceof k?y.errors:[y]}var _=this._finalizers;if(_){this._finalizers=null;try{for(var h=P(_),m=h.next();!m.done;m=h.next()){var T=m.value;try{ie(T)}catch(y){i=i??[],y instanceof k?i=x(x([],O(i)),O(y.errors)):i.push(y)}}}catch(y){r={error:y}}finally{try{m&&!m.done&&(s=h.return)&&s.call(h)}finally{if(r)throw r.error}}}if(i)throw new k(i)}},t.prototype.add=function(n){var e;if(n&&n!==this)if(this.closed)ie(n);else{if(n instanceof t){if(n.closed||n._hasParent(this))return;n._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(n)}},t.prototype._hasParent=function(n){var e=this._parentage;return e===n||Array.isArray(e)&&e.includes(n)},t.prototype._addParent=function(n){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(n),e):e?[e,n]:n},t.prototype._removeParent=function(n){var e=this._parentage;e===n?this._parentage=null:Array.isArray(e)&&ne(e,n)},t.prototype.remove=function(n){var e=this._finalizers;e&&ne(e,n),n instanceof t&&n._removeParent(this)},t.EMPTY=function(){var n=new t;return n.closed=!0,n}(),t}();W.EMPTY;function re(t){return t instanceof W||t&&"closed"in t&&v(t.remove)&&v(t.add)&&v(t.unsubscribe)}function ie(t){v(t)?t():t.unsubscribe()}var se={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},F={setTimeout:function(t,n){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];var s=F.delegate;return s!=null&&s.setTimeout?s.setTimeout.apply(s,x([t,n],O(e))):setTimeout.apply(void 0,x([t,n],O(e)))},clearTimeout:function(t){var n=F.delegate;return((n==null?void 0:n.clearTimeout)||clearTimeout)(t)},delegate:void 0};function oe(t){F.setTimeout(function(){throw t})}function H(){}function Ie(t){t()}var G=function(t){L(n,t);function n(e){var r=t.call(this)||this;return r.isStopped=!1,e?(r.destination=e,re(e)&&e.add(r)):r.destination=Te,r}return n.create=function(e,r,s){return new V(e,r,s)},n.prototype.next=function(e){this.isStopped||this._next(e)},n.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},n.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},n.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,t.prototype.unsubscribe.call(this),this.destination=null)},n.prototype._next=function(e){this.destination.next(e)},n.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},n.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},n}(W),Ce=Function.prototype.bind;function Y(t,n){return Ce.call(t,n)}var Re=function(){function t(n){this.partialObserver=n}return t.prototype.next=function(n){var e=this.partialObserver;if(e.next)try{e.next(n)}catch(r){M(r)}},t.prototype.error=function(n){var e=this.partialObserver;if(e.error)try{e.error(n)}catch(r){M(r)}else M(n)},t.prototype.complete=function(){var n=this.partialObserver;if(n.complete)try{n.complete()}catch(e){M(e)}},t}(),V=function(t){L(n,t);function n(e,r,s){var i=t.call(this)||this,o;if(v(e)||!e)o={next:e??void 0,error:r??void 0,complete:s??void 0};else{var a;i&&se.useDeprecatedNextContext?(a=Object.create(e),a.unsubscribe=function(){return i.unsubscribe()},o={next:e.next&&Y(e.next,a),error:e.error&&Y(e.error,a),complete:e.complete&&Y(e.complete,a)}):o=e}return i.destination=new Re(o),i}return n}(G);function M(t){oe(t)}function Ee(t){throw t}var Te={closed:!0,next:H,error:Ee,complete:H},z=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Oe(t){return t}function xe(t){return t.length===0?Oe:t.length===1?t[0]:function(e){return t.reduce(function(r,s){return s(r)},e)}}var w=function(){function t(n){n&&(this._subscribe=n)}return t.prototype.lift=function(n){var e=new t;return e.source=this,e.operator=n,e},t.prototype.subscribe=function(n,e,r){var s=this,i=Ue(n)?n:new V(n,e,r);return Ie(function(){var o=s,a=o.operator,u=o.source;i.add(a?a.call(i,u):u?s._subscribe(i):s._trySubscribe(i))}),i},t.prototype._trySubscribe=function(n){try{return this._subscribe(n)}catch(e){n.error(e)}},t.prototype.forEach=function(n,e){var r=this;return e=ce(e),new e(function(s,i){var o=new V({next:function(a){try{n(a)}catch(u){i(u),o.unsubscribe()}},error:i,complete:s});r.subscribe(o)})},t.prototype._subscribe=function(n){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(n)},t.prototype[z]=function(){return this},t.prototype.pipe=function(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];return xe(n)(this)},t.prototype.toPromise=function(n){var e=this;return n=ce(n),new n(function(r,s){var i;e.subscribe(function(o){return i=o},function(o){return s(o)},function(){return r(i)})})},t.create=function(n){return new t(n)},t}();function ce(t){var n;return(n=t??se.Promise)!==null&&n!==void 0?n:Promise}function Me(t){return t&&v(t.next)&&v(t.error)&&v(t.complete)}function Ue(t){return t&&t instanceof G||Me(t)&&re(t)}function qe(t){return v(t[z])}function je(t){return Symbol.asyncIterator&&v(t==null?void 0:t[Symbol.asyncIterator])}function Ae(t){return new TypeError("You provided "+(t!==null&&typeof t=="object"?"an invalid object":"'"+t+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function De(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var $e=De();function Le(t){return v(t==null?void 0:t[$e])}function ke(t){return _e(this,arguments,function(){var e,r,s,i;return te(this,function(o){switch(o.label){case 0:e=t.getReader(),o.label=1;case 1:o.trys.push([1,,9,10]),o.label=2;case 2:return[4,g(e.read())];case 3:return r=o.sent(),s=r.value,i=r.done,i?[4,g(void 0)]:[3,5];case 4:return[2,o.sent()];case 5:return[4,g(s)];case 6:return[4,o.sent()];case 7:return o.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function We(t){return v(t==null?void 0:t.getReader)}function Fe(t){if(t instanceof w)return t;if(t!=null){if(qe(t))return He(t);if(we(t))return Ge(t);if(ge(t))return Ye(t);if(je(t))return ae(t);if(Le(t))return Ve(t);if(We(t))return ze(t)}throw Ae(t)}function He(t){return new w(function(n){var e=t[z]();if(v(e.subscribe))return e.subscribe(n);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function Ge(t){return new w(function(n){for(var e=0;e<t.length&&!n.closed;e++)n.next(t[e]);n.complete()})}function Ye(t){return new w(function(n){t.then(function(e){n.closed||(n.next(e),n.complete())},function(e){return n.error(e)}).then(null,oe)})}function Ve(t){return new w(function(n){var e,r;try{for(var s=P(t),i=s.next();!i.done;i=s.next()){var o=i.value;if(n.next(o),n.closed)return}}catch(a){e={error:a}}finally{try{i&&!i.done&&(r=s.return)&&r.call(s)}finally{if(e)throw e.error}}n.complete()})}function ae(t){return new w(function(n){Be(t,n).catch(function(e){return n.error(e)})})}function ze(t){return ae(ke(t))}function Be(t,n){var e,r,s,i;return me(this,void 0,void 0,function(){var o,a;return te(this,function(u){switch(u.label){case 0:u.trys.push([0,5,6,11]),e=Se(t),u.label=1;case 1:return[4,e.next()];case 2:if(r=u.sent(),!!r.done)return[3,4];if(o=r.value,n.next(o),n.closed)return[2];u.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return a=u.sent(),s={error:a},[3,11];case 6:return u.trys.push([6,,9,10]),r&&!r.done&&(i=e.return)?[4,i.call(e)]:[3,8];case 7:u.sent(),u.label=8;case 8:return[3,10];case 9:if(s)throw s.error;return[7];case 10:return[7];case 11:return n.complete(),[2]}})})}function B(t,n,e,r,s){return new Je(t,n,e,r,s)}var Je=function(t){L(n,t);function n(e,r,s,i,o,a){var u=t.call(this,e)||this;return u.onFinalize=o,u.shouldUnsubscribe=a,u._next=r?function(c){try{r(c)}catch(f){e.error(f)}}:t.prototype._next,u._error=i?function(c){try{i(c)}catch(f){e.error(f)}finally{this.unsubscribe()}}:t.prototype._error,u._complete=s?function(){try{s()}catch(c){e.error(c)}finally{this.unsubscribe()}}:t.prototype._complete,u}return n.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var r=this.closed;t.prototype.unsubscribe.call(this),!r&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},n}(G),Qe=new w(function(t){return t.complete()});function Xe(t){return t<=0?function(){return Qe}:D(function(n,e){var r=0;n.subscribe(B(e,function(s){++r<=t&&(e.next(s),t<=r&&e.complete())}))})}function Ze(t,n){return D(function(e,r){var s=0;e.subscribe(B(r,function(i){return t.call(n,i,s++)&&r.next(i)}))})}function U(t){return D(function(n,e){Fe(t).subscribe(B(e,function(){return e.complete()},H)),!e.closed&&n.subscribe(e)})}var Ke=Object.defineProperty,Ne=Object.getOwnPropertyDescriptor,ue=(t,n,e,r)=>{for(var s=r>1?void 0:r?Ne(n,e):n,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(r?o(n,e,s):o(s))||s);return r&&s&&Ke(n,e,s),s},J=(t,n)=>(e,r)=>n(e,r,t);const Q="univer.remote-sync-service",q=d.createIdentifier(Q);let X=class{constructor(t){this._commandService=t}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{local:!0,fromSync:!0})}};X=ue([J(0,l.ICommandService)],X);const Z="univer.remote-instance-service",j=d.createIdentifier(Z);let K=class{constructor(t,n){this._univerInstanceService=t,this._commandService=n}async syncMutation(t){return this._commandService.syncExecuteCommand(t.mutationInfo.id,t.mutationInfo.params,{local:!0,fromSync:!0})}async createInstance(t){const{type:n,snapshot:e}=t;try{switch(n){case l.DocumentType.SHEET:return!!this._univerInstanceService.createSheet(e);default:throw new Error(`[RemoteInstanceReplicaService]: cannot create replica for document type: ${n}.`)}}catch(r){throw r instanceof Error?r:new Error(`${r}`)}}async disposeInstance(t){return this._univerInstanceService.disposeDocument(t.unitID)}};K=ue([J(0,l.IUniverInstanceService),J(1,l.ICommandService)],K);function le(t){const n=t;return new class{call(e,r){const s=n[e];if(typeof s=="function"){let i=s.apply(n,[r]);return i instanceof Promise||(i=Promise.resolve(i)),i}throw new Error(`[RPC]: method not found for ${e}!`)}subscribe(e,r){const s=n[e];if(typeof s=="function"){const i=s.apply(n,r);return S.isObservable(i)?i:S.of(i)}throw new Error(`[RPC]: observable method not found for ${e}!`)}}}function fe(t){return new Proxy({},{get(n,e){return function(...r){return et(e)?t.subscribe(e,r[0]):t.call(e,r[0])}}})}function et(t){return t.endsWith("$")}class he extends l.RxDisposable{constructor(e){super();b(this,"_initialized",new S.BehaviorSubject(!1));b(this,"_lastRequestCounter",0);b(this,"_pendingRequests",new Map);b(this,"_pendingSubscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(U(this.dispose$)).subscribe(r=>this._onResponse(r))}getChannel(e){const r=this;return{call(s,i){return r._disposed?Promise.reject():r._remoteCall(e,s,i)},subscribe(s,i){if(r._disposed)throw new Error("[ChannelClient]: client is disposed!");return r._remoteSubscribe(e,s,i)}}}_whenReady(){return S.firstValueFrom(this._initialized.pipe(Ze(e=>e),Xe(1)))}async _remoteCall(e,r,s){await this._whenReady();const i=++this._lastRequestCounter,a={seq:i,type:100,channelName:e,method:r,args:s},u=this;return new Promise((c,f)=>{const _={handle(h){switch(h.type){case 100:u._pendingRequests.delete(i),c(h.data);break;case 101:u._pendingRequests.delete(i),f(h.data);break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(i,_),this._sendRequest(a)})}_remoteSubscribe(e,r,s){return new S.Observable(i=>{let o=-1;return this._whenReady().then(()=>{o=++this._lastRequestCounter;const u={seq:o,type:101,channelName:e,method:r,args:s},c={handle(f){switch(f.type){case 200:i.next(f.data);break;case 201:i.error(f.data);break;case 202:i.complete();break;default:throw new Error("[ChannelClient]: unknown response type!")}}};this._pendingRequests.set(o,c),this._sendRequest(u)}),()=>{if(o===-1)return;const a={type:102,seq:o,channelName:e,method:r};this._sendRequest(a)}})}_sendRequest(e){this._protocol.send(e)}_onResponse(e){var r;switch(e.type){case 0:this._initialized.next(!0);break;case 100:case 101:case 200:case 202:case 201:(r=this._pendingRequests.get(e.seq))==null||r.handle(e);break}}}class pe extends l.RxDisposable{constructor(e){super();b(this,"_channels",new Map);b(this,"_subscriptions",new Map);this._protocol=e,this._protocol.onMessage.pipe(U(this.dispose$)).subscribe(r=>this._onRequest(r)),this._sendResponse({seq:-1,type:0})}registerChannel(e,r){this._channels.set(e,r)}_onRequest(e){switch(e.type){case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break}}_onMethodCall(e){const r=this._channels.get(e.channelName);let s;try{if(!r)throw new Error("[ChannelServer]: Channel not found!");s=r.call(e.method,e.args)}catch(i){s=Promise.reject(i)}s.then(i=>{this._sendResponse({seq:e.seq,type:100,data:i})}).catch(i=>{i instanceof Error?this._sendResponse({seq:e.seq,type:101,data:i.message}):this._sendResponse({seq:e.seq,type:101,data:String(i)})})}_onSubscribe(e){const r=this._channels.get(e.channelName);try{if(!r)throw new Error("[ChannelServer]: Channel not found!");const i=r.subscribe(e.method,e.args).subscribe({next:o=>{this._sendResponse({seq:e.seq,type:200,data:o})},error:o=>{this._sendResponse({seq:e.seq,type:201,data:o.message}),this._sendResponse({seq:e.seq,type:202})},complete:()=>{this._sendResponse({seq:e.seq,type:202})}});this._subscriptions.set(e.seq,i)}catch(s){s instanceof Error?this._sendResponse({seq:e.seq,type:201,data:s.message}):this._sendResponse({seq:e.seq,type:201,data:String(s)})}}_onUnsubscribe(e){const r=this._subscriptions.get(e.seq);r&&(r.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}}const I=d.createIdentifier("IRPChannelService");class de{constructor(n){b(this,"_client");b(this,"_server");this._client=new he(n),this._server=new pe(n)}requestChannel(n){return this._client.getChannel(n)}registerChannel(n,e){this._server.registerChannel(n,e)}}var tt=Object.defineProperty,nt=Object.getOwnPropertyDescriptor,rt=(t,n,e,r)=>{for(var s=r>1?void 0:r?nt(n,e):n,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(r?o(n,e,s):o(s))||s);return r&&s&&tt(n,e,s),s},C=(t,n)=>(e,r)=>n(e,r,t);let R=class extends l.RxDisposable{constructor(n,e,r,s,i,o){super();b(this,"_remoteInstanceService");this._unsyncMutations=n,this._injector=e,this._commandService=r,this._univerInstanceService=s,this._rpcChannelService=i,this._remoteSyncService=o,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(Q,le(this._remoteSyncService)),this._injector.add([j,{useFactory:()=>fe(this._rpcChannelService.requestChannel(Z))}]),this._remoteInstanceService=this._injector.get(j)}_init(){this._univerInstanceService.sheetAdded$.pipe(U(this.dispose$)).subscribe(n=>{this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:l.DocumentType.SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.sheetDisposed$.pipe(U(this.dispose$)).subscribe(n=>{this._remoteInstanceService.disposeInstance({unitID:n.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===l.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&!this._unsyncMutations.has(n.id)&&this._remoteInstanceService.syncMutation({mutationInfo:n})}))}};R=rt([l.OnLifecycle(l.LifecycleStages.Starting,R),C(1,d.Inject(d.Injector)),C(2,l.ICommandService),C(3,l.IUniverInstanceService),C(4,I),C(5,q)],R);var it=Object.defineProperty,st=Object.getOwnPropertyDescriptor,ot=(t,n,e,r)=>{for(var s=r>1?void 0:r?st(n,e):n,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(r?o(n,e,s):o(s))||s);return r&&s&&it(n,e,s),s},A=(t,n)=>(e,r)=>n(e,r,t);let E=class extends l.Disposable{constructor(n,e,r,s){super();b(this,"_remoteSyncService");this._injector=n,this._remoteInstanceService=e,this._commandService=r,this._rpcChannelService=s,this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(Z,le(this._remoteInstanceService)),this._injector.add([q,{useFactory:()=>fe(this._rpcChannelService.requestChannel(Q))}]),this._remoteSyncService=this._injector.get(q)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((n,e)=>{n.type===l.CommandType.MUTATION&&!(e!=null&&e.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:n})}))}};E=ot([l.OnLifecycle(l.LifecycleStages.Starting,E),A(0,d.Inject(d.Injector)),A(1,j),A(2,l.ICommandService),A(3,I)],E);function ct(){return{send(t){postMessage(t)},onMessage:new S.Observable(t=>{const n=e=>{t.next(e.data)};return addEventListener("message",n),()=>removeEventListener("message",n)}).pipe(S.shareReplay(1))}}function at(t){return{send(n){t.postMessage(n)},onMessage:new S.Observable(n=>{const e=r=>{n.next(r.data)};return t.addEventListener("message",e),()=>t.removeEventListener("message",e)}).pipe(S.shareReplay(1))}}var ut=Object.defineProperty,lt=Object.getOwnPropertyDescriptor,ve=(t,n,e,r)=>{for(var s=r>1?void 0:r?lt(n,e):n,i=t.length-1,o;i>=0;i--)(o=t[i])&&(s=(r?o(n,e,s):o(s))||s);return r&&s&&ut(n,e,s),s},ye=(t,n)=>(e,r)=>n(e,r,t);p.UniverRPCMainThreadPlugin=(N=class extends l.Plugin{constructor(n,e){super("UNIVER_RPC_MAIN_THREAD_PLUGIN"),this._config=n,this._injector=e}async onStarting(n){const e=new Worker(this._config.workerURL),r=at(e);[[I,{useFactory:()=>new de(r)}],[R,{useFactory:()=>{var i;return n.createInstance(R,((i=this._config)==null?void 0:i.unsyncMutations)??new Set)}}],[q,{useClass:X}]].forEach(i=>n.add(i))}},b(N,"type",l.PluginType.Univer),N),p.UniverRPCMainThreadPlugin=ve([ye(1,d.Inject(d.Injector))],p.UniverRPCMainThreadPlugin),p.UniverRPCWorkerThreadPlugin=(ee=class extends l.Plugin{constructor(n,e){super("UNIVER_RPC_WORKER_THREAD_PLUGIN"),this._config=n,this._injector=e}onStarting(n){[[E],[I,{useFactory:()=>new de(ct())}],[j,{useClass:K}]].forEach(e=>n.add(e)),n.get(E)}},b(ee,"type",l.PluginType.Univer),ee),p.UniverRPCWorkerThreadPlugin=ve([ye(1,d.Inject(d.Injector))],p.UniverRPCWorkerThreadPlugin),p.ChannelClient=he,p.ChannelServer=pe,p.IRPChannelService=I,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "@univerjs/rpc",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.2",
4
4
  "keywords": [],
5
5
  "author": "DreamNum <developer@univer.ai>",
6
- "license": "MIT",
6
+ "license": "Apache-2.0",
7
7
  "main": "./lib/cjs/index.js",
8
- "module": "./lib/esm/index.js",
8
+ "module": "./lib/es/index.js",
9
9
  "types": "./lib/types/index.d.ts",
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
13
  "exports": {
14
14
  ".": {
15
- "import": "./lib/esm/index.js",
15
+ "import": "./lib/es/index.js",
16
16
  "require": "./lib/cjs/index.js",
17
17
  "types": "./lib/types/index.d.ts"
18
18
  },
19
19
  "./*": {
20
- "import": "./lib/esm/*",
20
+ "import": "./lib/es/*",
21
21
  "require": "./lib/cjs/*",
22
22
  "types": "./lib/types/index.d.ts"
23
23
  }
@@ -29,20 +29,24 @@
29
29
  "lib"
30
30
  ],
31
31
  "private": false,
32
- "dependencies": {
33
- "@wendellhu/redi": "^0.12.11",
34
- "rxjs": "^7.8.1",
35
- "@univerjs/core": "0.1.0-alpha.1"
36
- },
37
32
  "devDependencies": {
38
33
  "@types/numeral": "^2.0.5",
39
- "@vitest/coverage-istanbul": "^0.34.6",
40
- "esbuild": "^0.19.8",
41
- "ts-node": "^10.9.1",
42
- "typescript": "^5.3.2",
43
- "vitest": "^0.34.6"
34
+ "@vitest/coverage-istanbul": "^1.0.2",
35
+ "@wendellhu/redi": "^0.12.12",
36
+ "rxjs": "^7.8.1",
37
+ "typescript": "^5.3.3",
38
+ "vite": "^5.0.7",
39
+ "vite-plugin-dts": "^3.6.4",
40
+ "vite-plugin-externals": "^0.6.2",
41
+ "vitest": "^1.0.2",
42
+ "@univerjs/core": "0.1.0-alpha.2"
43
+ },
44
+ "peerDependencies": {
45
+ "@wendellhu/redi": ">=0.12.12",
46
+ "rxjs": ">=7.0.0",
47
+ "@univerjs/core": "0.1.0-alpha.2"
44
48
  },
45
49
  "scripts": {
46
- "esbuild": "node ./esbuild.config.mjs && tsc -p tsconfig.esbuild.json"
50
+ "build": "tsc && vite build"
47
51
  }
48
52
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2020-present, DreamNum
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.