"""
Small example to demonstrate properties:
"""

from flexx import event


class Example(event.Component):
    
    foo = event.AnyProp(settable=True, doc='This can be anything.')
    bar = event.IntProp(10, doc='This is an int.')
    eggs = event.TupleProp(settable=True)
    
    @event.action
    def raise_the_bar(self):
        """ Action to change the value of bar. """
        self._mutate_bar(self.bar + 1)

    @event.reaction
    def _report(self):
        # This gets automatically called when any of the used properties change
        print('foo is', self.foo)
        print('bar is', self.bar)
        print('eggs is', self.eggs)


c = Example(foo=42)  # This invokes set_foo(42)

# Use explicit action to modify bar
c.raise_the_bar()
c.raise_the_bar()

# Use autogenerated action to mutate eggs
# The value is validated and converted to tuple. (In JavaScript the value would
# still be an array, but with some of its methods disabled.)
c.set_eggs([3, 4, 5])

# c.set_bar(100) -> set_bar does not exist, because we did not mark it as settabe

event.loop.iter()
