• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language QML
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Quick Promise - QML Promise Library
Promises/A+ logo

Quick Promise - QML Promise Library

Build Status

The Promise object is widely used for deferred and asynchronous computation in Javascript Application. "Quick Promise” is a library that provides Promise object in a QML way. It comes with a Promise component with signal and property. It could be resolved via a binary expression, another promise object, then trigger your callback by QueuedConnection.

Moreover, it also provides Promise as a Javascript object that don’t need to declare in QML way. The API is fully compliant with Promises/A+ specification with all the test cases passed and therefore it just works like many other Promise solutions for Javascript application.

Example:

import QtQuick 2.0
import QuickPromise 1.0

Item {
    id: item
    opacity: 0

    Image {
        id: image
        asynchronous: true
        source: "https://lh3.googleusercontent.com/_yimBU8uLUTqQQ9gl5vODqDufdZ_cJEiKnx6O6uNkX6K9lT63MReAWiEonkAVatiPxvWQu7GDs8=s640-h400-e365-rw";
    }

    Timer {
        id: timer
        interval: 3000
    }

    Promise {
        // Resolve when the image is ready
        resolveWhen: image.status === Image.Ready

        // Reject when time out
        rejectWhen: timer.triggered

        onFulfilled:  {
            // If the timer is reached, the image will not be shown even it is ready.
            item.opacity = 1;
        }
    }
}

The code above demonstrated how Promise component could be used in asynchronous workflow for QML application. The resolveWhen property accepts a boolean expression, another Promise object and signal. Once the result of expression becomes truth, it will trigger the “onFulfilled” slot via QueuedConnection. The slot will be executed for once only.

Remarks: The QML Promise component is not fully compliant with Promises/A+ specification.

Related articles:

  1. JavaScript Promises with ArcGIS Runtime SDK for Qt | GeoNet

Feature List

  1. Promise in a QML way
  2. Trigger resolve()/reject() via binary expression, signal from resloveWhen / rejectWhen property
  3. isFulfilled / isRejected / isSettled properties for data binding.
  4. fulfulled , rejected , settled signals
  5. Promise in a Javascript way
  6. Unlike QML component, it don’t need to declare before use it.
  7. It is fully compatible with Promises/A+ specification. It is easy to get started.
  8. Extra API
  9. Q.setTimeout() - An implementation of setTimeout() function for QML.
  10. all()/allSettled() - Create a promise object from an array of promises

Installation Instruction (using qpm)

For user who are already using qpm from qpm.io

  1. Run qpm install
$ qpm install com.github.benlau.quickpromise
  1. Include vendor/vendor.pri in your .pro file

You may skip this step if you are already using qpm.

include(vendor/vendor.pri)
  1. Add "qrc://" to your QML import path
engine.addImportPath("qrc:///"); // QQmlEngine
  1. Add import statement in your QML file
import QuickPromise 1.0

Installation Instruction (without qpm)

  1. Clone this repository or download release to a folder within your source tree.

  2. Add this line to your profile file(.pro):

    include(quickpromise/quickpromise.pri) # You should modify the path by yourself

  3. Add "qrc://" to your QML import path

engine.addImportPath("qrc:///"); // QQmlEngine
  1. Add import statement in your QML file
import QuickPromise 1.0

Minimal Installation

Since v1.0.8, the C++ setTimeout function is deprecated and therefore it becomes a pure JavaScript library. You may copy the content of QuickPromise folder to your application.

In case you only need the JavaScript's promise. You only need to copy two files to your source code. They are

QuickPromise/promise.js
QuickPromise/PromiseTimer.qml

Usage

import "./promise.js" as Q

// ...

var promise = new Q.Promise(function(resolve, reject) {
    // ...
});

Reference: Q.promise()

What is Promise and how to use it?

Please refer to this site for core concept: Promises

Promise QML Component

Promise {
    property bool isFulfilled

    property bool isRejected

    property bool isSettled: isFulfilled || isRejected

    /// An expression that will trigger resolve() if the value become true or another promise object got resolved.
    property var resolveWhen

    /// An expression that will trigger reject() if the value become true. Don't assign another promise object here.
    property var rejectWhen

    signal fulfilled(var value)

    signal rejected(var reason)

    signal settled(var value)

    function setTimeout(func, timeout) { ... }

    function then(onFulfilled, onRejected) { ... }

    function resolve(value) { ... }

    function reject(reason) { ... }

    function all(promises) { ... }

    function allSettled(promises) { ... }
}

