diff --git a/owrx/property/__init__.py b/owrx/property/__init__.py index 8489fec..6c57a1d 100644 --- a/owrx/property/__init__.py +++ b/owrx/property/__init__.py @@ -144,12 +144,22 @@ class PropertyStack(PropertyManager): """ self.layers.append({"priority": priority, "props": pm}) + def eventClosure(name, value): + self.receiveEvent(pm, name, value) + + pm.wire(eventClosure) + + def receiveEvent(self, layer, name, value): + if layer != self._getTopLayer(name): + return + self._fireCallbacks(name, value) + def removeLayer(self, pm: PropertyManager): for layer in self.layers: if layer["props"] == pm: self.layers.remove(layer) - def _getLayer(self, item): + def _getTopLayer(self, item): layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])] for m in layers: if item in m: @@ -158,15 +168,15 @@ class PropertyStack(PropertyManager): return layers[0] def __getitem__(self, item): - layer = self._getLayer(item) + layer = self._getTopLayer(item) return layer.__getitem__(item) def __setitem__(self, key, value): - layer = self._getLayer(key) + layer = self._getTopLayer(key) return layer.__setitem__(key, value) def __contains__(self, item): - layer = self._getLayer(item) + layer = self._getTopLayer(item) return layer.__contains__(item) def __dict__(self): diff --git a/test/property/test_property_stack.py b/test/property/test_property_stack.py index 2102a2c..ff83635 100644 --- a/test/property/test_property_stack.py +++ b/test/property/test_property_stack.py @@ -41,3 +41,27 @@ class PropertyStackTest(TestCase): self.assertEqual(om["testkey"], "high value") om.removeLayer(high_pm) self.assertEqual(om["testkey"], "low value") + + def testPropertyChange(self): + layer = PropertyLayer() + stack = PropertyStack() + stack.addLayer(0, layer) + mock = Mock() + stack.wire(mock.method) + layer["testkey"] = "testvalue" + mock.method.assert_called_once_with("testkey", "testvalue") + + def testPropertyChangeEventPriority(self): + low_layer = PropertyLayer() + high_layer = PropertyLayer() + low_layer["testkey"] = "initial low value" + high_layer["testkey"] = "initial high value" + stack = PropertyStack() + stack.addLayer(1, low_layer) + stack.addLayer(0, high_layer) + mock = Mock() + stack.wire(mock.method) + low_layer["testkey"] = "modified low value" + mock.method.assert_not_called() + high_layer["testkey"] = "modified high value" + mock.method.assert_called_once_with("testkey", "modified high value")