@@ -11,7 +11,7 @@ actor SearchService {
11
11
}
12
12
}
13
13
14
- let serialExecutor : any SerialExecutor
14
+ let serialExecutor : OwnedExecutor
15
15
16
16
// Simple in-memory index: word -> positions
17
17
var index : [ String : [ Int ] ] = [ : ]
@@ -21,10 +21,10 @@ actor SearchService {
21
21
} ( )
22
22
23
23
nonisolated var unownedExecutor : UnownedSerialExecutor {
24
- return self . serialExecutor. asUnownedSerialExecutor ( )
24
+ return self . serialExecutor. unownedExecutor
25
25
}
26
26
27
- init ( serialExecutor: any SerialExecutor ) {
27
+ init ( serialExecutor: OwnedExecutor ) {
28
28
self . serialExecutor = serialExecutor
29
29
}
30
30
@@ -233,17 +233,45 @@ final class App {
233
233
}
234
234
}
235
235
236
+ /// The fallback executor actor is used when the dedicated worker is not available.
237
+ actor FallbackExecutorActor { }
238
+
239
+ enum OwnedExecutor {
240
+ case dedicated( WebWorkerDedicatedExecutor )
241
+ case fallback( FallbackExecutorActor )
242
+
243
+ var unownedExecutor : UnownedSerialExecutor {
244
+ switch self {
245
+ case . dedicated( let executor) :
246
+ return executor. asUnownedSerialExecutor ( )
247
+ case . fallback( let x) :
248
+ return x. unownedExecutor
249
+ }
250
+ }
251
+ }
252
+
253
+
236
254
@main struct Main {
237
255
@MainActor static var app : App ?
238
256
239
257
static func main( ) {
240
258
JavaScriptEventLoop . installGlobalExecutor ( )
241
259
WebWorkerTaskExecutor . installGlobalExecutor ( )
260
+ let useDedicatedWorker = !( JSObject . global. disableDedicatedWorker. boolean ?? false )
242
261
243
262
Task {
244
- // Create dedicated worker and search service
245
- let dedicatedWorker = try await WebWorkerDedicatedExecutor ( )
246
- let service = SearchService ( serialExecutor: dedicatedWorker)
263
+ let ownedExecutor : OwnedExecutor
264
+ if useDedicatedWorker {
265
+ // Create dedicated worker
266
+ let dedicatedWorker = try await WebWorkerDedicatedExecutor ( )
267
+ ownedExecutor = . dedicated( dedicatedWorker)
268
+ } else {
269
+ // Fallback to main thread executor
270
+ let fallbackExecutor = FallbackExecutorActor ( )
271
+ ownedExecutor = . fallback( fallbackExecutor)
272
+ }
273
+ // Create the service and app
274
+ let service = SearchService ( serialExecutor: ownedExecutor)
247
275
app = App ( service: service)
248
276
}
249
277
}
0 commit comments