@tokamakdev/platform-ios-simulator-x64 0.1.1-beta.89 → 0.1.1-beta.90

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.
@@ -49,6 +49,9 @@ void tokamak_runtime_stop(void *runtime);
49
49
  bool tokamak_runtime_emit(const void *runtime, const char *name,
50
50
  const char *event, uint64_t timeout_ms,
51
51
  TokamakBytes *reply, char *error, size_t error_len);
52
+ bool tokamak_runtime_fetch(const void *runtime, const char *path,
53
+ uint64_t timeout_ms, TokamakBytes *body, char *error,
54
+ size_t error_len);
52
55
  bool tokamak_runtime_set_foreground(const void *runtime, bool foreground);
53
56
  bool tokamak_runtime_is_foreground(const void *runtime);
54
57
  void tokamak_runtime_reply(const void *runtime, uint64_t id, const char *result);
@@ -117,15 +117,39 @@ final class TokamakApp {
117
117
  completion: @escaping (Result<Any?, Error>) -> Void
118
118
  ) {
119
119
  let deadline = Date() + timeout
120
+ withRuntime(on: queue, completion: completion) { runtime in
121
+ try runtime.emit(name, event: event, timeout: deadline.timeIntervalSinceNow)
122
+ }
123
+ }
124
+
125
+ /// Fetches what the app serves at `path` and calls `completion` on the main
126
+ /// thread with the body. Fails unless it arrives within `timeout`, which
127
+ /// includes runtime startup. Call it on the main thread.
128
+ func fetch(
129
+ _ path: String,
130
+ timeout: TimeInterval,
131
+ completion: @escaping (Result<Data, Error>) -> Void
132
+ ) {
133
+ let deadline = Date() + timeout
134
+ withRuntime(on: .global(qos: .userInitiated), completion: completion) { runtime in
135
+ try runtime.fetch(path, timeout: deadline.timeIntervalSinceNow)
136
+ }
137
+ }
138
+
139
+ /// Runs `work` with the started runtime on `queue`, and calls `completion`
140
+ /// on the main thread with its result.
141
+ private func withRuntime<Value>(
142
+ on queue: DispatchQueue,
143
+ completion: @escaping (Result<Value, Error>) -> Void,
144
+ work: @escaping (RuntimeHandle) throws -> Value
145
+ ) {
120
146
  whenStarted { result in
121
147
  switch result {
122
148
  case .failure(let error):
123
149
  completion(.failure(error))
124
150
  case .success(let runtime):
125
151
  queue.async {
126
- let outcome = Result {
127
- try runtime.emit(name, event: event, timeout: deadline.timeIntervalSinceNow)
128
- }
152
+ let outcome = Result { try work(runtime) }
129
153
  DispatchQueue.main.async { completion(outcome) }
130
154
  }
131
155
  }
@@ -416,6 +440,30 @@ final class RuntimeHandle {
416
440
  return value is NSNull ? nil : value
417
441
  }
418
442
 
443
+ /// What the app serves at `path`, blocking until it arrives or `timeout`
444
+ /// passes.
445
+ func fetch(_ path: String, timeout: TimeInterval) throws -> Data {
446
+ var body = TokamakBytes()
447
+ var error = [CChar](repeating: 0, count: 1024)
448
+ let succeeded = path.withCString { path in
449
+ error.withUnsafeMutableBufferPointer { error in
450
+ tokamak_runtime_fetch(
451
+ handle,
452
+ path,
453
+ UInt64(max(timeout, 0) * 1000),
454
+ &body,
455
+ error.baseAddress,
456
+ error.count
457
+ )
458
+ }
459
+ }
460
+ defer { tokamak_bytes_free(body) }
461
+ guard succeeded else {
462
+ throw RuntimeError.runtime(String(cString: error))
463
+ }
464
+ return body.contents ?? Data()
465
+ }
466
+
419
467
  /// Records whether the app is in the foreground, which the Worker's
420
468
  /// `getLifecycleStage()` reports from then on, and returns whether that
421
469
  /// changed.
@@ -51,6 +51,18 @@ final class TokamakHost {
51
51
  ) {
52
52
  app.emit("\(plugin).\(name)", event: event, timeout: timeout, completion: completion)
53
53
  }
54
+
55
+ /// Fetches what the app serves at `path`, as its WebView would load it: a
56
+ /// file the app ships or the Worker's response. Calls `completion` on the
57
+ /// main thread with the body. Fails unless it arrives within `timeout`,
58
+ /// which includes runtime startup. Call it on the main thread.
59
+ func fetch(
60
+ _ path: String,
61
+ timeout: TimeInterval,
62
+ completion: @escaping (Result<Data, Error>) -> Void
63
+ ) {
64
+ app.fetch(path, timeout: timeout, completion: completion)
65
+ }
54
66
  }
55
67
 
56
68
  /// A native plugin, created once per process when the app launches. The page
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-ios-simulator-x64",
3
- "version": "0.1.1-beta.89",
3
+ "version": "0.1.1-beta.90",
4
4
  "description": "The tokamak platform pack for ios-simulator-x64",
5
5
  "repository": {
6
6
  "type": "git",