@windowkit/appkit 0.9.0 → 0.10.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.
@@ -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,9 +54,9 @@ 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.
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.
58
60
  static std::vector<Napi::ThreadSafeFunction> gWaiting;
59
61
 
60
62
  // cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
@@ -63,16 +65,21 @@ static void Deliver(Napi::ThreadSafeFunction tsfn, const ColorAnswer& answer) {
63
65
  ColorAnswer* a = new ColorAnswer(answer);
64
66
  napi_status st = tsfn.BlockingCall(
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
  });
@@ -121,18 +128,22 @@ static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
121
128
  .ThrowAsJavaScriptException();
122
129
  return env.Undefined();
123
130
  }
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
- }
131
+ // made in the caller's environment, which is the one answered
132
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
133
+ env, info[0].As<Napi::Function>(), "appkit:sampleScreenColor", 0, 1);
134
+ CALOnUI(^{
135
+ @autoreleasepool {
136
+ BEnsureApp();
137
+ bool sessionUp = !gWaiting.empty();
138
+ gWaiting.push_back(tsfn);
139
+ if (sessionUp) return;
140
+ // Nothing here keeps the sampler alive: AppKit retains it for as long
141
+ // as the session lasts, which its header promises in as many words.
142
+ [[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
143
+ FinishSession(c);
144
+ }];
145
+ }
146
+ });
136
147
  return env.Undefined();
137
148
  }
138
149