vaadin4spring icon indicating copy to clipboard operation
vaadin4spring copied to clipboard

ScopedEventBus serializable problem

Open Emmenemoi opened this issue 5 years ago • 3 comments

ListenerCollection implements Serializable but not WeakHashMap. As a consequence ListenerCollection is NOT Serializable because of weakListeners property.

Emmenemoi avatar Jan 15 '20 20:01 Emmenemoi

Would it be a solution ?

@@ -188,4 +191,24 @@ class ListenerCollection implements Serializable {
             }
         }
     }
+
+    private  void readObject(ObjectInputStream ois)
+            throws IOException, ClassNotFoundException {
+
+        this.listeners = (Set<Listener>) ois.readObject();
+        this.weakListeners = Collections.newSetFromMap(new WeakHashMap<Listener, Boolean>());
+        this.weakListeners.addAll( (Set<Listener>) ois.readObject() ) ;
+        this.logger = LoggerFactory.getLogger(getClass());
+        logger.debug("after readObject weakListeners = {}", Arrays.deepToString(weakListeners.toArray()));
+    }
+
+    private  void writeObject(ObjectOutputStream oos)
+            throws IOException {
+
+        oos.writeObject(listeners);
+        oos.writeObject(new HashSet<>(weakListeners));
+        logger.debug("writeObject weakListeners = {}", Arrays.deepToString(weakListeners.toArray()));
+
+    }
 }

Emmenemoi avatar Jan 20 '20 18:01 Emmenemoi

@peholmst Hey Petter, is your side project applicable for BFP? We currently ran into the same issue using your eventbus addon in one of our applications (V8 + addon 2-0-0).

knoobie avatar Feb 07 '20 22:02 knoobie

I'll add a pull request, the idea is here but it needs testing

Emmenemoi avatar Feb 11 '20 22:02 Emmenemoi