Skip to content

Multiplatform support #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 84 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,38 +1,106 @@
buildscript {
ext.kotlin_version = '1.3.21'
ext.kotlin_version = '1.3.72'
ext.atomicfu_version = '0.14.2'

repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlinx:atomicfu-gradle-plugin:$atomicfu_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.8.0'
}
}

group GROUP
version VERSION_NAME

apply plugin: 'com.vanniktech.maven.publish'
apply plugin: 'kotlin'
apply plugin: "org.jetbrains.kotlin.multiplatform"
apply plugin: "kotlinx-atomicfu"

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

testImplementation 'junit:junit:4.12'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0'
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation 'org.assertj:assertj-core:3.11.1'
}
kotlin {
jvm("jvm6")
jvm("jvm8")

compileKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
compileTestKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
js {
browser {
}
nodejs {
}
}
// For ARM, should be changed to iosArm32 or iosArm64
// For Linux, should be changed to e.g. linuxX64
// For MacOS, should be changed to e.g. macosX64
// For Windows, should be changed to e.g. mingwX64
macosX64("macos")
linuxX64()
mingwX64()

sourceSets {
commonMain {
kotlin.srcDir('src/main/')
dependencies {
implementation kotlin('stdlib-common')
implementation "org.jetbrains.kotlinx:atomicfu-common:$atomicfu_version"
}
}
commonTest {
kotlin.srcDir('src/test/')
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}

jvm6Main {
dependencies {
implementation kotlin('stdlib')
implementation kotlin('test-annotations-common')
}

}
jvm6Test {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}

jvm8Main {
task {
kotlinOption.jvmTarget = JavaVersion.VERSION_1_8
}

dependencies {
implementation kotlin('stdlib-jdk8')
implementation kotlin('test-annotations-common')
}
}

jvm8Test {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
jsMain {
dependencies {
implementation kotlin('stdlib-js') }
}
jsTest {
dependencies {
implementation kotlin('test-js')
}
}
macosMain {
}
macosTest {
}

}
}
34 changes: 20 additions & 14 deletions src/main/kotlin/com/tinder/StateMachine.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package com.tinder

import java.util.concurrent.atomic.AtomicReference
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
import kotlin.reflect.KClass

class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private constructor(
private val graph: Graph<STATE, EVENT, SIDE_EFFECT>
) {
private val lock = SynchronizedObject()
private val stateRef = atomic<STATE>(graph.initialState)

private val stateRef = AtomicReference<STATE>(graph.initialState)

val state: STATE
get() = stateRef.get()
var state: STATE
get() = stateRef.value
private set(value) {
stateRef.value = value
}

fun transition(event: EVENT): Transition<STATE, EVENT, SIDE_EFFECT> {
val transition = synchronized(this) {
val fromState = stateRef.get()
val transition = synchronized(lock) {
val fromState = state
val transition = fromState.getTransition(event)
if (transition is Transition.Valid) {
stateRef.set(transition.toState)
state = transition.toState
}
transition
}
Expand Down Expand Up @@ -51,7 +57,7 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
private fun STATE.getDefinition() = graph.stateDefinitions
.filter { it.key.matches(this) }
.map { it.value }
.firstOrNull() ?: error("Missing definition for state ${this.javaClass.simpleName}!")
.firstOrNull() ?: error("Missing definition for state ${this::class.simpleName}!")

private fun STATE.notifyOnEnter(cause: EVENT) {
getDefinition().onEnterListeners.forEach { it(this, cause) }
Expand Down Expand Up @@ -101,7 +107,7 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
}
}

class Matcher<T : Any, out R : T> private constructor(private val clazz: Class<R>) {
class Matcher<T : Any, out R : T> private constructor(private val clazz: KClass<R>) {

private val predicates = mutableListOf<(T) -> Boolean>({ clazz.isInstance(it) })

Expand All @@ -115,9 +121,9 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
fun matches(value: T) = predicates.all { it(value) }

companion object {
fun <T : Any, R : T> any(clazz: Class<R>): Matcher<T, R> = Matcher(clazz)
fun <T : Any, R : T> any(clazz: KClass<R>): Matcher<T, R> = Matcher(clazz)

inline fun <T : Any, reified R : T> any(): Matcher<T, R> = any(R::class.java)
inline fun <T : Any, reified R : T> any(): Matcher<T, R> = any(R::class)

inline fun <T : Any, reified R : T> eq(value: R): Matcher<T, R> = any<T, R>().where { this == value }
}
Expand All @@ -135,8 +141,8 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
}

fun <S : STATE> state(
stateMatcher: Matcher<STATE, S>,
init: StateDefinitionBuilder<S>.() -> Unit
stateMatcher: Matcher<STATE, S>,
init: StateDefinitionBuilder<S>.() -> Unit
) {
stateDefinitions[stateMatcher] = StateDefinitionBuilder<S>().apply(init).build()
}
Expand Down
Loading