-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
library/src/commonMain/kotlin/com/tidal/networktime/internal/MutableState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.tidal.networktime.internal | ||
|
||
import kotlinx.coroutines.Job | ||
|
||
internal class MutableState(var job: Job? = null) |
25 changes: 25 additions & 0 deletions
25
library/src/commonMain/kotlin/com/tidal/networktime/internal/OperationCoordinator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.tidal.networktime.internal | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
internal class OperationCoordinator | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
constructor( | ||
private val mutableState: MutableState, | ||
private val coroutineScope: CoroutineScope, | ||
globalDispatcher: CoroutineDispatcher, | ||
private val privateDispatcher: CoroutineDispatcher = globalDispatcher.limitedParallelism(1), | ||
) { | ||
|
||
fun dispatchStartSync() = dispatch(StartSyncRunnable(mutableState)) | ||
|
||
fun dispatchStopSync() = dispatch(StopSyncRunnable(mutableState)) | ||
|
||
private fun dispatch(block: () -> Unit) = coroutineScope.launch { | ||
withContext(privateDispatcher) { block() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
library/src/commonMain/kotlin/com/tidal/networktime/internal/StartSyncRunnable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tidal.networktime.internal | ||
|
||
internal class StartSyncRunnable(private val mutableState: MutableState) : () -> Unit { | ||
override operator fun invoke() = mutableState.run { | ||
if (job != null) { | ||
return | ||
} | ||
job = TODO() | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
library/src/commonMain/kotlin/com/tidal/networktime/internal/StopSyncRunnable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.tidal.networktime.internal | ||
|
||
internal class StopSyncRunnable(private val mutableState: MutableState) : () -> Unit { | ||
override operator fun invoke() = mutableState.run { | ||
job?.cancel() ?: return | ||
job = null | ||
} | ||
} |