Skip to content

Commit

Permalink
[refactoring] Move files into folders (breaking change)
Browse files Browse the repository at this point in the history
This will break existing client code. But I have to store files in more structured way
  • Loading branch information
nsk90 committed Mar 5, 2024
1 parent 86f737d commit cc6106d
Show file tree
Hide file tree
Showing 94 changed files with 707 additions and 279 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.coroutines

import kotlinx.coroutines.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.statemachine

import kotlinx.coroutines.CoroutineScope
import ru.nsk.kstatemachine.coroutines.CoroutinesLibCoroutineAbstraction
import ru.nsk.kstatemachine.coroutines.createStateMachine
import ru.nsk.kstatemachine.state.ChildMode

/**
* Suspendable analog of [createStdLibStateMachine] function, with Kotlin Coroutines support.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.statemachine

import kotlinx.coroutines.flow.*
import ru.nsk.kstatemachine.StateMachineNotification.*
import ru.nsk.kstatemachine.statemachine.StateMachineNotification.*
import ru.nsk.kstatemachine.state.IState
import ru.nsk.kstatemachine.state.activeStates
import ru.nsk.kstatemachine.transition.TransitionParams

sealed class StateMachineNotification(val machine: StateMachine) {
class Started(machine: StateMachine) : StateMachineNotification(machine)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ru.nsk.kstatemachine

import ru.nsk.kstatemachine.state.IState
import ru.nsk.kstatemachine.state.InternalState
import ru.nsk.kstatemachine.statemachine.StateMachine

/**
* [forEach] analog which ignores internal state machines
* [forEach] analog which ignores internal [StateMachine]s
*/
internal suspend fun Iterable<InternalState>.forEachState(block: suspend (InternalState) -> Unit) {
forEach { if (it !is StateMachine) block(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package ru.nsk.kstatemachine

import ru.nsk.kstatemachine.StateMachine.PendingEventHandler
package ru.nsk.kstatemachine.statemachine

import ru.nsk.kstatemachine.coroutines.CoroutineAbstraction
import ru.nsk.kstatemachine.coroutines.StdLibCoroutineAbstraction
import ru.nsk.kstatemachine.coroutines.createStateMachine
import ru.nsk.kstatemachine.event.DestroyEvent
import ru.nsk.kstatemachine.event.Event
import ru.nsk.kstatemachine.event.StopEvent
import ru.nsk.kstatemachine.event.UndoEvent
import ru.nsk.kstatemachine.state.ChildMode
import ru.nsk.kstatemachine.state.IState
import ru.nsk.kstatemachine.state.State
import ru.nsk.kstatemachine.statemachine.StateMachine.PendingEventHandler
import ru.nsk.kstatemachine.transition.EventAndArgument
import ru.nsk.kstatemachine.transition.Transition
import ru.nsk.kstatemachine.transition.TransitionParams
import ru.nsk.kstatemachine.visitors.CoVisitor
import ru.nsk.kstatemachine.visitors.Visitor

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.nsk.kstatemachine

import ru.nsk.kstatemachine.state.InternalNode
import ru.nsk.kstatemachine.state.requireParentNode

/**
* @return Path from [targetState] to the lowest common ancestor (lca).
* Path order: Lca and its parent if [addLcaParent] is true are last elements in resulting list.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.coroutines

import ru.nsk.kstatemachine.state.ChildMode
import ru.nsk.kstatemachine.statemachine.BuildingStateMachine
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.StateMachineImpl
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.event

import ru.nsk.kstatemachine.state.DataState
import ru.nsk.kstatemachine.transition.TransitionParams

/**
* Allows to extract data for [DataState] from any [Event]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.event

import ru.nsk.kstatemachine.state.*
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.processEventBlocking

/**
* Base interface for events which may trigger transitions of [StateMachine]
Expand Down Expand Up @@ -64,29 +68,5 @@ internal class DestroyEvent(val stop: Boolean) : GeneratedEvent
* @param event original event
* @param argument original argument
*/
class WrappedEvent(val event: Event, val argument: Any?) : Event

@StateMachineDslMarker
data class TransitionParams<E : Event>(
val transition: Transition<E>,
val direction: TransitionDirection,
val event: E,
/**
* This parameter may be used to pass arbitrary data with the event,
* so there is no need to define [Event] subclasses every time.
* Subclassing should be preferred if the event always contains data of some type.
*/
val argument: Any? = null,
)

/**
* Convenience property for unwrapping original event.
* If the event is not [WrappedEvent] this is same as [TransitionParams.event] property
*/
val TransitionParams<*>.unwrappedEvent get() = if (event is WrappedEvent) event.event else event

/**
* Convenience property for unwrapping original argument.
* If the event is not [WrappedEvent] this is same as [TransitionParams.argument] property
*/
val TransitionParams<*>.unwrappedArgument get() = if (event is WrappedEvent) event.argument else argument
class WrappedEvent(val event: Event, val argument: Any?) :
Event
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.event

import ru.nsk.kstatemachine.state.IState
import ru.nsk.kstatemachine.transition.Transition
import ru.nsk.kstatemachine.transition.TransitionBuilder
import kotlin.reflect.KClass

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.metainfo

import ru.nsk.kstatemachine.state.IState
import ru.nsk.kstatemachine.transition.Transition

/**
* Additional static (designed to be immutable) info for library primitives like [IState] [Transition] etc.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package ru.nsk.kstatemachine

import ru.nsk.kstatemachine.ChildMode.EXCLUSIVE
import ru.nsk.kstatemachine.ChildMode.PARALLEL
import ru.nsk.kstatemachine.TransitionType.EXTERNAL
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.*
import ru.nsk.kstatemachine.event.Event
import ru.nsk.kstatemachine.event.FinishedEvent
import ru.nsk.kstatemachine.metainfo.MetaInfo
import ru.nsk.kstatemachine.state.ChildMode.EXCLUSIVE
import ru.nsk.kstatemachine.state.ChildMode.PARALLEL
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.destroyBlocking
import ru.nsk.kstatemachine.statemachine.machineNotify
import ru.nsk.kstatemachine.transition.*
import ru.nsk.kstatemachine.transition.TransitionType.EXTERNAL

/**
* Base [IState] implementation for all states
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.ChildMode.EXCLUSIVE
import ru.nsk.kstatemachine.event.*
import ru.nsk.kstatemachine.state.ChildMode.EXCLUSIVE
import ru.nsk.kstatemachine.transition.TransitionParams

/** inline constructor function */
inline fun <reified D : Any> defaultDataState(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.state.ChildMode.EXCLUSIVE

/**
* The most common state
*/
open class DefaultState(name: String? = null, childMode: ChildMode = EXCLUSIVE) :
BaseStateImpl(name, childMode), State

open class DefaultFinalState(name: String? = null) : DefaultState(name), FinalState
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package ru.nsk.kstatemachine

package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.event.DataExtractor
import ru.nsk.kstatemachine.event.defaultDataExtractor
import ru.nsk.kstatemachine.metainfo.MetaInfo
import ru.nsk.kstatemachine.state.pseudo.DefaultChoiceDataState
import ru.nsk.kstatemachine.state.pseudo.DefaultChoiceState
import ru.nsk.kstatemachine.state.pseudo.DefaultHistoryState
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.StateMachineDslMarker
import ru.nsk.kstatemachine.transition.EventAndArgument
import ru.nsk.kstatemachine.transition.TransitionDirection
import ru.nsk.kstatemachine.transition.TransitionDirectionProducerPolicy
import ru.nsk.kstatemachine.transition.TransitionParams
import ru.nsk.kstatemachine.visitors.CoVisitor
import ru.nsk.kstatemachine.visitors.GetActiveStatesVisitor
import ru.nsk.kstatemachine.visitors.Visitor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.TransitionDirectionProducerPolicy.DefaultPolicy
import ru.nsk.kstatemachine.PathNode
import ru.nsk.kstatemachine.event.Event
import ru.nsk.kstatemachine.transition.*
import ru.nsk.kstatemachine.transition.TransitionDirectionProducerPolicy.DefaultPolicy

/**
* Contains tree composition api for [InternalState].
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.transition.TransitionParams
import kotlin.properties.Delegates.observable

interface GroupListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.statemachine.InternalStateMachine
import ru.nsk.kstatemachine.statemachine.runDelayingException
import ru.nsk.kstatemachine.transition.TransitionParams

internal suspend inline fun InternalState.stateNotify(crossinline block: suspend IState.Listener.() -> Unit) {
val machine = machine as InternalStateMachine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ru.nsk.kstatemachine
package ru.nsk.kstatemachine.state

import ru.nsk.kstatemachine.EventMatcher.Companion.isInstanceOf
import ru.nsk.kstatemachine.TransitionType.LOCAL
import ru.nsk.kstatemachine.event.*
import ru.nsk.kstatemachine.event.EventMatcher.Companion.isInstanceOf
import ru.nsk.kstatemachine.metainfo.MetaInfo
import ru.nsk.kstatemachine.transition.*
import ru.nsk.kstatemachine.transition.TransitionType.LOCAL

/**
* Helper interface for [IState] to keep transitions logic separately.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.nsk.kstatemachine.state.pseudo

import ru.nsk.kstatemachine.event.Event
import ru.nsk.kstatemachine.state.*
import ru.nsk.kstatemachine.transition.Transition
import ru.nsk.kstatemachine.transition.TransitionParams

open class BasePseudoState(name: String?) : BaseStateImpl(name, ChildMode.EXCLUSIVE), PseudoState {
override suspend fun doEnter(transitionParams: TransitionParams<*>) = internalError()
override suspend fun doExit(transitionParams: TransitionParams<*>) = internalError()

override fun <L : IState.Listener> addListener(listener: L) =
throw UnsupportedOperationException("PseudoState $this can not have listeners")

override fun <S : IState> addState(state: S, init: StateBlock<S>?) =
throw UnsupportedOperationException("PseudoState $this can not have child states")


override fun <E : Event> addTransition(transition: Transition<E>) =
throw UnsupportedOperationException("PseudoState $this can not have transitions")

private fun internalError(): Nothing =
error("Internal error, PseudoState $this can not be entered or exited, looks that machine is purely configured")
}
Loading

0 comments on commit cc6106d

Please sign in to comment.