Cotton code
43 lines · 1.6 KB · SwiftRaw
// DEVELOPER TOOL (Cotton Dev only): a simulator's airplane mode. A simulator has none, and switching the Mac's own// network off would cut every other lane on it, so a walk flips the core's network cut (`developer.cutNetwork`)// with a system notification, as Face ID is matched:////   xcrun simctl spawn <udid> notifyutil -p cotton.dev.network.off    every dial fails as with no network//   xcrun simctl spawn <udid> notifyutil -p cotton.dev.network.on     back online//// Then the loop is told to try now, so the change shows without waiting out its backoff.#if COTTON_DEV && os(iOS)import CottonKitimport Foundationimport notify@MainActorenum DevNetworkCut {    private static var listening = false    static func listen(_ core: any CottonCalling) {        guard !listening else { return }        listening = true        register("cotton.dev.network.off") { cut(true, core) }        register("cotton.dev.network.on") { cut(false, core) }    }    private static func register(_ name: String, _ run: @escaping @MainActor () -> Void) {        var token: Int32 = 0        notify_register_dispatch(name, &token, .main) { _ in            MainActor.assumeIsolated { run() }        }    }    private struct Ask: Encodable, Sendable { let on: Bool }    private static func cut(_ on: Bool, _ core: any CottonCalling) {        Task {            _ = try? await core.callJSON("developer.cutNetwork", Ask(on: on))            Log.write("dev: the network is \(on ? "cut" : "back")")            _ = try? await core.callJSON(CottonMethod.syncNow, EmptyRequest())        }    }}#endif