From 981a354441d7d1af6024179253a1e33d5562765c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 27 Jul 2020 12:35:59 +0600 Subject: [PATCH] fixes and changes --- postssystem.core.publishing/build.gradle | 54 +++++++++++++++---- .../core/publishing/PublishingRegistrar.kt} | 14 ++--- .../core/publishing/PublishingTrigger.kt | 39 ++++++++++++++ .../publishing/repos/PublishingKeysRepo.kt | 3 -- .../core/publishing/PublishingTrigger.kt | 14 ----- .../core/post/BusinessPublishablePostsAPI.kt | 21 ++++++++ .../postssystem/core/post/api/PostsAPI.kt | 9 +--- .../core/post/api/WritePostsAPI.kt | 5 ++ .../postssystem/core/utils/HandleSafely.kt | 23 ++++++++ 9 files changed, 139 insertions(+), 43 deletions(-) rename postssystem.core.publishing/src/{main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrator.kt => commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrar.kt} (79%) create mode 100644 postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt rename postssystem.core.publishing/src/{main => commonMain}/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt (81%) delete mode 100644 postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt create mode 100644 postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/BusinessPublishablePostsAPI.kt create mode 100644 postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/HandleSafely.kt diff --git a/postssystem.core.publishing/build.gradle b/postssystem.core.publishing/build.gradle index 5373032e..2218c8be 100644 --- a/postssystem.core.publishing/build.gradle +++ b/postssystem.core.publishing/build.gradle @@ -13,14 +13,13 @@ buildscript { } plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" } project.version = "$core_version" project.group = "com.insanusmokrassar" -apply plugin: "java-library" -apply plugin: "kotlin" apply from: "./publish.gradle" repositories { @@ -30,15 +29,48 @@ repositories { maven { url "https://kotlin.bintray.com/kotlinx" } } -dependencies { - api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" +kotlin { + jvm() + js() - if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { - api "com.insanusmokrassar:postssystem.core:$core_version" - } else { - implementation project(":postssystem.core") + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + + if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { + api "com.insanusmokrassar:postssystem.core:$core_version" + } else { + implementation project(":postssystem.core") + } + } + } + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmMain { + dependencies { + implementation kotlin('stdlib-jdk8') + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + } + } + jsMain { + dependencies { + implementation kotlin('stdlib-js') + } + } + jsTest { + dependencies { + implementation kotlin('test-junit-js') + } + } } - - testImplementation "org.jetbrains.kotlin:kotlin-test" - testImplementation "org.jetbrains.kotlin:kotlin-test-junit" } + diff --git a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrator.kt b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrar.kt similarity index 79% rename from postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrator.kt rename to postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrar.kt index 33d408a0..6884eee4 100644 --- a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrator.kt +++ b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingRegistrar.kt @@ -5,16 +5,17 @@ import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow typealias TriggerControlKey = String -interface ReadPublishingRegistrator { +interface ReadPublishingRegistrar { suspend fun getPostIdByTriggerControlKey( key: TriggerControlKey ): PostId? } -interface WritePublishingRegistrator { +interface WritePublishingRegistrar { val unregisteredKeysFlow: Flow suspend fun registerTriggerForPost( @@ -23,14 +24,13 @@ interface WritePublishingRegistrator { ): Boolean } -interface PublishingRegistrator : ReadPublishingRegistrator, WritePublishingRegistrator +interface PublishingRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar -class BusinessPublishingRegistrator( +class BusinessPublishingRegistrar( private val repo: PublishingKeysRepo -) : PublishingRegistrator { +) : PublishingRegistrar { private val unregisteredKeysChannel: BroadcastChannel = BroadcastChannel(Channel.BUFFERED) - override val unregisteredKeysFlow: Flow - get() = TODO("Not yet implemented") + override val unregisteredKeysFlow: Flow = unregisteredKeysChannel.asFlow() override suspend fun getPostIdByTriggerControlKey( key: TriggerControlKey diff --git a/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt new file mode 100644 index 00000000..21e981a7 --- /dev/null +++ b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt @@ -0,0 +1,39 @@ +package com.insanusmokrassar.postssystem.core.publishing + +import com.insanusmokrassar.postssystem.core.post.PostId +import com.insanusmokrassar.postssystem.core.post.RegisteredPost +import com.insanusmokrassar.postssystem.core.post.api.PostsAPI +import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo +import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.* + +interface PublishingTrigger { + val postingTriggeredFlow: Flow + + suspend fun triggerPosting( + triggerControlKey: TriggerControlKey + ): PostId? +} + +class BusinessPublishingTrigger( + private val postsAPI: PostsAPI, + private val publishingKeysRepo: PublishingKeysRepo +) : PublishingTrigger { + private val postingTriggeredChannel: BroadcastChannel = BroadcastChannel(Channel.BUFFERED) + override val postingTriggeredFlow: Flow = postingTriggeredChannel.asFlow() + + override suspend fun triggerPosting(triggerControlKey: TriggerControlKey): PostId? { + val postId = publishingKeysRepo.getPostIdByTriggerControlKey(triggerControlKey) ?: return null + publishingKeysRepo.unsetPostTriggerControlKey(postId) + + return postsAPI.getPostById(postId) ?.let { post -> + if (postsAPI.deletePost(postId)) { + postingTriggeredChannel.send(post) + postId + } else { + null + } + } + } +} diff --git a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt similarity index 81% rename from postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt rename to postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt index ca18657d..02f340be 100644 --- a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt +++ b/postssystem.core.publishing/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishingKeysRepo.kt @@ -2,7 +2,6 @@ package com.insanusmokrassar.postssystem.core.publishing.repos import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.publishing.TriggerControlKey -import kotlinx.coroutines.flow.Flow interface ReadPublishingKeysRepo { suspend fun getPostIdByTriggerControlKey( @@ -14,8 +13,6 @@ interface ReadPublishingKeysRepo { } interface WritePublishingKeysRepo { - val postTriggerControlKeyUpdated: Flow> - val postTriggerControlKeyUnset: Flow suspend fun setPostTriggerControlKey( postId: PostId, key: TriggerControlKey diff --git a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt b/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt deleted file mode 100644 index 43d55206..00000000 --- a/postssystem.core.publishing/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.insanusmokrassar.postssystem.core.publishing - -import com.insanusmokrassar.postssystem.core.post.PostId -import kotlinx.coroutines.flow.Flow - -interface PublishingTrigger { - val postingTriggeredFlow: Flow - - suspend fun triggerPosting( - triggerControlKey: TriggerControlKey - ): PostId? -} - - diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/BusinessPublishablePostsAPI.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/BusinessPublishablePostsAPI.kt new file mode 100644 index 00000000..4876f2de --- /dev/null +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/BusinessPublishablePostsAPI.kt @@ -0,0 +1,21 @@ +package com.insanusmokrassar.postssystem.core.post + +import com.insanusmokrassar.postssystem.core.post.api.PostsAPI + +class BusinessPublishablePostsAPI( + /** + * Will be used for storing of unpublished data + */ + private val repoPostsAPI: PostsAPI, + /** + * Will be used to send information ab + */ + private val publishedPostsAPI: PostsAPI +) : PostsAPI by repoPostsAPI { + override suspend fun deletePost(id: PostId): Boolean { + return getPostById(id) ?.let { post -> + publishedPostsAPI.createPost(post) + repoPostsAPI.deletePost(id) + } ?: return false + } +} \ No newline at end of file diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/PostsAPI.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/PostsAPI.kt index 123184d5..a3fdceea 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/PostsAPI.kt +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/PostsAPI.kt @@ -1,10 +1,3 @@ package com.insanusmokrassar.postssystem.core.post.api -import com.insanusmokrassar.postssystem.core.post.RegisteredPost -import kotlinx.coroutines.flow.Flow - -interface PostsAPI : ReadPostsAPI, WritePostsAPI { - val postCreatedFlow: Flow - val postDeletedFlow: Flow - val postUpdatedFlow: Flow -} +interface PostsAPI : ReadPostsAPI, WritePostsAPI diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/WritePostsAPI.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/WritePostsAPI.kt index d1cecb91..4d8c0b87 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/WritePostsAPI.kt +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/api/WritePostsAPI.kt @@ -1,8 +1,13 @@ package com.insanusmokrassar.postssystem.core.post.api import com.insanusmokrassar.postssystem.core.post.* +import kotlinx.coroutines.flow.Flow interface WritePostsAPI { + val postCreatedFlow: Flow + val postDeletedFlow: Flow + val postUpdatedFlow: Flow + /** * For creating of post you need to create all its [com.insanusmokrassar.postssystem.core.content.RegisteredContent] * and (or just) retrieve their [ContentIds] and put it into some [Post] implementation line [SimplePost] diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/HandleSafely.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/HandleSafely.kt new file mode 100644 index 00000000..22b38904 --- /dev/null +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/HandleSafely.kt @@ -0,0 +1,23 @@ +package com.insanusmokrassar.postssystem.core.utils + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.supervisorScope + + +typealias ExceptionHandler = suspend (Throwable) -> T +/** + * It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions + * + * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this + * exception will be available for catching + */ +suspend inline fun safely( + noinline onException: ExceptionHandler = { throw it }, + noinline block: suspend CoroutineScope.() -> T +): T { + return try { + supervisorScope(block) + } catch (e: Throwable) { + onException(e) + } +}