This commit is contained in:
Wojtek Mach 2023-06-20 12:30:11 +02:00
parent 7a207bd631
commit fcae74a01b
5 changed files with 47 additions and 37 deletions

View file

@ -19,17 +19,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
button = NSButton(title: "Press me!", target: self, action: #selector(pressMe)) button = NSButton(title: "Press me!", target: self, action: #selector(pressMe))
button.isEnabled = false button.isEnabled = false
ElixirKit.API.start( NotificationCenter.default.addObserver(forName: ElixirKit.API.ready, object: nil, queue: .current) { n in
name: "demo",
readyHandler: {
// GUI updates need to happen on the main thread.
DispatchQueue.main.sync {
self.button.isEnabled = true self.button.isEnabled = true
}
ElixirKit.API.publish("log", "Hello from AppKit!") ElixirKit.API.publish("log", "Hello from AppKit!")
ElixirKit.API.addObserver(queue: .main) { (name, data) in }
ElixirKit.API.addObserver(queue: .current) { (name, data) in
switch name { switch name {
case "log": case "log":
print("[client] " + data) print("[client] " + data)
@ -37,8 +33,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
fatalError("unknown event \(name)") fatalError("unknown event \(name)")
} }
} }
},
ElixirKit.API.start(
name: "demo",
terminationHandler: { _ in terminationHandler: { _ in
print("terminating...")
NSApp.terminate(nil) NSApp.terminate(nil)
} }
) )
@ -69,11 +68,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
func applicationWillTerminate(_ aNotification: Notification) { func applicationWillTerminate(_ aNotification: Notification) {
ElixirKit.API.stop() print("applicationWillTerminate")
/* ElixirKit.API.stop() */
} }
@objc @objc
func pressMe() { func pressMe() {
print("[client] button pressed!")
ElixirKit.API.publish("log", "button pressed!") ElixirKit.API.publish("log", "button pressed!")
} }
} }

View file

@ -10,9 +10,7 @@ struct Demo {
exit(signal) exit(signal)
} }
ElixirKit.API.start( NotificationCenter.default.addObserver(forName: ElixirKit.API.ready, object: nil, queue: .main) { n in
name: "demo",
readyHandler: {
ElixirKit.API.publish("log", "Hello from Swift!") ElixirKit.API.publish("log", "Hello from Swift!")
ElixirKit.API.addObserver(queue: .main) { (name, data) in ElixirKit.API.addObserver(queue: .main) { (name, data) in
@ -24,8 +22,8 @@ struct Demo {
} }
} }
} }
)
ElixirKit.API.start(name: "demo")
ElixirKit.API.waitUntilExit() ElixirKit.API.waitUntilExit()
} }
} }

View file

@ -5,6 +5,8 @@ public class API {
static var process: Process? static var process: Process?
private static var release: Release? private static var release: Release?
public static let ready = NSNotification.Name("elixirkit.ready")
static let didReceiveEvent = NSNotification.Name("elixirkit.event") static let didReceiveEvent = NSNotification.Name("elixirkit.event")
public static var isRunning: Bool { public static var isRunning: Bool {
@ -13,13 +15,14 @@ public class API {
} }
} }
internal static var isReady = false
public static func start( public static func start(
name: String, name: String,
logPath: String? = nil, logPath: String? = nil,
readyHandler: @escaping () -> Void,
terminationHandler: ((Process) -> Void)? = nil) { terminationHandler: ((Process) -> Void)? = nil) {
release = Release(name: name, logPath: logPath, readyHandler: readyHandler, terminationHandler: terminationHandler) release = Release(name: name, logPath: logPath, terminationHandler: terminationHandler)
} }
public static func publish(_ name: String, _ data: String) { public static func publish(_ name: String, _ data: String) {
@ -47,7 +50,6 @@ private class Release {
let logger: Logger let logger: Logger
let listener: NWListener let listener: NWListener
var connection: Connection? var connection: Connection?
let readyHandler: () -> Void
let semaphore = DispatchSemaphore(value: 0) let semaphore = DispatchSemaphore(value: 0)
var isRunning: Bool { var isRunning: Bool {
@ -59,10 +61,8 @@ private class Release {
init( init(
name: String, name: String,
logPath: String? = nil, logPath: String? = nil,
readyHandler: @escaping () -> Void,
terminationHandler: ((Process) -> Void)? = nil) { terminationHandler: ((Process) -> Void)? = nil) {
self.readyHandler = readyHandler
logger = Logger(logPath: logPath) logger = Logger(logPath: logPath)
listener = try! NWListener(using: .tcp, on: .any) listener = try! NWListener(using: .tcp, on: .any)
@ -134,7 +134,8 @@ private class Release {
private func didAccept(conn: NWConnection) { private func didAccept(conn: NWConnection) {
self.connection = Connection(conn: conn, logger: logger) self.connection = Connection(conn: conn, logger: logger)
readyHandler() API.isReady = true
NotificationCenter.default.post(name: API.ready, object: nil)
} }
} }

View file

@ -61,3 +61,13 @@ if Mix.target() == :app do
end end
end end
end end
if Mix.target() == :app or Mix.env() == :test do
defmodule LivebookApp.Utils do
@moduledoc false
def url_for_open("") do
(LivebookWeb.Endpoint.access_url())
end
end
end

View file

@ -140,7 +140,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
struct NewNotebookIntent: AppIntent { struct NewNotebookIntent: AppIntent {
static var title: LocalizedStringResource = "Create New Livebook (v2)" static var title: LocalizedStringResource = "Create New Livebook"
static var description = IntentDescription("Create a new notebook.") static var description = IntentDescription("Create a new notebook.")