• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mobalazs / rotor-framework / 20465444012

23 Dec 2025 03:59PM UTC coverage: 85.314% (-0.3%) from 85.603%
20465444012

push

github

mobalazs
fix(RotorFrameworkTask): change onTick parameter type from function to dynamic in sync method

1981 of 2322 relevant lines covered (85.31%)

1.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.72
/src/source/RotorFrameworkTask.bs
1
' =========================================================================
2
' ▗▄▄▖  ▗▄▖▗▄▄▄▖▗▄▖ ▗▄▄▖     ▗▄▄▄▖▗▄▄▖  ▗▄▖ ▗▖  ▗▖▗▄▄▄▖▗▖ ▗▖ ▗▄▖ ▗▄▄▖ ▗▖ ▗▖
3
' ▐▌ ▐▌▐▌ ▐▌ █ ▐▌ ▐▌▐▌ ▐▌    ▐▌   ▐▌ ▐▌▐▌ ▐▌▐▛▚▞▜▌▐▌   ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌▗▞▘
4
' ▐▛▀▚▖▐▌ ▐▌ █ ▐▌ ▐▌▐▛▀▚▖    ▐▛▀▀▘▐▛▀▚▖▐▛▀▜▌▐▌  ▐▌▐▛▀▀▘▐▌ ▐▌▐▌ ▐▌▐▛▀▚▖▐▛▚▖
5
' ▐▌ ▐▌▝▚▄▞▘ █ ▝▚▄▞▘▐▌ ▐▌    ▐▌   ▐▌ ▐▌▐▌ ▐▌▐▌  ▐▌▐▙▄▄▖▐▙█▟▌▝▚▄▞▘▐▌ ▐▌▐▌ ▐▌
6
' Rotor Framework™
7
' Version 0.5.5
8
' © 2025 Balázs Molnár — Apache License 2.0
9
' =========================================================================
10

11
' constants
12
import "engine/Constants.bs"
13

14
' engine
15
import "engine/providers/DispatcherProvider.bs"
16
import "engine/providers/Dispatcher.bs"
17

18
' base classes
19
import "base/DispatcherCreator.bs"
20
import "base/DispatcherExternal.bs"
21
import "base/BaseReducer.bs"
22
import "base/BaseModel.bs"
23
import "base/BaseStack.bs"
24

25
' utils
26
import "utils/GeneralUtils.bs"
27
import "utils/NodeUtils.bs"
28
import "utils/ArrayUtils.bs"
29

30
namespace Rotor
31
    ' =====================================================================
32
    ' FrameworkTask - Task thread version of Rotor Framework for MVI
33
    '
34
    ' Task thread version of the Rotor Framework that enables cross-thread MVI
35
    ' (Model-View-Intent) architecture. This class manages state and dispatchers
36
    ' on a separate task thread, allowing heavy computations and state management
37
    ' to run off the render thread for better performance.
38
    '
39
    ' Configuration:
40
    '   - tasks (array, optional): List of additional task node names to synchronize with.
41
    '                             Allows multiple task threads to communicate and share
42
    '                             dispatchers across different threads.
43
    '
44
    ' USAGE NOTES:
45
    ' The FrameworkTask must be instantiated in the task's init() function and the sync()
46
    ' method MUST be called at the end of your task function to establish the message loop.
47
    '
48
    ' IMPORTANT: The sync() method creates an infinite loop that handles cross-thread
49
    ' communication and dispatcher synchronization. This call should be the LAST statement
50
    ' in your task function, after all dispatcher initialization.
51
    '
52
    ' Example:
53
    '   File: MyTask.task.bs
54
    '   import "pkg:/source/RotorFrameworkTask.bs"
55
    '   import "pkg:/source/MyDispatcher.bs"
56
    '
57
    '   sub init()
58
    '       m.top.functionName = "task"
59
    '       m.appFw = new Rotor.FrameworkTask({
60
    '           tasks: ["AnotherTask"]
61
    '       })
62
    '   end sub
63
    '
64
    '   sub task()
65
    '       m.fooDispatcher = createFooDispatcher()
66
    '       m.barDispatcher = createBarDispatcher()
67
    '       m.appFw.sync()
68
    '   end sub
69
    ' =====================================================================
70
    class FrameworkTask
71

72
        name = "Rotor Framework"
73
        version = "0.5.5"
74

75
        config = {
76
            tasks: invalid, ' optional
77
            debug: {
78
            }
79
        }
80

81
        threadType = Rotor.Const.ThreadType.TASK
82

83
        keepAlive = true
84

85
        ' helper vars
86
        taskNode as object
87
        dispatcherProvider as object
88
        port as object
89
        asyncTransferRegistry = {} ' transferId -> { dispatcherId, context }
90
        onTick as function
91

92
        ' ---------------------------------------------------------------------
93
        ' new - Initializes the FrameworkTask instance
94
        '
95
        ' Sets up the task thread dispatcher provider, message port, and
