@fluidframework/register-collection 1.4.0-115997 → 2.0.0-dev-rc.1.0.0.224419
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/.eslintrc.js +5 -7
- package/.mocharc.js +12 -0
- package/CHANGELOG.md +117 -0
- package/README.md +29 -8
- package/api-extractor-lint.json +4 -0
- package/api-extractor.json +2 -2
- package/api-report/register-collection.api.md +90 -0
- package/dist/{consensusRegisterCollection.js → consensusRegisterCollection.cjs} +31 -29
- package/dist/consensusRegisterCollection.cjs.map +1 -0
- package/dist/consensusRegisterCollection.d.ts +2 -1
- package/dist/consensusRegisterCollection.d.ts.map +1 -1
- package/dist/{consensusRegisterCollectionFactory.js → consensusRegisterCollectionFactory.cjs} +5 -4
- package/dist/consensusRegisterCollectionFactory.cjs.map +1 -0
- package/dist/consensusRegisterCollectionFactory.d.ts +2 -1
- package/dist/consensusRegisterCollectionFactory.d.ts.map +1 -1
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/{interfaces.js → interfaces.cjs} +3 -2
- package/dist/interfaces.cjs.map +1 -0
- package/dist/interfaces.d.ts +9 -5
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/{packageVersion.js → packageVersion.cjs} +2 -2
- package/dist/packageVersion.cjs.map +1 -0
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/register-collection-alpha.d.ts +163 -0
- package/dist/register-collection-beta.d.ts +25 -0
- package/dist/register-collection-public.d.ts +25 -0
- package/dist/register-collection-untrimmed.d.ts +163 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/{consensusRegisterCollection.d.ts → consensusRegisterCollection.d.mts} +4 -3
- package/lib/consensusRegisterCollection.d.mts.map +1 -0
- package/lib/{consensusRegisterCollection.js → consensusRegisterCollection.mjs} +27 -25
- package/lib/consensusRegisterCollection.mjs.map +1 -0
- package/lib/{consensusRegisterCollectionFactory.d.ts → consensusRegisterCollectionFactory.d.mts} +3 -2
- package/lib/consensusRegisterCollectionFactory.d.mts.map +1 -0
- package/lib/{consensusRegisterCollectionFactory.js → consensusRegisterCollectionFactory.mjs} +5 -4
- package/lib/consensusRegisterCollectionFactory.mjs.map +1 -0
- package/lib/index.d.mts +8 -0
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +8 -0
- package/lib/index.mjs.map +1 -0
- package/lib/{interfaces.d.ts → interfaces.d.mts} +9 -5
- package/lib/interfaces.d.mts.map +1 -0
- package/lib/{interfaces.js → interfaces.mjs} +2 -1
- package/lib/interfaces.mjs.map +1 -0
- package/lib/{packageVersion.d.ts → packageVersion.d.mts} +1 -1
- package/lib/{packageVersion.d.ts.map → packageVersion.d.mts.map} +1 -1
- package/lib/{packageVersion.js → packageVersion.mjs} +2 -2
- package/lib/packageVersion.mjs.map +1 -0
- package/lib/register-collection-alpha.d.mts +163 -0
- package/lib/register-collection-beta.d.mts +25 -0
- package/lib/register-collection-public.d.mts +25 -0
- package/lib/register-collection-untrimmed.d.mts +163 -0
- package/package.json +97 -62
- package/prettier.config.cjs +8 -0
- package/src/consensusRegisterCollection.ts +307 -284
- package/src/consensusRegisterCollectionFactory.ts +39 -33
- package/src/index.ts +8 -3
- package/src/interfaces.ts +52 -43
- package/src/packageVersion.ts +1 -1
- package/tsc-multi.test.json +4 -0
- package/tsconfig.json +11 -13
- package/dist/consensusRegisterCollection.js.map +0 -1
- package/dist/consensusRegisterCollectionFactory.js.map +0 -1
- package/dist/index.js +0 -20
- package/dist/index.js.map +0 -1
- package/dist/interfaces.js.map +0 -1
- package/dist/packageVersion.js.map +0 -1
- package/lib/consensusRegisterCollection.d.ts.map +0 -1
- package/lib/consensusRegisterCollection.js.map +0 -1
- package/lib/consensusRegisterCollectionFactory.d.ts.map +0 -1
- package/lib/consensusRegisterCollectionFactory.js.map +0 -1
- package/lib/index.d.ts +0 -8
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -8
- package/lib/index.js.map +0 -1
- package/lib/interfaces.d.ts.map +0 -1
- package/lib/interfaces.js.map +0 -1
- package/lib/packageVersion.js.map +0 -1
- package/tsconfig.esnext.json +0 -7
|
@@ -3,71 +3,80 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { bufferToString } from "@fluid-internal/client-utils";
|
|
7
|
+
import { assert, unreachableCase } from "@fluidframework/core-utils";
|
|
7
8
|
import { ISequencedDocumentMessage, MessageType } from "@fluidframework/protocol-definitions";
|
|
8
9
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
IChannelAttributes,
|
|
11
|
+
IFluidDataStoreRuntime,
|
|
12
|
+
IChannelStorageService,
|
|
12
13
|
} from "@fluidframework/datastore-definitions";
|
|
13
14
|
import { ISummaryTreeWithStats } from "@fluidframework/runtime-definitions";
|
|
14
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
createSingleBlobSummary,
|
|
17
|
+
IFluidSerializer,
|
|
18
|
+
SharedObject,
|
|
19
|
+
} from "@fluidframework/shared-object-base";
|
|
15
20
|
import { ConsensusRegisterCollectionFactory } from "./consensusRegisterCollectionFactory";
|
|
16
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
IConsensusRegisterCollection,
|
|
23
|
+
ReadPolicy,
|
|
24
|
+
IConsensusRegisterCollectionEvents,
|
|
25
|
+
} from "./interfaces";
|
|
17
26
|
|
|
18
27
|
interface ILocalData<T> {
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
// Atomic version
|
|
29
|
+
atomic: ILocalRegister<T>;
|
|
21
30
|
|
|
22
|
-
|
|
23
|
-
|
|
31
|
+
// All concurrent versions awaiting consensus
|
|
32
|
+
versions: ILocalRegister<T>[];
|
|
24
33
|
}
|
|
25
34
|
|
|
26
35
|
interface ILocalRegister<T> {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
// Register value, wrapped for backwards compatibility with < 0.17
|
|
37
|
+
value: {
|
|
38
|
+
type: "Plain";
|
|
39
|
+
value: T;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// The sequence number when last consensus was reached
|
|
43
|
+
sequenceNumber: number;
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
const newLocalRegister = <T>(sequenceNumber: number, value: T): ILocalRegister<T> => ({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
sequenceNumber,
|
|
48
|
+
value: {
|
|
49
|
+
type: "Plain",
|
|
50
|
+
value,
|
|
51
|
+
},
|
|
43
52
|
});
|
|
44
53
|
|
|
45
54
|
/**
|
|
46
55
|
* An operation for consensus register collection
|
|
47
56
|
*/
|
|
48
57
|
interface IRegisterOperation {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
key: string;
|
|
59
|
+
type: "write";
|
|
60
|
+
serializedValue: string;
|
|
61
|
+
|
|
62
|
+
// Message can be delivered with delay - resubmitted on reconnect.
|
|
63
|
+
// As such, refSeq needs to reference seq # at the time op was created,
|
|
64
|
+
// not when op was actually sent over wire (ISequencedDocumentMessage.referenceSequenceNumber),
|
|
65
|
+
// as client can ingest ops in between.
|
|
66
|
+
refSeq: number | undefined;
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
/**
|
|
61
70
|
* IRegisterOperation format in versions \< 0.17
|
|
62
71
|
*/
|
|
63
72
|
interface IRegisterOperationOld<T> {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
key: string;
|
|
74
|
+
type: "write";
|
|
75
|
+
value: {
|
|
76
|
+
type: "Plain";
|
|
77
|
+
value: T;
|
|
78
|
+
};
|
|
79
|
+
refSeq: number;
|
|
71
80
|
}
|
|
72
81
|
|
|
73
82
|
/** Incoming ops could match any of these types */
|
|
@@ -82,251 +91,265 @@ type PendingResolve = (winner: boolean) => void;
|
|
|
82
91
|
const snapshotFileName = "header";
|
|
83
92
|
|
|
84
93
|
/**
|
|
85
|
-
*
|
|
94
|
+
* {@inheritDoc IConsensusRegisterCollection}
|
|
95
|
+
* @alpha
|
|
86
96
|
*/
|
|
87
97
|
export class ConsensusRegisterCollection<T>
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
98
|
+
extends SharedObject<IConsensusRegisterCollectionEvents>
|
|
99
|
+
implements IConsensusRegisterCollection<T>
|
|
100
|
+
{
|
|
101
|
+
/**
|
|
102
|
+
* Create a new consensus register collection
|
|
103
|
+
*
|
|
104
|
+
* @param runtime - data store runtime the new consensus register collection belongs to
|
|
105
|
+
* @param id - optional name of the consensus register collection
|
|
106
|
+
* @returns newly create consensus register collection (but not attached yet)
|
|
107
|
+
*/
|
|
108
|
+
public static create<T>(runtime: IFluidDataStoreRuntime, id?: string) {
|
|
109
|
+
return runtime.createChannel(
|
|
110
|
+
id,
|
|
111
|
+
ConsensusRegisterCollectionFactory.Type,
|
|
112
|
+
) as ConsensusRegisterCollection<T>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get a factory for ConsensusRegisterCollection to register with the data store.
|
|
117
|
+
*
|
|
118
|
+
* @returns a factory that creates and load ConsensusRegisterCollection
|
|
119
|
+
*/
|
|
120
|
+
public static getFactory() {
|
|
121
|
+
return new ConsensusRegisterCollectionFactory();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private readonly data = new Map<string, ILocalData<T>>();
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Constructs a new consensus register collection. If the object is non-local an id and service interfaces will
|
|
128
|
+
* be provided
|
|
129
|
+
*/
|
|
130
|
+
public constructor(
|
|
131
|
+
id: string,
|
|
132
|
+
runtime: IFluidDataStoreRuntime,
|
|
133
|
+
attributes: IChannelAttributes,
|
|
134
|
+
) {
|
|
135
|
+
super(id, runtime, attributes, "fluid_consensusRegisterCollection_");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Creates a new register or writes a new value.
|
|
140
|
+
* Returns a promise that will resolve when the write is acked.
|
|
141
|
+
*
|
|
142
|
+
* @returns Promise<true> if write was non-concurrent
|
|
143
|
+
*/
|
|
144
|
+
public async write(key: string, value: T): Promise<boolean> {
|
|
145
|
+
const serializedValue = this.stringify(value, this.serializer);
|
|
146
|
+
|
|
147
|
+
if (!this.isAttached()) {
|
|
148
|
+
// JSON-roundtrip value for local writes to match the behavior of going through the wire
|
|
149
|
+
this.processInboundWrite(key, this.parse(serializedValue, this.serializer), 0, 0, true);
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const message: IRegisterOperation = {
|
|
154
|
+
key,
|
|
155
|
+
type: "write",
|
|
156
|
+
serializedValue,
|
|
157
|
+
refSeq: this.runtime.deltaManager.lastSequenceNumber,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return this.newAckBasedPromise<boolean>((resolve) => {
|
|
161
|
+
// Send the resolve function as the localOpMetadata. This will be provided back to us when the
|
|
162
|
+
// op is ack'd.
|
|
163
|
+
this.submitLocalMessage(message, resolve);
|
|
164
|
+
// If we fail due to runtime being disposed, it's better to return false then unhandled exception.
|
|
165
|
+
}).catch((error) => false);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns the most recent local value of a register.
|
|
170
|
+
* @param key - The key to read
|
|
171
|
+
* @param readPolicy - The ReadPolicy to apply. Defaults to Atomic.
|
|
172
|
+
*/
|
|
173
|
+
public read(key: string, readPolicy: ReadPolicy = ReadPolicy.Atomic): T | undefined {
|
|
174
|
+
if (readPolicy === ReadPolicy.Atomic) {
|
|
175
|
+
return this.readAtomic(key);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const versions = this.readVersions(key);
|
|
179
|
+
|
|
180
|
+
if (versions !== undefined) {
|
|
181
|
+
// We don't support deletion. So there should be at least one value.
|
|
182
|
+
assert(versions.length > 0, 0x06c /* "Value should be undefined or non-empty" */);
|
|
183
|
+
|
|
184
|
+
return versions[versions.length - 1];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public readVersions(key: string): T[] | undefined {
|
|
189
|
+
const data = this.data.get(key);
|
|
190
|
+
return data?.versions.map((element: ILocalRegister<T>) => element.value.value);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public keys(): string[] {
|
|
194
|
+
return [...this.data.keys()];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
protected summarizeCore(serializer: IFluidSerializer): ISummaryTreeWithStats {
|
|
198
|
+
const dataObj: { [key: string]: ILocalData<T> } = {};
|
|
199
|
+
this.data.forEach((v, k) => {
|
|
200
|
+
dataObj[k] = v;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return createSingleBlobSummary(snapshotFileName, this.stringify(dataObj, serializer));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
|
|
208
|
+
*/
|
|
209
|
+
protected async loadCore(storage: IChannelStorageService): Promise<void> {
|
|
210
|
+
const blob = await storage.readBlob(snapshotFileName);
|
|
211
|
+
const header = bufferToString(blob, "utf8");
|
|
212
|
+
const dataObj = this.parse(header, this.serializer);
|
|
213
|
+
|
|
214
|
+
for (const key of Object.keys(dataObj)) {
|
|
215
|
+
assert(
|
|
216
|
+
dataObj[key].atomic?.value.type !== "Shared",
|
|
217
|
+
0x06d /* "SharedObjects contained in ConsensusRegisterCollection can no longer be deserialized as of 0.17" */,
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
this.data.set(key, dataObj[key]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
protected onDisconnect() {}
|
|
225
|
+
|
|
226
|
+
protected processCore(
|
|
227
|
+
message: ISequencedDocumentMessage,
|
|
228
|
+
local: boolean,
|
|
229
|
+
localOpMetadata: unknown,
|
|
230
|
+
) {
|
|
231
|
+
if (message.type === MessageType.Operation) {
|
|
232
|
+
const op = message.contents as IIncomingRegisterOperation<T>;
|
|
233
|
+
switch (op.type) {
|
|
234
|
+
case "write": {
|
|
235
|
+
// backward compatibility: File at rest written with runtime <= 0.13 do not have refSeq
|
|
236
|
+
// when the refSeq property didn't exist
|
|
237
|
+
if (op.refSeq === undefined) {
|
|
238
|
+
op.refSeq = message.referenceSequenceNumber;
|
|
239
|
+
}
|
|
240
|
+
// Message can be delivered with delay - e.g. resubmitted on reconnect.
|
|
241
|
+
// Use the refSeq from when the op was created, not when it was transmitted
|
|
242
|
+
const refSeqWhenCreated = op.refSeq;
|
|
243
|
+
assert(
|
|
244
|
+
refSeqWhenCreated <= message.referenceSequenceNumber,
|
|
245
|
+
0x06e /* "Message's reference sequence number < op's reference sequence number!" */,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const value = incomingOpMatchesCurrentFormat(op)
|
|
249
|
+
? (this.parse(op.serializedValue, this.serializer) as T)
|
|
250
|
+
: op.value.value;
|
|
251
|
+
const winner = this.processInboundWrite(
|
|
252
|
+
op.key,
|
|
253
|
+
value,
|
|
254
|
+
refSeqWhenCreated,
|
|
255
|
+
message.sequenceNumber,
|
|
256
|
+
local,
|
|
257
|
+
);
|
|
258
|
+
if (local) {
|
|
259
|
+
// Resolve the pending promise for this operation now that we have received an ack for it.
|
|
260
|
+
const resolve = localOpMetadata as PendingResolve;
|
|
261
|
+
resolve(winner);
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
default:
|
|
266
|
+
unreachableCase(op.type);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private readAtomic(key: string): T | undefined {
|
|
272
|
+
const data = this.data.get(key);
|
|
273
|
+
return data?.atomic.value.value;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Process an inbound write op
|
|
278
|
+
* @param key - Key that was written to
|
|
279
|
+
* @param value - Incoming value
|
|
280
|
+
* @param refSeq - RefSeq at the time of write on the remote client
|
|
281
|
+
* @param sequenceNumber - Sequence Number of this write op
|
|
282
|
+
* @param local - Did this write originate on this client
|
|
283
|
+
*/
|
|
284
|
+
private processInboundWrite(
|
|
285
|
+
key: string,
|
|
286
|
+
value: T,
|
|
287
|
+
refSeq: number,
|
|
288
|
+
sequenceNumber: number,
|
|
289
|
+
local: boolean,
|
|
290
|
+
): boolean {
|
|
291
|
+
let data = this.data.get(key);
|
|
292
|
+
// Atomic update if it's a new register or the write was not concurrent,
|
|
293
|
+
// meaning our state was known to the remote client at the time of write
|
|
294
|
+
const winner = data === undefined || refSeq >= data.atomic.sequenceNumber;
|
|
295
|
+
if (winner) {
|
|
296
|
+
const atomicUpdate = newLocalRegister<T>(sequenceNumber, value);
|
|
297
|
+
if (data === undefined) {
|
|
298
|
+
data = {
|
|
299
|
+
atomic: atomicUpdate,
|
|
300
|
+
versions: [], // we'll update versions next, leave it empty for now
|
|
301
|
+
};
|
|
302
|
+
this.data.set(key, data);
|
|
303
|
+
} else {
|
|
304
|
+
data.atomic = atomicUpdate;
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
assert(!!data, 0x06f /* "data missing for non-atomic inbound update!" */);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Remove versions that were known to the remote client at the time of write
|
|
311
|
+
while (data.versions.length > 0 && refSeq >= data.versions[0].sequenceNumber) {
|
|
312
|
+
data.versions.shift();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const versionUpdate = newLocalRegister<T>(sequenceNumber, value);
|
|
316
|
+
|
|
317
|
+
// Asserts for data integrity
|
|
318
|
+
if (!this.isAttached()) {
|
|
319
|
+
assert(
|
|
320
|
+
refSeq === 0 && sequenceNumber === 0,
|
|
321
|
+
0x070 /* "sequence numbers are expected to be 0 when unattached" */,
|
|
322
|
+
);
|
|
323
|
+
} else if (data.versions.length > 0) {
|
|
324
|
+
assert(
|
|
325
|
+
// seqNum should always be increasing, except for the case of grouped batches (seqNum will be the same)
|
|
326
|
+
sequenceNumber >= data.versions[data.versions.length - 1].sequenceNumber,
|
|
327
|
+
0x071 /* "Versions should naturally be ordered by sequenceNumber" */,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Push the new element.
|
|
332
|
+
data.versions.push(versionUpdate);
|
|
333
|
+
|
|
334
|
+
// Raise events at the end, to avoid reentrancy issues
|
|
335
|
+
if (winner) {
|
|
336
|
+
this.emit("atomicChanged", key, value, local);
|
|
337
|
+
}
|
|
338
|
+
this.emit("versionChanged", key, value, local);
|
|
339
|
+
|
|
340
|
+
return winner;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private stringify(value: any, serializer: IFluidSerializer): string {
|
|
344
|
+
return serializer.stringify(value, this.handle);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private parse(content: string, serializer: IFluidSerializer): any {
|
|
348
|
+
return serializer.parse(content);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
protected applyStashedOp() {
|
|
352
|
+
// empty implementation
|
|
353
|
+
return () => {};
|
|
354
|
+
}
|
|
332
355
|
}
|