|
| 1 | +package io.ably.lib.`object` |
| 2 | + |
| 3 | +import io.ably.lib.util.Log |
| 4 | +import kotlinx.coroutines.CoroutineName |
| 5 | +import kotlinx.coroutines.CoroutineScope |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.SupervisorJob |
| 8 | +import io.ably.lib.types.AblyException |
| 9 | +import java.util.concurrent.CompletableFuture |
| 10 | +import java.util.concurrent.ConcurrentHashMap |
| 11 | +import java.util.concurrent.CopyOnWriteArrayList |
| 12 | +import kotlinx.coroutines.launch |
| 13 | + |
| 14 | +/** |
| 15 | + * The single abstract seam between this package and the realtime objects |
| 16 | + * system. The path/instance implementation classes depend ONLY on this |
| 17 | + * contract; a bridge (implemented outside this package, alongside the |
| 18 | + * realtime internals) provides the graph views, preconditions, publishing and |
| 19 | + * update fan-in. This keeps `io.ably.lib.object` free of any dependency on |
| 20 | + * `io.ably.lib.objects`. |
| 21 | + */ |
| 22 | +internal abstract class ObjectsBridge { |
| 23 | + |
| 24 | + /** The channel this objects instance belongs to (used for PAOM2e/PAOM3b). */ |
| 25 | + internal abstract val channelName: String |
| 26 | + |
| 27 | + /** The root InternalLiveMap view (objectId `root`), or null if unavailable. Spec: RTO3, RTPO2b */ |
| 28 | + internal abstract fun getRootNode(): MapNode? |
| 29 | + |
| 30 | + /** Looks up a non-tombstoned object view by id, or null. Spec: RTO3a */ |
| 31 | + internal abstract fun getNode(objectId: String): ObjectsNode? |
| 32 | + |
| 33 | + /** Access API preconditions; throws ErrorInfo-carrying AblyException on failure. Spec: RTO25 */ |
| 34 | + internal abstract fun throwIfInvalidAccessApiConfiguration() |
| 35 | + |
| 36 | + /** Write API preconditions; throws ErrorInfo-carrying AblyException on failure. Spec: RTO26 */ |
| 37 | + internal abstract fun throwIfInvalidWriteApiConfiguration() |
| 38 | + |
| 39 | + /** Publishes the messages and applies them locally on ACK. Spec: RTO15, RTO20 */ |
| 40 | + internal abstract suspend fun publish(messages: List<WireObjectMessage>) |
| 41 | + |
| 42 | + /** Current server time in epoch milliseconds. Spec: RTO16 */ |
| 43 | + internal abstract suspend fun getServerTime(): Long |
| 44 | + |
| 45 | + /** Ensures the channel is attached and objects are SYNCED. Spec: RTO23b, RTO23c, RTO23e */ |
| 46 | + internal abstract suspend fun ensureAttachedAndSynced() |
| 47 | + |
| 48 | + /** |
| 49 | + * Registry for path-based subscriptions (RTPO19). Bridge implementations |
| 50 | + * feed it via [notifyUpdated]. |
| 51 | + */ |
| 52 | + internal val pathSubscriptionRegister = PathObjectSubscriptionRegister(this) |
| 53 | + |
| 54 | + /** Scope used to expose suspend write operations as CompletableFutures. */ |
| 55 | + private val asyncScope = |
| 56 | + CoroutineScope(Dispatchers.Default + CoroutineName("ObjectsBridge") + SupervisorJob()) |
| 57 | + |
| 58 | + /** Per-object message-carrying update listeners (instance subscriptions, RTINS16). */ |
| 59 | + private val updateListeners = |
| 60 | + ConcurrentHashMap<String, CopyOnWriteArrayList<(Set<String>, WireObjectMessage?) -> Unit>>() |
| 61 | + |
| 62 | + /** |
| 63 | + * Subscribes to updates applied to the object with [objectId]. The listener |
| 64 | + * receives the set of updated map keys (empty for counters) and the source |
| 65 | + * message when the update originated from an operation. Returns an |
| 66 | + * unsubscribe handle. |
| 67 | + */ |
| 68 | + internal fun subscribeToUpdates(objectId: String, listener: (Set<String>, WireObjectMessage?) -> Unit): () -> Unit { |
| 69 | + val listeners = updateListeners.computeIfAbsent(objectId) { CopyOnWriteArrayList() } |
| 70 | + listeners.add(listener) |
| 71 | + return { listeners.remove(listener) } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Entry point for bridge implementations: call after an update has been |
| 76 | + * applied to an object, with the keys that changed (empty for counters) and |
| 77 | + * the source ObjectMessage when the update came from an operation (null for |
| 78 | + * sync-induced changes). Fans out to instance subscriptions (RTINS16) and |
| 79 | + * path subscriptions (RTPO19). |
| 80 | + */ |
| 81 | + internal fun notifyUpdated(objectId: String, updatedKeys: Set<String>, message: WireObjectMessage?) { |
| 82 | + updateListeners[objectId]?.forEach { listener -> |
| 83 | + try { |
| 84 | + listener(updatedKeys, message) |
| 85 | + } catch (t: Throwable) { |
| 86 | + Log.e("ObjectsBridge", "Error in update listener for objectId=$objectId", t) |
| 87 | + } |
| 88 | + } |
| 89 | + pathSubscriptionRegister.notifyObjectUpdated(objectId, updatedKeys, message) |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Runs a suspend write and exposes it as a CompletableFuture<Void>; |
| 94 | + * failures complete exceptionally with the underlying AblyException. |
| 95 | + */ |
| 96 | + internal fun launchWithVoidFuture(block: suspend () -> Unit): CompletableFuture<Void> { |
| 97 | + val future = CompletableFuture<Void>() |
| 98 | + asyncScope.launch { |
| 99 | + try { |
| 100 | + block() |
| 101 | + future.complete(null) |
| 102 | + } catch (throwable: Throwable) { |
| 103 | + when (throwable) { |
| 104 | + is AblyException -> future.completeExceptionally(throwable) |
| 105 | + else -> future.completeExceptionally( |
| 106 | + objectsException("Error executing operation", ObjectErrorCode.BadRequest, cause = throwable) |
| 107 | + ) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return future |
| 112 | + } |
| 113 | +} |
0 commit comments