@fluidframework/map 2.0.0-dev.2.2.0.111723 → 2.0.0-dev.3.1.0.125672

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.
Files changed (65) hide show
  1. package/.eslintrc.js +12 -14
  2. package/.mocharc.js +2 -2
  3. package/README.md +3 -3
  4. package/api-extractor.json +2 -2
  5. package/dist/directory.d.ts +6 -5
  6. package/dist/directory.d.ts.map +1 -1
  7. package/dist/directory.js +68 -30
  8. package/dist/directory.js.map +1 -1
  9. package/dist/interfaces.d.ts +27 -17
  10. package/dist/interfaces.d.ts.map +1 -1
  11. package/dist/interfaces.js.map +1 -1
  12. package/dist/internalInterfaces.d.ts +39 -0
  13. package/dist/internalInterfaces.d.ts.map +1 -1
  14. package/dist/internalInterfaces.js.map +1 -1
  15. package/dist/localValues.d.ts +12 -3
  16. package/dist/localValues.d.ts.map +1 -1
  17. package/dist/localValues.js +10 -0
  18. package/dist/localValues.js.map +1 -1
  19. package/dist/map.d.ts +5 -5
  20. package/dist/map.d.ts.map +1 -1
  21. package/dist/map.js +15 -2
  22. package/dist/map.js.map +1 -1
  23. package/dist/mapKernel.d.ts +5 -5
  24. package/dist/mapKernel.d.ts.map +1 -1
  25. package/dist/mapKernel.js +58 -33
  26. package/dist/mapKernel.js.map +1 -1
  27. package/dist/packageVersion.d.ts +1 -1
  28. package/dist/packageVersion.js +1 -1
  29. package/dist/packageVersion.js.map +1 -1
  30. package/lib/directory.d.ts +6 -5
  31. package/lib/directory.d.ts.map +1 -1
  32. package/lib/directory.js +70 -32
  33. package/lib/directory.js.map +1 -1
  34. package/lib/interfaces.d.ts +27 -17
  35. package/lib/interfaces.d.ts.map +1 -1
  36. package/lib/interfaces.js.map +1 -1
  37. package/lib/internalInterfaces.d.ts +39 -0
  38. package/lib/internalInterfaces.d.ts.map +1 -1
  39. package/lib/internalInterfaces.js.map +1 -1
  40. package/lib/localValues.d.ts +12 -3
  41. package/lib/localValues.d.ts.map +1 -1
  42. package/lib/localValues.js +10 -0
  43. package/lib/localValues.js.map +1 -1
  44. package/lib/map.d.ts +5 -5
  45. package/lib/map.d.ts.map +1 -1
  46. package/lib/map.js +16 -3
  47. package/lib/map.js.map +1 -1
  48. package/lib/mapKernel.d.ts +5 -5
  49. package/lib/mapKernel.d.ts.map +1 -1
  50. package/lib/mapKernel.js +59 -34
  51. package/lib/mapKernel.js.map +1 -1
  52. package/lib/packageVersion.d.ts +1 -1
  53. package/lib/packageVersion.js +1 -1
  54. package/lib/packageVersion.js.map +1 -1
  55. package/package.json +25 -22
  56. package/prettier.config.cjs +1 -1
  57. package/src/directory.ts +1957 -1848
  58. package/src/interfaces.ts +309 -288
  59. package/src/internalInterfaces.ts +83 -38
  60. package/src/localValues.ts +95 -93
  61. package/src/map.ts +364 -345
  62. package/src/mapKernel.ts +729 -676
  63. package/src/packageVersion.ts +1 -1
  64. package/tsconfig.esnext.json +5 -5
  65. package/tsconfig.json +9 -15
package/src/directory.ts CHANGED
@@ -6,41 +6,34 @@
6
6
  import { assert, TypedEventEmitter } from "@fluidframework/common-utils";
7
7
  import { UsageError } from "@fluidframework/container-utils";
8
8
  import { readAndParse } from "@fluidframework/driver-utils";
9
+ import { ISequencedDocumentMessage, MessageType } from "@fluidframework/protocol-definitions";
9
10
  import {
10
- ISequencedDocumentMessage,
11
- MessageType,
12
- } from "@fluidframework/protocol-definitions";
13
- import {
14
- IChannelAttributes,
15
- IFluidDataStoreRuntime,
16
- IChannelStorageService,
17
- IChannelServices,
18
- IChannelFactory,
11
+ IChannelAttributes,
12
+ IFluidDataStoreRuntime,
13
+ IChannelStorageService,
14
+ IChannelServices,
15
+ IChannelFactory,
19
16
  } from "@fluidframework/datastore-definitions";
20
17
  import { ISummaryTreeWithStats, ITelemetryContext } from "@fluidframework/runtime-definitions";
21
18
  import { IFluidSerializer, SharedObject, ValueType } from "@fluidframework/shared-object-base";
22
19
  import { SummaryTreeBuilder } from "@fluidframework/runtime-utils";
23
20
  import * as path from "path-browserify";
24
21
  import {
25
- IDirectory,
26
- IDirectoryEvents,
27
- IDirectoryValueChanged,
28
- ISerializableValue,
29
- ISerializedValue,
30
- ISharedDirectory,
31
- ISharedDirectoryEvents,
32
- IValueChanged,
22
+ IDirectory,
23
+ IDirectoryEvents,
24
+ IDirectoryValueChanged,
25
+ ISerializableValue,
26
+ ISerializedValue,
27
+ ISharedDirectory,
28
+ ISharedDirectoryEvents,
29
+ IValueChanged,
33
30
  } from "./interfaces";
34
- import {
35
- ILocalValue,
36
- LocalValueMaker,
37
- makeSerializable,
38
- } from "./localValues";
31
+ import { ILocalValue, LocalValueMaker, makeSerializable } from "./localValues";
39
32
  import { pkgVersion } from "./packageVersion";
40
33
 
41
34
  // We use path-browserify since this code can run safely on the server or the browser.
42
35
  // We standardize on using posix slashes everywhere.
43
- const posix: typeof import("path").posix = path.posix;
36
+ const posix = path.posix;
44
37
 
45
38
  const snapshotFileName = "header";
46
39
 
@@ -48,72 +41,68 @@ const snapshotFileName = "header";
48
41
  * Defines the means to process and submit a given op on a directory.
49
42
  */
50
43
  interface IDirectoryMessageHandler {
51
- /**
52
- * Apply the given operation.
53
- * @param op - The directory operation to apply
54
- * @param local - Whether the message originated from the local client
55
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
56
- * For messages from a remote client, this will be undefined.
57
- */
58
- process(
59
- op: IDirectoryOperation,
60
- local: boolean,
61
- localOpMetadata: unknown,
62
- ): void;
63
-
64
- /**
65
- * Communicate the operation to remote clients.
66
- * @param op - The directory operation to submit
67
- * @param localOpMetadata - The metadata to be submitted with the message.
68
- */
69
- submit(op: IDirectoryOperation, localOpMetadata: unknown): void;
70
-
71
- applyStashedOp(op: IDirectoryOperation): unknown;
44
+ /**
45
+ * Apply the given operation.
46
+ * @param op - The directory operation to apply
47
+ * @param local - Whether the message originated from the local client
48
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
49
+ * For messages from a remote client, this will be undefined.
50
+ */
51
+ process(op: IDirectoryOperation, local: boolean, localOpMetadata: unknown): void;
52
+
53
+ /**
54
+ * Communicate the operation to remote clients.
55
+ * @param op - The directory operation to submit
56
+ * @param localOpMetadata - The metadata to be submitted with the message.
57
+ */
58
+ submit(op: IDirectoryOperation, localOpMetadata: unknown): void;
59
+
60
+ applyStashedOp(op: IDirectoryOperation): unknown;
72
61
  }
73
62
 
74
63
  /**
75
64
  * Operation indicating a value should be set for a key.
76
65
  */
77
66
  export interface IDirectorySetOperation {
78
- /**
79
- * String identifier of the operation type.
80
- */
81
- type: "set";
82
-
83
- /**
84
- * Directory key being modified.
85
- */
86
- key: string;
87
-
88
- /**
89
- * Absolute path of the directory where the modified key is located.
90
- */
91
- path: string;
92
-
93
- /**
94
- * Value to be set on the key.
95
- */
96
- value: ISerializableValue;
67
+ /**
68
+ * String identifier of the operation type.
69
+ */
70
+ type: "set";
71
+
72
+ /**
73
+ * Directory key being modified.
74
+ */
75
+ key: string;
76
+
77
+ /**
78
+ * Absolute path of the directory where the modified key is located.
79
+ */
80
+ path: string;
81
+
82
+ /**
83
+ * Value to be set on the key.
84
+ */
85
+ value: ISerializableValue;
97
86
  }
98
87
 
99
88
  /**
100
89
  * Operation indicating a key should be deleted from the directory.
101
90
  */
102
91
  export interface IDirectoryDeleteOperation {
103
- /**
104
- * String identifier of the operation type.
105
- */
106
- type: "delete";
107
-
108
- /**
109
- * Directory key being modified.
110
- */
111
- key: string;
112
-
113
- /**
114
- * Absolute path of the directory where the modified key is located.
115
- */
116
- path: string;
92
+ /**
93
+ * String identifier of the operation type.
94
+ */
95
+ type: "delete";
96
+
97
+ /**
98
+ * Directory key being modified.
99
+ */
100
+ key: string;
101
+
102
+ /**
103
+ * Absolute path of the directory where the modified key is located.
104
+ */
105
+ path: string;
117
106
  }
118
107
 
119
108
  /**
@@ -125,15 +114,15 @@ export type IDirectoryKeyOperation = IDirectorySetOperation | IDirectoryDeleteOp
125
114
  * Operation indicating the directory should be cleared.
126
115
  */
127
116
  export interface IDirectoryClearOperation {
128
- /**
129
- * String identifier of the operation type.
130
- */
131
- type: "clear";
132
-
133
- /**
134
- * Absolute path of the directory being cleared.
135
- */
136
- path: string;
117
+ /**
118
+ * String identifier of the operation type.
119
+ */
120
+ type: "clear";
121
+
122
+ /**
123
+ * Absolute path of the directory being cleared.
124
+ */
125
+ path: string;
137
126
  }
138
127
 
139
128
  /**
@@ -145,47 +134,48 @@ export type IDirectoryStorageOperation = IDirectoryKeyOperation | IDirectoryClea
145
134
  * Operation indicating a subdirectory should be created.
146
135
  */
147
136
  export interface IDirectoryCreateSubDirectoryOperation {
148
- /**
149
- * String identifier of the operation type.
150
- */
151
- type: "createSubDirectory";
152
-
153
- /**
154
- * Absolute path of the directory that will contain the new subdirectory.
155
- */
156
- path: string;
157
-
158
- /**
159
- * Name of the new subdirectory.
160
- */
161
- subdirName: string;
137
+ /**
138
+ * String identifier of the operation type.
139
+ */
140
+ type: "createSubDirectory";
141
+
142
+ /**
143
+ * Absolute path of the directory that will contain the new subdirectory.
144
+ */
145
+ path: string;
146
+
147
+ /**
148
+ * Name of the new subdirectory.
149
+ */
150
+ subdirName: string;
162
151
  }
163
152
 
164
153
  /**
165
154
  * Operation indicating a subdirectory should be deleted.
166
155
  */
167
156
  export interface IDirectoryDeleteSubDirectoryOperation {
168
- /**
169
- * String identifier of the operation type.
170
- */
171
- type: "deleteSubDirectory";
172
-
173
- /**
174
- * Absolute path of the directory that contains the directory to be deleted.
175
- */
176
- path: string;
177
-
178
- /**
179
- * Name of the subdirectory to be deleted.
180
- */
181
- subdirName: string;
157
+ /**
158
+ * String identifier of the operation type.
159
+ */
160
+ type: "deleteSubDirectory";
161
+
162
+ /**
163
+ * Absolute path of the directory that contains the directory to be deleted.
164
+ */
165
+ path: string;
166
+
167
+ /**
168
+ * Name of the subdirectory to be deleted.
169
+ */
170
+ subdirName: string;
182
171
  }
183
172
 
184
173
  /**
185
174
  * An operation on the subdirectories within a directory
186
175
  */
187
- export type IDirectorySubDirectoryOperation = IDirectoryCreateSubDirectoryOperation
188
- | IDirectoryDeleteSubDirectoryOperation;
176
+ export type IDirectorySubDirectoryOperation =
177
+ | IDirectoryCreateSubDirectoryOperation
178
+ | IDirectoryDeleteSubDirectoryOperation;
189
179
 
190
180
  /**
191
181
  * Any operation on a directory
@@ -201,15 +191,15 @@ export type IDirectoryOperation = IDirectoryStorageOperation | IDirectorySubDire
201
191
  * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | JSON.parse}.
202
192
  */
203
193
  export interface IDirectoryDataObject {
204
- /**
205
- * Key/value date set by the user.
206
- */
207
- storage?: { [key: string]: ISerializableValue; };
208
-
209
- /**
210
- * Recursive sub-directories {@link IDirectoryDataObject | objects}.
211
- */
212
- subdirectories?: { [subdirName: string]: IDirectoryDataObject; };
194
+ /**
195
+ * Key/value date set by the user.
196
+ */
197
+ storage?: { [key: string]: ISerializableValue };
198
+
199
+ /**
200
+ * Recursive sub-directories {@link IDirectoryDataObject | objects}.
201
+ */
202
+ subdirectories?: { [subdirName: string]: IDirectoryDataObject };
213
203
  }
214
204
 
215
205
  /**
@@ -218,15 +208,15 @@ export interface IDirectoryDataObject {
218
208
  * @internal
219
209
  */
220
210
  export interface IDirectoryNewStorageFormat {
221
- /**
222
- * Blob IDs representing larger directory data that was serialized.
223
- */
224
- blobs: string[];
225
-
226
- /**
227
- * Storage content representing directory data that was not serialized.
228
- */
229
- content: IDirectoryDataObject;
211
+ /**
212
+ * Blob IDs representing larger directory data that was serialized.
213
+ */
214
+ blobs: string[];
215
+
216
+ /**
217
+ * Storage content representing directory data that was not serialized.
218
+ */
219
+ content: IDirectoryDataObject;
230
220
  }
231
221
 
232
222
  /**
@@ -235,57 +225,58 @@ export interface IDirectoryNewStorageFormat {
235
225
  * @sealed
236
226
  */
237
227
  export class DirectoryFactory implements IChannelFactory {
238
- /**
239
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory."type"}
240
- */
241
- public static readonly Type = "https://graph.microsoft.com/types/directory";
242
-
243
- /**
244
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.attributes}
245
- */
246
- public static readonly Attributes: IChannelAttributes = {
247
- type: DirectoryFactory.Type,
248
- snapshotFormatVersion: "0.1",
249
- packageVersion: pkgVersion,
250
- };
251
-
252
- /**
253
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory."type"}
254
- */
255
- public get type() {
256
- return DirectoryFactory.Type;
257
- }
258
-
259
- /**
260
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.attributes}
261
- */
262
- public get attributes() {
263
- return DirectoryFactory.Attributes;
264
- }
265
-
266
- /**
267
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.load}
268
- */
269
- public async load(
270
- runtime: IFluidDataStoreRuntime,
271
- id: string,
272
- services: IChannelServices,
273
- attributes: IChannelAttributes): Promise<ISharedDirectory> {
274
- const directory = new SharedDirectory(id, runtime, attributes);
275
- await directory.load(services);
276
-
277
- return directory;
278
- }
279
-
280
- /**
281
- * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.create}
282
- */
283
- public create(runtime: IFluidDataStoreRuntime, id: string): ISharedDirectory {
284
- const directory = new SharedDirectory(id, runtime, DirectoryFactory.Attributes);
285
- directory.initializeLocal();
286
-
287
- return directory;
288
- }
228
+ /**
229
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory."type"}
230
+ */
231
+ public static readonly Type = "https://graph.microsoft.com/types/directory";
232
+
233
+ /**
234
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.attributes}
235
+ */
236
+ public static readonly Attributes: IChannelAttributes = {
237
+ type: DirectoryFactory.Type,
238
+ snapshotFormatVersion: "0.1",
239
+ packageVersion: pkgVersion,
240
+ };
241
+
242
+ /**
243
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory."type"}
244
+ */
245
+ public get type(): string {
246
+ return DirectoryFactory.Type;
247
+ }
248
+
249
+ /**
250
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.attributes}
251
+ */
252
+ public get attributes(): IChannelAttributes {
253
+ return DirectoryFactory.Attributes;
254
+ }
255
+
256
+ /**
257
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.load}
258
+ */
259
+ public async load(
260
+ runtime: IFluidDataStoreRuntime,
261
+ id: string,
262
+ services: IChannelServices,
263
+ attributes: IChannelAttributes,
264
+ ): Promise<ISharedDirectory> {
265
+ const directory = new SharedDirectory(id, runtime, attributes);
266
+ await directory.load(services);
267
+
268
+ return directory;
269
+ }
270
+
271
+ /**
272
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.create}
273
+ */
274
+ public create(runtime: IFluidDataStoreRuntime, id: string): ISharedDirectory {
275
+ const directory = new SharedDirectory(id, runtime, DirectoryFactory.Attributes);
276
+ directory.initializeLocal();
277
+
278
+ return directory;
279
+ }
289
280
  }
290
281
 
291
282
  /**
@@ -300,1700 +291,1818 @@ export class DirectoryFactory implements IChannelFactory {
300
291
  *
301
292
  * @sealed
302
293
  */