isFullfilled

It is true if resolve() has been called on that promise object

isRejected

It is true if reject() has been called on that promise object

isSettled

It is true if either of isFullfilled or isRejected has been set.

resolveWhen

resolveWhen property is an alternative method to call resolve() in a QML way. You may bind a binary expression, another promise, signal to the "resolveWhen" property. That will trigger the resolve() depend on its type and value.

resolveWhen: binary expression

Once the expression become true, it will trigger resolve(true).

resolveWhen: signal

Listen the signal, once it is triggered, it will call resolve().

Example:

Timer {
    id: timer
    repeat: true
    interval: 50
}

Promise {
    id: promise
    resolveWhen: timer.onTriggered
}

resolveWhen: promise

It is equivalent to resolve(promise). It will adopt the state from the input promise object.

Let x be the input promise.

If x is fulfilled, call resolve().

If x is rejected, call reject().

If x is not settled, listen its state change. Once it is fulfilled/rejected, repeat the above steps.

rejectWhen

rejectWhen property is an alternative method to call reject() in a QML way. You may bind a binary expression, signal to this property. It may trigger the reject() depend on its type and value.

Remarks: rejectWhen can not take promise as parameter.

rejectWhen: binary expression

Once the expression become true, it will trigger reject(true).

rejectWhen: signal

Listen the signal, once it is triggered, it will call reject().

Q.promise()

Q.promise(executor) is the creator function of Promise object in a Javascript way. You won't need to declare a QML component before use it. As it is fully compliant with Promise/A+ specification, it is very easy to get started. But it don't support property binding (resolveWhen , rejectWhen) like the Promise QML component.

However, you may still pass a signal object to resolve()/reject(). In this case, the promise will not change its state until the signal is triggered. If multiple signal call are made, the first signal call takes precedence, and any further calls are ignored.

But it don't support to resolve by the result of Qt.binding(). It will just throw exception. In this case, you should use a QML Promise and pass it to resolveWhen property.

Remarks: Q.promise(executor) is equivalent to new Q.Promise(executor).

API

Q.Promise(executor)
Q.Promise.prototype.then = function(onFulfilled,onRejected) { ... }
Q.Promise.resolve = function(result) { ... }
Q.Promise.reject = function(reason) { ... }
Q.Promise.all = function(promises) { }
Q.Promise.allSettled = function(promises) { }

Instruction of using then/resolve/reject: Promise

Example Code

Extra API

Q.setTimeout(func, milliseconds)

The setTimeout() method will wait the specified number of milliseconds, and then execute the specified function. If the milliseconds is equal to zero, the behaviour will be same as triggering a signal via QueuedConnection.

Q.all(promises)

Given an array of promise / signal , it will create a promise object that will be fulfilled once all the input promises are fulfilled. And it will be rejected if any one of the input promises is rejected.

Q.allSettled(promises)

Given an array of promise / signal , it will create a promise object that will be fulfilled once all the input promises are fulfilled. And it will be rejected if any one of the input promises is rejected. It won't change the state until all the input promises are settled.

Advanced Usage

  1. Resolve by multiple signals.

Promise {
    resolveWhen: Q.all([timer.triggered, loader.loaded]);
}
  1. Resolve by signal and binary expression

Promise {
    resolveWhen: Q.all([timer.triggered, promise2]);

    Promise {
        id: promise2
        resolveWhen: image.status === Image.Ready
    }
}

Related Projects

Libaries

  1. benlau/quickcross - QML Cross Platform Utility Library
  2. benlau/qsyncable - Synchronize data between models
  3. benlau/testable - QML Unit Test Utilities
  4. benlau/quickflux - Message Dispatcher / Queue for Qt/QML
  5. benlau/biginteger - QML BigInteger library
  6. benlau/qtci - A set of scripts to install Qt in Linux command line environment (e.g travis)
  7. benlau/quickfuture - Using QFuture in QML
  8. benlau/fontawesome.pri - Using FontAwesome in QML
  9. benlau/quickandroid - QML Material Design Component and Support Library for Android

Tools

  1. SparkQML - QML Document Viewer for State and Transition Preview

More Repositories

1

quickflux

A Flux implementation for QML
C++
315
star
2

asyncfuture

