-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorprocesses.go
92 lines (81 loc) · 2.66 KB
/
errorprocesses.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
// Actress Copyright (C) 2024 Bjørn Tore Svinningen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package actress
import (
"context"
)
// processes holds information about what process functions
// who belongs to what event, and also a map of the started
// processes.
type errProcesses struct {
procMap map[EventType]*Process
}
// Add a new Event and it's process to the processes map.
func (p *errProcesses) add(et EventType, proc *Process) {
// Check if a process for the same event is defined, and if so we
// cancel the current process before we replace it with a new one.
if _, ok := p.procMap[et]; ok {
p.procMap[et].Cancel()
}
p.procMap[et] = proc
}
// // Delete an Event and it's process from the processes map.
// func (p *processes) delete(et EventType, proc *Process) {
// p.mu.Lock()
// defer p.mu.Unlock()
// p.procMap[et].cancel()
// delete(p.procMap, et)
// }
// Checks if the event is defined in the processes map, and returns true if it is.
func (p *errProcesses) IsEventDefined(ev EventType) bool {
if _, ok := p.procMap[ev]; !ok {
return false
}
return true
}
// Prepare and return a new *processes structure.
func newErrProcesses() *errProcesses {
p := errProcesses{
procMap: make(map[EventType]*Process),
}
return &p
}
// NewErrProcess will prepare and return a *Process. It will copy
// channels and map structures from the root process.
func NewErrProcess(ctx context.Context, parentP Process, event EventType, fn ETFunc) *Process {
ctx, cancel := context.WithCancel(ctx)
p := Process{
fn: nil,
InCh: make(chan Event, 1),
EventCh: parentP.EventCh,
ErrorCh: parentP.ErrorCh,
TestCh: parentP.TestCh,
DynCh: parentP.DynCh,
Event: event,
Processes: parentP.Processes,
DynProcesses: parentP.DynProcesses,
ErrProcesses: parentP.ErrProcesses,
isRoot: false,
Config: parentP.Config,
pids: parentP.pids,
PID: parentP.pids.next(),
Cancel: cancel,
}
p.ErrProcesses.add(event, &p)
if fn != nil {
p.fn = fn(ctx, &p)
}
return &p
}