Created
October 22, 2015 16:15
-
-
Save loopingz/76c1788a4af068113442 to your computer and use it in GitHub Desktop.
Qt - WebKit - Promise implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_promises = []; | |
promise_success = function(uid, result) { | |
if (_promises[uid] == undefined || _promises[uid]._success_callback == undefined) { | |
return; | |
} | |
_promises[uid]._success_callback(result); | |
delete _promises[uid]; | |
} | |
promise_error = function(uid, err) { | |
if (_promises[uid] == undefined || _promises[uid]._error_callback == undefined) { | |
return; | |
} | |
_promises[uid]._error_callback(err); | |
delete _promises[uid]; | |
} | |
when = function(result) { | |
return { | |
then: function(callback, error_callback) { | |
if (result && result['_promise_success'] !== undefined && result['_promise_error'] !== undefined) { | |
uid = result['_promise_uid'](); | |
if (_promises[uid] == undefined) { | |
console.log("set the promises for " + uid); | |
_promises[uid] = self | |
} | |
_promises[uid]._success_callback = callback; | |
_promises[uid]._error_callback = error_callback; | |
result._promise_success.connect(promise_success); | |
result._promise_error.connect(promise_error); | |
result.start(); | |
} else { | |
callback(result); | |
} | |
} | |
}; | |
} | |
// Usage of the Promise | |
when( drive.update_password_async(uid, password) ).then( | |
function (res) { | |
if (res == "") { | |
$scope.update_password_error = null; | |
$scope.engines = this.getEngines(); | |
// Waiting for better solution | |
top.location.reload(); | |
} else { | |
$scope.update_password_error = res; | |
$scope.password = ""; | |
} | |
$scope.currentAction = ""; | |
$scope.$apply(); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api(QObject) | |
... | |
@QtCore.pyqtSlot(str, str, result=str) | |
def update_password(self, uid, password): | |
return self._update_password(uid, password) | |
... | |
class Promise(Worker): | |
_promise_success = QtCore.pyqtSignal(str, str) | |
_promise_error = QtCore.pyqtSignal(str, str) | |
def __init__(self, runner, *args, **kwargs): | |
self._uid = uuid.uuid1().hex | |
self._runner = runner | |
self._kwargs = kwargs | |
self._args = args | |
super(Promise, self).__init__(name="Promise_" + self._uid) | |
self._result = None | |
@QtCore.pyqtSlot(result=str) | |
def _promise_uid(self): | |
return self._uid | |
@QtCore.pyqtSlot() | |
def start(self): | |
self._thread.started.connect(self.run) | |
self._thread.start() | |
def _execute(self): | |
try: | |
result = self._runner(*self._args, **self._kwargs) | |
self._promise_success.emit(self._uid, result) | |
except Exception as e: | |
self._promise_error.emit(self._uid, repr(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api(QObject) | |
# ... | |
@QtCore.pyqtSlot(str, str, result=str) | |
def update_password(self, uid, password): | |
return self._update_password(uid, password) | |
@QtCore.pyqtSlot(str, str, result=QtCore.QObject) | |
def update_password_async(self, uid, password): | |
return Promise(self._update_password, uid, password) | |
# ... | |
class PromiseWrapper(QtCore.QObject): | |
def __init__(self, promise): | |
super(PromiseWrapper, self).__init__() | |
self._promise = promise | |
@QtCore.pyqtSlot() | |
def run(self): | |
self._promise.run() | |
class Promise(Worker): | |
_promise_success = QtCore.pyqtSignal(str, str) | |
_promise_error = QtCore.pyqtSignal(str, str) | |
def __init__(self, runner, *args, **kwargs): | |
self._uid = uuid.uuid1().hex | |
self._runner = runner | |
self._kwargs = kwargs | |
self._args = args | |
super(Promise, self).__init__(name="Promise_" + self._uid) | |
self._result = None | |
self._wrapper = PromiseWrapper(self) | |
self._wrapper.moveToThread(self._thread) | |
def moveToThread(self, QThread): | |
# Prevent this object to be move to other threads | |
pass | |
@QtCore.pyqtSlot(result=str) | |
def _promise_uid(self): | |
return self._uid | |
@QtCore.pyqtSlot() | |
def start(self): | |
self._thread.started.connect(self._wrapper.run) | |
self._thread.start() | |
def _execute(self): | |
try: | |
result = self._runner(*self._args, **self._kwargs) | |
self._promise_success.emit(self._uid, result) | |
except Exception as e: | |
self._promise_error.emit(self._uid, repr(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the examples. But how should the Worker class look like? Just like this one: https://github.com/nuxeo/nuxeo-drive/blob/0737527b7d75d5c5b5e6047e5e6c01e0c9f6548e/nuxeo-drive-client/nxdrive/engine/workers.py#L26 ?