-
Notifications
You must be signed in to change notification settings - Fork 35
/
Reference.kt
45 lines (40 loc) · 1.65 KB
/
Reference.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright 2020 Google LLC.
*
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
*
* Code distributed by Google as part of this project is also subject to an additional IP rights
* grant found at
* http://polymer.github.io/PATENTS.txt
*/
package arcs.core.data
import arcs.core.common.Referencable
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
/**
* A reference to an [Entity] of type [T].
*
* References may be "alive" or "dead".
*
* * An "alive" [Reference] is one where the [Referencable] it refers to is still accessible via Arcs
* storage.
* * Conversely, a "dead" [Reference] is one whose [Referencable] has been removed from storage by
* some means. For example: the [Referencable]'s time-to-live could have elapsed.
*
* Developers can check the liveness of a [Reference] using either [isAlive] or [isDead].
*/
interface Reference<T : Referencable> : arcs.core.crdt.CrdtEntity.Reference {
/**
* Fetches the actual [Entity] value being referenced from storage.
*
* Returns `null` if this [Reference] is no longer alive.
*/
suspend fun dereference(coroutineContext: CoroutineContext = Dispatchers.IO): T?
/** Returns whether or not the [Entity] being referenced still exists. */
suspend fun isAlive(coroutineContext: CoroutineContext = Dispatchers.IO): Boolean =
dereference(coroutineContext) != null
/** Returns whether or not the [Entity] being referenced has been removed from storage. */
suspend fun isDead(coroutineContext: CoroutineContext = Dispatchers.IO): Boolean =
!isAlive(coroutineContext)
}