@windowkit/appkit 0.9.0 → 0.11.0

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.
@@ -60,9 +60,8 @@
60
60
  #include <string>
61
61
  #include <vector>
62
62
 
63
- // backend.mm: the one backend event callback
64
- bool CALHasBackendCb();
65
- void CALEmitBackendEvent(Napi::Env env, Napi::Object ev);
63
+ // backend.mm / threaded.mm: the one backend event path
64
+ #include "channel.h"
66
65
 
67
66
  static NSString* const kDefaultCategory = @"appkit.default";
68
67
  static NSString* const kUserInfoKey = @"appkit.userInfo";
@@ -158,62 +157,71 @@ struct NotifEvent {
158
157
 
159
158
  static void CallJsEvent(Napi::Env env, Napi::Function, void*, NotifEvent* ev);
160
159
  using EventTsfn = Napi::TypedThreadSafeFunction<void, NotifEvent, CallJsEvent>;
161
- static EventTsfn gEvents;
160
+ // Made in the first environment to reach the centre (ProbeCenter) and called
161
+ // from the centre's own thread, so through CALTsfn: once that environment
162
+ // has begun to end, an event is dropped rather than sent into it.
163
+ static CALTsfn gEvents;
162
164
  static std::vector<NotifEvent*> gHeld;
163
165
 
164
- static Napi::Object EventObject(Napi::Env env, const NotifEvent& ev) {
165
- Napi::Object o = Napi::Object::New(env);
166
- o.Set("type", ev.type);
167
- o.Set("identifier", ev.identifier);
168
- if (!ev.actionId.empty()) o.Set("actionId", ev.actionId);
169
- if (!ev.reason.empty()) o.Set("reason", ev.reason);
170
- o.Set("categoryId", ev.categoryId.empty()
171
- ? env.Null()
172
- : Napi::String::New(env, ev.categoryId));
173
- if (ev.hasUserInfo) {
174
- Napi::Value v = JsonParse(env, [NSString stringWithUTF8String:ev.userInfoJson.c_str()]);
175
- if (env.IsExceptionPending()) {
176
- // not our own output after all; hand it over as it is rather than fail
177
- (void)env.GetAndClearPendingException();
178
- v = Napi::String::New(env, ev.userInfoJson);
179
- }
180
- o.Set("userInfo", v);
181
- }
182
- if (ev.hasUserText) o.Set("userText", ev.userText);
166
+ static CALEvent EventRecord(const NotifEvent& ev) {
167
+ CALEvent o(ev.type.c_str());
168
+ o.Str("identifier", ev.identifier);
169
+ if (!ev.actionId.empty()) o.Str("actionId", ev.actionId);
170
+ if (!ev.reason.empty()) o.Str("reason", ev.reason);
171
+ if (ev.categoryId.empty()) o.Null("categoryId");
172
+ else o.Str("categoryId", ev.categoryId);
173
+ // parsed as it crosses; text that is not our own output after all is
174
+ // handed over as it is rather than fail
175
+ if (ev.hasUserInfo) o.Json("userInfo", ev.userInfoJson);
176
+ if (ev.hasUserText) o.Str("userText", ev.userText);
183
177
  return o;
184
178
  }
185
179
 
186
- static void EmitOrHold(Napi::Env env, NotifEvent* ev) {
187
- if (!CALHasBackendCb()) {
180
+ static void EmitOrHold(NotifEvent* ev) {
181
+ if (!CALListening()) {
188
182
  gHeld.push_back(ev);
189
183
  return;
190
184
  }
191
- Napi::HandleScope scope(env);
192
- CALEmitBackendEvent(env, EventObject(env, *ev));
185
+ CALEmit(EventRecord(*ev));
193
186
  delete ev;
194
187
  }
195
188
 
196
- // Called at the start of pump2 (backend.mm): what arrived before a listener
197
- // goes out ahead of that tick's input, in arrival order.
198
- void CALNotificationsReplayHeld(Napi::Env env) {
199
- if (gHeld.empty() || !CALHasBackendCb()) return;
189
+ // Called at the start of pump2, and as runMain opens the channel
190
+ // (backend.mm): what arrived before a listener goes out ahead of that
191
+ // tick's input, in arrival order.
192
+ void CALNotificationsReplayHeld() {
193
+ if (gHeld.empty() || !CALListening()) return;
200
194
  std::vector<NotifEvent*> held;
201
195
  held.swap(gHeld);
202
- for (NotifEvent* ev : held) EmitOrHold(env, ev);
196
+ for (NotifEvent* ev : held) EmitOrHold(ev);
203
197
  }
204
198
 
205
199
  static void CallJsEvent(Napi::Env env, Napi::Function, void*, NotifEvent* ev) {
206
- if ((napi_env)env == nullptr) { // the function is being torn down
200
+ // the function is being torn down, or the environment is (channel.h)
201
+ if ((napi_env)env == nullptr || !CALCanCallIntoJS(env)) {
207
202
  delete ev;
208
203
  return;
209
204
  }
210
- CALNotificationsReplayHeld(env); // keep order behind anything still held
211
- EmitOrHold(env, ev);
205
+ CALNotificationsReplayHeld(); // keep order behind anything still held
206
+ EmitOrHold(ev);
207
+ // pump mode's inline delivery leaves a listener's exception pending; from
208
+ // a threadsafe function's callback it is the uncaught exception (#62)
209
+ CALRaiseUncaughtIfPending(env);
212
210
  }
213
211
 
214
- // From any thread: onto node's loop, then to the listener or the held list.
212
+ // From any thread: onto node's loop, then to the listener or the held list
213
+ // — in pump mode. With threaded mode's channel open the record goes straight
214
+ // into it from the centre's own thread.
215
215
  static void QueueEvent(NotifEvent* ev) {
216
- if (gEvents.NonBlockingCall(ev) != napi_ok) delete ev;
216
+ if (CALChannelOpen()) {
217
+ CALEmit(EventRecord(*ev));
218
+ delete ev;
219
+ return;
220
+ }
221
+ bool queued = gEvents.Use(false, [ev](napi_threadsafe_function f) {
222
+ return EventTsfn(f).NonBlockingCall(ev);
223
+ });
224
+ if (!queued) delete ev;
217
225
  }
218
226
 
219
227
  static void QueueResponse(UNNotificationResponse* r) {
@@ -285,8 +293,9 @@ static UNNotificationCategory* DefaultCategory() {
285
293
  static void ProbeCenter(Napi::Env env) {
286
294
  if (gProbed) return;
287
295
  gProbed = true;
288
- gEvents = EventTsfn::New(env, "appkit:notifications", 0, 1);
289
- gEvents.Unref(env); // events never hold the loop open by themselves
296
+ EventTsfn events = EventTsfn::New(env, "appkit:notifications", 0, 1);
297
+ events.Unref(env); // events never hold the loop open by themselves
298
+ gEvents = CALTsfn::Watch(env, events);
290
299
  @autoreleasepool {
291
300
  NSBundle* main = NSBundle.mainBundle;
292
301
  if (!main.bundleIdentifier) {
@@ -411,24 +420,26 @@ static Napi::Value NotificationSettings(const Napi::CallbackInfo& info) {
411
420
  Napi::Env env = info.Env();
412
421
  if (!CallbackArg(info, 0, "notificationSettings", "(cb)"))
413
422
  return env.Undefined();
414
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
415
- env, info[0].As<Napi::Function>(), "appkit:notificationSettings", 0, 1);
423
+ CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
424
+ "appkit:notificationSettings");
416
425
  if (!gCenter) {
417
- tsfn.BlockingCall((void*)nullptr,
418
- [](Napi::Env env, Napi::Function cb, void*) {
419
- cb.Call({UnavailableObject(env)});
420
- });
421
- tsfn.Release();
426
+ tsfn.Answer((void*)nullptr, [](Napi::Env env, Napi::Function cb, void*) {
427
+ // an environment on its way out gets no answer (channel.h)
428
+ if (!CALCanCallIntoJS(env)) return;
429
+ CALCallJS(env, cb, {UnavailableObject(env)});
430
+ });
422
431
  return env.Undefined();
423
432
  }
424
433
  [gCenter getNotificationSettingsWithCompletionHandler:^(
425
434
  UNNotificationSettings* s) {
426
435
  void* p = (void*)CFBridgingRetain(s);
427
- tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
436
+ bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
428
437
  UNNotificationSettings* s = CFBridgingRelease(p);
429
- cb.Call({SettingsObject(env, s)});
438
+ // an environment on its way out gets no answer (channel.h)
439
+ if (!CALCanCallIntoJS(env)) return;
440
+ CALCallJS(env, cb, {SettingsObject(env, s)});
430
441
  });
431
- tsfn.Release();
442
+ if (!queued) CFRelease(p);
432
443
  }];
433
444
  return env.Undefined();
434
445
  }
@@ -493,23 +504,30 @@ static Napi::Value RequestNotificationAuthorization(
493
504
  if (!CallbackArg(info, 1, fn, "(options, cb)")) return env.Undefined();
494
505
  UNUserNotificationCenter* center = CenterOrThrow(env, fn);
495
506
  if (!center) return env.Undefined();
496
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
497
- env, info[1].As<Napi::Function>(), "appkit:requestNotificationAuthorization",
498
- 0, 1);
507
+ CALTsfn tsfn = CALTsfn::New(env, info[1].As<Napi::Function>(),
508
+ "appkit:requestNotificationAuthorization");
499
509
  [center requestAuthorizationWithOptions:opts
500
510
  completionHandler:^(BOOL granted, NSError* error) {
501
511
  Outcome* out = new Outcome{(bool)granted, error};
502
- tsfn.BlockingCall(
512
+ bool queued = tsfn.Answer(
503
513
  out, [](Napi::Env env, Napi::Function cb,
504
514
  Outcome* out) {
505
515
  if (out->ok && gCenter && gCategories) {
506
516
  [gCenter setNotificationCategories:gCategories];
507
517
  }
508
- cb.Call({Napi::Boolean::New(env, out->ok),
509
- ErrorValue(env, out->error)});
518
+ // an environment on its way out gets no
519
+ // answer (channel.h); the categories are the
520
+ // process's, re-applied either way
521
+ if (!CALCanCallIntoJS(env)) {
522
+ delete out;
523
+ return;
524
+ }
525
+ CALCallJS(env, cb,
526
+ {Napi::Boolean::New(env, out->ok),
527
+ ErrorValue(env, out->error)});
510
528
  delete out;
511
529
  });
512
- tsfn.Release();
530
+ if (!queued) delete out;
513
531
  }];
514
532
  return env.Undefined();
515
533
  }
@@ -639,19 +657,21 @@ static Napi::Value NotificationCategories(const Napi::CallbackInfo& info) {
639
657
  if (!CallbackArg(info, 0, fn, "(cb)")) return env.Undefined();
640
658
  UNUserNotificationCenter* center = CenterOrThrow(env, fn);
641
659
  if (!center) return env.Undefined();
642
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
643
- env, info[0].As<Napi::Function>(), "appkit:notificationCategories", 0, 1);
660
+ CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
661
+ "appkit:notificationCategories");
644
662
  [center getNotificationCategoriesWithCompletionHandler:^(
645
663
  NSSet<UNNotificationCategory*>* cats) {
646
664
  void* p = (void*)CFBridgingRetain(cats);
647
- tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
665
+ bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
648
666
  NSSet<UNNotificationCategory*>* cats = CFBridgingRelease(p);
667
+ // an environment on its way out gets no answer (channel.h)
668
+ if (!CALCanCallIntoJS(env)) return;
649
669
  Napi::Array out = Napi::Array::New(env);
650
670
  uint32_t n = 0;
651
671
  for (UNNotificationCategory* c in cats) out.Set(n++, CategoryObject(env, c));
652
- cb.Call({out});
672
+ CALCallJS(env, cb, {out});
653
673
  });
654
- tsfn.Release();
674
+ if (!queued) CFRelease(p);
655
675
  }];
656
676
  return env.Undefined();
657
677
  }
@@ -744,18 +764,23 @@ static Napi::Value Post(const Napi::CallbackInfo& info, size_t propsAt,
744
764
  if (!hasCb) {
745
765
  [center addNotificationRequest:req withCompletionHandler:nil];
746
766
  } else {
747
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
748
- env, info[cbAt].As<Napi::Function>(), "appkit:postNotification", 0,
749
- 1);
767
+ CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
768
+ "appkit:postNotification");
750
769
  [center addNotificationRequest:req
751
770
  withCompletionHandler:^(NSError* error) {
752
771
  Outcome* out = new Outcome{error == nil, error};
753
- tsfn.BlockingCall(
772
+ bool queued = tsfn.Answer(
754
773
  out, [](Napi::Env env, Napi::Function cb, Outcome* out) {
755
- cb.Call({ErrorValue(env, out->error)});
774
+ // an environment on its way out gets no answer
775
+ // (channel.h)
776
+ if (!CALCanCallIntoJS(env)) {
777
+ delete out;
778
+ return;
779
+ }
780
+ CALCallJS(env, cb, {ErrorValue(env, out->error)});
756
781
  delete out;
757
782
  });
758
- tsfn.Release();
783
+ if (!queued) delete out;
759
784
  }];
760
785
  }
761
786
  return Napi::String::New(env, req.identifier.UTF8String);
@@ -819,13 +844,15 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
819
844
  if (!CallbackArg(info, 0, fn, "(cb)")) return env.Undefined();
820
845
  UNUserNotificationCenter* center = CenterOrThrow(env, fn);
821
846
  if (!center) return env.Undefined();
822
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
823
- env, info[0].As<Napi::Function>(), "appkit:deliveredNotifications", 0, 1);
847
+ CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
848
+ "appkit:deliveredNotifications");
824
849
  [center getDeliveredNotificationsWithCompletionHandler:^(
825
850
  NSArray<UNNotification*>* list) {
826
851
  void* p = (void*)CFBridgingRetain(list);
827
- tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
852
+ bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
828
853
  NSArray<UNNotification*>* list = CFBridgingRelease(p);
854
+ // an environment on its way out gets no answer (channel.h)
855
+ if (!CALCanCallIntoJS(env)) return;
829
856
  Napi::Array out = Napi::Array::New(env);
830
857
  uint32_t n = 0;
831
858
  for (UNNotification* note in list) {
@@ -854,9 +881,9 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
854
881
  o.Set("date", note.date.timeIntervalSince1970 * 1000.0);
855
882
  out.Set(n++, o);
856
883
  }
857
- cb.Call({out});
884
+ CALCallJS(env, cb, {out});
858
885
  });
859
- tsfn.Release();
886
+ if (!queued) CFRelease(p);
860
887
  }];
