@@ -6,58 +6,54 @@ package kotlinx.coroutines
6
6
7
7
import kotlinx.coroutines.internal.*
8
8
import kotlinx.coroutines.intrinsics.*
9
- import kotlin.coroutines.intrinsics.*
10
9
import kotlin.coroutines.*
10
+ import kotlin.coroutines.intrinsics.*
11
11
12
12
/* *
13
13
* Defines a scope for new coroutines. Every coroutine builder
14
14
* is an extension on [CoroutineScope] and inherits its [coroutineContext][CoroutineScope.coroutineContext]
15
15
* to automatically propagate both context elements and cancellation.
16
16
*
17
+ * The best ways to obtain a standalone instance of the scope are [CoroutineScope()] and [MainScope()] factory functions.
18
+ * Additional context elements can be appended to the scope using [plus][CoroutineScope.plus] operator.
19
+ *
20
+ * Manual implementation of this interface is not recommended, implementation by delegation should be preferred instead.
21
+ * By convention, [context of the scope][CoroutineScope.coroutineContext] should contain an instance of a [job][Job] to enforce structured concurrency.
22
+ *
17
23
* Every coroutine builder (like [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc)
18
24
* and every scoping function (like [coroutineScope], [withContext], etc) provides _its own_ scope
19
25
* with its own [Job] instance into the inner block of code it runs.
20
- * By convention, they all wait for all the coroutines inside
21
- * their block to complete before completing themselves, thus enforcing the
22
- * discipline of **structured concurrency**.
26
+ * By convention, they all wait for all the coroutines inside their block to complete before completing themselves,
27
+ * thus enforcing the discipline of **structured concurrency**.
23
28
*
24
- * [CoroutineScope] should be implemented on entities with well-defined lifecycle that are responsible
29
+ * [CoroutineScope] should be implemented (or used as a field) on entities with a well-defined lifecycle that are responsible
25
30
* for launching children coroutines. Example of such entity on Android is Activity.
26
31
* Usage of this interface may look like this:
27
32
*
28
33
* ```
29
- * class MyActivity : AppCompatActivity(), CoroutineScope {
30
- * lateinit var job: Job
31
- * override val coroutineContext: CoroutineContext
32
- * get() = Dispatchers.Main + job
33
- *
34
- * override fun onCreate(savedInstanceState: Bundle?) {
35
- * super.onCreate(savedInstanceState)
36
- * job = Job()
37
- * }
38
- *
34
+ * class MyActivity : AppCompatActivity(), CoroutineScope by MainScope() {
39
35
* override fun onDestroy() {
40
- * super.onDestroy()
41
- * job.cancel() // Cancel job on activity destroy. After destroy all children jobs will be cancelled automatically
36
+ * cancel() // cancel is extension on CoroutineScope
42
37
* }
43
38
*
44
39
* /*
45
40
* * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
46
41
* * in this method throws an exception, then all nested coroutines are cancelled.
47
42
* */
48
43
* fun showSomeData() = launch { // <- extension on current activity, launched in the main thread
49
- * val data = withContext(Dispatchers.IO) {
50
- * // Provides withContext scope that is child of he outer launch scope
51
- * // blocking I/O operation
52
- * }
44
+ * // ... here we can use suspending functions or coroutine builders with other dispatchers
53
45
* draw(data) // draw in the main thread
54
46
* }
55
47
* }
56
48
* ```
57
49
*/
58
50
public interface CoroutineScope {
59
51
/* *
60
- * Context of this scope.
52
+ * The context of this scope.
53
+ * Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope.
54
+ * Accessing this property in general code is not recommended for any purposes except accessing [Job] instance for advanced usages.
55
+ *
56
+ * By convention, should contain an instance of a [job][Job] to enforce structured concurrency.
61
57
*/
62
58
public val coroutineContext: CoroutineContext
63
59
}
0 commit comments