Mercurial > libervia-backend
changeset 806:5d6c45d6ee1b
core: added "one_shot" option to registered callback:
when this keyword is used, the callback is automatically removed once it has been called.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 04 Feb 2014 18:24:27 +0100 |
parents | 7c05c39156a2 |
children | be4c5e24dab9 |
files | src/core/sat_main.py |
diffstat | 1 files changed, 14 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/core/sat_main.py Tue Feb 04 18:21:51 2014 +0100 +++ b/src/core/sat_main.py Tue Feb 04 18:24:27 2014 +0100 @@ -847,6 +847,7 @@ """ Register a callback. Use with_data=True in kwargs if the callback use the optional data dict use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible + use one_shot=True to delete callback once it have been called @param callback: any callable @return: id of the registered callback """ @@ -857,11 +858,21 @@ if callback_id in self._cb_map: raise exceptions.ConflictError(_(u"id already registered")) self._cb_map[callback_id] = (callback, args, kwargs) + + if "one_shot" in kwargs: # One Shot callback are removed after 30 min + def purgeCallback(): + try: + self.removeCallback(callback_id) + except KeyError: + pass + reactor.callLater(1800, purgeCallback) + return callback_id def removeCallback(self, callback_id): """ Remove a previously registered callback @param callback_id: id returned by [registerCallback] """ + debug("Removing callback [%s]" % callback_id) del self._cb_map[callback_id] def launchCallback(self, callback_id, data=None, profile_key="@NONE@"): @@ -889,6 +900,9 @@ kwargs["profile"] = profile del kwargs["with_data"] + if kwargs.pop('one_shot', False): + self.removeCallback(callback_id) + return defer.maybeDeferred(callback, *args, **kwargs) #Menus management