-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathruntime_event_dispatch.go
More file actions
244 lines (218 loc) · 6.55 KB
/
Copy pathruntime_event_dispatch.go
File metadata and controls
244 lines (218 loc) · 6.55 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
"slices"
coreevent "github.com/goplus/spx/v3/internal/core/event"
"github.com/goplus/spx/v3/internal/coroutine"
)
// scriptEventLifecycle registers an invocation before it can run and returns
// its cleanup. Handlers without this interface allow overlapping invocations.
type scriptEventLifecycle interface {
Start(coroutine.Thread) func()
}
// Each registration owns its execution state; clones re-register their handlers.
type scriptEventHandler[F any] struct {
coroutine.HandlerState
run F
}
type messageEventHandler = scriptEventHandler[func(string, any)]
func newScriptEventSink[F any](owner threadObj, run F, policy coroutine.HandlerPolicy, cond ...func(any) bool) eventSink {
handler := &scriptEventHandler[F]{HandlerState: coroutine.NewHandlerState(policy), run: run}
return coreevent.NewSink(owner, handler, cond...)
}
type scriptEventDispatch struct {
mode coroutine.BatchMode
matchData any
run func(coroutine.Thread, *eventSink)
}
func (event scriptEventDispatch) task(sink eventSink) coroutine.Task {
task := coroutine.Task{
Owner: sink.Owner,
Run: func(thread coroutine.Thread) { event.run(thread, &sink) },
}
if handler, ok := sink.Handler.(scriptEventLifecycle); ok {
task.Setup = handler.Start
}
return task
}
// Register all handlers before returning; keep engine calls live while waiting.
func withEventRegistrationBarrier(owner any, dispatch func()) {
if gco == nil || gco.IsInCoroutine() {
dispatch()
return
}
if gco.TryRunFromEngine(owner, dispatch) {
return
}
dispatcher := gco.Create(owner, func(coroutine.Thread) {
dispatch()
})
gco.Join(dispatcher)
}
func (p *scriptEventRegistry) globalSinks(bucket coreevent.Bucket) []eventSink {
return sinksInTargetOrder(p.game, p.manager.Snapshot(bucket))
}
func (p *scriptEventRegistry) dispatchGlobal(bucket coreevent.Bucket, event scriptEventDispatch) {
p.dispatchSinks(p.globalSinks(bucket), event)
}
func (p *scriptEventRegistry) dispatchTarget(bucket coreevent.Bucket, owner any, event scriptEventDispatch) {
sinks := p.manager.Snapshot(bucket)
var owned []eventSink
for _, sink := range sinks {
if sink.Owner == owner {
if owned == nil {
owned = make([]eventSink, 0, min(len(sinks), 8))
}
owned = append(owned, sink)
}
}
if len(owned) == 0 {
return
}
p.dispatchSinks(owned, event)
}
func (p *scriptEventRegistry) dispatchSinks(sinks []eventSink, event scriptEventDispatch) {
withEventRegistrationBarrier(p.game, func() {
// Complete matching before starting user handlers.
matched := matchingEventSinks(sinks, event.matchData)
dispatchMatchedScriptEventBatch(matched, event)
})
}
func (p *scriptEventRegistry) dispatchStartSinks(sinks []eventSink, event scriptEventDispatch) {
withEventRegistrationBarrier(p.game, func() {
p.dispatchStartEventBatch(sinks, event)
})
}
func (p *scriptEventRegistry) dispatchStartEventBatch(sinks []eventSink, event scriptEventDispatch) {
matched := matchingEventSinks(sinks, event.matchData)
if len(matched) == 0 || gco == nil {
dispatchMatchedScriptEventBatch(matched, event)
return
}
baseline := p.stopAllEpoch.Load()
tasks := make([]coroutine.Task, len(matched))
threads := make([]coroutine.Thread, 0, len(matched))
defer func() {
for _, thread := range threads {
p.pendingStartThreads.Delete(thread)
}
}()
for i, sink := range matched {
task := event.task(sink)
run := task.Run
task.Setup = func(thread coroutine.Thread) func() {
p.pendingStartThreads.Store(thread, struct{}{})
threads = append(threads, thread)
return nil
}
task.Run = func(thread coroutine.Thread) {
p.pendingStartThreads.Delete(thread)
if p.stopAllEpoch.Load() != baseline {
gco.StopAtNextYield(thread)
}
run(thread)
}
tasks[i] = task
}
gco.StartBatch(tasks, event.mode)
}
func (p *scriptEventRegistry) isPendingStartThread(thread coroutine.Thread) bool {
_, ok := p.pendingStartThreads.Load(thread)
return ok
}
func eventBatchMode(wait bool) coroutine.BatchMode {
if wait {
return coroutine.BatchWaitDone
}
return coroutine.BatchAsync
}
// sinksInTargetOrder uses the live front-to-back sprite order and puts
// the stage last while preserving registration order within each owner.
func sinksInTargetOrder(game *Game, sinks []eventSink) []eventSink {
if len(sinks) < 2 {
return sinks
}
if game == nil {
return slices.Clone(sinks)
}
shapes := game.getAllShapes()
// Link immutable snapshot entries to avoid per-sprite slices.
// Indices are one-based; zero ends a group.
heads := make(map[*SpriteImpl]int, len(shapes))
for _, shape := range shapes {
if sprite, ok := shape.(*SpriteImpl); ok {
heads[sprite] = 0
}
}
next := make([]int, len(sinks))
unknown, stage := 0, 0
for i := len(sinks) - 1; i >= 0; i-- {
switch owner := sinks[i].Owner.(type) {
case *SpriteImpl:
if head, live := heads[owner]; live {
next[i], heads[owner] = head, i+1
continue
}
case *Game:
if owner == game {
next[i], stage = stage, i+1
continue
}
}
next[i], unknown = unknown, i+1
}
ordered := make([]eventSink, 0, len(sinks))
appendGroup := func(head int) {
for head != 0 {
ordered = append(ordered, sinks[head-1])
head = next[head-1]
}
}
for i := len(shapes) - 1; i >= 0; i-- {
if sprite, ok := shapes[i].(*SpriteImpl); ok {
appendGroup(heads[sprite])
}
}
appendGroup(unknown)
appendGroup(stage)
return ordered
}
func matchingEventSinks(sinks []eventSink, matchData any) []eventSink {
matched := make([]eventSink, 0, len(sinks))
for _, sink := range sinks {
if sink.Cond == nil || sink.Cond(matchData) {
matched = append(matched, sink)
}
}
return matched
}
func dispatchMatchedScriptEventBatch(matched []eventSink, event scriptEventDispatch) {
if len(matched) == 0 {
return
}
if gco == nil {
for i := range matched {
event.run(nil, &matched[i])
}
return
}
tasks := make([]coroutine.Task, len(matched))
for i, sink := range matched {
tasks[i] = event.task(sink)
}
gco.StartBatch(tasks, event.mode)
}