mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-09-20 10:05:57 +08:00
wip
This commit is contained in:
parent
7a207bd631
commit
fcae74a01b
|
@ -19,26 +19,25 @@ 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
|
||||||
|
|
||||||
|
NotificationCenter.default.addObserver(forName: ElixirKit.API.ready, object: nil, queue: .current) { n in
|
||||||
|
self.button.isEnabled = true
|
||||||
|
ElixirKit.API.publish("log", "Hello from AppKit!")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ElixirKit.API.addObserver(queue: .current) { (name, data) in
|
||||||
|
switch name {
|
||||||
|
case "log":
|
||||||
|
print("[client] " + data)
|
||||||
|
default:
|
||||||
|
fatalError("unknown event \(name)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ElixirKit.API.start(
|
ElixirKit.API.start(
|
||||||
name: "demo",
|
name: "demo",
|
||||||
readyHandler: {
|
|
||||||
// GUI updates need to happen on the main thread.
|
|
||||||
DispatchQueue.main.sync {
|
|
||||||
self.button.isEnabled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
ElixirKit.API.publish("log", "Hello from AppKit!")
|
|
||||||
|
|
||||||
ElixirKit.API.addObserver(queue: .main) { (name, data) in
|
|
||||||
switch name {
|
|
||||||
case "log":
|
|
||||||
print("[client] " + data)
|
|
||||||
default:
|
|
||||||
fatalError("unknown event \(name)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,22 +10,20 @@ struct Demo {
|
||||||
exit(signal)
|
exit(signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
ElixirKit.API.start(
|
NotificationCenter.default.addObserver(forName: ElixirKit.API.ready, object: nil, queue: .main) { n in
|
||||||
name: "demo",
|
ElixirKit.API.publish("log", "Hello from Swift!")
|
||||||
readyHandler: {
|
|
||||||
ElixirKit.API.publish("log", "Hello from Swift!")
|
|
||||||
|
|
||||||
ElixirKit.API.addObserver(queue: .main) { (name, data) in
|
ElixirKit.API.addObserver(queue: .main) { (name, data) in
|
||||||
switch name {
|
switch name {
|
||||||
case "log":
|
case "log":
|
||||||
print("[client] " + data)
|
print("[client] " + data)
|
||||||
default:
|
default:
|
||||||
fatalError("unknown event \(name)")
|
fatalError("unknown event \(name)")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
|
|
||||||
|
ElixirKit.API.start(name: "demo")
|
||||||
ElixirKit.API.waitUntilExit()
|
ElixirKit.API.waitUntilExit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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.")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue