@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
swift 72 lines 2.11 KB
Raw
1 import ExpoModulesCore
2 import Foundation
3
4 public final class T3PreviewTunnelModule: Module {
5 public func definition() -> ModuleDefinition {
6 Name("T3PreviewTunnel")
7
8 Function("resolveAccess") { (requestId: String, url: String?) in
9 T3TunnelAccess.shared.resolve(requestId: requestId, url: url)
10 }
11
12 View(T3TunnelWebView.self) {
13 Events("onAccessRequest", "onNavigationChange", "onLoadError", "onPageScroll")
14
15 Prop("environmentId") { (view: T3TunnelWebView, environmentId: String) in
16 view.setEnvironment(environmentId)
17 }
18
19 Prop("topInset") { (view: T3TunnelWebView, inset: Double) in
20 view.setTopInset(CGFloat(inset))
21 }
22
23 Prop("url") { (view: T3TunnelWebView, url: String) in
24 view.load(url)
25 }
26
27 AsyncFunction("loadUrl") { (view: T3TunnelWebView, url: String) in
28 view.navigate(url)
29 }.runOnQueue(.main)
30
31 AsyncFunction("goBack") { (view: T3TunnelWebView) in
32 view.goBack()
33 }.runOnQueue(.main)
34
35 AsyncFunction("goForward") { (view: T3TunnelWebView) in
36 view.goForward()
37 }.runOnQueue(.main)
38
39 AsyncFunction("reload") { (view: T3TunnelWebView) in
40 view.reload()
41 }.runOnQueue(.main)
42 }
43 }
44 }
45
46 /// Pending asks for a tunnel URL. JavaScript owns the environment's credentials and the route in
47 /// use, so every connection asks it for a freshly ticketed URL.
48 final class T3TunnelAccess {
49 static let shared = T3TunnelAccess()
50
51 private let lock = NSLock()
52 private var pending: [String: (String?) -> Void] = [:]
53 private var nextId = 0
54
55 func request(ask: (String) -> Void, completion: @escaping (String?) -> Void) {
56 let requestId: String = lock.withLock {
57 nextId += 1
58 let requestId = "preview-tunnel-\(nextId)"
59 pending[requestId] = completion
60 return requestId
61 }
62 DispatchQueue.global().asyncAfter(deadline: .now() + 15) { [weak self] in
63 self?.resolve(requestId: requestId, url: nil)
64 }
65 ask(requestId)
66 }
67
68 func resolve(requestId: String, url: String?) {
69 let completion = lock.withLock { pending.removeValue(forKey: requestId) }
70 completion?(url)
71 }
72 }