@univerjs/rpc 0.25.1 → 1.0.0-alpha.1
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 +27 -9
- package/lib/es/index.js +27 -9
- package/lib/index.js +27 -9
- package/lib/types/controllers/data-sync/data-sync-primary.controller.d.ts +1 -0
- package/lib/types/index.d.ts +2 -1
- package/lib/types/services/remote-instance/remote-instance.service.d.ts +3 -3
- package/lib/umd/index.js +1 -1
- package/package.json +4 -4
package/lib/cjs/index.js
CHANGED
|
@@ -27,7 +27,7 @@ const configSymbolWorkerThread = Symbol(PLUGIN_CONFIG_KEY_WORKER_THREAD);
|
|
|
27
27
|
const defaultPluginWorkerThreadConfig = {};
|
|
28
28
|
|
|
29
29
|
//#endregion
|
|
30
|
-
//#region \0@oxc-project+runtime@0.
|
|
30
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
|
|
31
31
|
function __decorateParam(paramIndex, decorator) {
|
|
32
32
|
return function(target, key) {
|
|
33
33
|
decorator(target, key, paramIndex);
|
|
@@ -35,7 +35,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
//#endregion
|
|
38
|
-
//#region \0@oxc-project+runtime@0.
|
|
38
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
|
|
39
39
|
function __decorate(decorators, target, key, desc) {
|
|
40
40
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
41
41
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -94,6 +94,9 @@ let WebWorkerRemoteInstanceService = class WebWorkerRemoteInstanceService {
|
|
|
94
94
|
case _univerjs_core.UniverInstanceType.UNIVER_SHEET:
|
|
95
95
|
this._univerInstanceService.createUnit(_univerjs_core.UniverInstanceType.UNIVER_SHEET, snapshot);
|
|
96
96
|
return true;
|
|
97
|
+
case _univerjs_core.UniverInstanceType.UNIVER_BASE:
|
|
98
|
+
this._univerInstanceService.createUnit(_univerjs_core.UniverInstanceType.UNIVER_BASE, snapshot);
|
|
99
|
+
return true;
|
|
97
100
|
default: throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${type}.`);
|
|
98
101
|
}
|
|
99
102
|
} catch (err) {
|
|
@@ -122,7 +125,7 @@ WebWorkerRemoteInstanceService = __decorate([
|
|
|
122
125
|
], WebWorkerRemoteInstanceService);
|
|
123
126
|
|
|
124
127
|
//#endregion
|
|
125
|
-
//#region \0@oxc-project+runtime@0.
|
|
128
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
|
|
126
129
|
function _typeof(o) {
|
|
127
130
|
"@babel/helpers - typeof";
|
|
128
131
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -133,7 +136,7 @@ function _typeof(o) {
|
|
|
133
136
|
}
|
|
134
137
|
|
|
135
138
|
//#endregion
|
|
136
|
-
//#region \0@oxc-project+runtime@0.
|
|
139
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
|
|
137
140
|
function toPrimitive(t, r) {
|
|
138
141
|
if ("object" != _typeof(t) || !t) return t;
|
|
139
142
|
var e = t[Symbol.toPrimitive];
|
|
@@ -146,14 +149,14 @@ function toPrimitive(t, r) {
|
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
//#endregion
|
|
149
|
-
//#region \0@oxc-project+runtime@0.
|
|
152
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
|
|
150
153
|
function toPropertyKey(t) {
|
|
151
154
|
var i = toPrimitive(t, "string");
|
|
152
155
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
//#endregion
|
|
156
|
-
//#region \0@oxc-project+runtime@0.
|
|
159
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
|
|
157
160
|
function _defineProperty(e, r, t) {
|
|
158
161
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
159
162
|
value: t,
|
|
@@ -508,6 +511,7 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends _univerj
|
|
|
508
511
|
_defineProperty(this, "_remoteInstanceService", void 0);
|
|
509
512
|
_defineProperty(this, "_syncingUnits", /* @__PURE__ */ new Set());
|
|
510
513
|
_defineProperty(this, "_syncingMutations", /* @__PURE__ */ new Set());
|
|
514
|
+
_defineProperty(this, "_syncMutationQueue", Promise.resolve());
|
|
511
515
|
this._initRPCChannels();
|
|
512
516
|
this._init();
|
|
513
517
|
}
|
|
@@ -519,8 +523,19 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends _univerj
|
|
|
519
523
|
* sync other types of documents, you should manually call this method with that document's id.
|
|
520
524
|
*/
|
|
521
525
|
syncUnit(unitId) {
|
|
526
|
+
var _this$_univerInstance;
|
|
527
|
+
const alreadySyncing = this._syncingUnits.has(unitId);
|
|
522
528
|
this._syncingUnits.add(unitId);
|
|
523
|
-
|
|
529
|
+
const unit = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_SHEET)) !== null && _this$_univerInstance !== void 0 ? _this$_univerInstance : this._univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_BASE);
|
|
530
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.createInstance({
|
|
531
|
+
unitID: unit.getUnitId(),
|
|
532
|
+
type: unit.type,
|
|
533
|
+
snapshot: unit.getSnapshot()
|
|
534
|
+
});
|
|
535
|
+
return (0, _univerjs_core.toDisposable)(() => {
|
|
536
|
+
if (!alreadySyncing) this._syncingUnits.delete(unitId);
|
|
537
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.disposeInstance({ unitID: unit.getUnitId() });
|
|
538
|
+
});
|
|
524
539
|
}
|
|
525
540
|
_initRPCChannels() {
|
|
526
541
|
this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));
|
|
@@ -544,7 +559,10 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends _univerj
|
|
|
544
559
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo, options) => {
|
|
545
560
|
const { type, params, id } = commandInfo;
|
|
546
561
|
const unitId = (params === null || params === void 0 ? void 0 : params.unitId) || "";
|
|
547
|
-
if (type === _univerjs_core.CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id))
|
|
562
|
+
if (type === _univerjs_core.CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id)) {
|
|
563
|
+
const sync = () => this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo }, options);
|
|
564
|
+
this._syncMutationQueue = this._syncMutationQueue.then(sync, sync).catch(() => false);
|
|
565
|
+
}
|
|
548
566
|
}));
|
|
549
567
|
}
|
|
550
568
|
};
|
|
@@ -590,7 +608,7 @@ DataSyncReplicaController = __decorate([
|
|
|
590
608
|
//#endregion
|
|
591
609
|
//#region package.json
|
|
592
610
|
var name = "@univerjs/rpc";
|
|
593
|
-
var version = "0.
|
|
611
|
+
var version = "1.0.0-alpha.1";
|
|
594
612
|
|
|
595
613
|
//#endregion
|
|
596
614
|
//#region src/services/rpc/implementations/web-worker-rpc.service.ts
|
package/lib/es/index.js
CHANGED
|
@@ -26,7 +26,7 @@ const configSymbolWorkerThread = Symbol(PLUGIN_CONFIG_KEY_WORKER_THREAD);
|
|
|
26
26
|
const defaultPluginWorkerThreadConfig = {};
|
|
27
27
|
|
|
28
28
|
//#endregion
|
|
29
|
-
//#region \0@oxc-project+runtime@0.
|
|
29
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
|
|
30
30
|
function __decorateParam(paramIndex, decorator) {
|
|
31
31
|
return function(target, key) {
|
|
32
32
|
decorator(target, key, paramIndex);
|
|
@@ -34,7 +34,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
//#endregion
|
|
37
|
-
//#region \0@oxc-project+runtime@0.
|
|
37
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
|
|
38
38
|
function __decorate(decorators, target, key, desc) {
|
|
39
39
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
40
40
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -93,6 +93,9 @@ let WebWorkerRemoteInstanceService = class WebWorkerRemoteInstanceService {
|
|
|
93
93
|
case UniverInstanceType.UNIVER_SHEET:
|
|
94
94
|
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, snapshot);
|
|
95
95
|
return true;
|
|
96
|
+
case UniverInstanceType.UNIVER_BASE:
|
|
97
|
+
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_BASE, snapshot);
|
|
98
|
+
return true;
|
|
96
99
|
default: throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${type}.`);
|
|
97
100
|
}
|
|
98
101
|
} catch (err) {
|
|
@@ -121,7 +124,7 @@ WebWorkerRemoteInstanceService = __decorate([
|
|
|
121
124
|
], WebWorkerRemoteInstanceService);
|
|
122
125
|
|
|
123
126
|
//#endregion
|
|
124
|
-
//#region \0@oxc-project+runtime@0.
|
|
127
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
|
|
125
128
|
function _typeof(o) {
|
|
126
129
|
"@babel/helpers - typeof";
|
|
127
130
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -132,7 +135,7 @@ function _typeof(o) {
|
|
|
132
135
|
}
|
|
133
136
|
|
|
134
137
|
//#endregion
|
|
135
|
-
//#region \0@oxc-project+runtime@0.
|
|
138
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
|
|
136
139
|
function toPrimitive(t, r) {
|
|
137
140
|
if ("object" != _typeof(t) || !t) return t;
|
|
138
141
|
var e = t[Symbol.toPrimitive];
|
|
@@ -145,14 +148,14 @@ function toPrimitive(t, r) {
|
|
|
145
148
|
}
|
|
146
149
|
|
|
147
150
|
//#endregion
|
|
148
|
-
//#region \0@oxc-project+runtime@0.
|
|
151
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
|
|
149
152
|
function toPropertyKey(t) {
|
|
150
153
|
var i = toPrimitive(t, "string");
|
|
151
154
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
//#endregion
|
|
155
|
-
//#region \0@oxc-project+runtime@0.
|
|
158
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
|
|
156
159
|
function _defineProperty(e, r, t) {
|
|
157
160
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
158
161
|
value: t,
|
|
@@ -507,6 +510,7 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
507
510
|
_defineProperty(this, "_remoteInstanceService", void 0);
|
|
508
511
|
_defineProperty(this, "_syncingUnits", /* @__PURE__ */ new Set());
|
|
509
512
|
_defineProperty(this, "_syncingMutations", /* @__PURE__ */ new Set());
|
|
513
|
+
_defineProperty(this, "_syncMutationQueue", Promise.resolve());
|
|
510
514
|
this._initRPCChannels();
|
|
511
515
|
this._init();
|
|
512
516
|
}
|
|
@@ -518,8 +522,19 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
518
522
|
* sync other types of documents, you should manually call this method with that document's id.
|
|
519
523
|
*/
|
|
520
524
|
syncUnit(unitId) {
|
|
525
|
+
var _this$_univerInstance;
|
|
526
|
+
const alreadySyncing = this._syncingUnits.has(unitId);
|
|
521
527
|
this._syncingUnits.add(unitId);
|
|
522
|
-
|
|
528
|
+
const unit = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) !== null && _this$_univerInstance !== void 0 ? _this$_univerInstance : this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_BASE);
|
|
529
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.createInstance({
|
|
530
|
+
unitID: unit.getUnitId(),
|
|
531
|
+
type: unit.type,
|
|
532
|
+
snapshot: unit.getSnapshot()
|
|
533
|
+
});
|
|
534
|
+
return toDisposable(() => {
|
|
535
|
+
if (!alreadySyncing) this._syncingUnits.delete(unitId);
|
|
536
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.disposeInstance({ unitID: unit.getUnitId() });
|
|
537
|
+
});
|
|
523
538
|
}
|
|
524
539
|
_initRPCChannels() {
|
|
525
540
|
this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));
|
|
@@ -543,7 +558,10 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
543
558
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo, options) => {
|
|
544
559
|
const { type, params, id } = commandInfo;
|
|
545
560
|
const unitId = (params === null || params === void 0 ? void 0 : params.unitId) || "";
|
|
546
|
-
if (type === CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id))
|
|
561
|
+
if (type === CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id)) {
|
|
562
|
+
const sync = () => this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo }, options);
|
|
563
|
+
this._syncMutationQueue = this._syncMutationQueue.then(sync, sync).catch(() => false);
|
|
564
|
+
}
|
|
547
565
|
}));
|
|
548
566
|
}
|
|
549
567
|
};
|
|
@@ -589,7 +607,7 @@ DataSyncReplicaController = __decorate([
|
|
|
589
607
|
//#endregion
|
|
590
608
|
//#region package.json
|
|
591
609
|
var name = "@univerjs/rpc";
|
|
592
|
-
var version = "0.
|
|
610
|
+
var version = "1.0.0-alpha.1";
|
|
593
611
|
|
|
594
612
|
//#endregion
|
|
595
613
|
//#region src/services/rpc/implementations/web-worker-rpc.service.ts
|
package/lib/index.js
CHANGED
|
@@ -26,7 +26,7 @@ const configSymbolWorkerThread = Symbol(PLUGIN_CONFIG_KEY_WORKER_THREAD);
|
|
|
26
26
|
const defaultPluginWorkerThreadConfig = {};
|
|
27
27
|
|
|
28
28
|
//#endregion
|
|
29
|
-
//#region \0@oxc-project+runtime@0.
|
|
29
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
|
|
30
30
|
function __decorateParam(paramIndex, decorator) {
|
|
31
31
|
return function(target, key) {
|
|
32
32
|
decorator(target, key, paramIndex);
|
|
@@ -34,7 +34,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
//#endregion
|
|
37
|
-
//#region \0@oxc-project+runtime@0.
|
|
37
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
|
|
38
38
|
function __decorate(decorators, target, key, desc) {
|
|
39
39
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
40
40
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -93,6 +93,9 @@ let WebWorkerRemoteInstanceService = class WebWorkerRemoteInstanceService {
|
|
|
93
93
|
case UniverInstanceType.UNIVER_SHEET:
|
|
94
94
|
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, snapshot);
|
|
95
95
|
return true;
|
|
96
|
+
case UniverInstanceType.UNIVER_BASE:
|
|
97
|
+
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_BASE, snapshot);
|
|
98
|
+
return true;
|
|
96
99
|
default: throw new Error(`[WebWorkerRemoteInstanceService]: cannot create replica for document type: ${type}.`);
|
|
97
100
|
}
|
|
98
101
|
} catch (err) {
|
|
@@ -121,7 +124,7 @@ WebWorkerRemoteInstanceService = __decorate([
|
|
|
121
124
|
], WebWorkerRemoteInstanceService);
|
|
122
125
|
|
|
123
126
|
//#endregion
|
|
124
|
-
//#region \0@oxc-project+runtime@0.
|
|
127
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
|
|
125
128
|
function _typeof(o) {
|
|
126
129
|
"@babel/helpers - typeof";
|
|
127
130
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -132,7 +135,7 @@ function _typeof(o) {
|
|
|
132
135
|
}
|
|
133
136
|
|
|
134
137
|
//#endregion
|
|
135
|
-
//#region \0@oxc-project+runtime@0.
|
|
138
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
|
|
136
139
|
function toPrimitive(t, r) {
|
|
137
140
|
if ("object" != _typeof(t) || !t) return t;
|
|
138
141
|
var e = t[Symbol.toPrimitive];
|
|
@@ -145,14 +148,14 @@ function toPrimitive(t, r) {
|
|
|
145
148
|
}
|
|
146
149
|
|
|
147
150
|
//#endregion
|
|
148
|
-
//#region \0@oxc-project+runtime@0.
|
|
151
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
|
|
149
152
|
function toPropertyKey(t) {
|
|
150
153
|
var i = toPrimitive(t, "string");
|
|
151
154
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
//#endregion
|
|
155
|
-
//#region \0@oxc-project+runtime@0.
|
|
158
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
|
|
156
159
|
function _defineProperty(e, r, t) {
|
|
157
160
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
158
161
|
value: t,
|
|
@@ -507,6 +510,7 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
507
510
|
_defineProperty(this, "_remoteInstanceService", void 0);
|
|
508
511
|
_defineProperty(this, "_syncingUnits", /* @__PURE__ */ new Set());
|
|
509
512
|
_defineProperty(this, "_syncingMutations", /* @__PURE__ */ new Set());
|
|
513
|
+
_defineProperty(this, "_syncMutationQueue", Promise.resolve());
|
|
510
514
|
this._initRPCChannels();
|
|
511
515
|
this._init();
|
|
512
516
|
}
|
|
@@ -518,8 +522,19 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
518
522
|
* sync other types of documents, you should manually call this method with that document's id.
|
|
519
523
|
*/
|
|
520
524
|
syncUnit(unitId) {
|
|
525
|
+
var _this$_univerInstance;
|
|
526
|
+
const alreadySyncing = this._syncingUnits.has(unitId);
|
|
521
527
|
this._syncingUnits.add(unitId);
|
|
522
|
-
|
|
528
|
+
const unit = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) !== null && _this$_univerInstance !== void 0 ? _this$_univerInstance : this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_BASE);
|
|
529
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.createInstance({
|
|
530
|
+
unitID: unit.getUnitId(),
|
|
531
|
+
type: unit.type,
|
|
532
|
+
snapshot: unit.getSnapshot()
|
|
533
|
+
});
|
|
534
|
+
return toDisposable(() => {
|
|
535
|
+
if (!alreadySyncing) this._syncingUnits.delete(unitId);
|
|
536
|
+
if (!alreadySyncing && unit) this._remoteInstanceService.disposeInstance({ unitID: unit.getUnitId() });
|
|
537
|
+
});
|
|
523
538
|
}
|
|
524
539
|
_initRPCChannels() {
|
|
525
540
|
this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));
|
|
@@ -543,7 +558,10 @@ let DataSyncPrimaryController = class DataSyncPrimaryController extends RxDispos
|
|
|
543
558
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo, options) => {
|
|
544
559
|
const { type, params, id } = commandInfo;
|
|
545
560
|
const unitId = (params === null || params === void 0 ? void 0 : params.unitId) || "";
|
|
546
|
-
if (type === CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id))
|
|
561
|
+
if (type === CommandType.MUTATION && (!unitId || this._syncingUnits.has(unitId)) && !(options === null || options === void 0 ? void 0 : options.fromSync) && this._syncingMutations.has(id)) {
|
|
562
|
+
const sync = () => this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo }, options);
|
|
563
|
+
this._syncMutationQueue = this._syncMutationQueue.then(sync, sync).catch(() => false);
|
|
564
|
+
}
|
|
547
565
|
}));
|
|
548
566
|
}
|
|
549
567
|
};
|
|
@@ -589,7 +607,7 @@ DataSyncReplicaController = __decorate([
|
|
|
589
607
|
//#endregion
|
|
590
608
|
//#region package.json
|
|
591
609
|
var name = "@univerjs/rpc";
|
|
592
|
-
var version = "0.
|
|
610
|
+
var version = "1.0.0-alpha.1";
|
|
593
611
|
|
|
594
612
|
//#endregion
|
|
595
613
|
//#region src/services/rpc/implementations/web-worker-rpc.service.ts
|
|
@@ -32,6 +32,7 @@ export declare class DataSyncPrimaryController extends RxDisposable {
|
|
|
32
32
|
private _remoteInstanceService;
|
|
33
33
|
private readonly _syncingUnits;
|
|
34
34
|
private readonly _syncingMutations;
|
|
35
|
+
private _syncMutationQueue;
|
|
35
36
|
constructor(_injector: Injector, _commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _rpcChannelService: IRPCChannelService, _remoteSyncService: IRemoteSyncService);
|
|
36
37
|
registerSyncingMutations(mutation: IMutation<object>): void;
|
|
37
38
|
/**
|
package/lib/types/index.d.ts
CHANGED
|
@@ -21,4 +21,5 @@ export { UniverRPCMainThreadPlugin, UniverRPCWorkerThreadPlugin, } from './plugi
|
|
|
21
21
|
export { IRemoteInstanceService, RemoteInstanceServiceName, RemoteSyncPrimaryService, RemoteSyncServiceName, WebWorkerRemoteInstanceService, } from './services/remote-instance/remote-instance.service';
|
|
22
22
|
export { IRemoteSyncService } from './services/remote-instance/remote-instance.service';
|
|
23
23
|
export { ChannelService, IRPCChannelService } from './services/rpc/channel.service';
|
|
24
|
-
export { ChannelClient, ChannelServer, fromModule,
|
|
24
|
+
export { ChannelClient, ChannelServer, fromModule, toModule } from './services/rpc/rpc.service';
|
|
25
|
+
export type { IMessageProtocol } from './services/rpc/rpc.service';
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import type { IExecutionOptions, IMutationInfo, IWorkbookData } from '@univerjs/core';
|
|
16
|
+
import type { IBaseSnapshot, IExecutionOptions, IMutationInfo, IWorkbookData } from '@univerjs/core';
|
|
17
17
|
import { ICommandService, ILogService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
|
|
18
18
|
export interface IRemoteSyncMutationOptions extends IExecutionOptions {
|
|
19
19
|
/** If this mutation is executed after it was sent from the peer univer instance (e.g. in a web worker). */
|
|
@@ -52,7 +52,7 @@ export interface IRemoteInstanceService {
|
|
|
52
52
|
createInstance(params: {
|
|
53
53
|
unitID: string;
|
|
54
54
|
type: UniverInstanceType;
|
|
55
|
-
snapshot: IWorkbookData;
|
|
55
|
+
snapshot: IWorkbookData | IBaseSnapshot;
|
|
56
56
|
}): Promise<boolean>;
|
|
57
57
|
disposeInstance(params: {
|
|
58
58
|
unitID: string;
|
|
@@ -73,7 +73,7 @@ export declare class WebWorkerRemoteInstanceService implements IRemoteInstanceSe
|
|
|
73
73
|
createInstance(params: {
|
|
74
74
|
unitID: string;
|
|
75
75
|
type: UniverInstanceType;
|
|
76
|
-
snapshot: IWorkbookData;
|
|
76
|
+
snapshot: IWorkbookData | IBaseSnapshot;
|
|
77
77
|
}): Promise<boolean>;
|
|
78
78
|
disposeInstance(params: {
|
|
79
79
|
unitID: string;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs/operators`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverRpc={},e.UniverCore,e.rxjs.operators,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`rpc.main-thread.config`,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=`@univerjs/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});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs/operators"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs/operators`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverRpc={},e.UniverCore,e.rxjs.operators,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`rpc.main-thread.config`,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;case t.UniverInstanceType.UNIVER_BASE:return this._univerInstanceService.createUnit(t.UniverInstanceType.UNIVER_BASE,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),y(this,`_syncMutationQueue`,Promise.resolve()),this._initRPCChannels(),this._init()}registerSyncingMutations(e){this._syncingMutations.add(e.id)}syncUnit(e){var n;let r=this._syncingUnits.has(e);this._syncingUnits.add(e);let i=(n=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_BASE):n;return!r&&i&&this._remoteInstanceService.createInstance({unitID:i.getUnitId(),type:i.type,snapshot:i.getSnapshot()}),(0,t.toDisposable)(()=>{r||this._syncingUnits.delete(e),!r&&i&&this._remoteInstanceService.disposeInstance({unitID:i.getUnitId()})})}_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)||``;if(r===t.CommandType.MUTATION&&(!o||this._syncingUnits.has(o))&&!(n!=null&&n.fromSync)&&this._syncingMutations.has(a)){let t=()=>this._remoteInstanceService.syncMutation({mutationInfo:e},n);this._syncMutationQueue=this._syncMutationQueue.then(t,t).catch(()=>!1)}}))}};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=`@univerjs/rpc`,A=`1.0.0-alpha.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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Browser RPC layer for Univer main-thread and worker communication.",
|
|
6
6
|
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
@@ -52,13 +52,13 @@
|
|
|
52
52
|
"rxjs": ">=7.0.0"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@univerjs/core": "0.
|
|
55
|
+
"@univerjs/core": "1.0.0-alpha.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"rxjs": "^7.8.2",
|
|
59
59
|
"typescript": "^6.0.3",
|
|
60
|
-
"vitest": "^4.1.
|
|
61
|
-
"@univerjs-infra/shared": "0.
|
|
60
|
+
"vitest": "^4.1.9",
|
|
61
|
+
"@univerjs-infra/shared": "1.0.0-alpha.1"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"test": "vitest run",
|