1
+ import EventEmitter from "events" ;
1
2
import { connectedCloseness } from "graphology-metrics/layout-quality" ;
2
3
import { debounce , identity , pick } from "lodash" ;
3
4
import seedrandom from "seedrandom" ;
@@ -36,8 +37,6 @@ export const layoutStateAtom = atom<LayoutState>(getLocalStorageLayoutState());
36
37
export const startLayout = asyncAction ( async ( id : string , params : unknown ) => {
37
38
const { setNodePositions } = graphDatasetActions ;
38
39
const dataset = graphDatasetAtom . get ( ) ;
39
- const { quality } = layoutStateAtom . get ( ) ;
40
- const { computeLayoutQualityMetric } = layoutActions ;
41
40
42
41
// search the layout
43
42
const layout = LAYOUTS . find ( ( l ) => l . id === id ) ;
@@ -56,7 +55,6 @@ export const startLayout = asyncAction(async (id: string, params: unknown) => {
56
55
// To prevent resetting the camera before sigma receives new data, we
57
56
// need to wait a frame, and also wait for it to trigger a refresh:
58
57
setTimeout ( ( ) => {
59
- if ( quality . enabled ) computeLayoutQualityMetric ( ) ;
60
58
layoutStateAtom . set ( ( prev ) => ( { ...prev , type : "idle" } ) ) ;
61
59
resetCamera ( { forceRefresh : false } ) ;
62
60
} , 0 ) ;
@@ -94,7 +92,7 @@ export const setQuality: Producer<LayoutState, [LayoutQuality]> = (quality) => {
94
92
return ( state ) => ( { ...state , quality } ) ;
95
93
} ;
96
94
97
- export const computeLayoutQualityMetric : Producer < LayoutState , [ ] > = ( ) => {
95
+ const _computeLayoutQualityMetric : Producer < LayoutState , [ ] > = ( ) => {
98
96
const sigmaGraph = sigmaGraphAtom . get ( ) ;
99
97
try {
100
98
const metric = connectedCloseness ( sigmaGraph , {
@@ -110,7 +108,7 @@ export const layoutActions = {
110
108
startLayout,
111
109
stopLayout,
112
110
setQuality : producerToAction ( setQuality , layoutStateAtom ) ,
113
- computeLayoutQualityMetric : producerToAction ( computeLayoutQualityMetric , layoutStateAtom ) ,
111
+ computeLayoutQualityMetric : producerToAction ( _computeLayoutQualityMetric , layoutStateAtom ) ,
114
112
} ;
115
113
116
114
const gridEnabledAtom = derivedAtom ( layoutStateAtom , ( value ) => pick ( value . quality , "enabled" ) , {
@@ -125,10 +123,17 @@ gridEnabledAtom.bindEffect((enabled) => {
125
123
126
124
computeLayoutQualityMetric ( ) ;
127
125
const sigmaGraph = sigmaGraphAtom . get ( ) ;
126
+ // this event is triggered when sigma data are updated by the derived atom mechanism through a graph import
127
+ // this is a custom event
128
+ ( sigmaGraph as EventEmitter ) . on ( "graphImported" , fn ) ;
129
+
130
+ // this event is triggered by user manually changing node positions by dragging node
128
131
sigmaGraph . on ( "nodeAttributesUpdated" , fn ) ;
132
+ // this event is triggered by async layout
129
133
sigmaGraph . on ( "eachNodeAttributesUpdated" , fn ) ;
130
134
131
135
return ( ) => {
136
+ ( sigmaGraph as EventEmitter ) . off ( "graphImported" , fn ) ;
132
137
sigmaGraph . off ( "eachNodeAttributesUpdated" , fn ) ;
133
138
sigmaGraph . off ( "nodeAttributesUpdated" , fn ) ;
134
139
} ;
0 commit comments