861
888
  return env.Undefined();
862
889
  }
@@ -56,6 +56,8 @@
56
56
  #include <cstring>
57
57
  #include <string>
58
58
 
59
+ #include "channel.h" // CALOnUI, CALCanCallIntoJS
60
+
59
61
  // src/calendars.mm — the change observer the process's store gets as it is
60
62
  // created, so EKEventStoreChangedNotification is watched from the store's
61
63
  // first moment whichever verb made it.
@@ -294,21 +296,24 @@ struct Reply {
294
296
  bool granted;
295
297
  };
296
298
 
297
- static void Answer(Napi::ThreadSafeFunction tsfn, const char* status,
298
- bool granted) {
299
+ static void Answer(const CALTsfn& tsfn, const char* status, bool granted) {
299
300
  Reply* r = new Reply{status, granted};
300
- napi_status st = tsfn.BlockingCall(
301
+ bool queued = tsfn.Answer(
301
302
  r, [](Napi::Env env, Napi::Function cb, Reply* r) {
302
- cb.Call({Napi::Boolean::New(env, r->granted),
303
- Napi::String::New(env, r->status)});
303
+ // an environment on its way out gets no answer (channel.h)
304
+ if (!CALCanCallIntoJS(env)) {
305
+ delete r;
306
+ return;
307
+ }
308
+ CALCallJS(env, cb, {Napi::Boolean::New(env, r->granted),
309
+ Napi::String::New(env, r->status)});
304
310
  delete r;
305
311
  });
306
- if (st != napi_ok) delete r;
307
- tsfn.Release();
312
+ if (!queued) delete r;
308
313
  }
309
314
 
310
315
  // For every kind but the EventKit pair, granted is the one full grant.
311
- static void Answer(Napi::ThreadSafeFunction tsfn, const char* status) {
316
+ static void Answer(const CALTsfn& tsfn, const char* status) {
312
317
  Answer(tsfn, status, strcmp(status, kAuthorized) == 0);
313
318
  }
314
319
 
@@ -319,7 +324,7 @@ static void Answer(Napi::ThreadSafeFunction tsfn, const char* status) {
319
324
  @interface CALLocationRequest : NSObject <CLLocationManagerDelegate> {
320
325
  @public
321
326
  CLLocationManager* mgr_;
322
- Napi::ThreadSafeFunction tsfn_;
327
+ CALTsfn tsfn_;
323
328
  bool done_;
324
329
  }
325
330
  @end
@@ -338,15 +343,20 @@ static NSMutableArray<CALLocationRequest*>* gLocationRequests = nil;
338
343
  }
339
344
  @end
340
345
 
341
- static void StartLocationRequest(Napi::ThreadSafeFunction tsfn) {
342
- CALLocationRequest* r = [CALLocationRequest new];
343
- r->tsfn_ = tsfn;
344
- r->done_ = false;
345
- r->mgr_ = [CLLocationManager new];
346
- r->mgr_.delegate = r;
347
- if (!gLocationRequests) gLocationRequests = [NSMutableArray new];
348
- [gLocationRequests addObject:r];
349
- [r->mgr_ requestWhenInUseAuthorization];
346
+ // The manager reports on the run loop of the thread that made it, and a
347
+ // worker has none: it is made on the UI thread (inline in pump mode, a
348
+ // command in threaded mode), where the delegate call then arrives.
349
+ static void StartLocationRequest(CALTsfn tsfn) { // by value: the block copies it
350
+ CALOnUI(^{
351
+ CALLocationRequest* r = [CALLocationRequest new];
352
+ r->tsfn_ = tsfn;
353
+ r->done_ = false;
354
+ r->mgr_ = [CLLocationManager new];
355
+ r->mgr_.delegate = r;
356
+ if (!gLocationRequests) gLocationRequests = [NSMutableArray new];
357
+ [gLocationRequests addObject:r];
358
+ [r->mgr_ requestWhenInUseAuthorization];
359
+ });
350
360
  }
351
361
 
352
362
  // --- the natives -----------------------------------------------------------
@@ -408,9 +418,8 @@ static Napi::Value RequestAuthorization(const Napi::CallbackInfo& info) {
408
418
  return env.Undefined();
409
419
  }
410
420
  @autoreleasepool {
411
- Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
412
- env, info[cbAt].As<Napi::Function>(), "appkit:requestAuthorization",
413
- 0, 1);
421
+ CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
422
+ "appkit:requestAuthorization");
414
423
  switch (kind) {
415
424
  case Kind::Camera:
416
425
  case Kind::Microphone: {
@@ -40,6 +40,8 @@
40
40
  #include <string>
41
41
  #include <vector>
42
42
 
43
+ #include "channel.h" // CALOnUI: the session is the UI thread's; CALCallJS
44
+
43
45
  // src/backend.mm — NSApplication set up exactly once, whichever verb comes
44
46
  // first. The sampler is AppKit UI like a panel, so it wants an app.
45
47
  void BEnsureApp();
@@ -52,32 +54,36 @@ struct ColorAnswer {
52
54
  };
53
55
 
54
56
  // The callers of the session that is up, in the order they asked; empty when
55
- // none is. Touched on the JS thread only: the verb runs there, and so does
56
- // AppKit's handler — node's main thread is the process main thread, and the
57
- // handler arrives on it inside the pump.
58
- static std::vector<Napi::ThreadSafeFunction> gWaiting;
57
+ // none is. Touched on the UI thread only: the verb's own part runs there
58
+ // (inline in pump mode, a command from a worker), and so does AppKit's
59
+ // handler, inside the pump or the run.
60
+ static std::vector<CALTsfn> gWaiting;
59
61
 
60
62
  // cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
61
63
  // thread-safe function goes — which is what held the loop open meanwhile.
62
- static void Deliver(Napi::ThreadSafeFunction tsfn, const ColorAnswer& answer) {
64
+ static void Deliver(const CALTsfn& tsfn, const ColorAnswer& answer) {
63
65
  ColorAnswer* a = new ColorAnswer(answer);
64
- napi_status st = tsfn.BlockingCall(
66
+ bool queued = tsfn.Answer(
65
67
  a, [](Napi::Env env, Napi::Function cb, ColorAnswer* a) {
68
+ // an environment on its way out gets no answer (channel.h)
69
+ if (!CALCanCallIntoJS(env)) {
70
+ delete a;
71
+ return;
72
+ }
66
73
  if (!a->error.empty()) {
67
- cb.Call({Napi::Error::New(env, a->error).Value()});
74
+ CALCallJS(env, cb, {Napi::Error::New(env, a->error).Value()});
68
75
  } else if (a->cancelled) {
69
- cb.Call({env.Null(), env.Null()});
76
+ CALCallJS(env, cb, {env.Null(), env.Null()});
70
77
  } else {
71
78
  Napi::Object c = Napi::Object::New(env);
72
79
  c.Set("r", Napi::Number::New(env, a->r));
73
80
  c.Set("g", Napi::Number::New(env, a->g));
74
81
  c.Set("b", Napi::Number::New(env, a->b));
75
- cb.Call({env.Null(), c});
82
+ CALCallJS(env, cb, {env.Null(), c});
76
83
  }
77
84
  delete a;
78
85
  });
79
- if (st != napi_ok) delete a;
80
- tsfn.Release();
86
+ if (!queued) delete a;
81
87
  }
82
88
 
83
89
  // The one place a session answers. The waiting list comes off first, so a
@@ -105,9 +111,9 @@ static void FinishSession(NSColor* picked) {
105
111
  "sampleScreenColor: the sampler answered a colour with no sRGB form";
106
112
  }
107
113
  }
108
- std::vector<Napi::ThreadSafeFunction> waiting;
114
+ std::vector<CALTsfn> waiting;
109
115
  waiting.swap(gWaiting);
110
- for (Napi::ThreadSafeFunction& tsfn : waiting) Deliver(tsfn, a);
116
+ for (const CALTsfn& tsfn : waiting) Deliver(tsfn, a);
111
117
  }
112
118
 
113
119
  // sampleScreenColor(cb) — show the system sampler and answer once,
@@ -121,18 +127,22 @@ static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
121
127
  .ThrowAsJavaScriptException();
122
128
  return env.Undefined();
123
129
  }
124
- @autoreleasepool {
125
- BEnsureApp();
126
- bool sessionUp = !gWaiting.empty();
127
- gWaiting.push_back(Napi::ThreadSafeFunction::New(
128
- env, info[0].As<Napi::Function>(), "appkit:sampleScreenColor", 0, 1));
129
- if (sessionUp) return env.Undefined();
130
- // Nothing here keeps the sampler alive: AppKit retains it for as long as
131
- // the session lasts, which its header promises in as many words.
132
- [[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
133
- FinishSession(c);
134
- }];
135
- }
130
+ // made in the caller's environment, which is the one answered
131
+ CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
132
+ "appkit:sampleScreenColor");
133
+ CALOnUI(^{
134
+ @autoreleasepool {
135
+ BEnsureApp();
136
+ bool sessionUp = !gWaiting.empty();
137
+ gWaiting.push_back(tsfn);
138
+ if (sessionUp) return;
139
+ // Nothing here keeps the sampler alive: AppKit retains it for as long
140
+ // as the session lasts, which its header promises in as many words.
141
+ [[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
142
+ FinishSession(c);
143
+ }];
144
+ }
145
+ });
136
146
  return env.Undefined();
137
147
  }
138
148