303
- export class SharedDirectory extends SharedObject<ISharedDirectoryEvents> implements ISharedDirectory {
304
- /**
305
- * Create a new shared directory
306
- *
307
- * @param runtime - Data store runtime the new shared directory belongs to
308
- * @param id - Optional name of the shared directory
309
- * @returns Newly create shared directory (but not attached yet)
310
- */
311
- public static create(runtime: IFluidDataStoreRuntime, id?: string): SharedDirectory {
312
- return runtime.createChannel(id, DirectoryFactory.Type) as SharedDirectory;
313
- }
314
-
315
- /**
316
- * Get a factory for SharedDirectory to register with the data store.
317
- *
318
- * @returns A factory that creates and load SharedDirectory
319
- */
320
- public static getFactory(): IChannelFactory {
321
- return new DirectoryFactory();
322
- }
323
-
324
- /**
325
- * String representation for the class.
326
- */
327
- public [Symbol.toStringTag]: string = "SharedDirectory";
328
-
329
- /**
330
- * {@inheritDoc IDirectory.absolutePath}
331
- */
332
- public get absolutePath(): string {
333
- return this.root.absolutePath;
334
- }
335
-
336
- /**
337
- * @internal
338
- */
339
- public readonly localValueMaker: LocalValueMaker;
340
-
341
- /**
342
- * Root of the SharedDirectory, most operations on the SharedDirectory itself act on the root.
343
- */
344
- private readonly root: SubDirectory = new SubDirectory(this, this.runtime, this.serializer, posix.sep);
345
-
346
- /**
347
- * Mapping of op types to message handlers.
348
- */
349
- private readonly messageHandlers: Map<string, IDirectoryMessageHandler> = new Map();
350
-
351
- /**
352
- * Constructs a new shared directory. If the object is non-local an id and service interfaces will
353
- * be provided.
354
- * @param id - String identifier for the SharedDirectory
355
- * @param runtime - Data store runtime
356
- * @param type - Type identifier
357
- */
358
- constructor(
359
- id: string,
360
- runtime: IFluidDataStoreRuntime,
361
- attributes: IChannelAttributes,
362
- ) {
363
- super(id, runtime, attributes, "fluid_directory_");
364
- this.localValueMaker = new LocalValueMaker(this.serializer);
365
- this.setMessageHandlers();
366
- // Mirror the containedValueChanged op on the SharedDirectory
367
- this.root.on(
368
- "containedValueChanged",
369
- (changed: IValueChanged, local: boolean) => {
370
- this.emit("containedValueChanged", changed, local, this);
371
- },
372
- );
373
- this.root.on(
374
- "subDirectoryCreated",
375
- (relativePath: string, local: boolean) => {
376
- this.emit("subDirectoryCreated", relativePath, local, this);
377
- },
378
- );
379
- this.root.on(
380
- "subDirectoryDeleted",
381
- (relativePath: string, local: boolean) => {
382
- this.emit("subDirectoryDeleted", relativePath, local, this);
383
- },
384
- );
385
- }
386
-
387
- /**
388
- * {@inheritDoc IDirectory.get}
389
- */
390
- public get<T = any>(key: string): T | undefined {
391
- return this.root.get<T>(key);
392
- }
393
-
394
- /**
395
- * {@inheritDoc IDirectory.set}
396
- */
397
- public set<T = any>(key: string, value: T): this {
398
- this.root.set(key, value);
399
- return this;
400
- }
401
-
402
- public dispose(error?: Error): void {
403
- this.root.dispose(error);
404
- }
405
-
406
- public get disposed(): boolean {
407
- return this.root.disposed;
408
- }
409
-
410
- /**
411
- * Deletes the given key from within this IDirectory.
412
- * @param key - The key to delete
413
- * @returns True if the key existed and was deleted, false if it did not exist
414
- */
415
- public delete(key: string): boolean {
416
- return this.root.delete(key);
417
- }
418
-
419
- /**
420
- * Deletes all keys from within this IDirectory.
421
- */
422
- public clear(): void {
423
- this.root.clear();
424
- }
425
-
426
- /**
427
- * Checks whether the given key exists in this IDirectory.
428
- * @param key - The key to check
429
- * @returns True if the key exists, false otherwise
430
- */
431
- public has(key: string): boolean {
432
- return this.root.has(key);
433
- }
434
-
435
- /**
436
- * The number of entries under this IDirectory.
437
- */
438
- public get size(): number {
439
- return this.root.size;
440
- }
441
-
442
- /**
443
- * Issue a callback on each entry under this IDirectory.
444
- * @param callback - Callback to issue
445
- */
446
- public forEach(callback: (value: any, key: string, map: Map<string, any>) => void): void {
447
- this.root.forEach(callback);
448
- }
449
-
450
- /**
451
- * Get an iterator over the entries under this IDirectory.
452
- * @returns The iterator
453
- */
454
- public [Symbol.iterator](): IterableIterator<[string, any]> {
455
- return this.root[Symbol.iterator]();
456
- }
457
-
458
- /**
459
- * Get an iterator over the entries under this IDirectory.
460
- * @returns The iterator
461
- */
462
- public entries(): IterableIterator<[string, any]> {
463
- return this.root.entries();
464
- }
465
-
466
- /**
467
- * {@inheritDoc IDirectory.countSubDirectory}
468
- */
469
- public countSubDirectory(): number {
470
- return this.root.countSubDirectory();
471
- }
472
-
473
- /**
474
- * Get an iterator over the keys under this IDirectory.
475
- * @returns The iterator
476
- */
477
- public keys(): IterableIterator<string> {
478
- return this.root.keys();
479
- }
480
-
481
- /**
482
- * Get an iterator over the values under this IDirectory.
483
- * @returns The iterator
484
- */
485
- public values(): IterableIterator<any> {
486
- return this.root.values();
487
- }
488
-
489
- /**
490
- * {@inheritDoc IDirectory.createSubDirectory}
491
- */
492
- public createSubDirectory(subdirName: string): IDirectory {
493
- return this.root.createSubDirectory(subdirName);
494
- }
495
-
496
- /**
497
- * {@inheritDoc IDirectory.getSubDirectory}
498
- */
499
- public getSubDirectory(subdirName: string): IDirectory | undefined {
500
- return this.root.getSubDirectory(subdirName);
501
- }
502
-
503
- /**
504
- * {@inheritDoc IDirectory.hasSubDirectory}
505
- */
506
- public hasSubDirectory(subdirName: string): boolean {
507
- return this.root.hasSubDirectory(subdirName);
508
- }
509
-
510
- /**
511
- * {@inheritDoc IDirectory.deleteSubDirectory}
512
- */
513
- public deleteSubDirectory(subdirName: string): boolean {
514
- return this.root.deleteSubDirectory(subdirName);
515
- }
516
-
517
- /**
518
- * {@inheritDoc IDirectory.subdirectories}
519
- */
520
- public subdirectories(): IterableIterator<[string, IDirectory]> {
521
- return this.root.subdirectories();
522
- }
523
-
524
- /**
525
- * {@inheritDoc IDirectory.getWorkingDirectory}
526
- */
527
- public getWorkingDirectory(relativePath: string): IDirectory | undefined {
528
- const absolutePath = this.makeAbsolute(relativePath);
529
- if (absolutePath === posix.sep) {
530
- return this.root;
531
- }
532
-
533
- let currentSubDir = this.root;
534
- const subdirs = absolutePath.substr(1).split(posix.sep);
535
- for (const subdir of subdirs) {
536
- currentSubDir = currentSubDir.getSubDirectory(subdir) as SubDirectory;
537
- if (!currentSubDir) {
538
- return undefined;
539
- }
540
- }
541
- return currentSubDir;
542
- }
543
-
544
- /**
545
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.summarizeCore}
546
- * @internal
547
- */
548
- protected summarizeCore(
549
- serializer: IFluidSerializer,
550
- telemetryContext?: ITelemetryContext,
551
- ): ISummaryTreeWithStats {
552
- return this.serializeDirectory(this.root, serializer);
553
- }
554
-
555
- /**
556
- * Submits an operation
557
- * @param op - Op to submit
558
- * @param localOpMetadata - The local metadata associated with the op. We send a unique id that is used to track
559
- * this op while it has not been ack'd. This will be sent when we receive this op back from the server.
560
- * @internal
561
- */
562
- public submitDirectoryMessage(op: IDirectoryOperation, localOpMetadata: unknown) {
563
- this.submitLocalMessage(op, localOpMetadata);
564
- }
565
-
566
- /**
567
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
568
- * @internal
569
- */
570
- protected onDisconnect() { }
571
-
572
- /**
573
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.reSubmitCore}
574
- * @internal
575
- */
576
- protected reSubmitCore(content: any, localOpMetadata: unknown) {
577
- const message = content as IDirectoryOperation;
578
- const handler = this.messageHandlers.get(message.type);
579
- assert(handler !== undefined, 0x00d /* Missing message handler for message type */);
580
- handler.submit(message, localOpMetadata);
581
- }
582
-
583
- /**
584
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
585
- * @internal
586
- */
587
- protected async loadCore(storage: IChannelStorageService) {
588
- const data = await readAndParse(storage, snapshotFileName);
589
- const newFormat = data as IDirectoryNewStorageFormat;
590
- if (Array.isArray(newFormat.blobs)) {
591
- // New storage format
592
- this.populate(newFormat.content);
593
- await Promise.all(newFormat.blobs.map(async (value) => {
594
- const dataExtra = await readAndParse(storage, value);
595
- this.populate(dataExtra as IDirectoryDataObject);
596
- }));
597
- } else {
598
- // Old storage format
599
- this.populate(data as IDirectoryDataObject);
600
- }
601
- }
602
-
603
- /**
604
- * Populate the directory with the given directory data.
605
- * @param data - A JSON string containing serialized directory data
606
- * @internal
607
- */
608
- protected populate(data: IDirectoryDataObject) {
609
- const stack: [SubDirectory, IDirectoryDataObject][] = [];
610
- stack.push([this.root, data]);
611
- while (stack.length > 0) {
612
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
613
- const [currentSubDir, currentSubDirObject] = stack.pop()!;
614
- if (currentSubDirObject.subdirectories) {
615
- for (const [subdirName, subdirObject] of Object.entries(currentSubDirObject.subdirectories)) {
616
- let newSubDir = currentSubDir.getSubDirectory(subdirName) as SubDirectory;
617
- if (!newSubDir) {
618
- newSubDir = new SubDirectory(
619
- this,
620
- this.runtime,
621
- this.serializer,
622
- posix.join(currentSubDir.absolutePath, subdirName),
623
- );
624
- currentSubDir.populateSubDirectory(subdirName, newSubDir);
625
- }
626
- stack.push([newSubDir, subdirObject]);
627
- }
628
- }
629
-
630
- if (currentSubDirObject.storage) {
631
- for (const [key, serializable] of Object.entries(currentSubDirObject.storage)) {
632
- const localValue = this.makeLocal(
633
- key,
634
- currentSubDir.absolutePath,
635
- serializable,
636
- );
637
- currentSubDir.populateStorage(key, localValue);
638
- }
639
- }
640
- }
641
- }
642
-
643
- /**
644
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.processCore}
645
- * @internal
646
- */
647
- protected processCore(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown): void {
648
- if (message.type === MessageType.Operation) {
649
- const op: IDirectoryOperation = message.contents as IDirectoryOperation;
650
- const handler = this.messageHandlers.get(op.type);
651
- assert(handler !== undefined, 0x00e /* Missing message handler for message type */);
652
- handler.process(op, local, localOpMetadata);
653
- }
654
- }
655
-
656
- /**
657
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.rollback}
658
- * @internal
659
- */
660
- protected rollback(content: any, localOpMetadata: unknown) {
661
- const op: IDirectoryOperation = content as IDirectoryOperation;
662
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
663
- if (subdir) {
664
- subdir.rollback(op, localOpMetadata);
665
- }
666
- }
667
-
668
- /**
669
- * Converts the given relative path to absolute against the root.
670
- * @param relativePath - The path to convert
671
- */
672
- private makeAbsolute(relativePath: string): string {
673
- return posix.resolve(posix.sep, relativePath);
674
- }
675
-
676
- /**
677
- * The remote ISerializableValue we're receiving (either as a result of a snapshot load or an incoming set op)
678
- * will have the information we need to create a real object, but will not be the real object yet. For example,
679
- * we might know it's a map and the ID but not have the actual map or its data yet. makeLocal's job
680
- * is to convert that information into a real object for local usage.
681
- * @param key - Key of element being converted
682
- * @param absolutePath - Path of element being converted
683
- * @param serializable - The remote information that we can convert into a real object
684
- * @returns The local value that was produced
685
- */
686
- private makeLocal(
687
- key: string,
688
- absolutePath: string,
689
- serializable: ISerializableValue,
690
- ): ILocalValue {
691
- assert(
692
- serializable.type === ValueType[ValueType.Plain] || serializable.type === ValueType[ValueType.Shared],
693
- 0x1e4 /* "Unexpected serializable type" */,
694
- );
695
- return this.localValueMaker.fromSerializable(serializable);
696
- }
697
-
698
- /**
699
- * Set the message handlers for the directory.
700
- */
701
- private setMessageHandlers(): void {
702
- this.messageHandlers.set(
703
- "clear",
704
- {
705
- process: (op: IDirectoryClearOperation, local, localOpMetadata) => {
706
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
707
- if (subdir) {
708
- subdir.processClearMessage(op, local, localOpMetadata);
709
- }
710
- },
711
- submit: (op: IDirectoryClearOperation, localOpMetadata: unknown) => {
712
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
713
- if (subdir) {
714
- subdir.resubmitClearMessage(op, localOpMetadata);
715
- }
716
- },
717
- applyStashedOp: (op: IDirectoryClearOperation): IClearLocalOpMetadata | undefined => {
718
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
719
- if (subdir) {
720
- return subdir.applyStashedClearMessage(op);
721
- }
722
- },
723
- },
724
- );
725
- this.messageHandlers.set(
726
- "delete",
727
- {
728
- process: (op: IDirectoryDeleteOperation, local, localOpMetadata) => {
729
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
730
- if (subdir) {
731
- subdir.processDeleteMessage(op, local, localOpMetadata);
732
- }
733
- },
734
- submit: (op: IDirectoryDeleteOperation, localOpMetadata: unknown) => {
735
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
736
- if (subdir) {
737
- subdir.resubmitKeyMessage(op, localOpMetadata);
738
- }
739
- },
740
- applyStashedOp: (op: IDirectoryDeleteOperation): IKeyEditLocalOpMetadata | undefined => {
741
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
742
- if (subdir) {
743
- return subdir.applyStashedDeleteMessage(op);
744
- }
745
- },
746
- },
747
- );
748
- this.messageHandlers.set(
749
- "set",
750
- {
751
- process: (op: IDirectorySetOperation, local, localOpMetadata) => {
752
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
753
- if (subdir) {
754
- const context = local ? undefined : this.makeLocal(op.key, op.path, op.value);
755
- subdir.processSetMessage(op, context, local, localOpMetadata);
756
- }
757
- },
758
- submit: (op: IDirectorySetOperation, localOpMetadata: unknown) => {
759
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
760
- if (subdir) {
761
- subdir.resubmitKeyMessage(op, localOpMetadata);
762
- }
763
- },
764
- applyStashedOp: (op: IDirectorySetOperation): IKeyEditLocalOpMetadata | undefined => {
765
- const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
766
- if (subdir) {
767
- const context = this.makeLocal(op.key, op.path, op.value);
768
- return subdir.applyStashedSetMessage(op, context);
769
- }
770
- },
771
- },
772
- );
773
-
774
- this.messageHandlers.set(
775
- "createSubDirectory",
776
- {
777
- process: (op: IDirectoryCreateSubDirectoryOperation, local, localOpMetadata) => {
778
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
779
- if (parentSubdir) {
780
- parentSubdir.processCreateSubDirectoryMessage(op, local, localOpMetadata);
781
- }
782
- },
783
- submit: (op: IDirectoryCreateSubDirectoryOperation, localOpMetadata: unknown) => {
784
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
785
- if (parentSubdir) {
786
- // We don't reuse the metadata but send a new one on each submit.
787
- parentSubdir.resubmitSubDirectoryMessage(op, localOpMetadata);
788
- }
789
- },
790
- // eslint-disable-next-line max-len
791
- applyStashedOp: (op: IDirectoryCreateSubDirectoryOperation): ICreateSubDirLocalOpMetadata | undefined => {
792
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
793
- if (parentSubdir) {
794
- return parentSubdir.applyStashedCreateSubDirMessage(op);
795
- }
796
- },
797
- },
798
- );
799
-
800
- this.messageHandlers.set(
801
- "deleteSubDirectory",
802
- {
803
- process: (op: IDirectoryDeleteSubDirectoryOperation, local, localOpMetadata) => {
804
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
805
- if (parentSubdir) {
806
- parentSubdir.processDeleteSubDirectoryMessage(op, local, localOpMetadata);
807
- }
808
- },
809
- submit: (op: IDirectoryDeleteSubDirectoryOperation, localOpMetadata: unknown) => {
810
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
811
- if (parentSubdir) {
812
- // We don't reuse the metadata but send a new one on each submit.
813
- parentSubdir.resubmitSubDirectoryMessage(op, localOpMetadata);
814
- }
815
- },
816
- // eslint-disable-next-line max-len
817
- applyStashedOp: (op: IDirectoryDeleteSubDirectoryOperation): IDeleteSubDirLocalOpMetadata | undefined => {
818
- const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
819
- if (parentSubdir) {
820
- return parentSubdir.applyStashedDeleteSubDirMessage(op);
821
- }
822
- },
823
- },
824
- );
825
- }
826
-
827
- /**
828
- * @internal
829
- */
830
- protected applyStashedOp(op) {
831
- const handler = this.messageHandlers.get(op.type);
832
- if (handler === undefined) {
833
- throw new Error("no apply stashed op handler");
834
- }
835
- return handler.applyStashedOp(op as IDirectoryOperation);
836
- }
837
-
838
- private serializeDirectory(
839
- root: SubDirectory,
840
- serializer: IFluidSerializer,
841
- telemetryContext?: ITelemetryContext,
842
- ): ISummaryTreeWithStats {
843
- const MinValueSizeSeparateSnapshotBlob = 8 * 1024;
844
-
845
- const builder = new SummaryTreeBuilder();
846
- let counter = 0;
847
- const blobs: string[] = [];
848
-
849
- const stack: [SubDirectory, IDirectoryDataObject][] = [];
850
- const content: IDirectoryDataObject = {};
851
- stack.push([root, content]);
852
-
853
- while (stack.length > 0) {
854
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
855
- const [currentSubDir, currentSubDirObject] = stack.pop()!;
856
- for (const [key, value] of currentSubDir.getSerializedStorage(serializer)) {
857
- if (!currentSubDirObject.storage) {
858
- currentSubDirObject.storage = {};
859
- }
860
- const result: ISerializableValue = {
861
- type: value.type,
862
- // eslint-disable-next-line @typescript-eslint/ban-types
863
- value: value.value && JSON.parse(value.value) as object,
864
- };
865
- if (value.value && value.value.length >= MinValueSizeSeparateSnapshotBlob) {
866
- const extraContent: IDirectoryDataObject = {};
867
- let largeContent = extraContent;
868
- if (currentSubDir.absolutePath !== posix.sep) {
869
- for (const dir of currentSubDir.absolutePath.substr(1).split(posix.sep)) {
870
- const subDataObject: IDirectoryDataObject = {};
871
- largeContent.subdirectories = { [dir]: subDataObject };
872
- largeContent = subDataObject;
873
- }
874
- }
875
- largeContent.storage = { [key]: result };
876
- const blobName = `blob${counter}`;
877
- counter++;
878
- blobs.push(blobName);
879
- builder.addBlob(blobName, JSON.stringify(extraContent));
880
- } else {
881
- currentSubDirObject.storage[key] = result;
882
- }
883
- }
884
-
885
- for (const [subdirName, subdir] of currentSubDir.subdirectories()) {
886
- if (!currentSubDirObject.subdirectories) {
887
- currentSubDirObject.subdirectories = {};
888
- }
889
- const subDataObject: IDirectoryDataObject = {};
890
- currentSubDirObject.subdirectories[subdirName] = subDataObject;
891
- stack.push([subdir as SubDirectory, subDataObject]);
892
- }
893
- }
894
-
895
- const newFormat: IDirectoryNewStorageFormat = {
896
- blobs,
897
- content,
898
- };
899
- builder.addBlob(snapshotFileName, JSON.stringify(newFormat));
900
-
901
- return builder.getSummaryTree();
902
- }
294
+ export class SharedDirectory
295
+ extends SharedObject<ISharedDirectoryEvents>
296
+ implements ISharedDirectory
297
+ {
298
+ /**
299
+ * Create a new shared directory
300
+ *
301
+ * @param runtime - Data store runtime the new shared directory belongs to
302
+ * @param id - Optional name of the shared directory
303
+ * @returns Newly create shared directory (but not attached yet)
304
+ */
305
+ public static create(runtime: IFluidDataStoreRuntime, id?: string): SharedDirectory {
306
+ return runtime.createChannel(id, DirectoryFactory.Type) as SharedDirectory;
307
+ }
308
+
309
+ /**
310
+ * Get a factory for SharedDirectory to register with the data store.
311
+ *
312
+ * @returns A factory that creates and load SharedDirectory
313
+ */
314
+ public static getFactory(): IChannelFactory {
315
+ return new DirectoryFactory();
316
+ }
317
+
318
+ /**
319
+ * String representation for the class.
320
+ */
321
+ public [Symbol.toStringTag]: string = "SharedDirectory";
322
+
323
+ /**
324
+ * {@inheritDoc IDirectory.absolutePath}
325
+ */
326
+ public get absolutePath(): string {
327
+ return this.root.absolutePath;
328
+ }
329
+
330
+ /**
331
+ * @internal
332
+ */
333
+ public readonly localValueMaker: LocalValueMaker;
334
+
335
+ /**
336
+ * Root of the SharedDirectory, most operations on the SharedDirectory itself act on the root.
337
+ */
338
+ private readonly root: SubDirectory = new SubDirectory(
339
+ this,
340
+ this.runtime,
341
+ this.serializer,
342
+ posix.sep,
343
+ );
344
+
345
+ /**
346
+ * Mapping of op types to message handlers.
347
+ */
348
+ private readonly messageHandlers: Map<string, IDirectoryMessageHandler> = new Map();
349
+
350
+ /**
351
+ * Constructs a new shared directory. If the object is non-local an id and service interfaces will
352
+ * be provided.
353
+ * @param id - String identifier for the SharedDirectory
354
+ * @param runtime - Data store runtime
355
+ * @param type - Type identifier
356
+ */
357
+ public constructor(
358
+ id: string,
359
+ runtime: IFluidDataStoreRuntime,
360
+ attributes: IChannelAttributes,
361
+ ) {
362
+ super(id, runtime, attributes, "fluid_directory_");
363
+ this.localValueMaker = new LocalValueMaker(this.serializer);
364
+ this.setMessageHandlers();
365
+ // Mirror the containedValueChanged op on the SharedDirectory
366
+ this.root.on("containedValueChanged", (changed: IValueChanged, local: boolean) => {
367
+ this.emit("containedValueChanged", changed, local, this);
368
+ });
369
+ this.root.on("subDirectoryCreated", (relativePath: string, local: boolean) => {
370
+ this.emit("subDirectoryCreated", relativePath, local, this);
371
+ });
372
+ this.root.on("subDirectoryDeleted", (relativePath: string, local: boolean) => {
373
+ this.emit("subDirectoryDeleted", relativePath, local, this);
374
+ });
375
+ }
376
+
377
+ /**
378
+ * {@inheritDoc IDirectory.get}
379
+ */
380
+ // TODO: Use `unknown` instead (breaking change).
381
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
382
+ public get<T = any>(key: string): T | undefined {
383
+ return this.root.get<T>(key);
384
+ }
385
+
386
+ /**
387
+ * {@inheritDoc IDirectory.set}
388
+ */
389
+ public set<T = unknown>(key: string, value: T): this {
390
+ this.root.set(key, value);
391
+ return this;
392
+ }
393
+
394
+ public dispose(error?: Error): void {
395
+ this.root.dispose(error);
396
+ }
397
+
398
+ public get disposed(): boolean {
399
+ return this.root.disposed;
400
+ }
401
+
402
+ /**
403
+ * Deletes the given key from within this IDirectory.
404
+ * @param key - The key to delete
405
+ * @returns True if the key existed and was deleted, false if it did not exist
406
+ */
407
+ public delete(key: string): boolean {
408
+ return this.root.delete(key);
409
+ }
410
+
411
+ /**
412
+ * Deletes all keys from within this IDirectory.
413
+ */
414
+ public clear(): void {
415
+ this.root.clear();
416
+ }
417
+
418
+ /**
419
+ * Checks whether the given key exists in this IDirectory.
420
+ * @param key - The key to check
421
+ * @returns True if the key exists, false otherwise
422
+ */
423
+ public has(key: string): boolean {
424
+ return this.root.has(key);
425
+ }
426
+
427
+ /**
428
+ * The number of entries under this IDirectory.
429
+ */
430
+ public get size(): number {
431
+ return this.root.size;
432
+ }
433
+
434
+ /**
435
+ * Issue a callback on each entry under this IDirectory.
436
+ * @param callback - Callback to issue
437
+ */
438
+ // TODO: Use `unknown` instead (breaking change).
439
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
440
+ public forEach(callback: (value: any, key: string, map: Map<string, any>) => void): void {
441
+ // eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-callback-reference
442
+ this.root.forEach(callback);
443
+ }
444
+
445
+ /**
446
+ * Get an iterator over the entries under this IDirectory.
447
+ * @returns The iterator
448
+ */
449
+ // TODO: Use `unknown` instead (breaking change).
450
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
451
+ public [Symbol.iterator](): IterableIterator<[string, any]> {
452
+ return this.root[Symbol.iterator]();
453
+ }
454
+
455
+ /**
456
+ * Get an iterator over the entries under this IDirectory.
457
+ * @returns The iterator
458
+ */
459
+ // TODO: Use `unknown` instead (breaking change).
460
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
461
+ public entries(): IterableIterator<[string, any]> {
462
+ return this.root.entries();
463
+ }
464
+
465
+ /**
466
+ * {@inheritDoc IDirectory.countSubDirectory}
467
+ */
468
+ public countSubDirectory(): number {
469
+ return this.root.countSubDirectory();
470
+ }
471
+
472
+ /**
473
+ * Get an iterator over the keys under this IDirectory.
474
+ * @returns The iterator
475
+ */
476
+ public keys(): IterableIterator<string> {
477
+ return this.root.keys();
478
+ }
479
+
480
+ /**
481
+ * Get an iterator over the values under this IDirectory.
482
+ * @returns The iterator
483
+ */
484
+ // TODO: Use `unknown` instead (breaking change).
485
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
486
+ public values(): IterableIterator<any> {
487
+ return this.root.values();
488
+ }
489
+
490
+ /**
491
+ * {@inheritDoc IDirectory.createSubDirectory}
492
+ */
493
+ public createSubDirectory(subdirName: string): IDirectory {
494
+ return this.root.createSubDirectory(subdirName);
495
+ }
496
+
497
+ /**
498
+ * {@inheritDoc IDirectory.getSubDirectory}
499
+ */
500
+ public getSubDirectory(subdirName: string): IDirectory | undefined {
501
+ return this.root.getSubDirectory(subdirName);
502
+ }
503
+
504
+ /**
505
+ * {@inheritDoc IDirectory.hasSubDirectory}
506
+ */
507
+ public hasSubDirectory(subdirName: string): boolean {
508
+ return this.root.hasSubDirectory(subdirName);
509
+ }
510
+
511
+ /**
512
+ * {@inheritDoc IDirectory.deleteSubDirectory}
513
+ */
514
+ public deleteSubDirectory(subdirName: string): boolean {
515
+ return this.root.deleteSubDirectory(subdirName);
516
+ }
517
+
518
+ /**
519
+ * {@inheritDoc IDirectory.subdirectories}
520
+ */
521
+ public subdirectories(): IterableIterator<[string, IDirectory]> {
522
+ return this.root.subdirectories();
523
+ }
524
+
525
+ /**
526
+ * {@inheritDoc IDirectory.getWorkingDirectory}
527
+ */
528
+ public getWorkingDirectory(relativePath: string): IDirectory | undefined {
529
+ const absolutePath = this.makeAbsolute(relativePath);
530
+ if (absolutePath === posix.sep) {
531
+ return this.root;
532
+ }
533
+
534
+ let currentSubDir = this.root;
535
+ const subdirs = absolutePath.slice(1).split(posix.sep);
536
+ for (const subdir of subdirs) {
537
+ currentSubDir = currentSubDir.getSubDirectory(subdir) as SubDirectory;
538
+ if (!currentSubDir) {
539
+ return undefined;
540
+ }
541
+ }
542
+ return currentSubDir;
543
+ }
544
+
545
+ /**
546
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.summarizeCore}
547
+ * @internal
548
+ */
549
+ protected summarizeCore(
550
+ serializer: IFluidSerializer,
551
+ telemetryContext?: ITelemetryContext,
552
+ ): ISummaryTreeWithStats {
553
+ return this.serializeDirectory(this.root, serializer);
554
+ }
555
+
556
+ /**
557
+ * Submits an operation
558
+ * @param op - Op to submit
559
+ * @param localOpMetadata - The local metadata associated with the op. We send a unique id that is used to track
560
+ * this op while it has not been ack'd. This will be sent when we receive this op back from the server.
561
+ * @internal
562
+ */
563
+ public submitDirectoryMessage(op: IDirectoryOperation, localOpMetadata: unknown): void {
564
+ this.submitLocalMessage(op, localOpMetadata);
565
+ }
566
+
567
+ /**
568
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
569
+ * @internal
570
+ */
571
+ protected onDisconnect(): void {}
572
+
573
+ /**
574
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.reSubmitCore}
575
+ * @internal
576
+ */
577
+ protected reSubmitCore(content: unknown, localOpMetadata: unknown): void {
578
+ const message = content as IDirectoryOperation;
579
+ const handler = this.messageHandlers.get(message.type);
580
+ assert(handler !== undefined, 0x00d /* Missing message handler for message type */);
581
+ handler.submit(message, localOpMetadata);
582
+ }
583
+
584
+ /**
585
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
586
+ * @internal
587
+ */
588
+ protected async loadCore(storage: IChannelStorageService): Promise<void> {
589
+ const data = await readAndParse(storage, snapshotFileName);
590
+ const newFormat = data as IDirectoryNewStorageFormat;
591
+ if (Array.isArray(newFormat.blobs)) {
592
+ // New storage format
593
+ this.populate(newFormat.content);
594
+ await Promise.all(
595
+ newFormat.blobs.map(async (value) => {
596
+ const dataExtra = await readAndParse(storage, value);
597
+ this.populate(dataExtra as IDirectoryDataObject);
598
+ }),
599
+ );
600
+ } else {
601
+ // Old storage format
602
+ this.populate(data as IDirectoryDataObject);
603
+ }
604
+ }
605
+
606
+ /**
607
+ * Populate the directory with the given directory data.
608
+ * @param data - A JSON string containing serialized directory data
609
+ * @internal
610
+ */
611
+ protected populate(data: IDirectoryDataObject): void {
612
+ const stack: [SubDirectory, IDirectoryDataObject][] = [];
613
+ stack.push([this.root, data]);
614
+ while (stack.length > 0) {
615
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
616
+ const [currentSubDir, currentSubDirObject] = stack.pop()!;
617
+ if (currentSubDirObject.subdirectories) {
618
+ for (const [subdirName, subdirObject] of Object.entries(
619
+ currentSubDirObject.subdirectories,
620
+ )) {
621
+ let newSubDir = currentSubDir.getSubDirectory(subdirName) as SubDirectory;
622
+ if (!newSubDir) {
623
+ newSubDir = new SubDirectory(
624
+ this,
625
+ this.runtime,
626
+ this.serializer,
627
+ posix.join(currentSubDir.absolutePath, subdirName),
628
+ );
629
+ currentSubDir.populateSubDirectory(subdirName, newSubDir);
630
+ }
631
+ stack.push([newSubDir, subdirObject]);
632
+ }
633
+ }
634
+
635
+ if (currentSubDirObject.storage) {
636
+ for (const [key, serializable] of Object.entries(currentSubDirObject.storage)) {
637
+ const localValue = this.makeLocal(
638
+ key,
639
+ currentSubDir.absolutePath,
640
+ serializable,
641
+ );
642
+ currentSubDir.populateStorage(key, localValue);
643
+ }
644
+ }
645
+ }
646
+ }
647
+
648
+ /**
649
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.processCore}
650
+ * @internal
651
+ */
652
+ protected processCore(
653
+ message: ISequencedDocumentMessage,
654
+ local: boolean,
655
+ localOpMetadata: unknown,
656
+ ): void {
657
+ if (message.type === MessageType.Operation) {
658
+ const op: IDirectoryOperation = message.contents as IDirectoryOperation;
659
+ const handler = this.messageHandlers.get(op.type);
660
+ assert(handler !== undefined, 0x00e /* Missing message handler for message type */);
661
+ handler.process(op, local, localOpMetadata);
662
+ }
663
+ }
664
+
665
+ /**
666
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.rollback}
667
+ * @internal
668
+ */
669
+ protected rollback(content: unknown, localOpMetadata: unknown): void {
670
+ const op: IDirectoryOperation = content as IDirectoryOperation;
671
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
672
+ if (subdir) {
673
+ subdir.rollback(op, localOpMetadata);
674
+ }
675
+ }
676
+
677
+ /**
678
+ * Converts the given relative path to absolute against the root.
679
+ * @param relativePath - The path to convert
680
+ */
681
+ private makeAbsolute(relativePath: string): string {
682
+ return posix.resolve(posix.sep, relativePath);
683
+ }
684
+
685
+ /**
686
+ * The remote ISerializableValue we're receiving (either as a result of a snapshot load or an incoming set op)
687
+ * will have the information we need to create a real object, but will not be the real object yet. For example,
688
+ * we might know it's a map and the ID but not have the actual map or its data yet. makeLocal's job
689
+ * is to convert that information into a real object for local usage.
690
+ * @param key - Key of element being converted
691
+ * @param absolutePath - Path of element being converted
692
+ * @param serializable - The remote information that we can convert into a real object
693
+ * @returns The local value that was produced
694
+ */
695
+ private makeLocal(
696
+ key: string,
697
+ absolutePath: string,
698
+ serializable: ISerializableValue,
699
+ ): ILocalValue {
700
+ assert(
701
+ serializable.type === ValueType[ValueType.Plain] ||
702
+ serializable.type === ValueType[ValueType.Shared],
703
+ 0x1e4 /* "Unexpected serializable type" */,
704
+ );
705
+ return this.localValueMaker.fromSerializable(serializable);
706
+ }
707
+
708
+ /**
709
+ * Set the message handlers for the directory.
710
+ */
711
+ private setMessageHandlers(): void {
712
+ this.messageHandlers.set("clear", {
713
+ process: (op: IDirectoryClearOperation, local, localOpMetadata) => {
714
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
715
+ if (subdir) {
716
+ subdir.processClearMessage(op, local, localOpMetadata);
717
+ }
718
+ },
719
+ submit: (op: IDirectoryClearOperation, localOpMetadata: unknown) => {
720
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
721
+ if (subdir) {
722
+ subdir.resubmitClearMessage(op, localOpMetadata);
723
+ }
724
+ },
725
+ applyStashedOp: (op: IDirectoryClearOperation): IClearLocalOpMetadata | undefined => {
726
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
727
+ if (subdir) {
728
+ return subdir.applyStashedClearMessage(op);
729
+ }
730
+ },
731
+ });
732
+ this.messageHandlers.set("delete", {
733
+ process: (op: IDirectoryDeleteOperation, local, localOpMetadata) => {
734
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
735
+ if (subdir) {
736
+ subdir.processDeleteMessage(op, local, localOpMetadata);
737
+ }
738
+ },
739
+ submit: (op: IDirectoryDeleteOperation, localOpMetadata: unknown) => {
740
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
741
+ if (subdir) {
742
+ subdir.resubmitKeyMessage(op, localOpMetadata);
743
+ }
744
+ },
745
+ applyStashedOp: (
746
+ op: IDirectoryDeleteOperation,
747
+ ): IKeyEditLocalOpMetadata | undefined => {
748
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
749
+ if (subdir) {
750
+ return subdir.applyStashedDeleteMessage(op);
751
+ }
752
+ },
753
+ });
754
+ this.messageHandlers.set("set", {
755
+ process: (op: IDirectorySetOperation, local, localOpMetadata) => {
756
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
757
+ if (subdir) {
758
+ const context = local ? undefined : this.makeLocal(op.key, op.path, op.value);
759
+ subdir.processSetMessage(op, context, local, localOpMetadata);
760
+ }
761
+ },
762
+ submit: (op: IDirectorySetOperation, localOpMetadata: unknown) => {
763
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
764
+ if (subdir) {
765
+ subdir.resubmitKeyMessage(op, localOpMetadata);
766
+ }
767
+ },
768
+ applyStashedOp: (op: IDirectorySetOperation): IKeyEditLocalOpMetadata | undefined => {
769
+ const subdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
770
+ if (subdir) {
771
+ const context = this.makeLocal(op.key, op.path, op.value);
772
+ return subdir.applyStashedSetMessage(op, context);
773
+ }
774
+ },
775
+ });
776
+
777
+ this.messageHandlers.set("createSubDirectory", {
778
+ process: (op: IDirectoryCreateSubDirectoryOperation, local, localOpMetadata) => {
779
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
780
+ if (parentSubdir) {
781
+ parentSubdir.processCreateSubDirectoryMessage(op, local, localOpMetadata);
782
+ }
783
+ },
784
+ submit: (op: IDirectoryCreateSubDirectoryOperation, localOpMetadata: unknown) => {
785
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
786
+ if (parentSubdir) {
787
+ // We don't reuse the metadata but send a new one on each submit.
788
+ parentSubdir.resubmitSubDirectoryMessage(op, localOpMetadata);
789
+ }
790
+ },
791
+ applyStashedOp: (
792
+ op: IDirectoryCreateSubDirectoryOperation,
793
+ ): ICreateSubDirLocalOpMetadata | undefined => {
794
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
795
+ if (parentSubdir) {
796
+ return parentSubdir.applyStashedCreateSubDirMessage(op);
797
+ }
798
+ },
799
+ });
800
+
801
+ this.messageHandlers.set("deleteSubDirectory", {
802
+ process: (op: IDirectoryDeleteSubDirectoryOperation, local, localOpMetadata) => {
803
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
804
+ if (parentSubdir) {
805
+ parentSubdir.processDeleteSubDirectoryMessage(op, local, localOpMetadata);
806
+ }
807
+ },
808
+ submit: (op: IDirectoryDeleteSubDirectoryOperation, localOpMetadata: unknown) => {
809
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
810
+ if (parentSubdir) {
811
+ // We don't reuse the metadata but send a new one on each submit.
812
+ parentSubdir.resubmitSubDirectoryMessage(op, localOpMetadata);
813
+ }
814
+ },
815
+ applyStashedOp: (
816
+ op: IDirectoryDeleteSubDirectoryOperation,
817
+ ): IDeleteSubDirLocalOpMetadata | undefined => {
818
+ const parentSubdir = this.getWorkingDirectory(op.path) as SubDirectory | undefined;
819
+ if (parentSubdir) {
820
+ return parentSubdir.applyStashedDeleteSubDirMessage(op);
821
+ }
822
+ },
823
+ });
824
+ }
825
+
826
+ /**
827
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObjectCore.applyStashedOp}
828
+ * @internal
829
+ */
830
+ protected applyStashedOp(op: unknown): unknown {
831
+ const handler = this.messageHandlers.get((op as IDirectoryOperation).type);
832
+ if (handler === undefined) {
833
+ throw new Error("no apply stashed op handler");
834
+ }
835
+ return handler.applyStashedOp(op as IDirectoryOperation);
836
+ }
837
+
838
+ private serializeDirectory(
839
+ root: SubDirectory,
840
+ serializer: IFluidSerializer,
841
+ telemetryContext?: ITelemetryContext,
842
+ ): ISummaryTreeWithStats {
843
+ const MinValueSizeSeparateSnapshotBlob = 8 * 1024;
844
+
845
+ const builder = new SummaryTreeBuilder();
846
+ let counter = 0;
847
+ const blobs: string[] = [];
848
+
849
+ const stack: [SubDirectory, IDirectoryDataObject][] = [];
850
+ const content: IDirectoryDataObject = {};
851
+ stack.push([root, content]);
852
+
853
+ while (stack.length > 0) {
854
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
855
+ const [currentSubDir, currentSubDirObject] = stack.pop()!;
856
+ for (const [key, value] of currentSubDir.getSerializedStorage(serializer)) {
857
+ if (!currentSubDirObject.storage) {
858
+ currentSubDirObject.storage = {};
859
+ }
860
+ const result: ISerializableValue = {
861
+ type: value.type,
862
+ value: value.value && (JSON.parse(value.value) as object),
863
+ };
864
+ if (value.value && value.value.length >= MinValueSizeSeparateSnapshotBlob) {
865
+ const extraContent: IDirectoryDataObject = {};
866
+ let largeContent = extraContent;
867
+ if (currentSubDir.absolutePath !== posix.sep) {
868
+ for (const dir of currentSubDir.absolutePath.slice(1).split(posix.sep)) {
869
+ const subDataObject: IDirectoryDataObject = {};
870
+ largeContent.subdirectories = { [dir]: subDataObject };
871
+ largeContent = subDataObject;
872
+ }
873
+ }
874
+ largeContent.storage = { [key]: result };
875
+ const blobName = `blob${counter}`;
876
+ counter++;
877
+ blobs.push(blobName);
878
+ builder.addBlob(blobName, JSON.stringify(extraContent));
879
+ } else {
880
+ currentSubDirObject.storage[key] = result;
881
+ }
882
+ }
883
+
884
+ for (const [subdirName, subdir] of currentSubDir.subdirectories()) {
885
+ if (!currentSubDirObject.subdirectories) {
886
+ currentSubDirObject.subdirectories = {};
887
+ }
888
+ const subDataObject: IDirectoryDataObject = {};
889
+ currentSubDirObject.subdirectories[subdirName] = subDataObject;
890
+ stack.push([subdir as SubDirectory, subDataObject]);
891
+ }
892
+ }
893
+
894
+ const newFormat: IDirectoryNewStorageFormat = {
895
+ blobs,
896
+ content,
897
+ };
898
+ builder.addBlob(snapshotFileName, JSON.stringify(newFormat));
899
+
900
+ return builder.getSummaryTree();
901
+ }
903
902
  }
904
903
 
905
904
  interface IKeyEditLocalOpMetadata {
906
- type: "edit";
907
- pendingMessageId: number;
908
- previousValue: ILocalValue | undefined;
905
+ type: "edit";
906
+ pendingMessageId: number;
907
+ previousValue: ILocalValue | undefined;
909
908
  }
910
909
 
911
910
  interface IClearLocalOpMetadata {
912
- type: "clear";
913
- pendingMessageId: number;
914
- previousStorage: Map<string, ILocalValue>;
911
+ type: "clear";
912
+ pendingMessageId: number;
913
+ previousStorage: Map<string, ILocalValue>;
915
914
  }
916
915
 
917
916
  interface ICreateSubDirLocalOpMetadata {
918
- type: "createSubDir";
919
- pendingMessageId: number;
920
- previouslyExisted: boolean;
917
+ type: "createSubDir";
918
+ pendingMessageId: number;
919
+ previouslyExisted: boolean;
921
920
  }
922
921
 
923
922
  interface IDeleteSubDirLocalOpMetadata {
924
- type: "deleteSubDir";
925
- pendingMessageId: number;
926
- subDirectory: SubDirectory | undefined;
923
+ type: "deleteSubDir";
924
+ pendingMessageId: number;
925
+ subDirectory: SubDirectory | undefined;
927
926
  }
928
927
 
929
928
  type SubDirLocalOpMetadata = ICreateSubDirLocalOpMetadata | IDeleteSubDirLocalOpMetadata;
930
- type DirectoryLocalOpMetadata = IClearLocalOpMetadata | IKeyEditLocalOpMetadata | SubDirLocalOpMetadata;
929
+ type DirectoryLocalOpMetadata =
930
+ | IClearLocalOpMetadata
931
+ | IKeyEditLocalOpMetadata
932
+ | SubDirLocalOpMetadata;
933
+
934
+ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */
931
935
 
932
936
  function isKeyEditLocalOpMetadata(metadata: any): metadata is IKeyEditLocalOpMetadata {
933
- return metadata !== undefined && typeof metadata.pendingMessageId === "number" && metadata.type === "edit";
937
+ return (
938
+ metadata !== undefined &&
939
+ typeof metadata.pendingMessageId === "number" &&
940
+ metadata.type === "edit"
941
+ );
934
942
  }
935
943
 
936
944
  function isClearLocalOpMetadata(metadata: any): metadata is IClearLocalOpMetadata {
937
- return metadata !== undefined && metadata.type === "clear" && typeof metadata.pendingMessageId === "number" &&
938
- typeof metadata.previousStorage === "object";
945
+ return (
946
+ metadata !== undefined &&
947
+ metadata.type === "clear" &&
948
+ typeof metadata.pendingMessageId === "number" &&
949
+ typeof metadata.previousStorage === "object"
950
+ );
939
951
  }
940
952
 
941
953
  function isSubDirLocalOpMetadata(metadata: any): metadata is SubDirLocalOpMetadata {
942
- return metadata !== undefined && typeof metadata.pendingMessageId === "number" &&
943
- ((metadata.type === "createSubDir" && typeof metadata.previouslyExisted === "boolean") ||
944
- metadata.type === "deleteSubDir");
954
+ return (
955
+ metadata !== undefined &&
956
+ typeof metadata.pendingMessageId === "number" &&
957
+ ((metadata.type === "createSubDir" && typeof metadata.previouslyExisted === "boolean") ||
958
+ metadata.type === "deleteSubDir")
959
+ );
945
960
  }
946
961
 
947
962
  function isDirectoryLocalOpMetadata(metadata: any): metadata is DirectoryLocalOpMetadata {
948
- return metadata !== undefined && typeof metadata.pendingMessageId === "number" &&
949
- (metadata.type === "edit" || metadata.type === "deleteSubDir" ||
950
- (metadata.type === "clear" && typeof metadata.previousStorage === "object") ||
951
- (metadata.type === "createSubDir" && typeof metadata.previouslyExisted === "boolean"));
963
+ return (
964
+ metadata !== undefined &&
965
+ typeof metadata.pendingMessageId === "number" &&
966
+ (metadata.type === "edit" ||
967
+ metadata.type === "deleteSubDir" ||
968
+ (metadata.type === "clear" && typeof metadata.previousStorage === "object") ||
969
+ (metadata.type === "createSubDir" && typeof metadata.previouslyExisted === "boolean"))
970
+ );
952
971
  }
953
972
 
973
+ /* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */
974
+
954
975
  /**
955
976
  * Node of the directory tree.
956
977
  * @sealed
957
978
  */
958
979
  class SubDirectory extends TypedEventEmitter<IDirectoryEvents> implements IDirectory {
959
- /**
960
- * Tells if the sub directory is deleted or not.
961
- */
962
- private _deleted = false;
963
-
964
- /**
965
- * String representation for the class.
966
- */
967
- public [Symbol.toStringTag]: string = "SubDirectory";
968
-
969
- /**
970
- * The in-memory data the directory is storing.
971
- */
972
- private readonly _storage: Map<string, ILocalValue> = new Map();
973
-
974
- /**
975
- * The subdirectories the directory is holding.
976
- */
977
- private readonly _subdirectories: Map<string, SubDirectory> = new Map();
978
-
979
- /**
980
- * Keys that have been modified locally but not yet ack'd from the server.
981
- */
982
- private readonly pendingKeys: Map<string, number[]> = new Map();
983
-
984
- /**
985
- * Subdirectories that have been modified locally but not yet ack'd from the server.
986
- */
987
- private readonly pendingSubDirectories: Map<string, number[]> = new Map();
988
-
989
- /**
990
- * This is used to assign a unique id to every outgoing operation and helps in tracking unack'd ops.
991
- */
992
- private pendingMessageId: number = -1;
993
-
994
- /**
995
- * The pending ids of any clears that have been performed locally but not yet ack'd from the server
996
- */
997
- private readonly pendingClearMessageIds: number[] = [];
998
-
999
- /**
1000
- * Constructor.
1001
- * @param directory - Reference back to the SharedDirectory to perform operations
1002
- * @param runtime - The data store runtime this directory is associated with
1003
- * @param serializer - The serializer to serialize / parse handles
1004
- * @param absolutePath - The absolute path of this IDirectory
1005
- */
1006
- constructor(
1007
- private readonly directory: SharedDirectory,
1008
- private readonly runtime: IFluidDataStoreRuntime,
1009
- private readonly serializer: IFluidSerializer,
1010
- public readonly absolutePath: string,
1011
- ) {
1012
- super();
1013
- }
1014
-
1015
- public dispose(error?: Error): void {
1016
- this._deleted = true;
1017
- this.emit("disposed", this);
1018
- }
1019
-
1020
- /**
1021
- * Unmark the deleted property when rolling back delete.
1022
- */
1023
- private undispose(): void {
1024
- this._deleted = false;
1025
- }
1026
-
1027
- public get disposed(): boolean {
1028
- return this._deleted;
1029
- }
1030
-
1031
- private throwIfDisposed() {
1032
- if (this._deleted) {
1033
- throw new UsageError("Cannot access Disposed subDirectory");
1034
- }
1035
- }
1036
-
1037
- /**
1038
- * Checks whether the given key exists in this IDirectory.
1039
- * @param key - The key to check
1040
- * @returns True if the key exists, false otherwise
1041
- */
1042
- public has(key: string): boolean {
1043
- this.throwIfDisposed();
1044
- return this._storage.has(key);
1045
- }
1046
-
1047
- /**
1048
- * {@inheritDoc IDirectory.get}
1049
- */
1050
- public get<T = any>(key: string): T | undefined {
1051
- this.throwIfDisposed();
1052
- return this._storage.get(key)?.value as T | undefined;
1053
- }
1054
-
1055
- /**
1056
- * {@inheritDoc IDirectory.set}
1057
- */
1058
- public set<T = any>(key: string, value: T): this {
1059
- this.throwIfDisposed();
1060
- // Undefined/null keys can't be serialized to JSON in the manner we currently snapshot.
1061
- if (key === undefined || key === null) {
1062
- throw new Error("Undefined and null keys are not supported");
1063
- }
1064
-
1065
- // Create a local value and serialize it.
1066
- const localValue = this.directory.localValueMaker.fromInMemory(value);
1067
- const serializableValue = makeSerializable(
1068
- localValue,
1069
- this.serializer,
1070
- this.directory.handle);
1071
-
1072
- // Set the value locally.
1073
- const previousValue = this.setCore(
1074
- key,
1075
- localValue,
1076
- true,
1077
- );
1078
-
1079
- // If we are not attached, don't submit the op.
1080
- if (!this.directory.isAttached()) {
1081
- return this;
1082
- }
1083
-
1084
- const op: IDirectorySetOperation = {
1085
- key,
1086
- path: this.absolutePath,
1087
- type: "set",
1088
- value: serializableValue,
1089
- };
1090
- this.submitKeyMessage(op, previousValue);
1091
- return this;
1092
- }
1093
-
1094
- /**
1095
- * {@inheritDoc IDirectory.countSubDirectory}
1096
- */
1097
- public countSubDirectory(): number {
1098
- return this._subdirectories.size;
1099
- }
1100
-
1101
- /**
1102
- * {@inheritDoc IDirectory.createSubDirectory}
1103
- */
1104
- public createSubDirectory(subdirName: string): IDirectory {
1105
- this.throwIfDisposed();
1106
- // Undefined/null subdirectory names can't be serialized to JSON in the manner we currently snapshot.
1107
- if (subdirName === undefined || subdirName === null) {
1108
- throw new Error("SubDirectory name may not be undefined or null");
1109
- }
1110
-
1111
- if (subdirName.includes(posix.sep)) {
1112
- throw new Error(`SubDirectory name may not contain ${posix.sep}`);
1113
- }
1114
-
1115
- // Create the sub directory locally first.
1116
- const isNew = this.createSubDirectoryCore(subdirName, true);
1117
-
1118
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1119
- const subDir: IDirectory = this._subdirectories.get(subdirName)!;
1120
-
1121
- // If we are not attached, don't submit the op.
1122
- if (!this.directory.isAttached()) {
1123
- return subDir;
1124
- }
1125
-
1126
- const op: IDirectoryCreateSubDirectoryOperation = {
1127
- path: this.absolutePath,
1128
- subdirName,
1129
- type: "createSubDirectory",
1130
- };
1131
- this.submitCreateSubDirectoryMessage(op, !isNew);
1132
-
1133
- return subDir;
1134
- }
1135
-
1136
- /**
1137
- * {@inheritDoc IDirectory.getSubDirectory}
1138
- */
1139
- public getSubDirectory(subdirName: string): IDirectory | undefined {
1140
- this.throwIfDisposed();
1141
- return this._subdirectories.get(subdirName);
1142
- }
1143
-
1144
- /**
1145
- * {@inheritDoc IDirectory.hasSubDirectory}
1146
- */
1147
- public hasSubDirectory(subdirName: string): boolean {
1148
- this.throwIfDisposed();
1149
- return this._subdirectories.has(subdirName);
1150
- }
1151
-
1152
- /**
1153
- * {@inheritDoc IDirectory.deleteSubDirectory}
1154
- */
1155
- public deleteSubDirectory(subdirName: string): boolean {
1156
- this.throwIfDisposed();
1157
- // Delete the sub directory locally first.
1158
- const subDir = this.deleteSubDirectoryCore(subdirName, true);
1159
-
1160
- // If we are not attached, don't submit the op.
1161
- if (!this.directory.isAttached()) {
1162
- return subDir !== undefined;
1163
- }
1164
-
1165
- const op: IDirectoryDeleteSubDirectoryOperation = {
1166
- path: this.absolutePath,
1167
- subdirName,
1168
- type: "deleteSubDirectory",
1169
- };
1170
-
1171
- this.submitDeleteSubDirectoryMessage(op, subDir);
1172
- return subDir !== undefined;
1173
- }
1174
-
1175
- /**
1176
- * {@inheritDoc IDirectory.subdirectories}
1177
- */
1178
- public subdirectories(): IterableIterator<[string, IDirectory]> {
1179
- this.throwIfDisposed();
1180
- return this._subdirectories.entries();
1181
- }
1182
-
1183
- /**
1184
- * {@inheritDoc IDirectory.getWorkingDirectory}
1185
- */
1186
- public getWorkingDirectory(relativePath: string): IDirectory | undefined {
1187
- this.throwIfDisposed();
1188
- return this.directory.getWorkingDirectory(this.makeAbsolute(relativePath));
1189
- }
1190
-
1191
- /**
1192
- * Deletes the given key from within this IDirectory.
1193
- * @param key - The key to delete
1194
- * @returns True if the key existed and was deleted, false if it did not exist
1195
- */
1196
- public delete(key: string): boolean {
1197
- this.throwIfDisposed();
1198
- // Delete the key locally first.
1199
- const previousValue = this.deleteCore(key, true);
1200
-
1201
- // If we are not attached, don't submit the op.
1202
- if (!this.directory.isAttached()) {
1203
- return previousValue !== undefined;
1204
- }
1205
-
1206
- const op: IDirectoryDeleteOperation = {
1207
- key,
1208
- path: this.absolutePath,
1209
- type: "delete",
1210
- };
1211
-
1212
- this.submitKeyMessage(op, previousValue);
1213
- return previousValue !== undefined;
1214
- }
1215
-
1216
- /**
1217
- * Deletes all keys from within this IDirectory.
1218
- */
1219
- public clear(): void {
1220
- this.throwIfDisposed();
1221
-
1222
- // If we are not attached, don't submit the op.
1223
- if (!this.directory.isAttached()) {
1224
- this.clearCore(true);
1225
- return;
1226
- }
1227
-
1228
- const copy = new Map<string, ILocalValue>(this._storage);
1229
- this.clearCore(true);
1230
- const op: IDirectoryClearOperation = {
1231
- path: this.absolutePath,
1232
- type: "clear",
1233
- };
1234
- this.submitClearMessage(op, copy);
1235
- }
1236
-
1237
- /**
1238
- * Issue a callback on each entry under this IDirectory.
1239
- * @param callback - Callback to issue
1240
- */
1241
- public forEach(callback: (value: any, key: string, map: Map<string, any>) => void): void {
1242
- this.throwIfDisposed();
1243
- this._storage.forEach((localValue, key, map) => {
1244
- callback(localValue.value, key, map);
1245
- });
1246
- }
1247
-
1248
- /**
1249
- * The number of entries under this IDirectory.
1250
- */
1251
- public get size(): number {
1252
- this.throwIfDisposed();
1253
- return this._storage.size;
1254
- }
1255
-
1256
- /**
1257
- * Get an iterator over the entries under this IDirectory.
1258
- * @returns The iterator
1259
- */
1260
- public entries(): IterableIterator<[string, any]> {
1261
- this.throwIfDisposed();
1262
- const localEntriesIterator = this._storage.entries();
1263
- const iterator = {
1264
- next(): IteratorResult<[string, any]> {
1265
- const nextVal = localEntriesIterator.next();
1266
- return nextVal.done
1267
- ? { value: undefined, done: true }
1268
- : { value: [nextVal.value[0], nextVal.value[1].value], done: false };
1269
- },
1270
- [Symbol.iterator]() {
1271
- return this;
1272
- },
1273
- };
1274
- return iterator;
1275
- }
1276
-
1277
- /**
1278
- * Get an iterator over the keys under this IDirectory.
1279
- * @returns The iterator
1280
- */
1281
- public keys(): IterableIterator<string> {
1282
- this.throwIfDisposed();
1283
- return this._storage.keys();
1284
- }
1285
-
1286
- /**
1287
- * Get an iterator over the values under this IDirectory.
1288
- * @returns The iterator
1289
- */
1290
- public values(): IterableIterator<any> {
1291
- this.throwIfDisposed();
1292
- const localValuesIterator = this._storage.values();
1293
- const iterator = {
1294
- next(): IteratorResult<any> {
1295
- const nextVal = localValuesIterator.next();
1296
- return nextVal.done
1297
- ? { value: undefined, done: true }
1298
- : { value: nextVal.value.value, done: false };
1299
- },
1300
- [Symbol.iterator]() {
1301
- return this;
1302
- },
1303
- };
1304
- return iterator;
1305
- }
1306
-
1307
- /**
1308
- * Get an iterator over the entries under this IDirectory.
1309
- * @returns The iterator
1310
- */
1311
- public [Symbol.iterator](): IterableIterator<[string, any]> {
1312
- this.throwIfDisposed();
1313
- return this.entries();
1314
- }
1315
-
1316
- /**
1317
- * Process a clear operation.
1318
- * @param op - The op to process
1319
- * @param local - Whether the message originated from the local client
1320
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1321
- * For messages from a remote client, this will be undefined.
1322
- * @internal
1323
- */
1324
- public processClearMessage(
1325
- op: IDirectoryClearOperation,
1326
- local: boolean,
1327
- localOpMetadata: unknown,
1328
- ): void {
1329
- this.throwIfDisposed();
1330
- if (local) {
1331
- assert(isClearLocalOpMetadata(localOpMetadata),
1332
- 0x00f /* pendingMessageId is missing from the local client's operation */);
1333
- const pendingClearMessageId = this.pendingClearMessageIds.shift();
1334
- assert(pendingClearMessageId === localOpMetadata.pendingMessageId,
1335
- 0x32a /* pendingMessageId does not match */);
1336
- return;
1337
- }
1338
- this.clearExceptPendingKeys(false);
1339
- }
1340
-
1341
- /**
1342
- * Apply clear operation locally and generate metadata
1343
- * @param op - Op to apply
1344
- * @returns metadata generated for stahed op
1345
- */
1346
- public applyStashedClearMessage(op: IDirectoryClearOperation): IClearLocalOpMetadata {
1347
- this.throwIfDisposed();
1348
- const previousValue = new Map<string, ILocalValue>(this._storage);
1349
- this.clearExceptPendingKeys(true);
1350
- const pendingMsgId = ++this.pendingMessageId;
1351
- this.pendingClearMessageIds.push(pendingMsgId);
1352
- const metadata: IClearLocalOpMetadata = {
1353
- type: "clear",
1354
- pendingMessageId: pendingMsgId,
1355
- previousStorage: previousValue,
1356
- };
1357
- return metadata;
1358
- }
1359
-
1360
- /**
1361
- * Process a delete operation.
1362
- * @param op - The op to process
1363
- * @param local - Whether the message originated from the local client
1364
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1365
- * For messages from a remote client, this will be undefined.
1366
- * @internal
1367
- */
1368
- public processDeleteMessage(
1369
- op: IDirectoryDeleteOperation,
1370
- local: boolean,
1371
- localOpMetadata: unknown,
1372
- ): void {
1373
- this.throwIfDisposed();
1374
- if (!this.needProcessStorageOperation(op, local, localOpMetadata)) {
1375
- return;
1376
- }
1377
- this.deleteCore(op.key, local);
1378
- }
1379
-
1380
- /**
1381
- * Apply delete operation locally and generate metadata
1382
- * @param op - Op to apply
1383
- * @returns metadata generated for stahed op
1384
- */
1385
- public applyStashedDeleteMessage(op: IDirectoryDeleteOperation): IKeyEditLocalOpMetadata {
1386
- this.throwIfDisposed();
1387
- const previousValue = this.deleteCore(op.key, true);
1388
- const pendingMessageId = this.getKeyMessageId(op);
1389
- const localMetadata: IKeyEditLocalOpMetadata = { type: "edit", pendingMessageId, previousValue };
1390
- return localMetadata;
1391
- }
1392
-
1393
- /**
1394
- * Process a set operation.
1395
- * @param op - The op to process
1396
- * @param local - Whether the message originated from the local client
1397
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1398
- * For messages from a remote client, this will be undefined.
1399
- * @internal
1400
- */
1401
- public processSetMessage(
1402
- op: IDirectorySetOperation,
1403
- context: ILocalValue | undefined,
1404
- local: boolean,
1405
- localOpMetadata: unknown,
1406
- ): void {
1407
- this.throwIfDisposed();
1408
- if (!this.needProcessStorageOperation(op, local, localOpMetadata)) {
1409
- return;
1410
- }
1411
-
1412
- // needProcessStorageOperation should have returned false if local is true
1413
- // so we can assume context is not undefined
1414
-
1415
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1416
- this.setCore(op.key, context!, local);
1417
- }
1418
-
1419
- /**
1420
- * Apply set operation locally and generate metadata
1421
- * @param op - Op to apply
1422
- * @returns metadata generated for stahed op
1423
- */
1424
- public applyStashedSetMessage(op: IDirectorySetOperation, context: ILocalValue): IKeyEditLocalOpMetadata {
1425
- this.throwIfDisposed();
1426
- // Set the value locally.
1427
- const previousValue = this.setCore(
1428
- op.key,
1429
- context,
1430
- true,
1431
- );
1432
-
1433
- // Create metadata
1434
- const pendingMessageId = this.getKeyMessageId(op);
1435
- const localMetadata: IKeyEditLocalOpMetadata = { type: "edit", pendingMessageId, previousValue };
1436
- return localMetadata;
1437
- }
1438
- /**
1439
- * Process a create subdirectory operation.
1440
- * @param op - The op to process
1441
- * @param local - Whether the message originated from the local client
1442
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1443
- * For messages from a remote client, this will be undefined.
1444
- * @internal
1445
- */
1446
- public processCreateSubDirectoryMessage(
1447
- op: IDirectoryCreateSubDirectoryOperation,
1448
- local: boolean,
1449
- localOpMetadata: unknown,
1450
- ): void {
1451
- this.throwIfDisposed();
1452
- if (!this.needProcessSubDirectoryOperation(op, local, localOpMetadata)) {
1453
- return;
1454
- }
1455
- this.createSubDirectoryCore(op.subdirName, local);
1456
- }
1457
-
1458
- /**
1459
- * Apply createSubDirectory operation locally and generate metadata
1460
- * @param op - Op to apply
1461
- * @returns metadata generated for stahed op
1462
- */
1463
- public applyStashedCreateSubDirMessage(op: IDirectoryCreateSubDirectoryOperation):
1464
- ICreateSubDirLocalOpMetadata {
1465
- this.throwIfDisposed();
1466
- // Create the sub directory locally first.
1467
- const isNew = this.createSubDirectoryCore(op.subdirName, true);
1468
- const newMessageId = this.getSubDirMessageId(op);
1469
-
1470
- const localOpMetadata: ICreateSubDirLocalOpMetadata = {
1471
- type: "createSubDir",
1472
- pendingMessageId: newMessageId,
1473
- previouslyExisted: !isNew,
1474
- };
1475
- return localOpMetadata;
1476
- }
1477
-
1478
- /**
1479
- * Process a delete subdirectory operation.
1480
- * @param op - The op to process
1481
- * @param local - Whether the message originated from the local client
1482
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1483
- * For messages from a remote client, this will be undefined.
1484
- * @internal
1485
- */
1486
- public processDeleteSubDirectoryMessage(
1487
- op: IDirectoryDeleteSubDirectoryOperation,
1488
- local: boolean,
1489
- localOpMetadata: unknown,
1490
- ): void {
1491
- this.throwIfDisposed();
1492
- if (!this.needProcessSubDirectoryOperation(op, local, localOpMetadata)) {
1493
- return;
1494
- }
1495
- this.deleteSubDirectoryCore(op.subdirName, local);
1496
- }
1497
-
1498
- /**
1499
- * Apply deleteSubDirectory operation locally and generate metadata
1500
- * @param op - Op to apply
1501
- * @returns metadata generated for stahed op
1502
- */
1503
- public applyStashedDeleteSubDirMessage(op: IDirectoryDeleteSubDirectoryOperation): IDeleteSubDirLocalOpMetadata {
1504
- this.throwIfDisposed();
1505
- const subDir = this.deleteSubDirectoryCore(op.subdirName, true);
1506
- const newMessageId = this.getSubDirMessageId(op);
1507
- const metadata: IDeleteSubDirLocalOpMetadata = {
1508
- type: "deleteSubDir",
1509
- pendingMessageId: newMessageId,
1510
- subDirectory: subDir,
1511
- };
1512
- return metadata;
1513
- }
1514
-
1515
- /**
1516
- * Submit a clear operation.
1517
- * @param op - The operation
1518
- */
1519
- private submitClearMessage(op: IDirectoryClearOperation,
1520
- previousValue: Map<string, ILocalValue>): void {
1521
- this.throwIfDisposed();
1522
- const pendingMsgId = ++this.pendingMessageId;
1523
- this.pendingClearMessageIds.push(pendingMsgId);
1524
- const metadata: IClearLocalOpMetadata = {
1525
- type: "clear",
1526
- pendingMessageId: pendingMsgId,
1527
- previousStorage: previousValue,
1528
- };
1529
- this.directory.submitDirectoryMessage(op, metadata);
1530
- }
1531
-
1532
- /**
1533
- * Resubmit a clear operation.
1534
- * @param op - The operation
1535
- * @internal
1536
- */
1537
- public resubmitClearMessage(op: IDirectoryClearOperation, localOpMetadata: unknown): void {
1538
- assert(isClearLocalOpMetadata(localOpMetadata), 0x32b /* Invalid localOpMetadata for clear */);
1539
- // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1540
- const pendingClearMessageId = this.pendingClearMessageIds.shift();
1541
- assert(pendingClearMessageId === localOpMetadata.pendingMessageId,
1542
- 0x32c /* pendingMessageId does not match */);
1543
- this.submitClearMessage(op, localOpMetadata.previousStorage);
1544
- }
1545
-
1546
- /**
1547
- * Get a new pending message id for the op and cache it to track the pending op
1548
- */
1549
- private getKeyMessageId(op: IDirectoryKeyOperation): number {
1550
- // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1551
- const pendingMessageId = ++this.pendingMessageId;
1552
- const pendingMessageIds = this.pendingKeys.get(op.key);
1553
- if (pendingMessageIds !== undefined) {
1554
- pendingMessageIds.push(pendingMessageId);
1555
- } else {
1556
- this.pendingKeys.set(op.key, [pendingMessageId]);
1557
- }
1558
- return pendingMessageId;
1559
- }
1560
-
1561
- /**
1562
- * Submit a key operation.
1563
- * @param op - The operation
1564
- * @param previousValue - The value of the key before this op
1565
- */
1566
- private submitKeyMessage(op: IDirectoryKeyOperation, previousValue?: ILocalValue): void {
1567
- this.throwIfDisposed();
1568
- const pendingMessageId = this.getKeyMessageId(op);
1569
- const localMetadata = { type: "edit", pendingMessageId, previousValue };
1570
- this.directory.submitDirectoryMessage(op, localMetadata);
1571
- }
1572
-
1573
- /**
1574
- * Submit a key message to remote clients based on a previous submit.
1575
- * @param op - The map key message
1576
- * @param localOpMetadata - Metadata from the previous submit
1577
- * @internal
1578
- */
1579
- public resubmitKeyMessage(op: IDirectoryKeyOperation, localOpMetadata: unknown): void {
1580
- assert(isKeyEditLocalOpMetadata(localOpMetadata), 0x32d /* Invalid localOpMetadata in submit */);
1581
-
1582
- // clear the old pending message id
1583
- const pendingMessageIds = this.pendingKeys.get(op.key);
1584
- assert(pendingMessageIds !== undefined && pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1585
- 0x32e /* Unexpected pending message received */);
1586
- pendingMessageIds.shift();
1587
- if (pendingMessageIds.length === 0) {
1588
- this.pendingKeys.delete(op.key);
1589
- }
1590
-
1591
- this.submitKeyMessage(op, localOpMetadata.previousValue);
1592
- }
1593
-
1594
- /**
1595
- * Get a new pending message id for the op and cache it to track the pending op
1596
- */
1597
- private getSubDirMessageId(op: IDirectorySubDirectoryOperation): number {
1598
- // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1599
- const newMessageId = ++this.pendingMessageId;
1600
- const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1601
- if (pendingMessageIds !== undefined) {
1602
- pendingMessageIds.push(newMessageId);
1603
- } else {
1604
- this.pendingSubDirectories.set(op.subdirName, [newMessageId]);
1605
- }
1606
- return newMessageId;
1607
- }
1608
-
1609
- /**
1610
- * Submit a create subdirectory operation.
1611
- * @param op - The operation
1612
- * @param prevExisted - Whether the subdirectory existed before the op
1613
- */
1614
- private submitCreateSubDirectoryMessage(op: IDirectorySubDirectoryOperation,
1615
- prevExisted: boolean): void {
1616
- this.throwIfDisposed();
1617
- const newMessageId = this.getSubDirMessageId(op);
1618
-
1619
- const localOpMetadata: ICreateSubDirLocalOpMetadata = {
1620
- type: "createSubDir",
1621
- pendingMessageId: newMessageId,
1622
- previouslyExisted: prevExisted,
1623
- };
1624
- this.directory.submitDirectoryMessage(op, localOpMetadata);
1625
- }
1626
-
1627
- /**
1628
- * Submit a delete subdirectory operation.
1629
- * @param op - The operation
1630
- * @param subDir - Any subdirectory deleted by the op
1631
- */
1632
- private submitDeleteSubDirectoryMessage(op: IDirectorySubDirectoryOperation,
1633
- subDir: SubDirectory | undefined): void {
1634
- this.throwIfDisposed();
1635
- const newMessageId = this.getSubDirMessageId(op);
1636
-
1637
- const localOpMetadata: IDeleteSubDirLocalOpMetadata = {
1638
- type: "deleteSubDir",
1639
- pendingMessageId: newMessageId,
1640
- subDirectory: subDir,
1641
- };
1642
- this.directory.submitDirectoryMessage(op, localOpMetadata);
1643
- }
1644
-
1645
- /**
1646
- * Submit a subdirectory operation again
1647
- * @param op - The operation
1648
- * @param localOpMetadata - metadata submitted with the op originally
1649
- * @internal
1650
- */
1651
- public resubmitSubDirectoryMessage(op: IDirectorySubDirectoryOperation, localOpMetadata: unknown): void {
1652
- assert(isSubDirLocalOpMetadata(localOpMetadata), 0x32f /* Invalid localOpMetadata for sub directory op */);
1653
-
1654
- // clear the old pending message id
1655
- const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1656
- assert(pendingMessageIds !== undefined && pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1657
- 0x330 /* Unexpected pending message received */);
1658
- pendingMessageIds.shift();
1659
- if (pendingMessageIds.length === 0) {
1660
- this.pendingSubDirectories.delete(op.subdirName);
1661
- }
1662
-
1663
- if (localOpMetadata.type === "createSubDir") {
1664
- this.submitCreateSubDirectoryMessage(op, localOpMetadata.previouslyExisted);
1665
- } else {
1666
- this.submitDeleteSubDirectoryMessage(op, localOpMetadata.subDirectory);
1667
- }
1668
- }
1669
-
1670
- /**
1671
- * Get the storage of this subdirectory in a serializable format, to be used in snapshotting.
1672
- * @param serializer - The serializer to use to serialize handles in its values.
1673
- * @returns The JSONable string representing the storage of this subdirectory
1674
- * @internal
1675
- */
1676
- public *getSerializedStorage(serializer: IFluidSerializer) {
1677
- this.throwIfDisposed();
1678
- for (const [key, localValue] of this._storage) {
1679
- const value = localValue.makeSerialized(serializer, this.directory.handle);
1680
- const res: [string, ISerializedValue] = [key, value];
1681
- yield res;
1682
- }
1683
- }
1684
-
1685
- /**
1686
- * Populate a key value in this subdirectory's storage, to be used when loading from snapshot.
1687
- * @param key - The key to populate
1688
- * @param localValue - The local value to populate into it
1689
- * @internal
1690
- */
1691
- public populateStorage(key: string, localValue: ILocalValue): void {
1692
- this.throwIfDisposed();
1693
- this._storage.set(key, localValue);
1694
- }
1695
-
1696
- /**
1697
- * Populate a subdirectory into this subdirectory, to be used when loading from snapshot.
1698
- * @param subdirName - The name of the subdirectory to add
1699
- * @param newSubDir - The new subdirectory to add
1700
- * @internal
1701
- */
1702
- public populateSubDirectory(subdirName: string, newSubDir: SubDirectory): void {
1703
- this.throwIfDisposed();
1704
- this._subdirectories.set(subdirName, newSubDir);
1705
- }
1706
-
1707
- /**
1708
- * Retrieve the local value at the given key. This is used to get value type information stashed on the local
1709
- * value so op handlers can be retrieved
1710
- * @param key - The key to retrieve from
1711
- * @returns The local value
1712
- * @internal
1713
- */
1714
- public getLocalValue<T extends ILocalValue = ILocalValue>(key: string): T {
1715
- this.throwIfDisposed();
1716
- return this._storage.get(key) as T;
1717
- }
1718
-
1719
- /**
1720
- * Remove the pendingMessageId from the map tracking it on rollback
1721
- * @param map - map tracking the pending messages
1722
- * @param key - key of the edit in the op
1723
- */
1724
- private rollbackPendingMessageId(map: Map<string, number[]>, key: string, pendingMessageId) {
1725
- const pendingMessageIds = map.get(key);
1726
- const lastPendingMessageId = pendingMessageIds?.pop();
1727
- if (!pendingMessageIds || lastPendingMessageId !== pendingMessageId) {
1728
- throw new Error("Rollback op does not match last pending");
1729
- }
1730
- if (pendingMessageIds.length === 0) {
1731
- map.delete(key);
1732
- }
1733
- }
1734
-
1735
- /**
1736
- * Rollback a local op
1737
- * @param op - The operation to rollback
1738
- * @param localOpMetadata - The local metadata associated with the op.
1739
- */
1740
- public rollback(op: any, localOpMetadata: unknown) {
1741
- if (!isDirectoryLocalOpMetadata(localOpMetadata)) {
1742
- throw new Error("Invalid localOpMetadata");
1743
- }
1744
-
1745
- if (op.type === "clear" && localOpMetadata.type === "clear") {
1746
- localOpMetadata.previousStorage.forEach((localValue, key) => {
1747
- this.setCore(key, localValue, true);
1748
- });
1749
-
1750
- const lastPendingClearId = this.pendingClearMessageIds.pop();
1751
- if (lastPendingClearId === undefined || lastPendingClearId !== localOpMetadata.pendingMessageId) {
1752
- throw new Error("Rollback op does match last clear");
1753
- }
1754
- } else if ((op.type === "delete" || op.type === "set") && localOpMetadata.type === "edit") {
1755
- if (localOpMetadata.previousValue === undefined) {
1756
- this.deleteCore(op.key, true);
1757
- } else {
1758
- this.setCore(op.key, localOpMetadata.previousValue, true);
1759
- }
1760
-
1761
- this.rollbackPendingMessageId(this.pendingKeys, op.key, localOpMetadata.pendingMessageId);
1762
- } else if (op.type === "createSubDirectory" && localOpMetadata.type === "createSubDir") {
1763
- if (!localOpMetadata.previouslyExisted) {
1764
- this.deleteSubDirectoryCore(op.subdirName, true);
1765
- }
1766
-
1767
- this.rollbackPendingMessageId(this.pendingSubDirectories, op.subdirName, localOpMetadata.pendingMessageId);
1768
- } else if (op.type === "deleteSubDirectory" && localOpMetadata.type === "deleteSubDir") {
1769
- if (localOpMetadata.subDirectory !== undefined) {
1770
- this.undeleteSubDirectoryTree(localOpMetadata.subDirectory);
1771
- // don't need to register events because deleting never unregistered
1772
- this._subdirectories.set(op.subdirName, localOpMetadata.subDirectory);
1773
- this.emit("subDirectoryCreated", op.subdirName, true, this);
1774
- }
1775
-
1776
- this.rollbackPendingMessageId(this.pendingSubDirectories, op.subdirName, localOpMetadata.pendingMessageId);
1777
- } else {
1778
- throw new Error("Unsupported op for rollback");
1779
- }
1780
- }
1781
-
1782
- /**
1783
- * Converts the given relative path into an absolute path.
1784
- * @param path - Relative path to convert
1785
- * @returns The equivalent absolute path
1786
- */
1787
- private makeAbsolute(relativePath: string): string {
1788
- return posix.resolve(this.absolutePath, relativePath);
1789
- }
1790
-
1791
- /**
1792
- * If our local operations that have not yet been ack'd will eventually overwrite an incoming operation, we should
1793
- * not process the incoming operation.
1794
- * @param op - Operation to check
1795
- * @param local - Whether the operation originated from the local client
1796
- * @param localOpMetadata - For local client ops, this is the metadata that was submitted with the op.
1797
- * For ops from a remote client, this will be undefined.
1798
- * @returns True if the operation should be processed, false otherwise
1799
- */
1800
- private needProcessStorageOperation(
1801
- op: IDirectoryKeyOperation,
1802
- local: boolean,
1803
- localOpMetadata: unknown,
1804
- ): boolean {
1805
- if (this.pendingClearMessageIds.length > 0) {
1806
- if (local) {
1807
- assert(localOpMetadata !== undefined && isKeyEditLocalOpMetadata(localOpMetadata) &&
1808
- localOpMetadata.pendingMessageId < this.pendingClearMessageIds[0],
1809
- 0x010 /* "Received out of order storage op when there is an unackd clear message" */);
1810
- }
1811
- // If I have a NACK clear, we can ignore all ops.
1812
- return false;
1813
- }
1814
-
1815
- const pendingKeyMessageId = this.pendingKeys.get(op.key);
1816
- if (pendingKeyMessageId !== undefined) {
1817
- // Found an NACK op, clear it from the directory if the latest sequence number in the directory
1818
- // match the message's and don't process the op.
1819
- if (local) {
1820
- assert(localOpMetadata !== undefined && isKeyEditLocalOpMetadata(localOpMetadata),
1821
- 0x011 /* pendingMessageId is missing from the local client's operation */);
1822
- const pendingMessageIds = this.pendingKeys.get(op.key);
1823
- assert(pendingMessageIds !== undefined && pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1824
- 0x331 /* Unexpected pending message received */);
1825
- pendingMessageIds.shift();
1826
- if (pendingMessageIds.length === 0) {
1827
- this.pendingKeys.delete(op.key);
1828
- }
1829
- }
1830
- return false;
1831
- }
1832
-
1833
- // If we don't have a NACK op on the key, we need to process the remote ops.
1834
- return !local;
1835
- }
1836
-
1837
- /**
1838
- * If our local operations that have not yet been ack'd will eventually overwrite an incoming operation, we should
1839
- * not process the incoming operation.
1840
- * @param op - Operation to check
1841
- * @param local - Whether the message originated from the local client
1842
- * @param message - The message
1843
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1844
- * For messages from a remote client, this will be undefined.
1845
- * @returns True if the operation should be processed, false otherwise
1846
- */
1847
- private needProcessSubDirectoryOperation(
1848
- op: IDirectorySubDirectoryOperation,
1849
- local: boolean,
1850
- localOpMetadata: unknown,
1851
- ): boolean {
1852
- const pendingSubDirectoryMessageId = this.pendingSubDirectories.get(op.subdirName);
1853
- if (pendingSubDirectoryMessageId !== undefined) {
1854
- if (local) {
1855
- assert(isSubDirLocalOpMetadata(localOpMetadata),
1856
- 0x012 /* pendingMessageId is missing from the local client's operation */);
1857
- const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1858
- assert(pendingMessageIds !== undefined && pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1859
- 0x332 /* Unexpected pending message received */);
1860
- pendingMessageIds.shift();
1861
- if (pendingMessageIds.length === 0) {
1862
- this.pendingSubDirectories.delete(op.subdirName);
1863
- }
1864
- }
1865
- return false;
1866
- }
1867
-
1868
- return !local;
1869
- }
1870
-
1871
- /**
1872
- * Clear all keys in memory in response to a remote clear, but retain keys we have modified but not yet been ack'd.
1873
- */
1874
- private clearExceptPendingKeys(local: boolean) {
1875
- // Assuming the pendingKeys is small and the map is large
1876
- // we will get the value for the pendingKeys and clear the map
1877
- const temp = new Map<string, ILocalValue>();
1878
- this.pendingKeys.forEach((value, key, map) => {
1879
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1880
- temp.set(key, this._storage.get(key)!);
1881
- });
1882
- this.clearCore(local);
1883
- temp.forEach((value, key, map) => {
1884
- this.setCore(key, value, true);
1885
- });
1886
- }
1887
-
1888
- /**
1889
- * Clear implementation used for both locally sourced clears as well as incoming remote clears.
1890
- * @param local - Whether the message originated from the local client
1891
- */
1892
- private clearCore(local: boolean) {
1893
- this._storage.clear();
1894
- this.directory.emit("clear", local, this.directory);
1895
- }
1896
-
1897
- /**
1898
- * Delete implementation used for both locally sourced deletes as well as incoming remote deletes.
1899
- * @param key - The key being deleted
1900
- * @param local - Whether the message originated from the local client
1901
- * @returns Previous local value of the key if it existed, undefined if it did not exist
1902
- */
1903
- private deleteCore(key: string, local: boolean): ILocalValue | undefined {
1904
- const previousLocalValue = this._storage.get(key);
1905
- const previousValue = previousLocalValue?.value;
1906
- const successfullyRemoved = this._storage.delete(key);
1907
- if (successfullyRemoved) {
1908
- const event: IDirectoryValueChanged = { key, path: this.absolutePath, previousValue };
1909
- this.directory.emit("valueChanged", event, local, this.directory);
1910
- const containedEvent: IValueChanged = { key, previousValue };
1911
- this.emit("containedValueChanged", containedEvent, local, this);
1912
- }
1913
- return previousLocalValue;
1914
- }
1915
-
1916
- /**
1917
- * Set implementation used for both locally sourced sets as well as incoming remote sets.
1918
- * @param key - The key being set
1919
- * @param value - The value being set
1920
- * @param local - Whether the message originated from the local client
1921
- * @returns Previous local value of the key, if any
1922
- */
1923
- private setCore(key: string, value: ILocalValue, local: boolean): ILocalValue | undefined {
1924
- const previousLocalValue = this._storage.get(key);
1925
- const previousValue = previousLocalValue?.value;
1926
- this._storage.set(key, value);
1927
- const event: IDirectoryValueChanged = { key, path: this.absolutePath, previousValue };
1928
- this.directory.emit("valueChanged", event, local, this.directory);
1929
- const containedEvent: IValueChanged = { key, previousValue };
1930
- this.emit("containedValueChanged", containedEvent, local, this);
1931
- return previousLocalValue;
1932
- }
1933
-
1934
- /**
1935
- * Create subdirectory implementation used for both locally sourced creation as well as incoming remote creation.
1936
- * @param subdirName - The name of the subdirectory being created
1937
- * @param local - Whether the message originated from the local client
1938
- * @returns - True if is newly created, false if it already existed.
1939
- */
1940
- private createSubDirectoryCore(subdirName: string, local: boolean): boolean {
1941
- if (!this._subdirectories.has(subdirName)) {
1942
- const absolutePath = posix.join(this.absolutePath, subdirName);
1943
- const subDir = new SubDirectory(this.directory, this.runtime, this.serializer, absolutePath);
1944
- this.registerEventsOnSubDirectory(subDir, subdirName);
1945
- this._subdirectories.set(subdirName, subDir);
1946
- this.emit("subDirectoryCreated", subdirName, local, this);
1947
- return true;
1948
- }
1949
- return false;
1950
- }
1951
-
1952
- private registerEventsOnSubDirectory(subDirectory: SubDirectory, subDirName: string) {
1953
- subDirectory.on("subDirectoryCreated", (relativePath: string, local: boolean) => {
1954
- this.emit("subDirectoryCreated", posix.join(subDirName, relativePath), local, this);
1955
- });
1956
- subDirectory.on("subDirectoryDeleted", (relativePath: string, local: boolean) => {
1957
- this.emit("subDirectoryDeleted", posix.join(subDirName, relativePath), local, this);
1958
- });
1959
- }
1960
-
1961
- /**
1962
- * Delete subdirectory implementation used for both locally sourced creation as well as incoming remote creation.
1963
- * @param subdirName - The name of the subdirectory being deleted
1964
- * @param local - Whether the message originated from the local client
1965
- */
1966
- private deleteSubDirectoryCore(subdirName: string, local: boolean) {
1967
- const previousValue = this._subdirectories.get(subdirName);
1968
- // This should make the subdirectory structure unreachable so it can be GC'd and won't appear in snapshots
1969
- // Might want to consider cleaning out the structure more exhaustively though? But not when rollback.
1970
- if (previousValue !== undefined) {
1971
- this._subdirectories.delete(subdirName);
1972
- this.disposeSubDirectoryTree(previousValue);
1973
- this.emit("subDirectoryDeleted", subdirName, local, this);
1974
- }
1975
- return previousValue;
1976
- }
1977
-
1978
- private disposeSubDirectoryTree(directory: IDirectory | undefined) {
1979
- if (!directory) {
1980
- return;
1981
- }
1982
- // Dispose the subdirectory tree. This will dispose the subdirectories from bottom to top.
1983
- const subDirectories = directory.subdirectories();
1984
- for (const [_, subDirectory] of subDirectories) {
1985
- this.disposeSubDirectoryTree(subDirectory);
1986
- }
1987
- if (typeof directory.dispose === "function") {
1988
- directory.dispose();
1989
- }
1990
- }
1991
-
1992
- private undeleteSubDirectoryTree(directory: SubDirectory) {
1993
- // Restore deleted subdirectory tree. This will unmark "deleted" from the subdirectories from bottom to top.
1994
- for (const [_, subDirectory] of this._subdirectories.entries()) {
1995
- this.undeleteSubDirectoryTree(subDirectory);
1996
- }
1997
- directory.undispose();
1998
- }
980
+ /**
981
+ * Tells if the sub directory is deleted or not.
982
+ */
983
+ private _deleted = false;
984
+
985
+ /**
986
+ * String representation for the class.
987
+ */
988
+ public [Symbol.toStringTag]: string = "SubDirectory";
989
+
990
+ /**
991
+ * The in-memory data the directory is storing.
992
+ */
993
+ private readonly _storage: Map<string, ILocalValue> = new Map();
994
+
995
+ /**
996
+ * The subdirectories the directory is holding.
997
+ */
998
+ private readonly _subdirectories: Map<string, SubDirectory> = new Map();
999
+
1000
+ /**
1001
+ * Keys that have been modified locally but not yet ack'd from the server.
1002
+ */
1003
+ private readonly pendingKeys: Map<string, number[]> = new Map();
1004
+
1005
+ /**
1006
+ * Subdirectories that have been modified locally but not yet ack'd from the server.
1007
+ */
1008
+ private readonly pendingSubDirectories: Map<string, number[]> = new Map();
1009
+
1010
+ /**
1011
+ * This is used to assign a unique id to every outgoing operation and helps in tracking unack'd ops.
1012
+ */
1013
+ private pendingMessageId: number = -1;
1014
+
1015
+ /**
1016
+ * The pending ids of any clears that have been performed locally but not yet ack'd from the server
1017
+ */
1018
+ private readonly pendingClearMessageIds: number[] = [];
1019
+
1020
+ /**
1021
+ * Constructor.
1022
+ * @param directory - Reference back to the SharedDirectory to perform operations
1023
+ * @param runtime - The data store runtime this directory is associated with
1024
+ * @param serializer - The serializer to serialize / parse handles
1025
+ * @param absolutePath - The absolute path of this IDirectory
1026
+ */
1027
+ public constructor(
1028
+ private readonly directory: SharedDirectory,
1029
+ private readonly runtime: IFluidDataStoreRuntime,
1030
+ private readonly serializer: IFluidSerializer,
1031
+ public readonly absolutePath: string,
1032
+ ) {
1033
+ super();
1034
+ }
1035
+
1036
+ public dispose(error?: Error): void {
1037
+ this._deleted = true;
1038
+ this.emit("disposed", this);
1039
+ }
1040
+
1041
+ /**
1042
+ * Unmark the deleted property only when rolling back delete.
1043
+ */
1044
+ private undispose(): void {
1045
+ this._deleted = false;
1046
+ this.emit("undisposed", this);
1047
+ }
1048
+
1049
+ public get disposed(): boolean {
1050
+ return this._deleted;
1051
+ }
1052
+
1053
+ private throwIfDisposed(): void {
1054
+ if (this._deleted) {
1055
+ throw new UsageError("Cannot access Disposed subDirectory");
1056
+ }
1057
+ }
1058
+
1059
+ /**
1060
+ * Checks whether the given key exists in this IDirectory.
1061
+ * @param key - The key to check
1062
+ * @returns True if the key exists, false otherwise
1063
+ */
1064
+ public has(key: string): boolean {
1065
+ this.throwIfDisposed();
1066
+ return this._storage.has(key);
1067
+ }
1068
+
1069
+ /**
1070
+ * {@inheritDoc IDirectory.get}
1071
+ */
1072
+ public get<T = unknown>(key: string): T | undefined {
1073
+ this.throwIfDisposed();
1074
+ return this._storage.get(key)?.value as T | undefined;
1075
+ }
1076
+
1077
+ /**
1078
+ * {@inheritDoc IDirectory.set}
1079
+ */
1080
+ public set<T = unknown>(key: string, value: T): this {
1081
+ this.throwIfDisposed();
1082
+ // Undefined/null keys can't be serialized to JSON in the manner we currently snapshot.
1083
+ if (key === undefined || key === null) {
1084
+ throw new Error("Undefined and null keys are not supported");
1085
+ }
1086
+
1087
+ // Create a local value and serialize it.
1088
+ const localValue = this.directory.localValueMaker.fromInMemory(value);
1089
+ const serializableValue = makeSerializable(
1090
+ localValue,
1091
+ this.serializer,
1092
+ this.directory.handle,
1093
+ );
1094
+
1095
+ // Set the value locally.
1096
+ const previousValue = this.setCore(key, localValue, true);
1097
+
1098
+ // If we are not attached, don't submit the op.
1099
+ if (!this.directory.isAttached()) {
1100
+ return this;
1101
+ }
1102
+
1103
+ const op: IDirectorySetOperation = {
1104
+ key,
1105
+ path: this.absolutePath,
1106
+ type: "set",
1107
+ value: serializableValue,
1108
+ };
1109
+ this.submitKeyMessage(op, previousValue);
1110
+ return this;
1111
+ }
1112
+
1113
+ /**
1114
+ * {@inheritDoc IDirectory.countSubDirectory}
1115
+ */
1116
+ public countSubDirectory(): number {
1117
+ return this._subdirectories.size;
1118
+ }
1119
+
1120
+ /**
1121
+ * {@inheritDoc IDirectory.createSubDirectory}
1122
+ */
1123
+ public createSubDirectory(subdirName: string): IDirectory {
1124
+ this.throwIfDisposed();
1125
+ // Undefined/null subdirectory names can't be serialized to JSON in the manner we currently snapshot.
1126
+ if (subdirName === undefined || subdirName === null) {
1127
+ throw new Error("SubDirectory name may not be undefined or null");
1128
+ }
1129
+
1130
+ if (subdirName.includes(posix.sep)) {
1131
+ throw new Error(`SubDirectory name may not contain ${posix.sep}`);
1132
+ }
1133
+
1134
+ // Create the sub directory locally first.
1135
+ const isNew = this.createSubDirectoryCore(subdirName, true);
1136
+
1137
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1138
+ const subDir: IDirectory = this._subdirectories.get(subdirName)!;
1139
+
1140
+ // If we are not attached, don't submit the op.
1141
+ if (!this.directory.isAttached()) {
1142
+ return subDir;
1143
+ }
1144
+
1145
+ const op: IDirectoryCreateSubDirectoryOperation = {
1146
+ path: this.absolutePath,
1147
+ subdirName,
1148
+ type: "createSubDirectory",
1149
+ };
1150
+ this.submitCreateSubDirectoryMessage(op, !isNew);
1151
+
1152
+ return subDir;
1153
+ }
1154
+
1155
+ /**
1156
+ * {@inheritDoc IDirectory.getSubDirectory}
1157
+ */
1158
+ public getSubDirectory(subdirName: string): IDirectory | undefined {
1159
+ this.throwIfDisposed();
1160
+ return this._subdirectories.get(subdirName);
1161
+ }
1162
+
1163
+ /**
1164
+ * {@inheritDoc IDirectory.hasSubDirectory}
1165
+ */
1166
+ public hasSubDirectory(subdirName: string): boolean {
1167
+ this.throwIfDisposed();
1168
+ return this._subdirectories.has(subdirName);
1169
+ }
1170
+
1171
+ /**
1172
+ * {@inheritDoc IDirectory.deleteSubDirectory}
1173
+ */
1174
+ public deleteSubDirectory(subdirName: string): boolean {
1175
+ this.throwIfDisposed();
1176
+ // Delete the sub directory locally first.
1177
+ const subDir = this.deleteSubDirectoryCore(subdirName, true);
1178
+
1179
+ // If we are not attached, don't submit the op.
1180
+ if (!this.directory.isAttached()) {
1181
+ return subDir !== undefined;
1182
+ }
1183
+
1184
+ const op: IDirectoryDeleteSubDirectoryOperation = {
1185
+ path: this.absolutePath,
1186
+ subdirName,
1187
+ type: "deleteSubDirectory",
1188
+ };
1189
+
1190
+ this.submitDeleteSubDirectoryMessage(op, subDir);
1191
+ return subDir !== undefined;
1192
+ }
1193
+
1194
+ /**
1195
+ * {@inheritDoc IDirectory.subdirectories}
1196
+ */
1197
+ public subdirectories(): IterableIterator<[string, IDirectory]> {
1198
+ this.throwIfDisposed();
1199
+ return this._subdirectories.entries();
1200
+ }
1201
+
1202
+ /**
1203
+ * {@inheritDoc IDirectory.getWorkingDirectory}
1204
+ */
1205
+ public getWorkingDirectory(relativePath: string): IDirectory | undefined {
1206
+ this.throwIfDisposed();
1207
+ return this.directory.getWorkingDirectory(this.makeAbsolute(relativePath));
1208
+ }
1209
+
1210
+ /**
1211
+ * Deletes the given key from within this IDirectory.
1212
+ * @param key - The key to delete
1213
+ * @returns True if the key existed and was deleted, false if it did not exist
1214
+ */
1215
+ public delete(key: string): boolean {
1216
+ this.throwIfDisposed();
1217
+ // Delete the key locally first.
1218
+ const previousValue = this.deleteCore(key, true);
1219
+
1220
+ // If we are not attached, don't submit the op.
1221
+ if (!this.directory.isAttached()) {
1222
+ return previousValue !== undefined;
1223
+ }
1224
+
1225
+ const op: IDirectoryDeleteOperation = {
1226
+ key,
1227
+ path: this.absolutePath,
1228
+ type: "delete",
1229
+ };
1230
+
1231
+ this.submitKeyMessage(op, previousValue);
1232
+ return previousValue !== undefined;
1233
+ }
1234
+
1235
+ /**
1236
+ * Deletes all keys from within this IDirectory.
1237
+ */
1238
+ public clear(): void {
1239
+ this.throwIfDisposed();
1240
+
1241
+ // If we are not attached, don't submit the op.
1242
+ if (!this.directory.isAttached()) {
1243
+ this.clearCore(true);
1244
+ return;
1245
+ }
1246
+
1247
+ const copy = new Map<string, ILocalValue>(this._storage);
1248
+ this.clearCore(true);
1249
+ const op: IDirectoryClearOperation = {
1250
+ path: this.absolutePath,
1251
+ type: "clear",
1252
+ };
1253
+ this.submitClearMessage(op, copy);
1254
+ }
1255
+
1256
+ /**
1257
+ * Issue a callback on each entry under this IDirectory.
1258
+ * @param callback - Callback to issue
1259
+ */
1260
+ public forEach(
1261
+ callback: (value: unknown, key: string, map: Map<string, unknown>) => void,
1262
+ ): void {
1263
+ this.throwIfDisposed();
1264
+ // eslint-disable-next-line unicorn/no-array-for-each
1265
+ this._storage.forEach((localValue, key, map) => {
1266
+ callback(localValue.value, key, map);
1267
+ });
1268
+ }
1269
+
1270
+ /**
1271
+ * The number of entries under this IDirectory.
1272
+ */
1273
+ public get size(): number {
1274
+ this.throwIfDisposed();
1275
+ return this._storage.size;
1276
+ }
1277
+
1278
+ /**
1279
+ * Get an iterator over the entries under this IDirectory.
1280
+ * @returns The iterator
1281
+ */
1282
+ public entries(): IterableIterator<[string, unknown]> {
1283
+ this.throwIfDisposed();
1284
+ const localEntriesIterator = this._storage.entries();
1285
+ const iterator = {
1286
+ next(): IteratorResult<[string, unknown]> {
1287
+ const nextVal = localEntriesIterator.next();
1288
+ return nextVal.done
1289
+ ? { value: undefined, done: true }
1290
+ : { value: [nextVal.value[0], nextVal.value[1].value], done: false };
1291
+ },
1292
+ [Symbol.iterator](): IterableIterator<[string, unknown]> {
1293
+ return this;
1294
+ },
1295
+ };
1296
+ return iterator;
1297
+ }
1298
+
1299
+ /**
1300
+ * Get an iterator over the keys under this IDirectory.
1301
+ * @returns The iterator
1302
+ */
1303
+ public keys(): IterableIterator<string> {
1304
+ this.throwIfDisposed();
1305
+ return this._storage.keys();
1306
+ }
1307
+
1308
+ /**
1309
+ * Get an iterator over the values under this IDirectory.
1310
+ * @returns The iterator
1311
+ */
1312
+ public values(): IterableIterator<unknown> {
1313
+ this.throwIfDisposed();
1314
+ const localValuesIterator = this._storage.values();
1315
+ const iterator = {
1316
+ next(): IteratorResult<unknown> {
1317
+ const nextVal = localValuesIterator.next();
1318
+ return nextVal.done
1319
+ ? { value: undefined, done: true }
1320
+ : { value: nextVal.value.value, done: false };
1321
+ },
1322
+ [Symbol.iterator](): IterableIterator<unknown> {
1323
+ return this;
1324
+ },
1325
+ };
1326
+ return iterator;
1327
+ }
1328
+
1329
+ /**
1330
+ * Get an iterator over the entries under this IDirectory.
1331
+ * @returns The iterator
1332
+ */
1333
+ public [Symbol.iterator](): IterableIterator<[string, unknown]> {
1334
+ this.throwIfDisposed();
1335
+ return this.entries();
1336
+ }
1337
+
1338
+ /**
1339
+ * Process a clear operation.
1340
+ * @param op - The op to process
1341
+ * @param local - Whether the message originated from the local client
1342
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1343
+ * For messages from a remote client, this will be undefined.
1344
+ * @internal
1345
+ */
1346
+ public processClearMessage(
1347
+ op: IDirectoryClearOperation,
1348
+ local: boolean,
1349
+ localOpMetadata: unknown,
1350
+ ): void {
1351
+ this.throwIfDisposed();
1352
+ if (local) {
1353
+ assert(
1354
+ isClearLocalOpMetadata(localOpMetadata),
1355
+ 0x00f /* pendingMessageId is missing from the local client's operation */,
1356
+ );
1357
+ const pendingClearMessageId = this.pendingClearMessageIds.shift();
1358
+ assert(
1359
+ pendingClearMessageId === localOpMetadata.pendingMessageId,
1360
+ 0x32a /* pendingMessageId does not match */,
1361
+ );
1362
+ return;
1363
+ }
1364
+ this.clearExceptPendingKeys(false);
1365
+ }
1366
+
1367
+ /**
1368
+ * Apply clear operation locally and generate metadata
1369
+ * @param op - Op to apply
1370
+ * @returns metadata generated for stahed op
1371
+ */
1372
+ public applyStashedClearMessage(op: IDirectoryClearOperation): IClearLocalOpMetadata {
1373
+ this.throwIfDisposed();
1374
+ const previousValue = new Map<string, ILocalValue>(this._storage);
1375
+ this.clearExceptPendingKeys(true);
1376
+ const pendingMsgId = ++this.pendingMessageId;
1377
+ this.pendingClearMessageIds.push(pendingMsgId);
1378
+ const metadata: IClearLocalOpMetadata = {
1379
+ type: "clear",
1380
+ pendingMessageId: pendingMsgId,
1381
+ previousStorage: previousValue,
1382
+ };
1383
+ return metadata;
1384
+ }
1385
+
1386
+ /**
1387
+ * Process a delete operation.
1388
+ * @param op - The op to process
1389
+ * @param local - Whether the message originated from the local client
1390
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1391
+ * For messages from a remote client, this will be undefined.
1392
+ * @internal
1393
+ */
1394
+ public processDeleteMessage(
1395
+ op: IDirectoryDeleteOperation,
1396
+ local: boolean,
1397
+ localOpMetadata: unknown,
1398
+ ): void {
1399
+ this.throwIfDisposed();
1400
+ if (!this.needProcessStorageOperation(op, local, localOpMetadata)) {
1401
+ return;
1402
+ }
1403
+ this.deleteCore(op.key, local);
1404
+ }
1405
+
1406
+ /**
1407
+ * Apply delete operation locally and generate metadata
1408
+ * @param op - Op to apply
1409
+ * @returns metadata generated for stahed op
1410
+ */
1411
+ public applyStashedDeleteMessage(op: IDirectoryDeleteOperation): IKeyEditLocalOpMetadata {
1412
+ this.throwIfDisposed();
1413
+ const previousValue = this.deleteCore(op.key, true);
1414
+ const pendingMessageId = this.getKeyMessageId(op);
1415
+ const localMetadata: IKeyEditLocalOpMetadata = {
1416
+ type: "edit",
1417
+ pendingMessageId,
1418
+ previousValue,
1419
+ };
1420
+ return localMetadata;
1421
+ }
1422
+
1423
+ /**
1424
+ * Process a set operation.
1425
+ * @param op - The op to process
1426
+ * @param local - Whether the message originated from the local client
1427
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1428
+ * For messages from a remote client, this will be undefined.
1429
+ * @internal
1430
+ */
1431
+ public processSetMessage(
1432
+ op: IDirectorySetOperation,
1433
+ context: ILocalValue | undefined,
1434
+ local: boolean,
1435
+ localOpMetadata: unknown,
1436
+ ): void {
1437
+ this.throwIfDisposed();
1438
+ if (!this.needProcessStorageOperation(op, local, localOpMetadata)) {
1439
+ return;
1440
+ }
1441
+
1442
+ // needProcessStorageOperation should have returned false if local is true
1443
+ // so we can assume context is not undefined
1444
+
1445
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1446
+ this.setCore(op.key, context!, local);
1447
+ }
1448
+
1449
+ /**
1450
+ * Apply set operation locally and generate metadata
1451
+ * @param op - Op to apply
1452
+ * @returns metadata generated for stahed op
1453
+ */
1454
+ public applyStashedSetMessage(
1455
+ op: IDirectorySetOperation,
1456
+ context: ILocalValue,
1457
+ ): IKeyEditLocalOpMetadata {
1458
+ this.throwIfDisposed();
1459
+ // Set the value locally.
1460
+ const previousValue = this.setCore(op.key, context, true);
1461
+
1462
+ // Create metadata
1463
+ const pendingMessageId = this.getKeyMessageId(op);
1464
+ const localMetadata: IKeyEditLocalOpMetadata = {
1465
+ type: "edit",
1466
+ pendingMessageId,
1467
+ previousValue,
1468
+ };
1469
+ return localMetadata;
1470
+ }
1471
+ /**
1472
+ * Process a create subdirectory operation.
1473
+ * @param op - The op to process
1474
+ * @param local - Whether the message originated from the local client
1475
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1476
+ * For messages from a remote client, this will be undefined.
1477
+ * @internal
1478
+ */
1479
+ public processCreateSubDirectoryMessage(
1480
+ op: IDirectoryCreateSubDirectoryOperation,
1481
+ local: boolean,
1482
+ localOpMetadata: unknown,
1483
+ ): void {
1484
+ this.throwIfDisposed();
1485
+ if (!this.needProcessSubDirectoryOperation(op, local, localOpMetadata)) {
1486
+ return;
1487
+ }
1488
+ this.createSubDirectoryCore(op.subdirName, local);
1489
+ }
1490
+
1491
+ /**
1492
+ * Apply createSubDirectory operation locally and generate metadata
1493
+ * @param op - Op to apply
1494
+ * @returns metadata generated for stahed op
1495
+ */
1496
+ public applyStashedCreateSubDirMessage(
1497
+ op: IDirectoryCreateSubDirectoryOperation,
1498
+ ): ICreateSubDirLocalOpMetadata {
1499
+ this.throwIfDisposed();
1500
+ // Create the sub directory locally first.
1501
+ const isNew = this.createSubDirectoryCore(op.subdirName, true);
1502
+ const newMessageId = this.getSubDirMessageId(op);
1503
+
1504
+ const localOpMetadata: ICreateSubDirLocalOpMetadata = {
1505
+ type: "createSubDir",
1506
+ pendingMessageId: newMessageId,
1507
+ previouslyExisted: !isNew,
1508
+ };
1509
+ return localOpMetadata;
1510
+ }
1511
+
1512
+ /**
1513
+ * Process a delete subdirectory operation.
1514
+ * @param op - The op to process
1515
+ * @param local - Whether the message originated from the local client
1516
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1517
+ * For messages from a remote client, this will be undefined.
1518
+ * @internal
1519
+ */
1520
+ public processDeleteSubDirectoryMessage(
1521
+ op: IDirectoryDeleteSubDirectoryOperation,
1522
+ local: boolean,
1523
+ localOpMetadata: unknown,
1524
+ ): void {
1525
+ this.throwIfDisposed();
1526
+ if (!this.needProcessSubDirectoryOperation(op, local, localOpMetadata)) {
1527
+ return;
1528
+ }
1529
+ this.deleteSubDirectoryCore(op.subdirName, local);
1530
+ }
1531
+
1532
+ /**
1533
+ * Apply deleteSubDirectory operation locally and generate metadata
1534
+ * @param op - Op to apply
1535
+ * @returns metadata generated for stahed op
1536
+ */
1537
+ public applyStashedDeleteSubDirMessage(
1538
+ op: IDirectoryDeleteSubDirectoryOperation,
1539
+ ): IDeleteSubDirLocalOpMetadata {
1540
+ this.throwIfDisposed();
1541
+ const subDir = this.deleteSubDirectoryCore(op.subdirName, true);
1542
+ const newMessageId = this.getSubDirMessageId(op);
1543
+ const metadata: IDeleteSubDirLocalOpMetadata = {
1544
+ type: "deleteSubDir",
1545
+ pendingMessageId: newMessageId,
1546
+ subDirectory: subDir,
1547
+ };
1548
+ return metadata;
1549
+ }
1550
+
1551
+ /**
1552
+ * Submit a clear operation.
1553
+ * @param op - The operation
1554
+ */
1555
+ private submitClearMessage(
1556
+ op: IDirectoryClearOperation,
1557
+ previousValue: Map<string, ILocalValue>,
1558
+ ): void {
1559
+ this.throwIfDisposed();
1560
+ const pendingMsgId = ++this.pendingMessageId;
1561
+ this.pendingClearMessageIds.push(pendingMsgId);
1562
+ const metadata: IClearLocalOpMetadata = {
1563
+ type: "clear",
1564
+ pendingMessageId: pendingMsgId,
1565
+ previousStorage: previousValue,
1566
+ };
1567
+ this.directory.submitDirectoryMessage(op, metadata);
1568
+ }
1569
+
1570
+ /**
1571
+ * Resubmit a clear operation.
1572
+ * @param op - The operation
1573
+ * @internal
1574
+ */
1575
+ public resubmitClearMessage(op: IDirectoryClearOperation, localOpMetadata: unknown): void {
1576
+ assert(
1577
+ isClearLocalOpMetadata(localOpMetadata),
1578
+ 0x32b /* Invalid localOpMetadata for clear */,
1579
+ );
1580
+ // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1581
+ const pendingClearMessageId = this.pendingClearMessageIds.shift();
1582
+ assert(
1583
+ pendingClearMessageId === localOpMetadata.pendingMessageId,
1584
+ 0x32c /* pendingMessageId does not match */,
1585
+ );
1586
+ this.submitClearMessage(op, localOpMetadata.previousStorage);
1587
+ }
1588
+
1589
+ /**
1590
+ * Get a new pending message id for the op and cache it to track the pending op
1591
+ */
1592
+ private getKeyMessageId(op: IDirectoryKeyOperation): number {
1593
+ // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1594
+ const pendingMessageId = ++this.pendingMessageId;
1595
+ const pendingMessageIds = this.pendingKeys.get(op.key);
1596
+ if (pendingMessageIds !== undefined) {
1597
+ pendingMessageIds.push(pendingMessageId);
1598
+ } else {
1599
+ this.pendingKeys.set(op.key, [pendingMessageId]);
1600
+ }
1601
+ return pendingMessageId;
1602
+ }
1603
+
1604
+ /**
1605
+ * Submit a key operation.
1606
+ * @param op - The operation
1607
+ * @param previousValue - The value of the key before this op
1608
+ */
1609
+ private submitKeyMessage(op: IDirectoryKeyOperation, previousValue?: ILocalValue): void {
1610
+ this.throwIfDisposed();
1611
+ const pendingMessageId = this.getKeyMessageId(op);
1612
+ const localMetadata = { type: "edit", pendingMessageId, previousValue };
1613
+ this.directory.submitDirectoryMessage(op, localMetadata);
1614
+ }
1615
+
1616
+ /**
1617
+ * Submit a key message to remote clients based on a previous submit.
1618
+ * @param op - The map key message
1619
+ * @param localOpMetadata - Metadata from the previous submit
1620
+ * @internal
1621
+ */
1622
+ public resubmitKeyMessage(op: IDirectoryKeyOperation, localOpMetadata: unknown): void {
1623
+ assert(
1624
+ isKeyEditLocalOpMetadata(localOpMetadata),
1625
+ 0x32d /* Invalid localOpMetadata in submit */,
1626
+ );
1627
+
1628
+ // clear the old pending message id
1629
+ const pendingMessageIds = this.pendingKeys.get(op.key);
1630
+ assert(
1631
+ pendingMessageIds !== undefined &&
1632
+ pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1633
+ 0x32e /* Unexpected pending message received */,
1634
+ );
1635
+ pendingMessageIds.shift();
1636
+ if (pendingMessageIds.length === 0) {
1637
+ this.pendingKeys.delete(op.key);
1638
+ }
1639
+
1640
+ this.submitKeyMessage(op, localOpMetadata.previousValue);
1641
+ }
1642
+
1643
+ /**
1644
+ * Get a new pending message id for the op and cache it to track the pending op
1645
+ */
1646
+ private getSubDirMessageId(op: IDirectorySubDirectoryOperation): number {
1647
+ // We don't reuse the metadata pendingMessageId but send a new one on each submit.
1648
+ const newMessageId = ++this.pendingMessageId;
1649
+ const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1650
+ if (pendingMessageIds !== undefined) {
1651
+ pendingMessageIds.push(newMessageId);
1652
+ } else {
1653
+ this.pendingSubDirectories.set(op.subdirName, [newMessageId]);
1654
+ }
1655
+ return newMessageId;
1656
+ }
1657
+
1658
+ /**
1659
+ * Submit a create subdirectory operation.
1660
+ * @param op - The operation
1661
+ * @param prevExisted - Whether the subdirectory existed before the op
1662
+ */
1663
+ private submitCreateSubDirectoryMessage(
1664
+ op: IDirectorySubDirectoryOperation,
1665
+ prevExisted: boolean,
1666
+ ): void {
1667
+ this.throwIfDisposed();
1668
+ const newMessageId = this.getSubDirMessageId(op);
1669
+
1670
+ const localOpMetadata: ICreateSubDirLocalOpMetadata = {
1671
+ type: "createSubDir",
1672
+ pendingMessageId: newMessageId,
1673
+ previouslyExisted: prevExisted,
1674
+ };
1675
+ this.directory.submitDirectoryMessage(op, localOpMetadata);
1676
+ }
1677
+
1678
+ /**
1679
+ * Submit a delete subdirectory operation.
1680
+ * @param op - The operation
1681
+ * @param subDir - Any subdirectory deleted by the op
1682
+ */
1683
+ private submitDeleteSubDirectoryMessage(
1684
+ op: IDirectorySubDirectoryOperation,
1685
+ subDir: SubDirectory | undefined,
1686
+ ): void {
1687
+ this.throwIfDisposed();
1688
+ const newMessageId = this.getSubDirMessageId(op);
1689
+
1690
+ const localOpMetadata: IDeleteSubDirLocalOpMetadata = {
1691
+ type: "deleteSubDir",
1692
+ pendingMessageId: newMessageId,
1693
+ subDirectory: subDir,
1694
+ };
1695
+ this.directory.submitDirectoryMessage(op, localOpMetadata);
1696
+ }
1697
+
1698
+ /**
1699
+ * Submit a subdirectory operation again
1700
+ * @param op - The operation
1701
+ * @param localOpMetadata - metadata submitted with the op originally
1702
+ * @internal
1703
+ */
1704
+ public resubmitSubDirectoryMessage(
1705
+ op: IDirectorySubDirectoryOperation,
1706
+ localOpMetadata: unknown,
1707
+ ): void {
1708
+ assert(
1709
+ isSubDirLocalOpMetadata(localOpMetadata),
1710
+ 0x32f /* Invalid localOpMetadata for sub directory op */,
1711
+ );
1712
+
1713
+ // clear the old pending message id
1714
+ const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1715
+ assert(
1716
+ pendingMessageIds !== undefined &&
1717
+ pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1718
+ 0x330 /* Unexpected pending message received */,
1719
+ );
1720
+ pendingMessageIds.shift();
1721
+ if (pendingMessageIds.length === 0) {
1722
+ this.pendingSubDirectories.delete(op.subdirName);
1723
+ }
1724
+
1725
+ if (localOpMetadata.type === "createSubDir") {
1726
+ this.submitCreateSubDirectoryMessage(op, localOpMetadata.previouslyExisted);
1727
+ } else {
1728
+ this.submitDeleteSubDirectoryMessage(op, localOpMetadata.subDirectory);
1729
+ }
1730
+ }
1731
+
1732
+ /**
1733
+ * Get the storage of this subdirectory in a serializable format, to be used in snapshotting.
1734
+ * @param serializer - The serializer to use to serialize handles in its values.
1735
+ * @returns The JSONable string representing the storage of this subdirectory
1736
+ * @internal
1737
+ */
1738
+ public *getSerializedStorage(
1739
+ serializer: IFluidSerializer,
1740
+ ): Generator<[string, ISerializedValue], void> {
1741
+ this.throwIfDisposed();
1742
+ for (const [key, localValue] of this._storage) {
1743
+ const value = localValue.makeSerialized(serializer, this.directory.handle);
1744
+ const res: [string, ISerializedValue] = [key, value];
1745
+ yield res;
1746
+ }
1747
+ }
1748
+
1749
+ /**
1750
+ * Populate a key value in this subdirectory's storage, to be used when loading from snapshot.
1751
+ * @param key - The key to populate
1752
+ * @param localValue - The local value to populate into it
1753
+ * @internal
1754
+ */
1755
+ public populateStorage(key: string, localValue: ILocalValue): void {
1756
+ this.throwIfDisposed();
1757
+ this._storage.set(key, localValue);
1758
+ }
1759
+
1760
+ /**
1761
+ * Populate a subdirectory into this subdirectory, to be used when loading from snapshot.
1762
+ * @param subdirName - The name of the subdirectory to add
1763
+ * @param newSubDir - The new subdirectory to add
1764
+ * @internal
1765
+ */
1766
+ public populateSubDirectory(subdirName: string, newSubDir: SubDirectory): void {
1767
+ this.throwIfDisposed();
1768
+ this._subdirectories.set(subdirName, newSubDir);
1769
+ }
1770
+
1771
+ /**
1772
+ * Retrieve the local value at the given key. This is used to get value type information stashed on the local
1773
+ * value so op handlers can be retrieved
1774
+ * @param key - The key to retrieve from
1775
+ * @returns The local value
1776
+ * @internal
1777
+ */
1778
+ public getLocalValue<T extends ILocalValue = ILocalValue>(key: string): T {
1779
+ this.throwIfDisposed();
1780
+ return this._storage.get(key) as T;
1781
+ }
1782
+
1783
+ /**
1784
+ * Remove the pendingMessageId from the map tracking it on rollback
1785
+ * @param map - map tracking the pending messages
1786
+ * @param key - key of the edit in the op
1787
+ */
1788
+ private rollbackPendingMessageId(
1789
+ map: Map<string, number[]>,
1790
+ key: string,
1791
+ pendingMessageId,
1792
+ ): void {
1793
+ const pendingMessageIds = map.get(key);
1794
+ const lastPendingMessageId = pendingMessageIds?.pop();
1795
+ if (!pendingMessageIds || lastPendingMessageId !== pendingMessageId) {
1796
+ throw new Error("Rollback op does not match last pending");
1797
+ }
1798
+ if (pendingMessageIds.length === 0) {
1799
+ map.delete(key);
1800
+ }
1801
+ }
1802
+
1803
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
1804
+
1805
+ /**
1806
+ * Rollback a local op
1807
+ * @param op - The operation to rollback
1808
+ * @param localOpMetadata - The local metadata associated with the op.
1809
+ */
1810
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1811
+ public rollback(op: any, localOpMetadata: unknown): void {
1812
+ if (!isDirectoryLocalOpMetadata(localOpMetadata)) {
1813
+ throw new Error("Invalid localOpMetadata");
1814
+ }
1815
+
1816
+ if (op.type === "clear" && localOpMetadata.type === "clear") {
1817
+ for (const [key, localValue] of localOpMetadata.previousStorage.entries()) {
1818
+ this.setCore(key, localValue, true);
1819
+ }
1820
+
1821
+ const lastPendingClearId = this.pendingClearMessageIds.pop();
1822
+ if (
1823
+ lastPendingClearId === undefined ||
1824
+ lastPendingClearId !== localOpMetadata.pendingMessageId
1825
+ ) {
1826
+ throw new Error("Rollback op does match last clear");
1827
+ }
1828
+ } else if ((op.type === "delete" || op.type === "set") && localOpMetadata.type === "edit") {
1829
+ if (localOpMetadata.previousValue === undefined) {
1830
+ this.deleteCore(op.key as string, true);
1831
+ } else {
1832
+ this.setCore(op.key as string, localOpMetadata.previousValue, true);
1833
+ }
1834
+
1835
+ this.rollbackPendingMessageId(
1836
+ this.pendingKeys,
1837
+ op.key as string,
1838
+ localOpMetadata.pendingMessageId,
1839
+ );
1840
+ } else if (op.type === "createSubDirectory" && localOpMetadata.type === "createSubDir") {
1841
+ if (!localOpMetadata.previouslyExisted) {
1842
+ this.deleteSubDirectoryCore(op.subdirName as string, true);
1843
+ }
1844
+
1845
+ this.rollbackPendingMessageId(
1846
+ this.pendingSubDirectories,
1847
+ op.subdirName as string,
1848
+ localOpMetadata.pendingMessageId,
1849
+ );
1850
+ } else if (op.type === "deleteSubDirectory" && localOpMetadata.type === "deleteSubDir") {
1851
+ if (localOpMetadata.subDirectory !== undefined) {
1852
+ this.undeleteSubDirectoryTree(localOpMetadata.subDirectory);
1853
+ // don't need to register events because deleting never unregistered
1854
+ this._subdirectories.set(op.subdirName as string, localOpMetadata.subDirectory);
1855
+ this.emit("subDirectoryCreated", op.subdirName, true, this);
1856
+ }
1857
+
1858
+ this.rollbackPendingMessageId(
1859
+ this.pendingSubDirectories,
1860
+ op.subdirName as string,
1861
+ localOpMetadata.pendingMessageId,
1862
+ );
1863
+ } else {
1864
+ throw new Error("Unsupported op for rollback");
1865
+ }
1866
+ }
1867
+
1868
+ /* eslint-enable @typescript-eslint/no-unsafe-member-access */
1869
+
1870
+ /**
1871
+ * Converts the given relative path into an absolute path.
1872
+ * @param path - Relative path to convert
1873
+ * @returns The equivalent absolute path
1874
+ */
1875
+ private makeAbsolute(relativePath: string): string {
1876
+ return posix.resolve(this.absolutePath, relativePath);
1877
+ }
1878
+
1879
+ /**
1880
+ * If our local operations that have not yet been ack'd will eventually overwrite an incoming operation, we should
1881
+ * not process the incoming operation.
1882
+ * @param op - Operation to check
1883
+ * @param local - Whether the operation originated from the local client
1884
+ * @param localOpMetadata - For local client ops, this is the metadata that was submitted with the op.
1885
+ * For ops from a remote client, this will be undefined.
1886
+ * @returns True if the operation should be processed, false otherwise
1887
+ */
1888
+ private needProcessStorageOperation(
1889
+ op: IDirectoryKeyOperation,
1890
+ local: boolean,
1891
+ localOpMetadata: unknown,
1892
+ ): boolean {
1893
+ if (this.pendingClearMessageIds.length > 0) {
1894
+ if (local) {
1895
+ assert(
1896
+ localOpMetadata !== undefined &&
1897
+ isKeyEditLocalOpMetadata(localOpMetadata) &&
1898
+ localOpMetadata.pendingMessageId < this.pendingClearMessageIds[0],
1899
+ 0x010 /* "Received out of order storage op when there is an unackd clear message" */,
1900
+ );
1901
+ }
1902
+ // If I have a NACK clear, we can ignore all ops.
1903
+ return false;
1904
+ }
1905
+
1906
+ const pendingKeyMessageId = this.pendingKeys.get(op.key);
1907
+ if (pendingKeyMessageId !== undefined) {
1908
+ // Found an NACK op, clear it from the directory if the latest sequence number in the directory
1909
+ // match the message's and don't process the op.
1910
+ if (local) {
1911
+ assert(
1912
+ localOpMetadata !== undefined && isKeyEditLocalOpMetadata(localOpMetadata),
1913
+ 0x011 /* pendingMessageId is missing from the local client's operation */,
1914
+ );
1915
+ const pendingMessageIds = this.pendingKeys.get(op.key);
1916
+ assert(
1917
+ pendingMessageIds !== undefined &&
1918
+ pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1919
+ 0x331 /* Unexpected pending message received */,
1920
+ );
1921
+ pendingMessageIds.shift();
1922
+ if (pendingMessageIds.length === 0) {
1923
+ this.pendingKeys.delete(op.key);
1924
+ }
1925
+ }
1926
+ return false;
1927
+ }
1928
+
1929
+ // If we don't have a NACK op on the key, we need to process the remote ops.
1930
+ return !local;
1931
+ }
1932
+
1933
+ /**
1934
+ * If our local operations that have not yet been ack'd will eventually overwrite an incoming operation, we should
1935
+ * not process the incoming operation.
1936
+ * @param op - Operation to check
1937
+ * @param local - Whether the message originated from the local client
1938
+ * @param message - The message
1939
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
1940
+ * For messages from a remote client, this will be undefined.
1941
+ * @returns True if the operation should be processed, false otherwise
1942
+ */
1943
+ private needProcessSubDirectoryOperation(
1944
+ op: IDirectorySubDirectoryOperation,
1945
+ local: boolean,
1946
+ localOpMetadata: unknown,
1947
+ ): boolean {
1948
+ const pendingSubDirectoryMessageId = this.pendingSubDirectories.get(op.subdirName);
1949
+ if (pendingSubDirectoryMessageId !== undefined) {
1950
+ if (local) {
1951
+ assert(
1952
+ isSubDirLocalOpMetadata(localOpMetadata),
1953
+ 0x012 /* pendingMessageId is missing from the local client's operation */,
1954
+ );
1955
+ const pendingMessageIds = this.pendingSubDirectories.get(op.subdirName);
1956
+ assert(
1957
+ pendingMessageIds !== undefined &&
1958
+ pendingMessageIds[0] === localOpMetadata.pendingMessageId,
1959
+ 0x332 /* Unexpected pending message received */,
1960
+ );
1961
+ pendingMessageIds.shift();
1962
+ if (pendingMessageIds.length === 0) {
1963
+ this.pendingSubDirectories.delete(op.subdirName);
1964
+ }
1965
+ }
1966
+ return false;
1967
+ }
1968
+
1969
+ return !local;
1970
+ }
1971
+
1972
+ /**
1973
+ * Clear all keys in memory in response to a remote clear, but retain keys we have modified but not yet been ack'd.
1974
+ */
1975
+ private clearExceptPendingKeys(local: boolean): void {
1976
+ // Assuming the pendingKeys is small and the map is large
1977
+ // we will get the value for the pendingKeys and clear the map
1978
+ const temp = new Map<string, ILocalValue>();
1979
+
1980
+ for (const [key] of this.pendingKeys) {
1981
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1982
+ temp.set(key, this._storage.get(key)!);
1983
+ }
1984
+
1985
+ this.clearCore(local);
1986
+
1987
+ for (const [key, value] of temp.entries()) {
1988
+ this.setCore(key, value, true);
1989
+ }
1990
+ }
1991
+
1992
+ /**
1993
+ * Clear implementation used for both locally sourced clears as well as incoming remote clears.
1994
+ * @param local - Whether the message originated from the local client
1995
+ */
1996
+ private clearCore(local: boolean): void {
1997
+ this._storage.clear();
1998
+ this.directory.emit("clear", local, this.directory);
1999
+ }
2000
+
2001
+ /**
2002
+ * Delete implementation used for both locally sourced deletes as well as incoming remote deletes.
2003
+ * @param key - The key being deleted
2004
+ * @param local - Whether the message originated from the local client
2005
+ * @returns Previous local value of the key if it existed, undefined if it did not exist
2006
+ */
2007
+ private deleteCore(key: string, local: boolean): ILocalValue | undefined {
2008
+ const previousLocalValue = this._storage.get(key);
2009
+ const previousValue: unknown = previousLocalValue?.value;
2010
+ const successfullyRemoved = this._storage.delete(key);
2011
+ if (successfullyRemoved) {
2012
+ const event: IDirectoryValueChanged = { key, path: this.absolutePath, previousValue };
2013
+ this.directory.emit("valueChanged", event, local, this.directory);
2014
+ const containedEvent: IValueChanged = { key, previousValue };
2015
+ this.emit("containedValueChanged", containedEvent, local, this);
2016
+ }
2017
+ return previousLocalValue;
2018
+ }
2019
+
2020
+ /**
2021
+ * Set implementation used for both locally sourced sets as well as incoming remote sets.
2022
+ * @param key - The key being set
2023
+ * @param value - The value being set
2024
+ * @param local - Whether the message originated from the local client
2025
+ * @returns Previous local value of the key, if any
2026
+ */
2027
+ private setCore(key: string, value: ILocalValue, local: boolean): ILocalValue | undefined {
2028
+ const previousLocalValue = this._storage.get(key);
2029
+ const previousValue: unknown = previousLocalValue?.value;
2030
+ this._storage.set(key, value);
2031
+ const event: IDirectoryValueChanged = { key, path: this.absolutePath, previousValue };
2032
+ this.directory.emit("valueChanged", event, local, this.directory);
2033
+ const containedEvent: IValueChanged = { key, previousValue };
2034
+ this.emit("containedValueChanged", containedEvent, local, this);
2035
+ return previousLocalValue;
2036
+ }
2037
+
2038
+ /**
2039
+ * Create subdirectory implementation used for both locally sourced creation as well as incoming remote creation.
2040
+ * @param subdirName - The name of the subdirectory being created
2041
+ * @param local - Whether the message originated from the local client
2042
+ * @returns - True if is newly created, false if it already existed.
2043
+ */
2044
+ private createSubDirectoryCore(subdirName: string, local: boolean): boolean {
2045
+ if (!this._subdirectories.has(subdirName)) {
2046
+ const absolutePath = posix.join(this.absolutePath, subdirName);
2047
+ const subDir = new SubDirectory(
2048
+ this.directory,
2049
+ this.runtime,
2050
+ this.serializer,
2051
+ absolutePath,
2052
+ );
2053
+ this.registerEventsOnSubDirectory(subDir, subdirName);
2054
+ this._subdirectories.set(subdirName, subDir);
2055
+ this.emit("subDirectoryCreated", subdirName, local, this);
2056
+ return true;
2057
+ }
2058
+ return false;
2059
+ }
2060
+
2061
+ private registerEventsOnSubDirectory(subDirectory: SubDirectory, subDirName: string): void {
2062
+ subDirectory.on("subDirectoryCreated", (relativePath: string, local: boolean) => {
2063
+ this.emit("subDirectoryCreated", posix.join(subDirName, relativePath), local, this);
2064
+ });
2065
+ subDirectory.on("subDirectoryDeleted", (relativePath: string, local: boolean) => {
2066
+ this.emit("subDirectoryDeleted", posix.join(subDirName, relativePath), local, this);
2067
+ });
2068
+ }
2069
+
2070
+ /**
2071
+ * Delete subdirectory implementation used for both locally sourced creation as well as incoming remote creation.
2072
+ * @param subdirName - The name of the subdirectory being deleted
2073
+ * @param local - Whether the message originated from the local client
2074
+ */
2075
+ private deleteSubDirectoryCore(subdirName: string, local: boolean): SubDirectory | undefined {
2076
+ const previousValue = this._subdirectories.get(subdirName);
2077
+ // This should make the subdirectory structure unreachable so it can be GC'd and won't appear in snapshots
2078
+ // Might want to consider cleaning out the structure more exhaustively though? But not when rollback.
2079
+ if (previousValue !== undefined) {
2080
+ this._subdirectories.delete(subdirName);
2081
+ this.disposeSubDirectoryTree(previousValue);
2082
+ this.emit("subDirectoryDeleted", subdirName, local, this);
2083
+ }
2084
+ return previousValue;
2085
+ }
2086
+
2087
+ private disposeSubDirectoryTree(directory: IDirectory | undefined): void {
2088
+ if (!directory) {
2089
+ return;
2090
+ }
2091
+ // Dispose the subdirectory tree. This will dispose the subdirectories from bottom to top.
2092
+ const subDirectories = directory.subdirectories();
2093
+ for (const [_, subDirectory] of subDirectories) {
2094
+ this.disposeSubDirectoryTree(subDirectory);
2095
+ }
2096
+ if (typeof directory.dispose === "function") {
2097
+ directory.dispose();
2098
+ }
2099
+ }
2100
+
2101
+ private undeleteSubDirectoryTree(directory: SubDirectory): void {
2102
+ // Restore deleted subdirectory tree. This will unmark "deleted" from the subdirectories from bottom to top.
2103
+ for (const [_, subDirectory] of this._subdirectories.entries()) {
2104
+ this.undeleteSubDirectoryTree(subDirectory);
2105
+ }
2106
+ directory.undispose();
2107
+ }
1999
2108
  }