Use QFuture like a Promise object
C++
241
star
3

quickandroid

QML Theme and Component Library for Android
QML
234
star
4

ihatecontentfarms

Content Farm Blocker Chrome Extension / 封鎖內容農場Chrome插件
JavaScript
187
star
5

quickios

QML Theme and Component Library for iOS
QML
164
star
6

qtci

Collection of scripts to build Qt application in command line environment
Shell
160
star
7

qsyncable

A solution of nested Json List Model
C++
112
star
8

androidnative.pri

Calling Android functions from Qt without using JNI
Java
101
star
9

sparkqml

SparkQML - A QML Document Viewer for State and Transition Preview
JavaScript
100
star
10

nactor

Node.js actor model framework for game
JavaScript
73
star
11

testable

QT/QML Test Runner and Utilities
C++
55
star
12

quickfuture

Using QFuture in QML
C++
51
star
13

fontawesome.pri

Using FontAwesome in QML
QML
51
star
14

quickcross

QML Cross Platform Utility Library
C++
49
star
15

dquest

DQuest - The C++ ORM (Object-relational mapping) for Qt framework.
C++
41
star
16

qtshell

Manipulate files by a shell command style API
C++
31
star
17

qredux

Redux for QML
JavaScript
25
star
18

qtandroidexamplecode

Example code of Qt for Android
QMake
19
star
19

sketchqml

Export to a QML in SketchApp
JavaScript
19
star
20

biginteger

QML BigInteger library
C++
18
star
21

underline

A C++ utility library provides useful functional programming helpers like lodash.js
C++
18
star
22

joplin-plugin-dddot

A Joplin plugin to provide a set of tools like recent notes, shortcuts, scratchpad, and .... in a single sidebar.
TypeScript
17
star
23

ipylivebash

Run shell script in Jupyter with live output
TypeScript
11
star
24

memoinjo

Stick memo on any website
JavaScript
10
star
25

viewstack.pri

ViewStack.pri - A stateless StackView
QML
9
star
26

tinyboy

Tinyboy - A 3D printer designed for education
CSS
8
star
27

quickjsm

A QML Port of Javascript State Machine Library
JavaScript
8
star
28

dualless

Dualless is a poor man's dual monitor solution , a extension for Chrome.
JavaScript
8
star
29

quickflux-project-template

Project template using QuickFlux with unit tests
C++
7
star
30

material2

Light Weight Material Design Components based on QQC2
QML
7
star
31

junkcode

Junkcode for demonstration
QML
7
star
32

qpm-qt-creator-project-template

Qt Creator Project Templates with qpm
C++
6
star
33

hotloader.pri

Hot Reload QML Files
C++
6
star
34

testrunner

Qt Test Runner - A tiny tool to execute multiple QTestLib application and combine the testing result into a summary report.
C++
4
star
35

jupyter-for-devops-demo

A demo project to show how to use Jupyter for DevOps
Jupyter Notebook
4
star
36

joplin-garden-worker

Export notes from Joplin to build a digital garden website
JavaScript
4
star
37

gaescripts

A collection of scripts for GAE application maintenance
Python
3
star
38

qtsdkdocimg

Collection of QtSDK Docker Image Build Scripts
Shell
3
star
39

linne-analyzer

Python
3
star
40

kifier

The missing tool to turn your Web 2.0 application into Wiki
JavaScript
3
star
41

openstandkit

Open Design Portable Standing Desk
3
star
42

qml-rebase-source

A script to update the source attributes within a QML file
JavaScript
2
star
43

snapcat

AngelHack Project
Python
2
star
44

bnloadingpromise

Angular Loading Promise
JavaScript
2
star
45

qtbugstatus

Qt Bug Status Tracker
JavaScript
2
star
46

joplin-plugin-tag-links

Insert tag links to Joplin note
TypeScript
2
star
47

moblin-image-creator.jaunty

Moblin Image Creator for Jaunty
Python
2
star
48

asyncapi-live-reload-demo

Edit asyncapi.yml then generate and reload the HTML doc automatically
2
star
49

sparkqml-website

CSS
1
star
50

sparkqml-builder

Shell
1
star
51

blockwork

A Project in EOS Hackathon
JavaScript
1
star
52

react-redux-webpack-mocha-starter-kit

A starter project for React Redux
JavaScript
1
star
53

l

App Link to Universal Web Link
1
star