96
        ' global framework helper for cross-thread communication.
97
        '
98
        ' @param {object} config - Configuration object (see class documentation)
99
        '
100
        sub new(config = {} as object)
101

102
            Rotor.Utils.deepExtendAA(m.config, config)
1✔
103

104
            globalScope = GetGlobalAA()
1✔
105
            globalScope.rotor_framework_helper = { ' this give to dispatcher instance the possibility to self-register
1✔
106
                threadType: m.threadType,
107
                frameworkInstance: m
108
            }
109
            m.taskNode = globalScope.top
1✔
110

111
            m.dispatcherProvider = new Rotor.DispatcherProvider(m.threadType)
1✔
112

113
            m.taskNode.addField("rotorSync", "assocarray", true)
1✔
114
            m.port = CreateObject("roMessagePort")
1✔
115
            m.taskNode.observeFieldScoped("rotorSync", m.port)
1✔
116

117
        end sub
118

119
        ' =====================================================================
120
        ' PUBLIC API
121
        ' =====================================================================
122

123
        ' ---------------------------------------------------------------------
124
        ' getDispatcher - Gets dispatcher facade by ID
125
        '
126
        ' @param {string} dispatcherId - Dispatcher identifier
127
        ' @returns {object} Dispatcher facade instance
128
        '
129
        public function getDispatcher(dispatcherId as string) as object
130
            return m.dispatcherProvider.getFacade(dispatcherId, GetGlobalAA())
×
131
        end function
132

133
        ' ---------------------------------------------------------------------
134
        ' registerAsyncTransfer - Registers an async transfer with dispatcher context
135
        '
136
        ' This method associates a roUrlTransfer identity with a dispatcher ID and
137
        ' optional context data. When the transfer completes and sends a roUrlEvent,
138
        ' the framework will route it to the correct dispatcher's asyncReducerCallback.
139
        '
140
        ' @param {string} transferId - The transfer.GetIdentity().ToStr() value
141
        ' @param {string} dispatcherId - Dispatcher ID that initiated the transfer
142
        ' @param {object} context - Optional user data to pass back in callback
143
        '
144
        public sub registerAsyncTransfer(transferId as string, dispatcherId as string, context = invalid as dynamic)
145
            m.asyncTransferRegistry[transferId] = {
×
146
                dispatcherId: dispatcherId,
147
                context: context
148
            }
149
        end sub
150

151
        ' ---------------------------------------------------------------------
152
        ' sync - Starts the message loop for cross-thread communication
153
        '
154
        ' IMPORTANT: This method creates an infinite loop that handles:
155
        '   - Intent dispatching from render thread
156
        '   - External dispatcher registration
157
        '   - State change notifications
158
        '   - Async reducer callbacks
159
        '
160
        ' This method MUST be the last call in your task function, as it
161
        ' blocks execution until the framework is destroyed.
162
        '
163
        sub sync(waitMs = 0 as integer, onTick = invalid as dynamic)
164
            m.notifySyncStatus(Rotor.Const.ThreadSyncType.TASK_SYNCING)
1✔
165

166
            keepAlive = true
1✔
167

168
            ' Initialize tick timer if waitMs > 0
169
            lastTickTime = invalid
1✔
170
            if waitMs > 0
2✔
171
                lastTickTime = CreateObject("roTimespan")
×
172
                lastTickTime.Mark()
×
173
            end if
174

175
            while true and keepAlive = true
1✔
176
                msg = wait(waitMs, m.port)
1✔
177

178
                if msg = invalid
2✔
179
                    ' Timeout - check if tick interval elapsed
180
                    if waitMs > 0 and onTick <> invalid and lastTickTime <> invalid
×
181
                        elapsedMs = lastTickTime.TotalMilliseconds()
×
182
                        if elapsedMs >= waitMs
×
183
                            ' Tick interval elapsed - call callback
184
                            Rotor.Utils.callbackScoped(onTick, GetGlobalAA())
×
185
                            ' Reset tick timer
186
                            lastTickTime.Mark()
×
187
                        end if
188
                    end if
189
                else if msg <> invalid
3✔
190
                    msgType = type(msg)
1✔
191
                    if msgType = "roSGNodeEvent"
3✔
192
                        fieldId = msg.getField()
1✔
193

194
                        if fieldId = "rotorSync"
3✔
195

196
                            sync = msg.getData() ' @type:AA
1✔
197

198
                            if sync.type = Rotor.Const.ThreadSyncType.DISPATCH
2✔
199

200

201
                                dispatcherId = sync.payload.dispatcherId
1✔
202
                                intent = sync.payload.intent
1✔
203
                                dispatcherInstance = m.dispatcherProvider.stack.LookupCI(dispatcherId)
1✔
204

205
                                ' taskIntent = Rotor.Utils.deepCopy(intent)
206
                                dispatcherInstance.dispatch(intent)
1✔
207

