-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathViewModelInstanceService.swift
More file actions
757 lines (702 loc) · 33.4 KB
/
Copy pathViewModelInstanceService.swift
File metadata and controls
757 lines (702 loc) · 33.4 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//
// ViewModelInstanceService.swift
// RiveRuntime
//
// Created by David Skuza on 11/19/25.
// Copyright © 2025 Rive. All rights reserved.
//
import Foundation
/// A service class that manages view model instance operations and coordinates with the command queue.
///
/// Implements `ViewModelInstanceListener` to receive callbacks from the command queue. Manages two
/// types of continuations: regular continuations for one-time value requests and stream continuations
/// for property subscriptions. All command queue operations must be performed on the main thread
/// (either marked `@MainActor` or dispatched to the main queue). Listener callbacks are dispatched
/// to the main actor to safely access continuations.
///
/// The service handles property subscriptions via `subscribe`/`unsubscribe` commands. When a stream
/// is terminated, the `onTermination` handler automatically unsubscribes and cleans up the stream continuation.
@MainActor
class ViewModelInstanceService: NSObject, ViewModelInstanceListener {
let dependencies: Dependencies
/// Regular continuations for one-time value requests (e.g., `stringValue`, `numberValue`).
/// Resumed when `onViewModelDataReceived` is called.
private var continuations: [UInt64: AnyContinuation] = [:]
/// Stream continuations for property subscriptions (e.g., `stringValueStream`, `numberValueStream`).
/// Yielded values when `onViewModelDataReceived` is called. Cleaned up when streams terminate.
private var streamContinuations: [UInt64: AnyAsyncThrowingStreamContinuation] = [:]
init(dependencies: Dependencies) {
self.dependencies = dependencies
}
// MARK: - Name
/// Retrieves the name of a view model instance.
///
/// The continuation is resumed when `onViewModelInstanceNameReceived` is called.
///
/// - Parameter instance: The view model instance handle
/// - Returns: The name of the view model instance
@MainActor
func name(for instance: ViewModelInstance.ViewModelInstanceHandle) async throws -> String {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceName(instance, requestID: requestID)
}
}
/// Creates a blank view model instance from an artboard.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createBlankViewModelInstance(
for artboard: Artboard,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createBlankViewModelInstance(
forArtboard: artboard.artboardHandle,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Creates a blank view model instance by view model name.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createBlankViewModelInstance(
named viewModelName: String,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createBlankViewModelInstanceNamed(
viewModelName,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Creates a default view model instance from an artboard.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createDefaultViewModelInstance(
for artboard: Artboard,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createDefaultViewModelInstance(
forArtboard: artboard.artboardHandle,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Creates a default view model instance by view model name.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createDefaultViewModelInstance(
named viewModelName: String,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createDefaultViewModelInstanceNamed(
viewModelName,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Creates a named view model instance from an artboard.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createViewModelInstanceNamed(
_ instanceName: String,
for artboard: Artboard,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createViewModelInstanceNamed(
instanceName,
forArtboard: artboard.artboardHandle,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Creates a named view model instance by view model name.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// No listener callback is invoked for this operation.
@MainActor
func createViewModelInstanceNamed(
_ instanceName: String,
viewModelName: String,
from file: File
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
let handle = dependencies.commandQueue.createViewModelInstanceNamed(
instanceName,
viewModelName: viewModelName,
fromFile: file.fileHandle,
observer: self,
requestID: requestID
)
return handle
}
/// Deletes a view model instance via the command queue.
///
/// After deletion, the instance handle becomes invalid. This operation is irreversible.
/// No listener callback is invoked for this operation.
@MainActor
func deleteViewModelInstance(_ instance: ViewModelInstance.ViewModelInstanceHandle) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.deleteViewModelInstance(instance, requestID: requestID)
}
// MARK: - StringProperty
/// Retrieves a string property value.
///
/// The continuation is resumed when `onViewModelDataReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The string property value
/// - Throws: `ViewModelInstanceError.missingData` if the property value is missing or invalid
@MainActor
func stringValue(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> String {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceString(instance, path: path, requestID: requestID)
}
}
/// Creates a stream that emits string property values as they change.
///
/// Subscribes to property changes via the command queue. Values are yielded when
/// `onViewModelDataReceived` is called. Automatically unsubscribes when the stream terminates.
@MainActor
func stringValueStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<String, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .string, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .string, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Sets a string property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setStringValue(_ value: String, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceString(instance, path: path, value: value, requestID: requestID)
}
// MARK: - NumberProperty
/// Retrieves a number property value.
///
/// The continuation is resumed when `onViewModelDataReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The number property value
/// - Throws: `ViewModelInstanceError.missingData` if the property value is missing or invalid
@MainActor
func numberValue(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> Float {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceNumber(instance, path: path, requestID: requestID)
}
}
/// Creates a stream that emits number property values as they change.
///
/// Subscribes to property changes via the command queue. Values are yielded when
/// `onViewModelDataReceived` is called. Automatically unsubscribes when the stream terminates.
@MainActor
func numberValueStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<Float, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .number, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .number, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Sets a number property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setNumberValue(_ value: Float, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceNumber(instance, path: path, value: value, requestID: requestID)
}
// MARK: - BoolProperty
/// Retrieves a boolean property value.
///
/// The continuation is resumed when `onViewModelDataReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The boolean property value
/// - Throws: `ViewModelInstanceError.missingData` if the property value is missing or invalid
@MainActor
func boolValue(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> Bool {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceBool(instance, path: path, requestID: requestID)
}
}
/// Creates a stream that emits boolean property values as they change.
///
/// Subscribes to property changes via the command queue. Values are yielded when
/// `onViewModelDataReceived` is called. Automatically unsubscribes when the stream terminates.
@MainActor
func boolValueStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<Bool, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .boolean, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .boolean, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Sets a boolean property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setBoolValue(_ value: Bool, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceBool(instance, path: path, value: value, requestID: requestID)
}
// MARK: - ColorProperty
/// Retrieves a color property value.
///
/// The continuation is resumed when `onViewModelDataReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The color property value
/// - Throws: `ViewModelInstanceError.missingData` if the property value is missing or invalid
@MainActor
func colorValue(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> Color {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceColor(instance, path: path, requestID: requestID)
}
}
/// Creates a stream that emits color property values as they change.
///
/// Subscribes to property changes via the command queue. Values are yielded when
/// `onViewModelDataReceived` is called. Automatically unsubscribes when the stream terminates.
@MainActor
func colorValueStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<Color, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .color, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .color, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Sets a color property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setColorValue(_ value: Color, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceColor(instance, path: path, value: value.argbValue, requestID: requestID)
}
// MARK: - EnumProperty
/// Retrieves an enum property value.
///
/// The continuation is resumed when `onViewModelDataReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The enum property value
/// - Throws: `ViewModelInstanceError.missingData` if the property value is missing or invalid
@MainActor
func enumValue(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> String {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceEnum(instance, path: path, requestID: requestID)
}
}
/// Creates a stream that emits enum property values as they change.
///
/// Subscribes to property changes via the command queue. Values are yielded when
/// `onViewModelDataReceived` is called. Automatically unsubscribes when the stream terminates.
@MainActor
func enumValueStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<String, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .enum, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .enum, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Sets an enum property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setEnumValue(_ value: String, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceEnum(instance, path: path, value: value, requestID: requestID)
}
// MARK: - TriggerProperty
/// Fires a trigger property.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func fireTrigger(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.fireViewModelTrigger(instance, path: path, requestID: requestID)
}
/// Creates a stream that emits events when a trigger property is fired.
///
/// Subscribes to trigger events via the command queue. Events are yielded when
/// `onViewModelDataReceived` is called with a trigger type. Automatically unsubscribes when the stream terminates.
@MainActor
func triggerStream(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) -> AsyncThrowingStream<Void, Error> {
return AsyncThrowingStream { continuation in
let commandQueue = dependencies.commandQueue
let requestID = commandQueue.nextRequestID
streamContinuations[requestID] = AnyAsyncThrowingStreamContinuation(continuation)
commandQueue.subscribe(toViewModelProperty: instance, path: path, type: .trigger, requestID: requestID)
continuation.onTermination = { [weak self] _ in
Task { @MainActor in
commandQueue.unsubscribe(toViewModelProperty: instance, path: path, type: .trigger, requestID: requestID)
guard let self else { return }
self.streamContinuations.removeValue(forKey: requestID)
}
}
}
}
// MARK: - ImageProperty
/// Sets an image property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setImageValue(_ value: Image.ImageHandle, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceImage(instance, path: path, value: value, requestID: requestID)
}
// MARK: - ArtboardProperty
/// Sets an artboard property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setArtboardValue(_ value: Artboard, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceArtboard(instance, path: path, value: value.artboardHandle, requestID: requestID)
}
// MARK: - ViewModelInstanceProperty
/// Retrieves a nested view model instance property value.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// The provided observer receives callbacks for the nested instance.
func viewModelInstanceValue(
for instance: ViewModelInstance.ViewModelInstanceHandle,
path: String,
observer: ViewModelInstanceListener
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
return dependencies.commandQueue.referenceNestedViewModelInstance(instance, path: path, observer: observer, requestID: requestID)
}
/// Sets a view model instance property value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func setViewModelInstanceValue(_ value: ViewModelInstance.ViewModelInstanceHandle, for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.setViewModelInstanceNestedViewModel(instance, path: path, value: value, requestID: requestID)
}
// MARK: - ListProperty
/// Retrieves the size of a list property.
///
/// The continuation is resumed when `onViewModelListSizeReceived` is called.
///
/// - Parameters:
/// - instance: The view model instance handle
/// - path: The property path
/// - Returns: The size of the list property
/// - Throws: `ViewModelInstanceError` if the request fails
@MainActor
func listSize(for instance: ViewModelInstance.ViewModelInstanceHandle, path: String) async throws -> Int {
let commandQueue = dependencies.commandQueue
return try await withCheckedThrowingContinuation { continuation in
let requestID = commandQueue.nextRequestID
continuations[requestID] = AnyContinuation(continuation)
commandQueue.requestViewModelInstanceListSize(instance, path: path, requestID: requestID)
}
}
/// Retrieves a view model instance from a list property at the specified index.
///
/// Delegates to the command queue. Returns immediately with the instance handle.
/// The provided observer receives callbacks for the list item instance.
@MainActor
func listViewModelInstanceValue(
for instance: ViewModelInstance.ViewModelInstanceHandle,
path: String,
index: Int32,
observer: ViewModelInstanceListener
) -> ViewModelInstance.ViewModelInstanceHandle {
let requestID = dependencies.commandQueue.nextRequestID
return dependencies.commandQueue.referenceListViewModelInstance(
instance,
path: path,
index: index,
observer: observer,
requestID: requestID
)
}
/// Appends a view model instance to a list property.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func appendViewModelInstance(
_ base: ViewModelInstance.ViewModelInstanceHandle,
path: String,
value: ViewModelInstance.ViewModelInstanceHandle
) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.appendViewModelInstanceListViewModel(
base,
path: path,
value: value,
requestID: requestID
)
}
/// Inserts a view model instance into a list property at the specified index.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func insertViewModelInstance(
_ base: ViewModelInstance.ViewModelInstanceHandle,
path: String,
value: ViewModelInstance.ViewModelInstanceHandle,
index: Int32
) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.insertViewModelInstanceListViewModel(
base,
path: path,
value: value,
index: index,
requestID: requestID
)
}
/// Removes a view model instance from a list property at the specified index.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func removeViewModelInstanceListViewModelAtIndex(
_ base: ViewModelInstance.ViewModelInstanceHandle,
path: String,
index: Int32,
value: ViewModelInstance.ViewModelInstanceHandle
) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.removeViewModelInstanceListViewModelAtIndex(
base,
path: path,
index: index,
value: value,
requestID: requestID
)
}
/// Removes a view model instance from a list property by value.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func removeViewModelInstanceListViewModelByValue(
_ base: ViewModelInstance.ViewModelInstanceHandle,
path: String,
value: ViewModelInstance.ViewModelInstanceHandle
) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.removeViewModelInstanceListViewModelByValue(
base,
path: path,
value: value,
requestID: requestID
)
}
/// Swaps two view model instances in a list property at the specified indices.
///
/// Delegates to the command queue. No listener callback is invoked for this operation.
@MainActor
func swapViewModelInstanceListValues(
_ base: ViewModelInstance.ViewModelInstanceHandle,
path: String,
atIndex: Int32,
withIndex: Int32
) {
let requestID = dependencies.commandQueue.nextRequestID
dependencies.commandQueue.swapViewModelInstanceListValues(
base,
path: path,
atIndex: atIndex,
withIndex: withIndex,
requestID: requestID
)
}
/// Called when view model data is received.
///
/// Listener callback invoked by the command server. Handles both regular continuations
/// (one-time value requests) and stream continuations (property subscriptions). For streams,
/// values are yielded as they arrive. Dispatches to main actor to safely access continuations.
nonisolated public func onViewModelDataReceived(
_ viewModelInstanceHandle: UInt64,
requestID: UInt64,
data: RiveViewModelInstanceData
) {
Task { @MainActor in
if let continuation = continuations.removeValue(forKey: requestID) {
do {
if let stringValue = data.stringValue {
try continuation.resume(with: .success(stringValue))
} else if let numberValue = data.numberValue {
try continuation.resume(with: .success(numberValue.floatValue))
} else if let boolValue = data.boolValue {
try continuation.resume(with: .success(boolValue.boolValue))
} else if let colorValue = data.colorValue {
let argbValue = colorValue.uint32Value
try continuation.resume(with: .success(Color(argbValue)))
} else {
try continuation.resume(with: .failure(ViewModelInstanceError.missingData))
}
} catch AnyContinuationError.typeMismatch(expected: let expected, actual: let actual) {
try continuation.resume(with: .failure(ViewModelInstanceError.valueMismatch(expected, actual)))
}
return
}
if let streamContinuation = streamContinuations[requestID] {
do {
// Trigger is a special case where we want to yield ()
// and ignore any data values
if case .trigger = data.type {
try streamContinuation.yield(())
} else {
if let stringValue = data.stringValue {
try streamContinuation.yield(stringValue)
} else if let numberValue = data.numberValue {
try streamContinuation.yield(numberValue.floatValue)
} else if let boolValue = data.boolValue {
try streamContinuation.yield(boolValue.boolValue)
} else if let colorValue = data.colorValue {
let argbValue = colorValue.uint32Value
try streamContinuation.yield(Color(argbValue))
} else {
streamContinuation.finish(throwing: ViewModelInstanceError.missingData)
streamContinuations.removeValue(forKey: requestID)
}
}
} catch AnyAsyncThrowingStreamContinuationError.typeMismatch(expected: let expected, actual: let actual) {
streamContinuation.finish(throwing: ViewModelInstanceError.valueMismatch(expected, actual))
streamContinuations.removeValue(forKey: requestID)
} catch {
streamContinuation.finish(throwing: ViewModelInstanceError.error(error))
streamContinuations.removeValue(forKey: requestID)
}
}
}
}
/// Called when a view model instance name is received.
///
/// Listener callback invoked by the command server. Dispatches to main actor to access
/// continuations dictionary and resume the continuation with the instance name.
nonisolated public func onViewModelInstanceNameReceived(
_ viewModelInstanceHandle: UInt64,
requestID: UInt64,
name: String
) {
Task { @MainActor in
if let continuation = continuations.removeValue(forKey: requestID) {
try continuation.resume(with: .success(name))
}
}
}
/// Called when view model list size is received.
///
/// Listener callback invoked by the command server. Dispatches to main actor to access
/// continuations dictionary and resume the continuation with the list size.
nonisolated public func onViewModelListSizeReceived(
_ viewModelInstanceHandle: UInt64,
requestID: UInt64,
path: String,
size: Int
) {
Task { @MainActor in
if let continuation = continuations.removeValue(forKey: requestID) {
try continuation.resume(with: .success(size))
}
}
}
}
extension ViewModelInstanceService {
/// Container for all dependencies required by the view model instance service.
struct Dependencies {
/// The command queue used to send view model instance-related commands to the C++ runtime.
/// The service registers itself as a `ViewModelInstanceListener` observer when calling command
/// queue methods. All operations must be performed on the main thread.
let commandQueue: any CommandQueueProtocol
}
}