@tokamakdev/platform-macos-x64 0.1.1-beta.154 → 0.1.1-beta.157

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,21 +8,27 @@ import WebKit
8
8
  import AppKit
9
9
  #endif
10
10
 
11
- struct TokamakPluginError: Error {
12
- let name: String
13
- let message: String
11
+ public struct TokamakPluginError: Error {
12
+ public let name: String
13
+ public let message: String
14
14
  /// JSON details the Worker receives as the error's `cause`.
15
- var cause: Any? = nil
15
+ public var cause: Any?
16
16
 
17
- static func notSupported(_ message: String) -> Self {
17
+ public init(name: String, message: String, cause: Any? = nil) {
18
+ self.name = name
19
+ self.message = message
20
+ self.cause = cause
21
+ }
22
+
23
+ public static func notSupported(_ message: String) -> Self {
18
24
  Self(name: "NotSupportedError", message: message)
19
25
  }
20
26
 
21
27
  /// A native screen was needed while the app has no window.
22
- static let noWindow = Self(name: "InvalidStateError", message: "The app has no window")
28
+ public static let noWindow = Self(name: "InvalidStateError", message: "The app has no window")
23
29
 
24
30
  /// UI was needed while the app cannot show it.
25
- static let needsUI = Self(
31
+ public static let needsUI = Self(
26
32
  name: "NeedsUIError",
27
33
  message: "The app cannot show UI while it is in the background"
28
34
  )
@@ -31,7 +37,7 @@ struct TokamakPluginError: Error {
31
37
  extension TokamakPluginError {
32
38
  /// `error` for the Worker: itself, or an `OperationError` whose cause has
33
39
  /// the platform error's domain and code.
34
- init(_ error: any Error) {
40
+ public init(_ error: any Error) {
35
41
  if let error = error as? Self {
36
42
  self = error
37
43
  return
@@ -43,24 +49,30 @@ extension TokamakPluginError {
43
49
  }
44
50
 
45
51
  /// The error as JSON: its name, message and cause.
46
- var json: [String: Any] {
52
+ public var json: [String: Any] {
47
53
  var json: [String: Any] = ["name": name, "message": message]
48
54
  json["cause"] = cause
49
55
  return json
50
56
  }
51
57
  }
52
58
 
53
- typealias TokamakPluginReply = (Result<Any?, TokamakPluginError>) -> Void
54
- typealias TokamakPluginBytesReply = (TokamakPluginResult) -> Void
59
+ public typealias TokamakPluginReply = (Result<Any?, TokamakPluginError>) -> Void
60
+ public typealias TokamakPluginBytesReply = (TokamakPluginResult) -> Void
55
61
 
56
- struct TokamakPluginResult {
57
- let payload: Data
58
- let failed: Bool
59
- let done: Bool
62
+ public struct TokamakPluginResult {
63
+ public let payload: Data
64
+ public let failed: Bool
65
+ public let done: Bool
66
+
67
+ public init(payload: Data, failed: Bool, done: Bool) {
68
+ self.payload = payload
69
+ self.failed = failed
70
+ self.done = done
71
+ }
60
72
  }
61
73
 
62
74
  extension TokamakPluginResult {
63
- init(_ result: Result<Data, TokamakPluginError>, done: Bool) {
75
+ public init(_ result: Result<Data, TokamakPluginError>, done: Bool) {
64
76
  self.done = done
65
77
  switch result {
66
78
  case .success(let payload):
@@ -74,7 +86,7 @@ extension TokamakPluginResult {
74
86
  }
75
87
 
76
88
  /// `value` as a payload: `Data` as it is, and any other value as JSON.
77
- func encodePluginValue(_ value: Any?) -> Result<Data, TokamakPluginError> {
89
+ public func encodePluginValue(_ value: Any?) -> Result<Data, TokamakPluginError> {
78
90
  if let data = value as? Data { return .success(data) }
79
91
  let value = value ?? NSNull()
80
92
  // JSONSerialization raises, rather than throws, for values it cannot encode.
@@ -86,7 +98,7 @@ func encodePluginValue(_ value: Any?) -> Result<Data, TokamakPluginError> {
86
98
  return .success(json)
87
99
  }
88
100
 
89
- func decodePluginValue(_ payload: Data) throws(TokamakPluginError) -> Any {
101
+ public func decodePluginValue(_ payload: Data) throws(TokamakPluginError) -> Any {
90
102
  do { return try JSONSerialization.jsonObject(with: payload, options: .fragmentsAllowed) } catch {
91
103
  throw .init(name: "DataCloneError", message: error.localizedDescription)
92
104
  }
@@ -94,32 +106,59 @@ func decodePluginValue(_ payload: Data) throws(TokamakPluginError) -> Any {
94
106
 
95
107
  #if os(macOS)
96
108
  /// The value of the app's entitlement `key`, or nil when its signature lacks it.
97
- func tokamakEntitlement(_ key: String) -> Any? {
109
+ public func tokamakEntitlement(_ key: String) -> Any? {
98
110
  guard let task = SecTaskCreateFromSelf(nil) else { return nil }
99
111
  return SecTaskCopyValueForEntitlement(task, key as CFString, nil)
100
112
  }
101
113
  #endif
102
114
 
115
+ /// The app that hosts the plugins: the Tokamak shell, or a test's stand-in.
116
+ public protocol TokamakHostApp: AnyObject {
117
+ var webView: WKWebView? { get }
118
+ var resources: TokamakResources { get }
119
+ /// Every plugin, by ID.
120
+ var plugins: [String: any TokamakPlugin] { get }
121
+
122
+ #if os(iOS)
123
+ /// Whether the app is in the foreground. Read it on the main thread.
124
+ var isInForeground: Bool { get }
125
+ #endif
126
+
127
+ /// Delivers the event `name`, whose encoded data is `payload`, to the
128
+ /// Worker's listeners, and calls `completion` on the main thread with their
129
+ /// encoded reply, which is empty when none replied. Fails unless they return
130
+ /// within `timeout`, which includes runtime startup.
131
+ func emit(
132
+ _ name: String, payload: Data, timeout: TimeInterval,
133
+ completion: @escaping (Result<Data, Error>) -> Void)
134
+
135
+ /// Fetches what the app serves at `path` and calls `completion` on the main
136
+ /// thread with the body. Fails unless it arrives within `timeout`, which
137
+ /// includes runtime startup.
138
+ func fetch(
139
+ _ path: String, timeout: TimeInterval, completion: @escaping (Result<Data, Error>) -> Void)
140
+ }
141
+
103
142
  /// The app as one plugin sees it.
104
- final class TokamakHost {
105
- private unowned let app: TokamakApp
143
+ public final class TokamakHost {
144
+ private unowned let app: any TokamakHostApp
106
145
  private let plugin: String
107
146
 
108
- init(app: TokamakApp, plugin: String) {
147
+ public init(app: any TokamakHostApp, plugin: String) {
109
148
  self.app = app
110
149
  self.plugin = plugin
111
150
  }
112
151
 
113
- var webView: WKWebView? { app.webView }
152
+ public var webView: WKWebView? { app.webView }
114
153
 
115
154
  /// The native objects plugins share with each other by ID.
116
- var resources: TokamakResources { app.resources }
155
+ public var resources: TokamakResources { app.resources }
117
156
 
118
157
  /// Throws `NeedsUIError` unless the app can show UI now: on iOS, while it
119
158
  /// is in the foreground. A hidden or minimised macOS app still shows system
120
159
  /// prompts. Call it on the main thread, where the plugin decides to show
121
160
  /// UI.
122
- func requireUI() throws(TokamakPluginError) {
161
+ public func requireUI() throws(TokamakPluginError) {
123
162
  #if os(iOS)
124
163
  guard app.isInForeground else { throw .needsUI }
125
164
  #endif
@@ -129,7 +168,7 @@ final class TokamakHost {
129
168
  /// The view controller to present a native screen from: the top of the
130
169
  /// app window's presentation stack. Throws `NeedsUIError` unless the app
131
170
  /// can show UI now. Call it on the main thread.
132
- func presenter() throws(TokamakPluginError) -> UIViewController {
171
+ public func presenter() throws(TokamakPluginError) -> UIViewController {
133
172
  try requireUI()
134
173
  guard var top = webView?.window?.rootViewController else { throw .noWindow }
135
174
  while let presented = top.presentedViewController { top = presented }
@@ -138,7 +177,7 @@ final class TokamakHost {
138
177
  #else
139
178
  /// The app window, to present a native screen over. Call it on the main
140
179
  /// thread.
141
- func presenter() throws(TokamakPluginError) -> NSWindow {
180
+ public func presenter() throws(TokamakPluginError) -> NSWindow {
142
181
  guard let window = webView?.window else { throw .noWindow }
143
182
  return window
144
183
  }
@@ -149,7 +188,7 @@ final class TokamakHost {
149
188
  /// `completion` on the main thread with their encoded reply, which is empty
150
189
  /// when none replied. Fails unless they return within `timeout`, which
151
190
  /// includes runtime startup. Call it on the main thread.
152
- func emitBytes(
191
+ public func emitBytes(
153
192
  _ name: String,
154
193
  payload: Data,
155
194
  timeout: TimeInterval,
@@ -159,10 +198,10 @@ final class TokamakHost {
159
198
  }
160
199
 
161
200
  /// Delivers `event` to the Worker's listeners of the plugin's event `name`,
162
- /// which `apple/Exports.swift` lists, then calls `completion` on the main
201
+ /// which `Exports.swift` lists, then calls `completion` on the main
163
202
  /// thread. Fails unless they return within `timeout`, which includes runtime
164
203
  /// startup. Call it on the main thread.
165
- func emit(
204
+ public func emit(
166
205
  _ name: String,
167
206
  event: some Encodable,
168
207
  timeout: TimeInterval,
@@ -174,7 +213,7 @@ final class TokamakHost {
174
213
  /// `emit` of an event whose listeners may reply, calling `completion` with
175
214
  /// the reply as the plugin's generated dispatch decodes it, which is a
176
215
  /// `Reply`, or nil when none replied.
177
- func emit<Reply>(
216
+ public func emit<Reply>(
178
217
  _ name: String,
179
218
  event: some Encodable,
180
219
  reply: Reply.Type,
@@ -228,7 +267,7 @@ final class TokamakHost {
228
267
  /// file the app ships or the Worker's response. Calls `completion` on the
229
268
  /// main thread with the body. Fails unless it arrives within `timeout`,
230
269
  /// which includes runtime startup. Call it on the main thread.
231
- func fetch(
270
+ public func fetch(
232
271
  _ path: String,
233
272
  timeout: TimeInterval,
234
273
  completion: @escaping (Result<Data, Error>) -> Void
@@ -238,7 +277,7 @@ final class TokamakHost {
238
277
  }
239
278
 
240
279
  /// A native plugin, created once per process.
241
- protocol TokamakPlugin: AnyObject {
280
+ public protocol TokamakPlugin: AnyObject {
242
281
  var id: String { get }
243
282
 
244
283
  init(host: TokamakHost)
@@ -276,9 +315,9 @@ protocol TokamakPlugin: AnyObject {
276
315
  }
277
316
 
278
317
  extension TokamakPlugin {
279
- func executor(for method: String) -> DispatchQueue { .main }
318
+ public func executor(for method: String) -> DispatchQueue { .main }
280
319
 
281
- func callBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
320
+ public func callBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
282
321
  throws(TokamakPluginError) -> (() -> Void)?
283
322
  {
284
323
  try call(method: method, arguments: decodePluginValue(payload)) {
@@ -286,20 +325,22 @@ extension TokamakPlugin {
286
325
  }
287
326
  }
288
327
 
289
- func callBytes(
328
+ public func callBytes(
290
329
  method: String, payload: Data, body: TokamakPluginBody,
291
330
  reply: @escaping TokamakPluginBytesReply
292
331
  ) throws(TokamakPluginError) -> (() -> Void)? {
293
332
  throw .notSupported("\(id).\(method) does not take a body")
294
333
  }
295
334
 
296
- func subscribeBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
335
+ public func subscribeBytes(
336
+ method: String, payload: Data, reply: @escaping TokamakPluginBytesReply
337
+ )
297
338
  throws(TokamakPluginError) -> (() -> Void)
298
339
  {
299
340
  throw .notSupported("\(id).\(method) is not supported")
300
341
  }
301
342
 
302
- func call(
343
+ public func call(
303
344
  method: String,
304
345
  arguments: Any,
305
346
  reply: @escaping TokamakPluginReply
@@ -308,11 +349,11 @@ extension TokamakPlugin {
308
349
  return nil
309
350
  }
310
351
 
311
- func didRegisterForRemoteNotifications(deviceToken: Data) {}
352
+ public func didRegisterForRemoteNotifications(deviceToken: Data) {}
312
353
 
313
- func didFailToRegisterForRemoteNotifications(error: Error) {}
354
+ public func didFailToRegisterForRemoteNotifications(error: Error) {}
314
355
 
315
- func didReceiveRemoteNotification(
356
+ public func didReceiveRemoteNotification(
316
357
  _ userInfo: [AnyHashable: Any],
317
358
  completion: @escaping (TokamakBackgroundResult) -> Void
318
359
  ) {
@@ -320,10 +361,29 @@ extension TokamakPlugin {
320
361
  }
321
362
  }
322
363
 
364
+ /// What a plugin did with a background event, reported to the system.
365
+ public enum TokamakBackgroundResult {
366
+ case newData
367
+ case noData
368
+ case failed
369
+
370
+ /// New data when any plugin fetched some, otherwise a failure when any
371
+ /// plugin failed.
372
+ public init(combining results: [Self]) {
373
+ if results.contains(.newData) {
374
+ self = .newData
375
+ } else if results.contains(.failed) {
376
+ self = .failed
377
+ } else {
378
+ self = .noData
379
+ }
380
+ }
381
+ }
382
+
323
383
  /// The bytes the Worker streams to a call, which the plugin reads one chunk
324
384
  /// at a time.
325
- final class TokamakPluginBody {
326
- typealias Chunk = (Result<Data?, TokamakPluginError>) -> Void
385
+ public final class TokamakPluginBody {
386
+ public typealias Chunk = (Result<Data?, TokamakPluginError>) -> Void
327
387
 
328
388
  private let queue: DispatchQueue
329
389
  private let pull: () -> Void
@@ -338,7 +398,7 @@ final class TokamakPluginBody {
338
398
  /// Reads the next chunk: its bytes, nil after the last, or the Worker's
339
399
  /// error. `handler` runs on the call's executor; read again only after it
340
400
  /// has.
341
- func read(_ handler: @escaping Chunk) {
401
+ public func read(_ handler: @escaping Chunk) {
342
402
  lock.withLock { reading = handler }
343
403
  pull()
344
404
  }
@@ -362,13 +422,13 @@ final class TokamakPluginBody {
362
422
  }
363
423
 
364
424
  /// What the Worker asked a plugin for.
365
- enum TokamakPluginRequest {
425
+ public enum TokamakPluginRequest {
366
426
  case call, callWithBody, subscribe
367
427
  }
368
428
 
369
429
  /// Runs the Worker's requests on the executor each plugin chooses, and passes
370
430
  /// replies straight to the runtime.
371
- final class TokamakPluginRequests<Key: Hashable> {
431
+ public final class TokamakPluginRequests<Key: Hashable> {
372
432
  /// A pending call or subscription. Only its serial executor touches
373
433
  /// `cancellation`, so a cancellation runs after the operation started.
374
434
  private final class Request {
@@ -386,7 +446,7 @@ final class TokamakPluginRequests<Key: Hashable> {
386
446
 
387
447
  /// Routes requests to `plugins`, sending their results with `send` and
388
448
  /// asking the Worker for the next chunk of a call's body with `pull`.
389
- init(
449
+ public init(
390
450
  plugins: [String: any TokamakPlugin],
391
451
  send: @escaping (Key, TokamakPluginResult) -> Void,
392
452
  pull: @escaping (Key) -> Void = { _ in }
@@ -396,7 +456,7 @@ final class TokamakPluginRequests<Key: Hashable> {
396
456
  self.pull = pull
397
457
  }
398
458
 
399
- func run(
459
+ public func run(
400
460
  _ key: Key, _ kind: TokamakPluginRequest, plugin: String, method: String, payload: Data
401
461
  ) {
402
462
  guard let target = plugins[plugin], !method.isEmpty else {
@@ -438,11 +498,11 @@ final class TokamakPluginRequests<Key: Hashable> {
438
498
 
439
499
  /// Answers the pending read of the call `key`'s body with the chunk the
440
500
  /// Worker sent.
441
- func chunk(_ key: Key, _ chunk: TokamakPluginResult) {
501
+ public func chunk(_ key: Key, _ chunk: TokamakPluginResult) {
442
502
  lock.withLock { pending[key]?.body }?.receive(chunk)
443
503
  }
444
504
 
445
- func cancel(_ key: Key) {
505
+ public func cancel(_ key: Key) {
446
506
  if let request = lock.withLock({ pending.removeValue(forKey: key) }) { cancel(request) }
447
507
  }
448
508
 
@@ -2,13 +2,15 @@ import Foundation
2
2
 
3
3
  /// Native objects that plugins share by ID. Each is kept until the
4
4
  /// subscription that retained it ends.
5
- final class TokamakResources {
5
+ public final class TokamakResources {
6
6
  private let lock = NSLock()
7
7
  private var objects: [String: Any] = [:]
8
8
 
9
+ public init() {}
10
+
9
11
  /// Keeps `object`, replies with its ID, and returns the release to return
10
12
  /// from `subscribe`.
11
- func retain(_ object: Any, reply: TokamakPluginBytesReply) -> () -> Void {
13
+ public func retain(_ object: Any, reply: TokamakPluginBytesReply) -> () -> Void {
12
14
  let id = UUID().uuidString
13
15
  lock.withLock { objects[id] = object }
14
16
  reply(TokamakPluginResult(encodePluginValue(id), done: false))
@@ -16,7 +18,7 @@ final class TokamakResources {
16
18
  }
17
19
 
18
20
  /// The object retained as `id`.
19
- func get<Object>(_ id: Any, as type: Object.Type) throws(TokamakPluginError) -> Object {
21
+ public func get<Object>(_ id: Any, as type: Object.Type) throws(TokamakPluginError) -> Object {
20
22
  guard let id = id as? String, let object = lock.withLock({ objects[id] }) else {
21
23
  throw TokamakPluginError(name: "InvalidStateError", message: "The native reference is closed")
22
24
  }
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/macos-x64/tools/tokamak-swift-describe
3
- size: 34569672 bytes
4
- sha256: 020048e744fe97f7c874aec5395f0ca717cacd52c7c038019e4c1257e46900c3
3
+ size: 34571400 bytes
4
+ sha256: 7af5bd818415642ee9cb96286538937466187797706cc72c07e478829973eebd
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-macos-x64",
3
- "version": "0.1.1-beta.154",
3
+ "version": "0.1.1-beta.157",
4
4
  "description": "The tokamak platform pack for macos-x64",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,4 +0,0 @@
1
- framework module TokamakRuntime {
2
- umbrella header "TokamakRuntime.h"
3
- export *
4
- }