Skip to content

Commit

Permalink
bootstrap 启动when时机
Browse files Browse the repository at this point in the history
  • Loading branch information
foxdock authored Sep 9, 2024
1 parent 78ae46b commit df88f85
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
13 changes: 12 additions & 1 deletion Sources/DFService/core/ServiceProvider.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
/// 应用内服务提供者
/// 在应用不同时机启动服务
open class ServiceProvider {
Expand All @@ -24,6 +25,10 @@ open class ServiceProvider {
return .splash
}

open var sortIndex: Int {
return 1_000
}

var name: String {
return Self.name
}
Expand All @@ -39,7 +44,13 @@ extension ServiceProvider: Equatable {
}
}

extension ServiceProvider: Identifiable {
//extension ServiceProvider: Identifiable {
//
//}
extension ServiceProvider: Comparable {
public static func < (lhs: ServiceProvider, rhs: ServiceProvider) -> Bool {
return lhs.sortIndex < rhs.sortIndex
}

}

Expand Down
12 changes: 12 additions & 0 deletions Sources/DFService/extension/app.string.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extension String: AppCompatible {}

public extension Space where Base == String {
var url: URL? {
if let url = URL(string: base) {
return url
} else if let str = base.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: str) {
return url
}
return nil
}
}
7 changes: 7 additions & 0 deletions Sources/DFService/service/LogService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ extension MockApiServiceImpl: DFLogHandle {
.debug
}
}


public extension Application {
var log: DFLogHandle {
return self[LogService.self]
}
}
33 changes: 33 additions & 0 deletions Sources/DFService/service/RuntimeService.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#if canImport(UIKit)
import UIKit
#endif
public struct RuntimeService: DFServiceKey {
public static var defaultValue: Value = Value()

public struct Value {}

private static var uuid: String?
}

public extension RuntimeService.Value {
Expand All @@ -15,4 +20,32 @@ public extension RuntimeService.Value {
return false
#endif
}

var deviceName: String {
#if canImport(UIKit)
return UIDevice.current.model
#else
return "unkown"
#endif
}
/// 设备id
var genDeviceId: String {
if let value = RuntimeService.uuid {
return value
}
#if canImport(UIKit)
let value = UIDevice.current.identifierForVendor?.uuidString ?? UUID().uuidString
#else
let value = UUID().uuidString
#endif
RuntimeService.uuid = value
return value

}
}

public extension Application {
var runtime: RuntimeService.Value {
return self[RuntimeService.self]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ actor Bootstrap {
}

func bootstrap(_ when: ServiceProvider.ProviderWhen) async {
target = when
if(target < when) {
target = when
}
await bootIfNeed()
}

Expand All @@ -21,7 +23,7 @@ actor Bootstrap {
}
let currentProviders = app.loadProviders.filter { provider in
return !provider.isBooted && provider.when <= current
}
}.sorted()

guard !currentProviders.isEmpty else {
if current < target {
Expand Down Expand Up @@ -83,6 +85,10 @@ class BootstrapServiceProvider: ServiceProvider {
override var when: ServiceProvider.ProviderWhen {
return .eager
}

override var sortIndex: Int {
return 0
}
}


Expand Down
6 changes: 6 additions & 0 deletions Sources/DFService/service/router/RouteRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public extension RouteRequest {
return .page404
}
}

var copy: RouteRequest {
let new = RouteRequest(action: routeAction, url: url)
new.param = param
return new
}
}
public extension Router.RoutePath {
var request: RouteRequest {
Expand Down
4 changes: 1 addition & 3 deletions Sources/DFService/service/router/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public extension Router {
pageBuilderMap[path] = builder
if path == .root {
Thread.app.mainTask {
self.rootPath = RouteRequest(action: rootPath.routeAction, url: rootPath.url)
self.rootPath = self.rootPath.copy
}

}
Expand All @@ -79,12 +79,10 @@ public extension Router {
guard let builder = pageBuilderMap[pid] else {
return AnyView(
page404(request)
.environmentObject(self)
)
}
return AnyView(
builder(request)
.environmentObject(self)
)
}
}

0 comments on commit df88f85

Please sign in to comment.