22 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
33 * Licensed under the MIT License.
44 */
5-
6- /* eslint-disable @typescript-eslint/explicit-function-return-type */
7-
85import { strict as assert } from "node:assert" ;
96
7+ import type { IEventThisPlaceHolder } from "@fluidframework/core-interfaces" ;
8+
109import type {
1110 IDirectory ,
1211 IDirectoryValueChanged ,
@@ -28,97 +27,87 @@ export class SharedDirectoryOracle {
2827 this . sharedDir . on ( "subDirectoryDeleted" , this . onSubDirDeleted ) ;
2928 this . sharedDir . on ( "containedValueChanged" , this . onContainedValueChanged ) ;
3029
31- this . captureInitialSnapshot ( sharedDir ) ;
30+ this . takeSnapshot ( sharedDir ) ;
3231 }
3332
34- private captureInitialSnapshot ( dir : IDirectory ) : void {
35- // Capture keys
36- for ( const [ key , value ] of dir . entries ( ) ) {
37- const pathKey = dir . absolutePath === "/" ? `/${ key } ` : `${ dir . absolutePath } /${ key } ` ;
38-
39- this . model . set ( pathKey , value ) ;
40- }
41-
42- for ( const [ , subDir ] of dir . subdirectories ( ) ) {
43- // Just recurse to capture keys inside the subdir
44- this . captureInitialSnapshot ( subDir ) ;
33+ private takeSnapshot ( dir : ISharedDirectory | IDirectory ) : void {
34+ for ( const [ k , v ] of this . sharedDir . entries ( ) ) {
35+ this . model . set ( k , v ) ;
4536 }
4637 }
4738
48- private readonly onValueChanged = ( change : IDirectoryValueChanged ) => {
39+ private readonly onValueChanged = (
40+ changed : IDirectoryValueChanged ,
41+ local : boolean ,
42+ target : IEventThisPlaceHolder ,
43+ ) : void => {
4944 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
50- const { key, previousValue } = change ;
51- const path = change . path ?? "" ;
52-
53- const pathKey = path === "/" ? `/${ key } ` : `${ path } /${ key } ` ;
54-
55- assert . strictEqual (
56- previousValue ,
57- this . model . get ( pathKey ) ,
58- `Mismatch on previous value for key="${ key } "` ,
59- ) ;
45+ const { path, key, previousValue } = changed ;
46+ const fullPath = path === "/" ? `/${ key } ` : `${ path } /${ key } ` ;
47+
48+ if ( this . model . has ( fullPath ) ) {
49+ const prevVal = this . model . get ( fullPath ) ;
50+ assert . strictEqual (
51+ prevVal ,
52+ previousValue ,
53+ `previous value mismatch at ${ fullPath } : expected: ${ prevVal } , actual: ${ previousValue } ` ,
54+ ) ;
55+ }
6056
61- const fuzzDir = this . sharedDir . getWorkingDirectory ( path ) ;
62- if ( ! fuzzDir ) return ;
57+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
58+ const newVal = this . sharedDir . get ( fullPath ) ;
6359
64- if ( fuzzDir . has ( key ) ) {
65- this . model . set ( pathKey , fuzzDir . get ( key ) ) ;
60+ if ( newVal === undefined ) {
61+ // deletion
62+ this . model . delete ( fullPath ) ;
6663 } else {
67- this . model . delete ( pathKey ) ;
64+ this . model . set ( fullPath , newVal ) ;
6865 }
6966 } ;
7067
71- private readonly onClear = ( local : boolean ) => {
68+ private readonly onClear = ( local : boolean , target : IEventThisPlaceHolder ) : void => {
7269 this . model . clear ( ) ;
7370 } ;
7471
7572 private readonly onSubDirCreated = (
76- subdirName : string ,
73+ path : string ,
7774 local : boolean ,
78- target : ISharedDirectory ,
79- ) => {
80- const { absolutePath } = target ;
81- if ( ! this . model . has ( `${ absolutePath } ${ subdirName } ` ) ) {
82- this . model . set ( `${ absolutePath } ${ subdirName } ` , undefined ) ;
83- }
75+ target : IEventThisPlaceHolder ,
76+ ) : void => {
77+ this . model . set ( path , undefined ) ;
8478 } ;
8579
86- private readonly onSubDirDeleted = ( path : string ) => {
87- const absPath = path . startsWith ( "/" ) ? path : `/${ path } ` ;
88- for ( const key of [ ...this . model . keys ( ) ] ) {
89- if ( key . startsWith ( absPath ) ) {
90- const deleted = this . model . delete ( key ) ;
91- if ( ! deleted ) {
92- assert ( "not deleted" ) ;
93- }
94- }
95- }
80+ private readonly onSubDirDeleted = (
81+ path : string ,
82+ local : boolean ,
83+ target : IEventThisPlaceHolder ,
84+ ) : void => {
85+ this . model . delete ( path ) ;
9686 } ;
9787
9888 private readonly onContainedValueChanged = (
99- change : IValueChanged ,
89+ changed : IValueChanged ,
10090 local : boolean ,
101- target : IDirectory ,
102- ) => {
91+ target : IEventThisPlaceHolder ,
92+ ) : void => {
10393 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
104- const { key, previousValue } = change ;
105- const { absolutePath } = target ;
106-
107- const pathKey = absolutePath === "/" ? `/ ${ key } ` : ` ${ absolutePath } / ${ key } ` ;
108-
109- assert . strictEqual (
110- previousValue ,
111- this . model . get ( pathKey ) ,
112- `Mismatch on previous value for key=" ${ key } "` ,
113- ) ;
94+ const { key, previousValue } = changed ;
95+
96+ if ( this . model . has ( key ) ) {
97+ const prevVal = this . model . get ( key ) ;
98+ assert . strictEqual (
99+ prevVal ,
100+ previousValue ,
101+ `contained previous value mismatch at ${ key } : expected: ${ prevVal } , actual: ${ previousValue } ` ,
102+ ) ;
103+ }
114104
115105 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
116- const newValue = target . get ( key ) ;
117-
118- if ( newValue === undefined ) {
119- this . model . delete ( pathKey ) ;
106+ const newVal = this . sharedDir . get ( key ) ;
107+ if ( newVal === undefined ) {
108+ this . model . delete ( key ) ;
120109 } else {
121- this . model . set ( pathKey , newValue ) ;
110+ this . model . set ( key , newVal ) ;
122111 }
123112 } ;
124113
0 commit comments