Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Tests/StackNudgePanelCoreTests/ExtensionHostServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ final class ExtensionHostServiceTests: XCTestCase {
XCTAssertTrue(first.messages.filter { $0["type"] as? String == "action" }.isEmpty)
}

// Running again, the tab still shows the dead process's document until
// the new one sends its own, and a key resolved against it would reach a
// process that never offered that action.
func testAKeyWaitsForTheRestartedProcesssOwnDocument() {
tiers = ["exchange": .service]
let host = host()
host.load(.init(installed: [manifest("exchange")]))
spawner.last.ready()
spawner.last.say(#"{"type":"document","document":{"schema":1,"actions":[{"id":"sync","label":"Sync","key":"s"}]}}"#)
spawner.last.exit(.exited(1))
if case .restarting(let delay) = host.services["exchange"] { clock.advance(delay) }
let second = spawner.last
second.ready()
XCTAssertEqual(host.services["exchange"], .running)
XCTAssertNotNil(host.pane("exchange").document)
XCTAssertFalse(host.tabKeys("exchange").actions)
XCTAssertFalse(host.handle(key: "s", on: "exchange"))
second.say(#"{"type":"document","document":{"schema":1,"actions":[{"id":"sync","label":"Sync","key":"s"}]}}"#)
XCTAssertTrue(host.handle(key: "s", on: "exchange"))
XCTAssertEqual(second.messages.filter { $0["type"] as? String == "action" }.count, 1)
}

// MARK: - Documents

func testADocumentFillsThePaneLikeAFetchDoes() {
Expand Down
5 changes: 3 additions & 2 deletions docs/design/service-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ user saves the extension's settings, so apply each one as it comes. Events are d
`document` exactly as a view's is: the selected row's binding first, then the
document's. It's what a view gets as `run --action approve --row d1`, and `row`
is left out for a document-level action. Sent only while the extension is
running; a key pressed while it starts or restarts sends nothing, since the
document on screen came from the process before. It grants nothing, so it needs
running and has sent a document of its own: a key pressed while it starts or
restarts, or after a restart before its first document, sends nothing, since
the document on screen came from the process before. It grants nothing, so it needs
no capability. Additive: an extension that predates it skips the type.

**Extension → app (stdout)**
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ are ignored rather than queued.
A service extension isn't run again. Its running process is sent the same
press as a line, `{"type": "action", "id": "open", "row": "h1"}`, and answers
with a new `document` if what it shows has changed. A press while it's starting
or restarting sends nothing. See the design note's
or restarting, or before a restarted process has sent its first document, sends
nothing. See the design note's
[Protocol](design/service-extensions.md#protocol).

#### Key bindings are requested, not granted
Expand Down
12 changes: 11 additions & 1 deletion panel/ExtensionHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ final class ExtensionHost: ObservableObject {
private var stopping: [String: ServiceSupervisor] = [:]
private var wanted: [String: (manifest: ExtensionManifest, allowed: [ExtensionCapability])] = [:]
private var shuttingDown = false
// Services whose tab shows a document from the process running now. One
// started since its last document still shows the dead process's, which
// its keys must not be resolved against.
private var freshDocument: Set<String> = []
private var extensionLog = ExtensionLog()
private let writeLog: (String) -> Void
private let now: () -> TimeInterval
Expand Down Expand Up @@ -569,7 +573,11 @@ final class ExtensionHost: ObservableObject {
}

func tabKeys(_ id: String) -> TabKeys {
Self.tabKeys(mode: manifest(id)?.mode ?? .view, state: services[id])
var keys = Self.tabKeys(mode: manifest(id)?.mode ?? .view, state: services[id])
// Running again after a restart, but still showing what the process
// before it sent: its actions wait for the new process's document.
if manifest(id)?.mode == .service, !freshDocument.contains(id) { keys.actions = false }
return keys
}

// A fresh supervisor, so a fresh crash window: the user has asked, and
Expand Down Expand Up @@ -679,6 +687,7 @@ final class ExtensionHost: ObservableObject {
services[id] = state
serviceStateChanged(id, state)
case .document(let document):
freshDocument.insert(id)
// Exactly the path a view fetch takes, so the tab can't tell them apart.
finish(id, .ok(document))
case .malformedDocument(let why):
Expand Down Expand Up @@ -712,6 +721,7 @@ final class ExtensionHost: ObservableObject {
var pane = self.pane(id)
switch state {
case .starting:
freshDocument.remove(id)
if pane.document == nil { pane.status = .loading }
case .running:
pane.status = pane.document == nil ? .loading : .idle
Expand Down
6 changes: 3 additions & 3 deletions panel/ServiceSupervisor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ final class ServiceSupervisor {
}
}

// Passes on a key the user pressed on the tab. Dropped unless running: a
// press made while it starts or restarts was made against a document from
// a process that has gone, so it may not mean the same thing to the next.
// Passes on a key the user pressed on the tab. The host offers keys only
// once this process has sent a document of its own (ExtensionHost.tabKeys);
// this drops one still on its way when the process stopped.
func perform(action id: String, row: String?) {
perform { [weak self] in
guard let self, self.state == .running else { return }
Expand Down
Loading