Debian Codegraph @

docs/integration.md

raw symbols SCIP
1 Title: Integrating GAsyncResult
2
3 Historically if you wanted to do asynchronous work in GObject-based applications you would use [[email protected]] and [[email protected]].
4
5 There are two ways to integrate with this form of asynchronous API.
6 In one direction, you can consume this historical API and provide the result as a [[email protected]].
7 In the other direction, you can provide this API in your application or library but implement it behind the scenes with [[email protected]].
8
9 # Converting GAsyncResult to Futures
10
11 A typical case to integrate, at least initially, is to extract the result of a [[email protected]] and propagate it to a future.
12
13 One way to do that is with a [[email protected]] which will resolve or reject from your async callback.
14
15 ```c
16 static void
17 my_callback (GObject *object,
18 GAsyncResult *result,
19 gpointer user_data)
20 {
21 g_autoptr(DexPromise) promise = user_data;
22 g_autoptr(GError) error = NULL;
23
24 if (thing_finish (THING (object), result, &error))
25 dex_promise_resolve_boolean (promise, TRUE);
26 else
27 dex_promise_reject (promise, g_steal_pointer (&error));
28 }
29
30 static DexFuture *
31 my_wrapper (Thing *thing)
32 {
33 DexPromise *promise = dex_promise_new_cancellable ();
34
35 thing_async (thing,
36 dex_promise_get_cancellable (promise),
37 my_callback,
38 dex_ref (promise));
39
40 return DEX_FUTURE (promise);
41 }
42 ```
43
44 # Implementing AsyncResult with Futures
45
46 In some cases you may not want to "leak" into your API that you are using [[email protected]].
47 For example, you may want to only expose a traditional GIO API or maybe even rewriting legacy code.
48
49 For these cases use [[email protected]].
50 It is designed to feel familiar to those that have used [[email protected]].
51
52 [[email protected]] allows taking the typical `cancellable`, `callback`, and `user_data` parameters similar to [[email protected]].
53
54 Then call [[email protected]] providing the future that will resolve or reject with error.
55 One completed, the users provided `callback` will be executed within the active scheduler at time of creation.
56
57 From your finish function, call the appropriate propagate API such as [[email protected]_int].
58
59 ```c
60 void
61 thing_async (Thing *thing,
62 GCancellable *cancellable,
63 GAsyncReadyCallback callback,
64 gpointer user_data)
65 {
66 g_autoptr(DexAsyncResult) result = NULL;
67
68 result = dex_async_result_new (thing, cancellable, callback, user_data);
69 dex_async_result_await (result, dex_future_new_true ());
70 }
71
72 gboolean
73 thing_finish (Thing *thing,
74 GAsyncResult *result,
75 GError **error)
76 {
77 return dex_async_result_propagate_boolean (DEX_ASYNC_RESULT (result), error);
78 }
79 ```
80
81 # Safety Notes
82
83 One thing that Libdex handles better than `GTask` is ensuring that your `user_data` is destroyed on the proper thread.
84 The design of [[email protected]] was done in such a way that both the result and `user_data` are passed back to the owning thread at the same time.
85 This ensures that your `user_data` will never be finalized on the wrong thread.