-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.go
95 lines (80 loc) · 2.31 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package fsm
import (
"sync"
)
// stateMachineRegistry manages state machine instances
type stateMachineRegistry struct {
stateMachines map[string]interface{}
mutex sync.RWMutex
}
// Global registry instance
var registry = &stateMachineRegistry{
stateMachines: make(map[string]interface{}),
}
// RegisterStateMachine adds a state machine to the registry
// Parameters:
//
// machineId: Unique identifier for the state machine
// stateMachine: State machine instance to register
//
// Returns:
//
// Error if a state machine with the same ID already exists
func RegisterStateMachine[S comparable, E comparable, P any](machineId string, stateMachine StateMachine[S, E, P]) error {
registry.mutex.Lock()
defer registry.mutex.Unlock()
if _, exists := registry.stateMachines[machineId]; exists {
return ErrStateMachineAlreadyExist
}
registry.stateMachines[machineId] = stateMachine
return nil
}
// GetStateMachine retrieves a state machine by ID
// Parameters:
//
// machineId: Unique identifier for the state machine to retrieve
//
// Returns:
//
// The state machine instance and error if not found or type mismatch
func GetStateMachine[S comparable, E comparable, P any](machineId string) (StateMachine[S, E, P], error) {
registry.mutex.RLock()
defer registry.mutex.RUnlock()
if sm, exists := registry.stateMachines[machineId]; exists {
if typedSM, ok := sm.(StateMachine[S, E, P]); ok {
return typedSM, nil
}
return nil, ErrStateMachineNotFound
}
return nil, ErrStateMachineNotFound
}
// ListStateMachines returns a list of all registered state machine IDs
// Returns:
//
// Slice of state machine IDs
func ListStateMachines() []string {
registry.mutex.RLock()
defer registry.mutex.RUnlock()
result := make([]string, 0, len(registry.stateMachines))
for id := range registry.stateMachines {
result = append(result, id)
}
return result
}
// RemoveStateMachine removes a state machine from the registry
// Parameters:
//
// machineId: Unique identifier for the state machine to remove
//
// Returns:
//
// True if the state machine was found and removed, false otherwise
func RemoveStateMachine(machineId string) bool {
registry.mutex.Lock()
defer registry.mutex.Unlock()
if _, exists := registry.stateMachines[machineId]; exists {
delete(registry.stateMachines, machineId)
return true
}
return false
}