|
| 1 | +package com.ably.chat |
| 2 | + |
| 3 | +import io.ably.lib.types.ErrorInfo |
| 4 | + |
| 5 | +/** |
| 6 | + * Default timeout for transient states before we attempt handle them as a state change. |
| 7 | + */ |
| 8 | +const val TRANSIENT_TIMEOUT = 5000 |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents a connection to Ably. |
| 12 | + */ |
| 13 | +interface ConnectionStatus { |
| 14 | + /** |
| 15 | + * The current status of the connection. |
| 16 | + */ |
| 17 | + val current: ConnectionLifecycle |
| 18 | + |
| 19 | + /** |
| 20 | + * The current error, if any, that caused the connection to enter the current status. |
| 21 | + */ |
| 22 | + val error: ErrorInfo? |
| 23 | + |
| 24 | + /** |
| 25 | + * Registers a listener that will be called whenever the connection status changes. |
| 26 | + * @param listener The function to call when the status changes. |
| 27 | + */ |
| 28 | + fun on(listener: Listener) |
| 29 | + |
| 30 | + /** |
| 31 | + * Unregisters a listener |
| 32 | + * @param listener The function to call when the status changes. |
| 33 | + */ |
| 34 | + fun off(listener: Listener) |
| 35 | + |
| 36 | + /** |
| 37 | + * An interface for listening to changes for the connection status |
| 38 | + */ |
| 39 | + fun interface Listener { |
| 40 | + /** |
| 41 | + * A function that can be called when the connection status changes. |
| 42 | + * @param change The change in status. |
| 43 | + */ |
| 44 | + fun connectionStatusChanged(change: ConnectionStatusChange) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * The different states that the connection can be in through its lifecycle. |
| 50 | + */ |
| 51 | +enum class ConnectionLifecycle(val stateName: String) { |
| 52 | + /** |
| 53 | + * A temporary state for when the library is first initialized. |
| 54 | + */ |
| 55 | + Initialized("initialized"), |
| 56 | + |
| 57 | + /** |
| 58 | + * The library is currently connecting to Ably. |
| 59 | + */ |
| 60 | + Connecting("connecting"), |
| 61 | + |
| 62 | + /** |
| 63 | + * The library is currently connected to Ably. |
| 64 | + */ |
| 65 | + Connected("connected"), |
| 66 | + |
| 67 | + /** |
| 68 | + * The library is currently disconnected from Ably, but will attempt to reconnect. |
| 69 | + */ |
| 70 | + Disconnected("disconnected"), |
| 71 | + |
| 72 | + /** |
| 73 | + * The library is in an extended state of disconnection, but will attempt to reconnect. |
| 74 | + */ |
| 75 | + Suspended("suspended"), |
| 76 | + |
| 77 | + /** |
| 78 | + * The library is currently disconnected from Ably and will not attempt to reconnect. |
| 79 | + */ |
| 80 | + Failed("failed"), |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Represents a change in the status of the connection. |
| 85 | + */ |
| 86 | +data class ConnectionStatusChange( |
| 87 | + /** |
| 88 | + * The new status of the connection. |
| 89 | + */ |
| 90 | + val current: ConnectionLifecycle, |
| 91 | + |
| 92 | + /** |
| 93 | + * The previous status of the connection. |
| 94 | + */ |
| 95 | + val previous: ConnectionLifecycle, |
| 96 | + |
| 97 | + /** |
| 98 | + * An error that provides a reason why the connection has |
| 99 | + * entered the new status, if applicable. |
| 100 | + */ |
| 101 | + val error: ErrorInfo?, |
| 102 | + |
| 103 | + /** |
| 104 | + * The time in milliseconds that the client will wait before attempting to reconnect. |
| 105 | + */ |
| 106 | + val retryIn: Long?, |
| 107 | +) |
0 commit comments