208
                            else if sync.type = Rotor.Const.ThreadSyncType.REGISTER_EXTERNAL_DISPATCHER
3✔
209

210
                                for each item in sync.externalDispatcherList
1✔
211
                                    m.dispatcherProvider.registerExternalDispatchers(item.dispatcherId, item.externalTaskNode)
1✔
212
                                end for
213

214
                                m.notifySyncStatus(Rotor.Const.ThreadSyncType.TASK_SYNCED)
1✔
215

216
                            else if sync.type = Rotor.Const.ThreadSyncType.DESTROY
3✔
217

218
                                keepAlive = false
1✔
219

220
                            end if
221
                        else
222

×
223
                            data = msg.getData()
×
224
                            extraInfo = msg.GetInfo() ' Info AA passed during observeFieldScoped
×
225

226
                            if extraInfo?.dispatcherId <> invalid and m.dispatcherProvider.get(extraInfo?.dispatcherId) <> invalid
×
227
                                ' Catch by dispatcherId
228
                                m.dispatcherProvider.get(extraInfo?.dispatcherId).asyncReducerCallback(msg, extraInfo?.context as dynamic)
×
229
                            else
×
230
                                dispatcherId = fieldId
×
231
                                dispatcherInstance = m.dispatcherProvider.get(dispatcherId)
×
232
                                dispatcherInstance.notifyListeners(data)
×
233
                            end if
234

235
                        end if
236
                    else if msgType = "roUrlEvent"
×
237
                        ' Handle async transfer responses
238
                        transferId = msg.GetSourceIdentity().ToStr()
×
239

240
                        if m.asyncTransferRegistry.DoesExist(transferId)
×
241
                            transferData = m.asyncTransferRegistry[transferId]
×
242
                            dispatcherId = transferData.dispatcherId
×
243

244
                            ' Cleanup registry entry (Note: order is important - this make it reusable immediately)
245
                            m.asyncTransferRegistry.delete(transferId)
×
246

247
                            dispatcherInstance = m.dispatcherProvider.get(dispatcherId)
×
248
                            if dispatcherInstance <> invalid
×
249
                                ' Route to dispatcher with wrapped message
250
                                dispatcherInstance.asyncReducerCallback(msg as roUrlEvent, transferData?.context as dynamic)
×
251
                            end if
252

253
                        end if
254
                    end if
255
                end if
256
            end while
257
            m.destroy()
1✔
258
        end sub
259

260
        ' =====================================================================
261
        ' INTERNAL METHODS
262
        ' =====================================================================
263

264
        ' ---------------------------------------------------------------------
265
        ' notifySyncStatus - Notifies render thread of sync status
266
        '
267
        ' Sends sync status message to render thread via rotorSync field.
268
        '
269
        ' @param {string} status - Sync status type (TASK_SYNCING or TASK_SYNCED)
270
        '
271
        sub notifySyncStatus(status as string)
272

273
            payload = {
1✔
274
                type: status,
275
                taskNode: m.taskNode
276
            }
277

278
            if status = Rotor.Const.ThreadSyncType.TASK_SYNCING
2✔
279
                payload.append({
1✔
280
                    dispatcherIds: m.dispatcherProvider.stack.Keys(),
281
                    tasks: m.config.tasks
282
                })
283
            end if
284

285
            m.taskNode.rootNode.setField("rotorSync", payload)
1✔
286

287
        end sub
288

289
        ' ---------------------------------------------------------------------
290
        ' addObserver - Adds field observer to task thread message port
291
        '
292
        ' @param {string} fieldId - Field name to observe
293
        ' @param {object} node - SceneGraph node to observe
294
        '
295
        sub addObserver(fieldId as string, node)
296
            node.observeFieldScoped(fieldId, m.port)
×
297
        end sub
298

299
        ' ---------------------------------------------------------------------
300
        ' removeObserver - Removes field observer from node
301
        '
302
        ' @param {string} fieldId - Field name to stop observing
303
        ' @param {object} node - SceneGraph node to unobserve
304
        '
305
        sub removeObserver(fieldId as string, node)
306
            node.unobserveFieldScoped(fieldId)
×
307
        end sub
308

309
        ' =====================================================================
310
        ' CLEANUP
311
        ' =====================================================================
312

313
        ' ---------------------------------------------------------------------
314
        ' destroy - Cleans up task thread resources
315
        '
316
        ' Destroys dispatcher provider and clears global framework helper.
317
        '
318
        public sub destroy()
319
            m.asyncTransferRegistry.clear()
1✔
320
            m.dispatcherProvider.destroy()
1✔
321

322
            globalScope = GetGlobalAA()
1✔
323
            globalScope.rotor_framework_helper = {
1✔
324
                frameworkInstance: invalid
325
            }
326

327
            m.taskNode.rootNode = invalid
1✔
328
            m.taskNode = invalid
1✔
329
        end sub
330

331
    end class
332

333
end namespace
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc