@@ -104,6 +104,7 @@ export class ViewGraph {
104104 ) ;
105105 return null ;
106106 }
107+
107108 if ( this . graph . hasEdge ( device1 . id , device2 . id ) ) {
108109 console . warn ( `Edge with ID ${ device1 . id } ,${ device2 . id } already exists.` ) ;
109110 return this . graph . getEdge ( device1 . id , device2 . id ) ;
@@ -197,8 +198,25 @@ export class ViewGraph {
197198 device . updateVisibility ( ) ;
198199 }
199200
201+ // Refresh and make all edges visible again
200202 for ( const [ , , edge ] of this . graph . getAllEdges ( ) ) {
201203 edge . refresh ( ) ;
204+ edge . makeVisible ( ) ;
205+ }
206+
207+ // Iterate until no changes are made
208+ for ( let i = 0 ; i < this . graph . getVertexCount ( ) ; i ++ ) {
209+ let hadChanges = false ;
210+ // For each iteration, update visibility of all edges.
211+ // This takes into account currently visible edges and devices.
212+ for ( const [ , , edge ] of this . graph . getAllEdges ( ) ) {
213+ const previousVisibility = edge . visible ;
214+ edge . updateVisibility ( ) ;
215+ hadChanges ||= previousVisibility !== edge . visible ;
216+ }
217+ if ( ! hadChanges ) {
218+ break ;
219+ }
202220 }
203221
204222 // warn Packet Manager that the layer has been changed
@@ -223,6 +241,52 @@ export class ViewGraph {
223241 return Array . from ( edges ) . map ( ( [ , edge ] ) => edge ) ;
224242 }
225243
244+ getVisibleConnectedDeviceIds ( deviceId : DeviceId ) : DeviceId [ ] {
245+ const visibleDevices : DeviceId [ ] = [ ] ; // Stores visible connected device IDs
246+
247+ const vertexFilter = ( device : ViewDevice , id : DeviceId ) : boolean => {
248+ // If device is visible, add it to the set and stop traversal
249+ if ( device . visible && id !== deviceId ) {
250+ visibleDevices . push ( id ) ;
251+ return false ;
252+ }
253+ return true ;
254+ } ;
255+
256+ this . graph . dfs ( deviceId , { vertexFilter } ) ;
257+
258+ return visibleDevices ; // Return the list of visible connected device IDs
259+ }
260+
261+ /**
262+ * Checks if a device can reach any visible device, excluding the specified device from traversal.
263+ * @param startId ID of the device to check for
264+ * @param excludeId ID of a device to be excluded from traversal
265+ * @returns True if the device can reach any visible device, otherwise false.
266+ */
267+ canReachVisibleDevice ( startId : DeviceId , excludeId : DeviceId ) : boolean {
268+ const visibleDevices = new Set < DeviceId > ( ) ;
269+
270+ const vertexFilter = ( device : ViewDevice , id : DeviceId ) : boolean => {
271+ // If the device is excluded, skip it and stop traversal
272+ if ( id === excludeId ) {
273+ return false ;
274+ }
275+ // If device is visible, add it to the set and stop traversal
276+ if ( device . visible ) {
277+ visibleDevices . add ( id ) ;
278+ return false ;
279+ }
280+ return true ;
281+ } ;
282+
283+ // Avoid invisible edges
284+ const edgeFilter = ( edge : Edge ) => edge . visible ;
285+
286+ this . graph . dfs ( startId , { vertexFilter, edgeFilter } ) ;
287+ return visibleDevices . size > 0 ;
288+ }
289+
226290 getAllConnections ( ) : Edge [ ] {
227291 return Array . from ( this . graph . getAllEdges ( ) ) . map ( ( [ , , edge ] ) => edge ) ;
228292 }
0 commit comments