@apollo-office/rpc 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/MODIFICATIONS.md +26 -0
- package/NOTICE +14 -0
- package/README.md +43 -0
- package/lib/cjs/index.js +749 -0
- package/lib/es/index.js +701 -0
- package/lib/index.js +701 -0
- package/lib/types/config/config.d.ts +26 -0
- package/lib/types/controllers/data-sync/data-sync-primary.controller.d.ts +44 -0
- package/lib/types/controllers/data-sync/data-sync-replica.controller.d.ts +32 -0
- package/lib/types/index.d.ts +24 -0
- package/lib/types/plugin.d.ts +46 -0
- package/lib/types/services/remote-instance/remote-instance.service.d.ts +82 -0
- package/lib/types/services/rpc/channel.service.d.ts +33 -0
- package/lib/types/services/rpc/implementations/web-worker-rpc.service.d.ts +27 -0
- package/lib/types/services/rpc/rpc.service.d.ts +87 -0
- package/lib/umd/index.js +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 declare const PLUGIN_CONFIG_KEY_MAIN_THREAD = "rpc.main-thread.config";
|
|
17
|
+
export declare const configSymbolMainThread: unique symbol;
|
|
18
|
+
export interface IUniverRPCMainThreadConfig {
|
|
19
|
+
workerURL?: string | URL | Worker;
|
|
20
|
+
}
|
|
21
|
+
export declare const defaultPluginMainThreadConfig: IUniverRPCMainThreadConfig;
|
|
22
|
+
export declare const PLUGIN_CONFIG_KEY_WORKER_THREAD = "rpc.worker-thread.config";
|
|
23
|
+
export declare const configSymbolWorkerThread: unique symbol;
|
|
24
|
+
export interface IUniverRPCWorkerThreadConfig {
|
|
25
|
+
}
|
|
26
|
+
export declare const defaultPluginWorkerThreadConfig: IUniverRPCWorkerThreadConfig;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 { IDisposable, IMutation } from '@apollo-office/core';
|
|
17
|
+
import { ICommandService, Injector, IUniverInstanceService, RxDisposable } from '@apollo-office/core';
|
|
18
|
+
import { IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';
|
|
19
|
+
import { IRPCChannelService } from '../../services/rpc/channel.service';
|
|
20
|
+
/**
|
|
21
|
+
* This controller is responsible for syncing data from the primary thread to
|
|
22
|
+
* the worker thread.
|
|
23
|
+
*
|
|
24
|
+
* Note that only spreadsheets will be synced to the remote calculation instance by default.
|
|
25
|
+
*/
|
|
26
|
+
export declare class DataSyncPrimaryController extends RxDisposable {
|
|
27
|
+
private readonly _injector;
|
|
28
|
+
private readonly _commandService;
|
|
29
|
+
private readonly _univerInstanceService;
|
|
30
|
+
private readonly _rpcChannelService;
|
|
31
|
+
private readonly _remoteSyncService;
|
|
32
|
+
private _remoteInstanceService;
|
|
33
|
+
private readonly _syncingUnits;
|
|
34
|
+
private readonly _syncingMutations;
|
|
35
|
+
constructor(_injector: Injector, _commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _rpcChannelService: IRPCChannelService, _remoteSyncService: IRemoteSyncService);
|
|
36
|
+
registerSyncingMutations(mutation: IMutation<object>): void;
|
|
37
|
+
/**
|
|
38
|
+
* Only spreadsheets would be synced to the web worker in normal situations. If you would like to
|
|
39
|
+
* sync other types of documents, you should manually call this method with that document's id.
|
|
40
|
+
*/
|
|
41
|
+
syncUnit(unitId: string): IDisposable;
|
|
42
|
+
private _initRPCChannels;
|
|
43
|
+
private _init;
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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, Injector } from '@apollo-office/core';
|
|
17
|
+
import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';
|
|
18
|
+
import { IRPCChannelService } from '../../services/rpc/channel.service';
|
|
19
|
+
/**
|
|
20
|
+
* This controller is responsible for syncing data from the worker thread to
|
|
21
|
+
* the primary thread.
|
|
22
|
+
*/
|
|
23
|
+
export declare class DataSyncReplicaController extends Disposable {
|
|
24
|
+
private readonly _injector;
|
|
25
|
+
private readonly _remoteInstanceService;
|
|
26
|
+
private readonly _commandService;
|
|
27
|
+
private readonly _rpcChannelService;
|
|
28
|
+
private _remoteSyncService;
|
|
29
|
+
constructor(_injector: Injector, _remoteInstanceService: IRemoteInstanceService, _commandService: ICommandService, _rpcChannelService: IRPCChannelService);
|
|
30
|
+
private _initRPCChannels;
|
|
31
|
+
private _init;
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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, IUniverRPCWorkerThreadConfig } from './config/config';
|
|
17
|
+
export { PLUGIN_CONFIG_KEY_MAIN_THREAD, PLUGIN_CONFIG_KEY_WORKER_THREAD } from './config/config';
|
|
18
|
+
export { DataSyncPrimaryController } from './controllers/data-sync/data-sync-primary.controller';
|
|
19
|
+
export { DataSyncReplicaController } from './controllers/data-sync/data-sync-replica.controller';
|
|
20
|
+
export { UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugin';
|
|
21
|
+
export { IRemoteInstanceService, RemoteInstanceServiceName, RemoteSyncPrimaryService, RemoteSyncServiceName, WebWorkerRemoteInstanceService, } from './services/remote-instance/remote-instance.service';
|
|
22
|
+
export { IRemoteSyncService } from './services/remote-instance/remote-instance.service';
|
|
23
|
+
export { ChannelService, IRPCChannelService } from './services/rpc/channel.service';
|
|
24
|
+
export { ChannelClient, ChannelServer, fromModule, type IMessageProtocol, toModule } from './services/rpc/rpc.service';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 { IUniverRPCMainThreadConfig, IUniverRPCWorkerThreadConfig } from './config/config';
|
|
17
|
+
import { IConfigService, Injector, Plugin } from '@apollo-office/core';
|
|
18
|
+
/**
|
|
19
|
+
* This plugin is used to register the RPC services on the main thread. It
|
|
20
|
+
* is also responsible for booting up the Web Worker instance of Univer.
|
|
21
|
+
*/
|
|
22
|
+
export declare class UniverRPCMainThreadPlugin extends Plugin {
|
|
23
|
+
private readonly _config;
|
|
24
|
+
protected readonly _injector: Injector;
|
|
25
|
+
private readonly _configService;
|
|
26
|
+
static pluginName: string;
|
|
27
|
+
static packageName: string;
|
|
28
|
+
static version: string;
|
|
29
|
+
private _internalWorker;
|
|
30
|
+
constructor(_config: Partial<IUniverRPCMainThreadConfig> | undefined, _injector: Injector, _configService: IConfigService);
|
|
31
|
+
dispose(): void;
|
|
32
|
+
onStarting(): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* This plugin is used to register the RPC services on the worker thread.
|
|
36
|
+
*/
|
|
37
|
+
export declare class UniverRPCWorkerThreadPlugin extends Plugin {
|
|
38
|
+
private readonly _config;
|
|
39
|
+
protected readonly _injector: Injector;
|
|
40
|
+
private readonly _configService;
|
|
41
|
+
static pluginName: string;
|
|
42
|
+
static packageName: string;
|
|
43
|
+
static version: string;
|
|
44
|
+
constructor(_config: Partial<IUniverRPCWorkerThreadConfig> | undefined, _injector: Injector, _configService: IConfigService);
|
|
45
|
+
onStarting(): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 { IExecutionOptions, IMutationInfo, IWorkbookData } from '@apollo-office/core';
|
|
17
|
+
import { ICommandService, ILogService, IUniverInstanceService, UniverInstanceType } from '@apollo-office/core';
|
|
18
|
+
export interface IRemoteSyncMutationOptions extends IExecutionOptions {
|
|
19
|
+
/** If this mutation is executed after it was sent from the peer univer instance (e.g. in a web worker). */
|
|
20
|
+
fromSync?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare const RemoteSyncServiceName = "rpc.remote-sync.service";
|
|
23
|
+
/**
|
|
24
|
+
* This service is provided by the primary Univer.
|
|
25
|
+
*
|
|
26
|
+
* Replica Univer could call this service to update mutations back to the primary Univer.
|
|
27
|
+
*/
|
|
28
|
+
export declare const IRemoteSyncService: import("@wendellhu/redi").IdentifierDecorator<IRemoteSyncService>;
|
|
29
|
+
export interface IRemoteSyncService {
|
|
30
|
+
syncMutation(params: {
|
|
31
|
+
mutationInfo: IMutationInfo;
|
|
32
|
+
}, options?: IExecutionOptions): Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
export declare class RemoteSyncPrimaryService implements IRemoteSyncService {
|
|
35
|
+
private readonly _commandService;
|
|
36
|
+
constructor(_commandService: ICommandService);
|
|
37
|
+
syncMutation(params: {
|
|
38
|
+
mutationInfo: IMutationInfo;
|
|
39
|
+
}, options?: IExecutionOptions): Promise<boolean>;
|
|
40
|
+
}
|
|
41
|
+
export declare const RemoteInstanceServiceName = "univer.remote-instance-service";
|
|
42
|
+
/**
|
|
43
|
+
* This service is provided by the replica Univer.
|
|
44
|
+
*
|
|
45
|
+
* Primary univer could call this service to init and dispose univer business instances
|
|
46
|
+
* and sync mutations to replica univer.
|
|
47
|
+
*/
|
|
48
|
+
export declare const IRemoteInstanceService: import("@wendellhu/redi").IdentifierDecorator<IRemoteInstanceService>;
|
|
49
|
+
export interface IRemoteInstanceService {
|
|
50
|
+
/** Tell other modules if the `IRemoteInstanceService` is ready to load files. */
|
|
51
|
+
whenReady(): Promise<true>;
|
|
52
|
+
createInstance(params: {
|
|
53
|
+
unitID: string;
|
|
54
|
+
type: UniverInstanceType;
|
|
55
|
+
snapshot: IWorkbookData;
|
|
56
|
+
}): Promise<boolean>;
|
|
57
|
+
disposeInstance(params: {
|
|
58
|
+
unitID: string;
|
|
59
|
+
}): Promise<boolean>;
|
|
60
|
+
syncMutation(params: {
|
|
61
|
+
mutationInfo: IMutationInfo;
|
|
62
|
+
}, options?: IExecutionOptions): Promise<boolean>;
|
|
63
|
+
}
|
|
64
|
+
export declare class WebWorkerRemoteInstanceService implements IRemoteInstanceService {
|
|
65
|
+
protected readonly _univerInstanceService: IUniverInstanceService;
|
|
66
|
+
protected readonly _commandService: ICommandService;
|
|
67
|
+
protected readonly _logService: ILogService;
|
|
68
|
+
constructor(_univerInstanceService: IUniverInstanceService, _commandService: ICommandService, _logService: ILogService);
|
|
69
|
+
whenReady(): Promise<true>;
|
|
70
|
+
syncMutation(params: {
|
|
71
|
+
mutationInfo: IMutationInfo;
|
|
72
|
+
}, options?: IExecutionOptions): Promise<boolean>;
|
|
73
|
+
createInstance(params: {
|
|
74
|
+
unitID: string;
|
|
75
|
+
type: UniverInstanceType;
|
|
76
|
+
snapshot: IWorkbookData;
|
|
77
|
+
}): Promise<boolean>;
|
|
78
|
+
disposeInstance(params: {
|
|
79
|
+
unitID: string;
|
|
80
|
+
}): Promise<boolean>;
|
|
81
|
+
protected _applyMutation(mutationInfo: IMutationInfo, options?: IExecutionOptions): boolean;
|
|
82
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 { IDisposable } from '@apollo-office/core';
|
|
17
|
+
import type { IChannel, IMessageProtocol } from './rpc.service';
|
|
18
|
+
export interface IRPCChannelService {
|
|
19
|
+
requestChannel(name: string): IChannel;
|
|
20
|
+
registerChannel(name: string, channel: IChannel): void;
|
|
21
|
+
}
|
|
22
|
+
export declare const IRPCChannelService: import("@wendellhu/redi").IdentifierDecorator<IRPCChannelService>;
|
|
23
|
+
/**
|
|
24
|
+
* This service is responsible for managing the RPC channels.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ChannelService implements IDisposable {
|
|
27
|
+
private readonly _client;
|
|
28
|
+
private readonly _server;
|
|
29
|
+
constructor(_messageProtocol: IMessageProtocol);
|
|
30
|
+
dispose(): void;
|
|
31
|
+
requestChannel(name: string): IChannel;
|
|
32
|
+
registerChannel(name: string, channel: IChannel): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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
|
+
* @returns A protocol wrapper around worker global messaging APIs.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createWebWorkerMessagePortOnWorker(): IMessageProtocol;
|
|
22
|
+
/**
|
|
23
|
+
* Generate an `IMessageProtocol` on the main thread side.
|
|
24
|
+
* @param worker The Web Worker object
|
|
25
|
+
* @returns A protocol wrapper around the given worker messaging APIs.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createWebWorkerMessagePortOnMain(worker: Worker): IMessageProtocol;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
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 { RxDisposable } from '@apollo-office/core';
|
|
17
|
+
import { Observable } from 'rxjs';
|
|
18
|
+
/** This protocol is for transferring data from the two peer univer instance running in different locations. */
|
|
19
|
+
export interface IMessageProtocol {
|
|
20
|
+
send(message: any): void;
|
|
21
|
+
onMessage: Observable<any>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Channel is a combination of methods and event sources. These methods and
|
|
25
|
+
* event sources are usually provided by the same service or controller.
|
|
26
|
+
*/
|
|
27
|
+
export interface IChannel {
|
|
28
|
+
call<T>(method: string, args?: any): Promise<T>;
|
|
29
|
+
subscribe<T>(event: string, args?: any): Observable<T>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wrapper a service or a controller into a channel so it could be invoked by
|
|
33
|
+
* a remote client. When the protocol is called, it would forward to the
|
|
34
|
+
* underlying service or controller.
|
|
35
|
+
*
|
|
36
|
+
* @param module the wrapper service or controller
|
|
37
|
+
* @returns the channel instance
|
|
38
|
+
*/
|
|
39
|
+
export declare function fromModule(module: unknown): IChannel;
|
|
40
|
+
/**
|
|
41
|
+
* Wrap a channel into a service or a controller so it could be invoked by
|
|
42
|
+
* the upper layer modules. When the service or controller is called, it would
|
|
43
|
+
* request the remote server by calling the channel.
|
|
44
|
+
*
|
|
45
|
+
* @param channel
|
|
46
|
+
* @returns A proxy object that forwards calls/subscriptions to the remote channel.
|
|
47
|
+
*/
|
|
48
|
+
export declare function toModule<T extends object>(channel: IChannel): T;
|
|
49
|
+
export interface IChannelClient {
|
|
50
|
+
getChannel<T extends IChannel>(channelName: string): T;
|
|
51
|
+
}
|
|
52
|
+
export interface IChannelServer {
|
|
53
|
+
registerChannel<T extends IChannel>(channelName: string, channel: T): void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This method provides implementation for `IChannel` and is responsible for
|
|
57
|
+
* transforming a local calling to a RPC calling.
|
|
58
|
+
*/
|
|
59
|
+
export declare class ChannelClient extends RxDisposable implements IChannelClient {
|
|
60
|
+
private readonly _protocol;
|
|
61
|
+
private _initialized;
|
|
62
|
+
private _lastRequestCounter;
|
|
63
|
+
private _pendingRequests;
|
|
64
|
+
constructor(_protocol: IMessageProtocol);
|
|
65
|
+
dispose(): void;
|
|
66
|
+
getChannel<T extends IChannel>(channelName: string): T;
|
|
67
|
+
private _whenReady;
|
|
68
|
+
private _remoteCall;
|
|
69
|
+
private _doRemoteCall;
|
|
70
|
+
private _remoteSubscribe;
|
|
71
|
+
private _sendRequest;
|
|
72
|
+
private _onMessage;
|
|
73
|
+
}
|
|
74
|
+
export declare class ChannelServer extends RxDisposable implements IChannelServer {
|
|
75
|
+
private readonly _protocol;
|
|
76
|
+
private _channels;
|
|
77
|
+
private _subscriptions;
|
|
78
|
+
constructor(_protocol: IMessageProtocol);
|
|
79
|
+
dispose(): void;
|
|
80
|
+
registerChannel(channelName: string, channel: IChannel): void;
|
|
81
|
+
private _onRequest;
|
|
82
|
+
private _sendInitialize;
|
|
83
|
+
private _onMethodCall;
|
|
84
|
+
private _onSubscribe;
|
|
85
|
+
private _onUnsubscribe;
|
|
86
|
+
private _sendResponse;
|
|
87
|
+
}
|
package/lib/umd/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@apollo-office/core"),require("rxjs/operators"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@apollo-office/core`,`rxjs/operators`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverRpc={},e.UniverCore,e.rxjs.operators,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`rpc.main-thread.config`,a={},o=`rpc.worker-thread.config`,s={};function c(e,t){return function(n,r){t(n,r,e)}}function l(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let u=`rpc.remote-sync.service`,d=(0,t.createIdentifier)(u),f=class{constructor(e){this._commandService=e}async syncMutation(e,t){let{fromCollab:n,...r}=t||{};return this._commandService.syncExecuteCommand(e.mutationInfo.id,e.mutationInfo.params,{...r,onlyLocal:!0,fromSync:!0})}};f=l([c(0,t.ICommandService)],f);let p=`univer.remote-instance-service`,m=(0,t.createIdentifier)(p),h=class{constructor(e,t,n){this._univerInstanceService=e,this._commandService=t,this._logService=n}whenReady(){return Promise.resolve(!0)}async syncMutation(e,t){return this._applyMutation(e.mutationInfo,t)}async createInstance(e){this._logService.debug(`[WebWorkerRemoteInstanceService]: Creating instance with id ${e.unitID}`);let{type:n,snapshot:r}=e;try{switch(n){case t.UniverInstanceType.UNIVER_SHEET:return this._univerInstanceService.createUnit(t.UniverInstanceType.UNIVER_SHEET,r),!0;default:throw Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${n}.`)}}catch(e){throw e instanceof Error?e:TypeError(`${e}`)}}async disposeInstance(e){return this._logService.debug(`[WebWorkerRemoteInstanceService]: Disposing instance with id ${e.unitID}`),this._univerInstanceService.disposeUnit(e.unitID)}_applyMutation(e,t){let{id:n,params:r}=e,{fromCollab:i,...a}=t||{};return this._commandService.syncExecuteCommand(n,r,{...a,onlyLocal:!0,fromSync:!0})}};h=l([c(0,t.IUniverInstanceService),c(1,t.ICommandService),c(2,t.ILogService)],h);function g(e){"@babel/helpers - typeof";return g=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function v(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function y(e,t,n){return(t=v(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function b(e){let t=e;return new class{call(e,n){let r=t[e];if(typeof r==`function`){let e=n?r.apply(t,n):r.call(t);return e instanceof Promise||(e=Promise.resolve(e)),e}throw Error(`[RPC]: method not found for ${e}!`)}subscribe(e,n){let i=t[e];if(typeof i==`function`){let e=n?i.apply(t,n):i.call(t);return(0,r.isObservable)(e)?e:(0,r.of)(e)}throw Error(`[RPC]: observable method not found for ${e}!`)}}}function x(e){return new Proxy({},{get(t,n){if(n!==`dispose`)return function(...t){return S(n)?e.subscribe(n,t):e.call(n,t)}}})}function S(e){return e.endsWith(`$`)}var C=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_initialized`,new r.BehaviorSubject(!1)),y(this,`_lastRequestCounter`,0),y(this,`_pendingRequests`,new Map),this._protocol.send({type:50}),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onMessage(e))}dispose(){this._pendingRequests.clear()}getChannel(e){return{call:(t,n)=>this._disposed?Promise.reject(Error(`[ChannelClient]: client is disposed!`)):this._remoteCall(e,t,n),subscribe:(t,n)=>{if(this._disposed)throw Error(`[ChannelClient]: client is disposed!`);return this._remoteSubscribe(e,t,n)}}}_whenReady(){return(0,r.firstValueFrom)(this._initialized.pipe((0,n.filter)(e=>e),(0,n.take)(1)))}_remoteCall(e,t,n){return this._initialized.getValue()?this._doRemoteCall(e,t,n):this._whenReady().then(()=>this._doRemoteCall(e,t,n))}_doRemoteCall(e,t,n){let r=++this._lastRequestCounter,i={seq:r,type:100,channelName:e,method:t,args:n},a=this._pendingRequests;return new Promise((e,t)=>{this._pendingRequests.set(r,{handle(n){switch(n.type){case 201:a.delete(r),e(n.data);break;case 202:a.delete(r),t(n.data);break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(i)})}_remoteSubscribe(e,t,n){return new r.Observable(r=>{let i=-1,a=()=>{i=++this._lastRequestCounter;let a={seq:i,type:101,channelName:e,method:t,args:n};this._pendingRequests.set(i,{handle(e){switch(e.type){case 300:r.next(e.data);break;case 301:r.error(e.data);break;case 302:r.complete();break;default:throw Error(`[ChannelClient]: unknown response type!`)}}}),this._sendRequest(a)};return this._initialized.getValue()?a():this._whenReady().then(a),()=>{if(i===-1)return;let n={type:102,seq:i,channelName:e,method:t};this._sendRequest(n)}})}_sendRequest(e){this._protocol.send(e)}_onMessage(e){let{type:t,seq:n}=e;switch(t){case 0:this._initialized.next(!0);break;case 201:case 202:case 300:case 302:case 301:{var r;let{_pendingRequests:i}=this;(r=i.get(n))==null||r.handle(e),t!==300&&i.delete(n);break}}}},w=class extends t.RxDisposable{constructor(e){super(),this._protocol=e,y(this,`_channels`,new Map),y(this,`_subscriptions`,new Map),this._protocol.onMessage.pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>this._onRequest(e)),this._sendInitialize()}dispose(){super.dispose(),this._subscriptions.clear(),this._channels.clear()}registerChannel(e,t){this._channels.set(e,t)}_onRequest(e){switch(e.type){case 50:this._sendInitialize();break;case 100:this._onMethodCall(e);break;case 101:this._onSubscribe(e);break;case 102:this._onUnsubscribe(e);break;default:break}}_sendInitialize(){this._sendResponse({seq:-1,type:0})}_onMethodCall(e){let{channelName:t,method:n,args:r}=e,i=this._channels.get(t),a;try{if(!i)throw Error(`[ChannelServer]: Channel ${t} not found!`);a=r?i.call(n,r):i.call(n)}catch(e){a=Promise.reject(e instanceof Error?e:Error(String(e)))}a.then(t=>{this._sendResponse({seq:e.seq,type:201,data:t})}).catch(t=>{t instanceof Error?this._sendResponse({seq:e.seq,type:202,data:t.message}):this._sendResponse({seq:e.seq,type:202,data:String(t)})})}_onSubscribe(e){let{channelName:t,seq:n}=e,r=this._channels.get(t);try{if(!r)throw Error(`[ChannelServer]: Channel ${t} not found!`);let i=r.subscribe(e.method,e.args).subscribe({next:e=>{this._sendResponse({seq:n,type:300,data:e})},error:e=>{this._sendResponse({seq:n,type:301,data:e.message}),this._sendResponse({seq:n,type:302})},complete:()=>{this._sendResponse({seq:n,type:302})}});this._subscriptions.set(e.seq,i)}catch(t){t instanceof Error?this._sendResponse({seq:e.seq,type:301,data:t.message}):this._sendResponse({seq:e.seq,type:301,data:String(t)})}}_onUnsubscribe(e){let t=this._subscriptions.get(e.seq);t&&(t.unsubscribe(),this._subscriptions.delete(e.seq))}_sendResponse(e){this._protocol.send(e)}};let T=(0,t.createIdentifier)(`IRPCChannelService`);var E=class{constructor(e){y(this,`_client`,void 0),y(this,`_server`,void 0),this._client=new C(e),this._server=new w(e)}dispose(){this._client.dispose(),this._server.dispose()}requestChannel(e){return this._client.getChannel(e)}registerChannel(e,t){this._server.registerChannel(e,t)}};let D=class extends t.RxDisposable{constructor(e,t,n,r,i){super(),this._injector=e,this._commandService=t,this._univerInstanceService=n,this._rpcChannelService=r,this._remoteSyncService=i,y(this,`_remoteInstanceService`,void 0),y(this,`_syncingUnits`,new Set),y(this,`_syncingMutations`,new Set),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){return this._syncingUnits.add(e),(0,t.toDisposable)(()=>this._syncingUnits.delete(e))}_initRPCChannels(){this._rpcChannelService.registerChannel(u,b(this._remoteSyncService)),this._injector.add([m,{useFactory:()=>x(this._rpcChannelService.requestChannel(p))}]),this._remoteInstanceService=this._injector.get(m)}_init(){this._univerInstanceService.getTypeOfUnitAdded$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{let{unit:n}=e;this._syncingUnits.add(n.getUnitId()),this._remoteInstanceService.createInstance({unitID:n.getUnitId(),type:t.UniverInstanceType.UNIVER_SHEET,snapshot:n.getSnapshot()})}),this._univerInstanceService.getTypeOfUnitDisposed$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,n.takeUntil)(this.dispose$)).subscribe(e=>{this._syncingUnits.delete(e.getUnitId()),this._remoteInstanceService.disposeInstance({unitID:e.getUnitId()})}),this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{let{type:r,params:i,id:a}=e,o=(i==null?void 0:i.unitId)||``;r===t.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)&&this._remoteInstanceService.syncMutation({mutationInfo:e},n)}))}};D=l([c(0,(0,t.Inject)(t.Injector)),c(1,t.ICommandService),c(2,t.IUniverInstanceService),c(3,T),c(4,d)],D);let O=class extends t.Disposable{constructor(e,t,n,r){super(),this._injector=e,this._remoteInstanceService=t,this._commandService=n,this._rpcChannelService=r,y(this,`_remoteSyncService`,void 0),this._initRPCChannels(),this._init()}_initRPCChannels(){this._rpcChannelService.registerChannel(p,b(this._remoteInstanceService)),this._injector.add([d,{useFactory:()=>x(this._rpcChannelService.requestChannel(u))}]),this._remoteSyncService=this._injector.get(d)}_init(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{e.type===t.CommandType.MUTATION&&!(n!=null&&n.fromSync)&&this._remoteSyncService.syncMutation({mutationInfo:e},n)}))}};O=l([c(0,(0,t.Inject)(t.Injector)),c(1,m),c(2,t.ICommandService),c(3,T)],O);var k=`@apollo-office/rpc`,A=`0.25.1`;function j(){return{send(e){postMessage(e)},onMessage:new r.Observable(e=>{let t=t=>{e.next(t.data)};return addEventListener(`message`,t),()=>removeEventListener(`message`,t)}).pipe((0,r.shareReplay)({bufferSize:1,refCount:!0}))}}function M(e){return{send(t){e.postMessage(t)},onMessage:new r.Observable(t=>{let n=e=>{t.next(e.data)};return e.addEventListener(`message`,n),()=>e.removeEventListener(`message`,n)}).pipe((0,r.shareReplay)({bufferSize:1,refCount:!0}))}}let N=class extends t.Plugin{constructor(e=a,n,r){super(),this._config=e,this._injector=n,this._configService=r,y(this,`_internalWorker`,null);let{...o}=(0,t.merge)({},a,this._config);this._configService.setConfig(i,o)}dispose(){super.dispose(),this._internalWorker&&(this._internalWorker.terminate(),this._internalWorker=null)}onStarting(){let{workerURL:e}=this._config;if(!e)throw Error(`[UniverRPCMainThreadPlugin]: The workerURL is required for the RPC main thread plugin.`);let t=e instanceof Worker?e:new Worker(e);this._internalWorker=e instanceof Worker?null:t;let n=M(t);[[T,{useFactory:()=>new E(n)}],[D],[d,{useClass:f}]].forEach(e=>this._injector.add(e)),this._injector.get(D)}};y(N,`pluginName`,`UNIVER_RPC_MAIN_THREAD_PLUGIN`),y(N,`packageName`,k),y(N,`version`,A),N=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],N);let P=class extends t.Plugin{constructor(e=s,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},s,this._config);this._configService.setConfig(o,i)}onStarting(){[[O],[T,{useFactory:()=>new E(j())}],[m,{useClass:h}]].forEach(e=>this._injector.add(e)),this._injector.get(O)}};y(P,`pluginName`,`UNIVER_RPC_WORKER_THREAD_PLUGIN`),y(P,`packageName`,k),y(P,`version`,A),P=l([c(1,(0,t.Inject)(t.Injector)),c(2,t.IConfigService)],P),e.ChannelClient=C,e.ChannelServer=w,e.ChannelService=E,Object.defineProperty(e,"DataSyncPrimaryController",{enumerable:!0,get:function(){return D}}),Object.defineProperty(e,"DataSyncReplicaController",{enumerable:!0,get:function(){return O}}),e.IRPCChannelService=T,e.IRemoteInstanceService=m,e.IRemoteSyncService=d,e.PLUGIN_CONFIG_KEY_MAIN_THREAD=i,e.PLUGIN_CONFIG_KEY_WORKER_THREAD=o,e.RemoteInstanceServiceName=p,Object.defineProperty(e,"RemoteSyncPrimaryService",{enumerable:!0,get:function(){return f}}),e.RemoteSyncServiceName=u,Object.defineProperty(e,"UniverRPCMainThreadPlugin",{enumerable:!0,get:function(){return N}}),Object.defineProperty(e,"UniverRPCWorkerThreadPlugin",{enumerable:!0,get:function(){return P}}),Object.defineProperty(e,"WebWorkerRemoteInstanceService",{enumerable:!0,get:function(){return h}}),e.fromModule=b,e.toModule=x});
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@apollo-office/rpc",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Browser RPC layer for Univer main-thread and worker communication.",
|
|
6
|
+
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://code.srdcloud.cn/YYZBTX/univer"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"univer",
|
|
14
|
+
"rpc",
|
|
15
|
+
"worker",
|
|
16
|
+
"message-channel",
|
|
17
|
+
"plugin"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./lib/es/index.js",
|
|
22
|
+
"require": "./lib/cjs/index.js",
|
|
23
|
+
"types": "./lib/types/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./*": {
|
|
26
|
+
"import": "./lib/es/*",
|
|
27
|
+
"require": "./lib/cjs/*",
|
|
28
|
+
"types": "./lib/types/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./lib/*": "./lib/*"
|
|
31
|
+
},
|
|
32
|
+
"main": "./lib/es/index.js",
|
|
33
|
+
"types": "./lib/types/index.d.ts",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"registry": "https://registry.npmjs.org/"
|
|
37
|
+
},
|
|
38
|
+
"directories": {
|
|
39
|
+
"lib": "lib"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"lib",
|
|
43
|
+
"LICENSE",
|
|
44
|
+
"NOTICE",
|
|
45
|
+
"MODIFICATIONS.md"
|
|
46
|
+
],
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"rxjs": ">=7.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@apollo-office/core": "1.0.0"
|
|
52
|
+
},
|
|
53
|
+
"module": "./lib/es/index.js",
|
|
54
|
+
"apolloOffice": {
|
|
55
|
+
"productVersion": "1.0.0",
|
|
56
|
+
"sourceCommit": "db6464ec2310d00322f395ce48f2a004219f3d3a",
|
|
57
|
+
"upstreamRepository": "https://github.com/dream-num/univer.git",
|
|
58
|
+
"upstreamTag": "v0.25.1",
|
|
59
|
+
"upstreamCommit": "c9c8607471013f110b7a401d79bfb248e59faa1c",
|
|
60
|
+
"derivativeDistribution": true,
|
|
61
|
+
"officialUniverRelease": false
|
|
62
|
+
}
|
|
63
|
+
}
|