@@ -11,7 +11,7 @@ actor SearchService {
1111 }
1212 }
1313
14- let serialExecutor : any SerialExecutor
14+ let serialExecutor : OwnedExecutor
1515
1616 // Simple in-memory index: word -> positions
1717 var index : [ String : [ Int ] ] = [ : ]
@@ -21,10 +21,10 @@ actor SearchService {
2121 } ( )
2222
2323 nonisolated var unownedExecutor : UnownedSerialExecutor {
24- return self . serialExecutor. asUnownedSerialExecutor ( )
24+ return self . serialExecutor. unownedExecutor
2525 }
2626
27- init ( serialExecutor: any SerialExecutor ) {
27+ init ( serialExecutor: OwnedExecutor ) {
2828 self . serialExecutor = serialExecutor
2929 }
3030
@@ -233,17 +233,45 @@ final class App {
233233 }
234234}
235235
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+
236254@main struct Main {
237255 @MainActor static var app : App ?
238256
239257 static func main( ) {
240258 JavaScriptEventLoop . installGlobalExecutor ( )
241259 WebWorkerTaskExecutor . installGlobalExecutor ( )
260+ let useDedicatedWorker = !( JSObject . global. disableDedicatedWorker. boolean ?? false )
242261
243262 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)
247275 app = App ( service: service)
248276 }
249277 }
0 commit comments