Skip to content

Commit

Permalink
new one shot support for observer in appier
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 14, 2015
1 parent 8eb5f3d commit d6abc39
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/appier/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def name_f(cls, name):
return name_f

@classmethod
def bind_g(cls, name, method):
def bind_g(cls, name, method, oneshot = False):
if oneshot: method.oneshot = oneshot
name_f = cls.name_f(name)
methods = cls._events_g.get(name_f, [])
methods.append(method)
Expand All @@ -86,17 +87,26 @@ def unbind_g(cls, name, method = None):

@classmethod
def trigger_g(cls, name, *args, **kwargs):
oneshots = None
name_f = cls.name_f(name)
methods = cls._events_g.get(name_f, [])
for method in methods: method(*args, **kwargs)
for method in methods:
method(*args, **kwargs)
if not hasattr(method, "oneshot"): continue
if not method.oneshot: continue
oneshots = [] if oneshots == None else oneshots
oneshots.append(method)
if not oneshots: return
for oneshot in oneshots: cls.unbind_g(name, oneshot)

def build(self):
pass

def destroy(self):
self.unbind_all()

def bind(self, name, method):
def bind(self, name, method, oneshot = False):
if oneshot: method.oneshot = oneshot
methods = self._events.get(name, [])
methods.append(method)
self._events[name] = methods
Expand All @@ -116,6 +126,14 @@ def trigger(self, name, *args, **kwargs):
self.trigger_l(name, cls, *args, **kwargs)

def trigger_l(self, name, level, *args, **kwargs):
oneshots = None
methods = self._events.get(name, [])
for method in methods: method(*args, **kwargs)
for method in methods:
method(*args, **kwargs)
if not hasattr(method, "oneshot"): continue
if not method.oneshot: continue
oneshots = [] if oneshots == None else oneshots
oneshots.append(method)
level.trigger_g(name, self, *args, **kwargs)
if not oneshots: return
for oneshot in oneshots: self.unbind(name, oneshot)

0 comments on commit d6abc39

Please sign in to comment.