Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0384.py @ 3672:e4054b648111
plugin XEP-0384: fix calls outside of main thread:
`callFromThread` is used instead of `callLater` with a 0 delay, as the later is not made
to be called outside of main thread (which was resulting in huge and random delays during
calls).
fix `OmemoStorage._callMainThread` which was returning wrong Deferred due a late lambda
evaluation.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 08 Sep 2021 17:58:48 +0200 |
parents | edc79cefe968 |
children | 09f5ac48ffe3 |
comparison
equal
deleted
inserted
replaced
3671:9c50d2f812c1 | 3672:e4054b648111 |
---|---|
113 @param promise_(promise.Promise): promise to convert | 113 @param promise_(promise.Promise): promise to convert |
114 @return (defer.Deferred): deferred instance linked to the promise | 114 @return (defer.Deferred): deferred instance linked to the promise |
115 """ | 115 """ |
116 d = defer.Deferred() | 116 d = defer.Deferred() |
117 promise_.then( | 117 promise_.then( |
118 lambda result: reactor.callLater(0, d.callback, result), | 118 lambda result: reactor.callFromThread(d.callback, result), |
119 lambda exc: reactor.callLater(0, d.errback, exc) | 119 lambda exc: reactor.callFromThread(d.errback, exc) |
120 ) | 120 ) |
121 return d | 121 return d |
122 | 122 |
123 | 123 |
124 class OmemoStorage(omemo.Storage): | 124 class OmemoStorage(omemo.Storage): |
143 """ | 143 """ |
144 deferred.addCallback(partial(callback, True)) | 144 deferred.addCallback(partial(callback, True)) |
145 deferred.addErrback(partial(callback, False)) | 145 deferred.addErrback(partial(callback, False)) |
146 | 146 |
147 def _callMainThread(self, callback, method, *args, check_jid=None): | 147 def _callMainThread(self, callback, method, *args, check_jid=None): |
148 d = method(*args) | 148 if check_jid is None: |
149 if check_jid is not None: | 149 d = method(*args) |
150 else: | |
150 check_jid_d = self._checkJid(check_jid) | 151 check_jid_d = self._checkJid(check_jid) |
151 check_jid_d.addCallback(lambda __: d) | 152 check_jid_d.addCallback(lambda __: method(*args)) |
152 d = check_jid_d | 153 d = check_jid_d |
154 | |
153 if callback is not None: | 155 if callback is not None: |
154 d.addCallback(partial(callback, True)) | 156 d.addCallback(partial(callback, True)) |
155 d.addErrback(partial(callback, False)) | 157 d.addErrback(partial(callback, False)) |
156 | 158 |
157 def _call(self, callback, method, *args, check_jid=None): | 159 def _call(self, callback, method, *args, check_jid=None): |
158 """Create Deferred and add Promise callback to it | 160 """Create Deferred and add Promise callback to it |
159 | 161 |
160 This method use reactor.callLater to launch Deferred in main thread | 162 This method use reactor.callLater to launch Deferred in main thread |
161 @param check_jid: run self._checkJid before method | 163 @param check_jid: run self._checkJid before method |
162 """ | 164 """ |
163 reactor.callLater( | 165 reactor.callFromThread( |
164 0, self._callMainThread, callback, method, *args, check_jid=check_jid | 166 self._callMainThread, callback, method, *args, check_jid=check_jid |
165 ) | 167 ) |
166 | 168 |
167 def _checkJid(self, bare_jid): | 169 def _checkJid(self, bare_jid): |
168 """Check if jid is known, and store it if not | 170 """Check if jid is known, and store it if not |
169 | 171 |
286 if callback is not None: | 288 if callback is not None: |
287 self.setCb(d, callback) | 289 self.setCb(d, callback) |
288 | 290 |
289 def deleteJID(self, callback, bare_jid): | 291 def deleteJID(self, callback, bare_jid): |
290 """Retrieve all (in)actives devices of bare_jid, and delete all related keys""" | 292 """Retrieve all (in)actives devices of bare_jid, and delete all related keys""" |
291 reactor.callLater(0, self._deleteJID, callback, bare_jid) | 293 reactor.callFromThread(self._deleteJID, callback, bare_jid) |
292 | 294 |
293 | 295 |
294 class SatOTPKPolicy(omemo.DefaultOTPKPolicy): | 296 class SatOTPKPolicy(omemo.DefaultOTPKPolicy): |
295 pass | 297 pass |
296 | 298 |