2121import java .io .StringReader ;
2222import java .io .Writer ;
2323import java .nio .charset .StandardCharsets ;
24+ import java .util .LinkedList ;
25+ import java .util .List ;
2426import java .util .Optional ;
27+ import java .util .function .Consumer ;
2528import javax .inject .Inject ;
2629import javax .inject .Singleton ;
2730
@@ -37,7 +40,7 @@ public class Project {
3740 @ Inject
3841 private PipelineRunner pipelineRunner ;
3942 private Optional <File > file = Optional .empty ();
40- private boolean saveIsDirty = false ;
43+ private final ObservableBoolean saveIsDirty = new ObservableBoolean () ;
4144
4245 @ Inject
4346 public void initialize (StepConverter stepConverter ,
@@ -111,7 +114,7 @@ void open(Reader reader) {
111114 this .pipeline .clear ();
112115 this .xstream .fromXML (reader );
113116 pipelineRunner .startAsync ();
114- saveIsDirty = false ;
117+ saveIsDirty . set ( false ) ;
115118 }
116119
117120 /**
@@ -127,19 +130,42 @@ public void save(File file) throws IOException {
127130
128131 public void save (Writer writer ) {
129132 this .xstream .toXML (this .pipeline , writer );
130- saveIsDirty = false ;
133+ saveIsDirty . set ( false ) ;
131134 }
132135
133136 public boolean isSaveDirty () {
134- return saveIsDirty ;
137+ return saveIsDirty .get ();
138+ }
139+
140+ public void addIsSaveDirtyConsumer (Consumer <Boolean > consumer ) {
141+ saveIsDirty .addConsumer (consumer );
135142 }
136143
137144 @ Subscribe
138145 public void onDirtiesSaveEvent (DirtiesSaveEvent dirtySaveEvent ) {
139146 // Only update the flag the save isn't already dirty
140147 // We don't need to be redundantly checking if the event dirties the save
141- if (!saveIsDirty && dirtySaveEvent .doesDirtySave ()) {
142- saveIsDirty = true ;
148+ if (!saveIsDirty .get () && dirtySaveEvent .doesDirtySave ()) {
149+ saveIsDirty .set (true );
150+ }
151+ }
152+
153+ private static final class ObservableBoolean {
154+
155+ private boolean b = false ;
156+ private final List <Consumer <Boolean >> consumers = new LinkedList <>();
157+
158+ public void set (boolean b ) {
159+ this .b = b ;
160+ consumers .parallelStream ().forEach (c -> c .accept (b ));
161+ }
162+
163+ public boolean get () {
164+ return b ;
165+ }
166+
167+ public void addConsumer (Consumer <Boolean > consumer ) {
168+ consumers .add (consumer );
143169 }
144170 }
145171}
0 commit comments