@windowkit/appkit 0.8.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.
- package/README.md +247 -4
- package/binding.gyp +3 -1
- package/index.js +23 -1
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/calayers.node +0 -0
- package/prebuilds/darwin-x64/calayers.node +0 -0
- package/src/addon.mm +574 -224
- package/src/backend.mm +2324 -1106
- package/src/calendars.mm +37 -25
- package/src/channel.h +232 -0
- package/src/notifications.mm +67 -42
- package/src/permissions.mm +22 -10
- package/src/screencolor.mm +157 -0
- package/src/threaded.mm +984 -0
package/src/calendars.mm
CHANGED
|
@@ -92,9 +92,8 @@
|
|
|
92
92
|
#include <utility>
|
|
93
93
|
#include <vector>
|
|
94
94
|
|
|
95
|
-
// backend.mm: the one backend event
|
|
96
|
-
|
|
97
|
-
void CALEmitBackendEvent(Napi::Env env, Napi::Object ev);
|
|
95
|
+
// backend.mm / threaded.mm: the one backend event path
|
|
96
|
+
#include "channel.h"
|
|
98
97
|
|
|
99
98
|
// permissions.mm: the process's one EKEventStore — created by the first
|
|
100
99
|
// EventKit verb and never before, since creating one never prompts — and
|
|
@@ -744,28 +743,33 @@ static Napi::Value ErrorJs(Napi::Env env, const CalAnswer& a) {
|
|
|
744
743
|
static void Deliver(Napi::ThreadSafeFunction tsfn, CalAnswer* a) {
|
|
745
744
|
napi_status st = tsfn.BlockingCall(
|
|
746
745
|
a, [](Napi::Env env, Napi::Function cb, CalAnswer* a) {
|
|
746
|
+
// an environment on its way out gets no answer (channel.h)
|
|
747
|
+
if (!CALCanCallIntoJS(env)) {
|
|
748
|
+
delete a;
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
747
751
|
if (!a->error.empty()) {
|
|
748
|
-
cb
|
|
752
|
+
CALCallJS(env, cb, {ErrorJs(env, *a)});
|
|
749
753
|
} else if (a->kind == Answer::Events) {
|
|
750
754
|
Napi::Array out = Napi::Array::New(env, a->events.size());
|
|
751
755
|
for (uint32_t i = 0; i < a->events.size(); i++) {
|
|
752
756
|
out.Set(i, EventJs(env, a->events[i]));
|
|
753
757
|
}
|
|
754
|
-
cb
|
|
758
|
+
CALCallJS(env, cb, {env.Null(), out});
|
|
755
759
|
} else if (a->kind == Answer::Calendars) {
|
|
756
760
|
Napi::Array out = Napi::Array::New(env, a->calendars.size());
|
|
757
761
|
for (uint32_t i = 0; i < a->calendars.size(); i++) {
|
|
758
762
|
out.Set(i, CalendarJs(env, a->calendars[i]));
|
|
759
763
|
}
|
|
760
|
-
cb
|
|
764
|
+
CALCallJS(env, cb, {env.Null(), out});
|
|
761
765
|
} else if (a->kind == Answer::Calendar) {
|
|
762
|
-
cb
|
|
763
|
-
|
|
764
|
-
|
|
766
|
+
CALCallJS(env, cb, {env.Null(), a->calendar
|
|
767
|
+
? CalendarJs(env, *a->calendar)
|
|
768
|
+
: env.Null().As<Napi::Value>()});
|
|
765
769
|
} else if (a->kind == Answer::Id) {
|
|
766
|
-
cb
|
|
770
|
+
CALCallJS(env, cb, {env.Null(), StringOrNull(env, a->id)});
|
|
767
771
|
} else {
|
|
768
|
-
cb
|
|
772
|
+
CALCallJS(env, cb, {env.Null()});
|
|
769
773
|
}
|
|
770
774
|
delete a;
|
|
771
775
|
});
|
|
@@ -782,35 +786,38 @@ static ChangedTsfn gChanged;
|
|
|
782
786
|
// framework coalesces, so what is held is that a change happened at all.
|
|
783
787
|
static bool gHeldChange = false;
|
|
784
788
|
|
|
785
|
-
static void EmitOrHoldChange(
|
|
786
|
-
if (!
|
|
789
|
+
static void EmitOrHoldChange() {
|
|
790
|
+
if (!CALListening()) {
|
|
787
791
|
gHeldChange = true;
|
|
788
792
|
return;
|
|
789
793
|
}
|
|
790
|
-
|
|
791
|
-
Napi::Object ev = Napi::Object::New(env);
|
|
792
|
-
ev.Set("type", "calendar-store-changed");
|
|
793
|
-
CALEmitBackendEvent(env, ev);
|
|
794
|
+
CALEmit(CALEvent("calendar-store-changed"));
|
|
794
795
|
}
|
|
795
796
|
|
|
796
|
-
// Called at the start of pump2
|
|
797
|
-
// listener existed goes out ahead of
|
|
798
|
-
|
|
799
|
-
|
|
797
|
+
// Called at the start of pump2, and as runMain opens the channel
|
|
798
|
+
// (backend.mm): a change from before the listener existed goes out ahead of
|
|
799
|
+
// that tick's input.
|
|
800
|
+
void CALCalendarsReplayHeld() {
|
|
801
|
+
if (!gHeldChange || !CALListening()) return;
|
|
800
802
|
gHeldChange = false;
|
|
801
|
-
EmitOrHoldChange(
|
|
803
|
+
EmitOrHoldChange();
|
|
802
804
|
}
|
|
803
805
|
|
|
804
806
|
static void CallJsChanged(Napi::Env env, Napi::Function, void*, void*) {
|
|
805
807
|
if ((napi_env)env == nullptr) return; // the function is being torn down
|
|
806
|
-
|
|
808
|
+
if (!CALCanCallIntoJS(env)) return; // or the environment is (channel.h)
|
|
809
|
+
EmitOrHoldChange();
|
|
810
|
+
// pump mode's inline delivery leaves a listener's exception pending; from
|
|
811
|
+
// a threadsafe function's callback it is the uncaught exception (#62)
|
|
812
|
+
CALRaiseUncaughtIfPending(env);
|
|
807
813
|
}
|
|
808
814
|
|
|
809
815
|
// permissions.mm calls this the once, as it creates the process's store: the
|
|
810
816
|
// observer is in place from the store's first moment, whichever verb made it.
|
|
811
817
|
// EKEventStoreChangedNotification carries no guarantee about its thread, so
|
|
812
818
|
// the crossing to node's loop is the same hand-off a notification response
|
|
813
|
-
// takes.
|
|
819
|
+
// takes — in pump mode. With threaded mode's channel open the record goes
|
|
820
|
+
// straight into it from whichever thread this is.
|
|
814
821
|
void CALCalendarsObserveStore(EKEventStore* store) {
|
|
815
822
|
static id observer = nil;
|
|
816
823
|
if (observer) return;
|
|
@@ -818,7 +825,12 @@ void CALCalendarsObserveStore(EKEventStore* store) {
|
|
|
818
825
|
addObserverForName:EKEventStoreChangedNotification
|
|
819
826
|
object:store
|
|
820
827
|
queue:nil
|
|
821
|
-
usingBlock:^(NSNotification*) {
|
|
828
|
+
usingBlock:^(NSNotification*) {
|
|
829
|
+
if (CALChannelOpen())
|
|
830
|
+
CALEmit(CALEvent("calendar-store-changed"));
|
|
831
|
+
else
|
|
832
|
+
gChanged.NonBlockingCall();
|
|
833
|
+
}];
|
|
822
834
|
}
|
|
823
835
|
|
|
824
836
|
// --- the natives ------------------------------------------------------------
|
package/src/channel.h
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// @windowkit/appkit channel.h — what every face of the addon shares with the
|
|
2
|
+
// threading core in threaded.mm (windowkit/appkit#50): the event record, the
|
|
3
|
+
// one way an event leaves the UI thread, and the one way a command reaches it.
|
|
4
|
+
//
|
|
5
|
+
// Two modes, one set of producers. In pump mode (today's, the default) JS
|
|
6
|
+
// runs on the process main thread and pumps AppKit through pump2(); an event
|
|
7
|
+
// is materialized and handed to the backend callback inline, exactly as it
|
|
8
|
+
// always was. In threaded mode the main thread is parked in [NSApp run]
|
|
9
|
+
// (runMain) and the renderer's JS runs on a Worker; an event is appended to
|
|
10
|
+
// a queue and crosses to the connected environment in batches. A producer
|
|
11
|
+
// cannot tell the two apart: it builds a CALEvent and calls CALEmit.
|
|
12
|
+
|
|
13
|
+
#pragma once
|
|
14
|
+
|
|
15
|
+
#include <napi.h>
|
|
16
|
+
#import <Foundation/Foundation.h>
|
|
17
|
+
|
|
18
|
+
#include <atomic>
|
|
19
|
+
#include <cstdint>
|
|
20
|
+
#include <initializer_list>
|
|
21
|
+
#include <string>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
// A JS value the producer's environment already holds (a status item's own
|
|
25
|
+
// handle), looked up when the event is materialized. Gets the environment
|
|
26
|
+
// the event is being delivered to and answers null when the value is not
|
|
27
|
+
// that environment's.
|
|
28
|
+
typedef Napi::Value (^CALValueBlock)(Napi::Env env);
|
|
29
|
+
|
|
30
|
+
// A backend event as plain data. Built on whatever thread AppKit calls in
|
|
31
|
+
// on, never as a Napi::Object, since no JS value may be made off its
|
|
32
|
+
// environment's thread; materialized there, key order as added.
|
|
33
|
+
class CALEvent {
|
|
34
|
+
public:
|
|
35
|
+
CALEvent() = default;
|
|
36
|
+
explicit CALEvent(const char* type) : type_(type) { Str("type", type); }
|
|
37
|
+
|
|
38
|
+
CALEvent& Str(const char* key, const std::string& v);
|
|
39
|
+
CALEvent& Str(const char* key, const char* v); // nullptr reads as ""
|
|
40
|
+
CALEvent& Num(const char* key, double v);
|
|
41
|
+
CALEvent& Bool(const char* key, bool v);
|
|
42
|
+
CALEvent& Null(const char* key);
|
|
43
|
+
CALEvent& Strs(const char* key, std::vector<std::string> v);
|
|
44
|
+
// JSON text, parsed as it crosses; the text itself if it does not parse
|
|
45
|
+
CALEvent& Json(const char* key, const std::string& v);
|
|
46
|
+
CALEvent& Handle(const char* key, CALValueBlock v);
|
|
47
|
+
// the JS handle CALWrapHandle registered for this id, or null when the
|
|
48
|
+
// delivering environment is not the one holding it (or it was collected)
|
|
49
|
+
CALEvent& HandleRef(const char* key, uint64_t id);
|
|
50
|
+
// In threaded mode an event of the same type and key queued right behind
|
|
51
|
+
// this one replaces it: consecutive motion crosses as its latest position.
|
|
52
|
+
CALEvent& FoldBy(double key) {
|
|
53
|
+
foldable_ = true;
|
|
54
|
+
foldKey_ = key;
|
|
55
|
+
return *this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Napi::Object ToObject(Napi::Env env) const;
|
|
59
|
+
bool FoldsOnto(const CALEvent& prev) const {
|
|
60
|
+
return foldable_ && prev.foldable_ && foldKey_ == prev.foldKey_ &&
|
|
61
|
+
type_ == prev.type_;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private:
|
|
65
|
+
struct Field {
|
|
66
|
+
enum Kind { kStr, kNum, kBool, kNull, kStrs, kJson, kHandle } kind;
|
|
67
|
+
std::string key;
|
|
68
|
+
std::string s;
|
|
69
|
+
double n = 0;
|
|
70
|
+
bool b = false;
|
|
71
|
+
std::vector<std::string> list;
|
|
72
|
+
CALValueBlock handle = nil;
|
|
73
|
+
};
|
|
74
|
+
Field& Add(Field::Kind kind, const char* key);
|
|
75
|
+
|
|
76
|
+
std::string type_;
|
|
77
|
+
std::vector<Field> fields_;
|
|
78
|
+
bool foldable_ = false;
|
|
79
|
+
double foldKey_ = 0;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// The event goes out: into the channel's queue in threaded mode (from any
|
|
83
|
+
// thread), to the backend callback inline in pump mode (the JS main thread
|
|
84
|
+
// only; dropped when no callback is installed, as before).
|
|
85
|
+
void CALEmit(CALEvent&& ev);
|
|
86
|
+
|
|
87
|
+
// Somebody will receive an event: the channel is open (runMain entered or an
|
|
88
|
+
// environment connected), or a pump-mode callback is installed. The test
|
|
89
|
+
// producers use before doing work, and the test for holding one back.
|
|
90
|
+
bool CALListening();
|
|
91
|
+
|
|
92
|
+
// The channel is open: CALEmit queues, from any thread. A producer that
|
|
93
|
+
// runs off the main thread (a UNUserNotificationCenter delegate, EventKit's
|
|
94
|
+
// change notification) emits directly when this holds, and takes its own
|
|
95
|
+
// hop to the JS main thread for pump mode when it does not.
|
|
96
|
+
bool CALChannelOpen();
|
|
97
|
+
|
|
98
|
+
// runMain has been entered and not returned: the main thread is AppKit's.
|
|
99
|
+
bool CALThreaded();
|
|
100
|
+
|
|
101
|
+
// Runs `block` on the UI thread: inline when called there (pump mode, as
|
|
102
|
+
// every verb does today), queued to the command source otherwise. Arguments
|
|
103
|
+
// are parsed on the calling thread and captured by value; no napi_value may
|
|
104
|
+
// cross. Queued blocks apply in order, a whole drain inside one
|
|
105
|
+
// CATransaction with implicit actions disabled.
|
|
106
|
+
void CALOnUI(void (^block)(void));
|
|
107
|
+
|
|
108
|
+
// The same for a block that starts a modal loop — a pop-up menu, an
|
|
109
|
+
// app-modal panel, a drag session. Queued, it runs from a run-loop callout
|
|
110
|
+
// of its own in the default mode once the drain it arrived in is done, so
|
|
111
|
+
// the rest of that batch never waits behind the gesture; called on the UI
|
|
112
|
+
// thread outside a drain it runs inline, as today.
|
|
113
|
+
void CALOnUIModal(void (^block)(void));
|
|
114
|
+
|
|
115
|
+
// On the UI thread: an application-defined event for a loop waiting in
|
|
116
|
+
// nextEventMatchingMask: to wake on — after a stopModal from a command, say,
|
|
117
|
+
// which the modal loop only notices on its next event.
|
|
118
|
+
void CALPostWakeEvent();
|
|
119
|
+
|
|
120
|
+
// Windows and status items alive — the objects a person can act on. The
|
|
121
|
+
// channel's threadsafe function holds the connected environment's loop open
|
|
122
|
+
// while any exist and lets it go when none do, so an app with nothing on
|
|
123
|
+
// screen can exit. Called on the connected environment's own thread (a
|
|
124
|
+
// worker's createWindow2) the reference changes at once, so a worker whose
|
|
125
|
+
// script ends right after making a window is still held.
|
|
126
|
+
void CALUIObjectsChanged(int delta);
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// handles allocated at the call (windowkit/appkit#51)
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
//
|
|
132
|
+
// JS never waits on the UI thread, so in threaded mode a verb that makes an
|
|
133
|
+
// AppKit object answers before the object exists: it allocates a CALHandle
|
|
134
|
+
// on the calling thread, hands JS an External over it, and queues the
|
|
135
|
+
// making; the UI thread binds the object into the handle as it makes it.
|
|
136
|
+
// Every later verb captures the handle and resolves it inside its own
|
|
137
|
+
// command, which runs after the making because commands apply in order.
|
|
138
|
+
// Pump mode still hands out the object itself, as it always has; the two
|
|
139
|
+
// helpers below take either.
|
|
140
|
+
|
|
141
|
+
@interface CALHandle : NSObject {
|
|
142
|
+
@public
|
|
143
|
+
uint64_t id_; // what an event's `handle` field names
|
|
144
|
+
id object_; // the UI thread's: nil until made, nil once gone
|
|
145
|
+
CALHandle* part_; // a window's root layer, allocated with it
|
|
146
|
+
std::atomic<long> number_; // a window's number once made (0 before); any thread
|
|
147
|
+
std::atomic<bool> released_; // destroyed / removed through a verb (counted once)
|
|
148
|
+
}
|
|
149
|
+
@end
|
|
150
|
+
|
|
151
|
+
CALHandle* CALNewHandle(); // any thread
|
|
152
|
+
|
|
153
|
+
// The External JS holds. `registered`: events may name it (a window, a
|
|
154
|
+
// status item), so a weak reference is kept for CALEvent::HandleRef; the
|
|
155
|
+
// External's finalizer drops it. The object is let go on the UI thread
|
|
156
|
+
// whichever thread the handle dies on.
|
|
157
|
+
Napi::Value CALWrapHandle(Napi::Env env, CALHandle* h, bool registered);
|
|
158
|
+
|
|
159
|
+
// Hold a registered handle's External strongly (pin) or let it go again,
|
|
160
|
+
// on the environment's own thread: a worker's status item stays in the bar
|
|
161
|
+
// until removeStatusItem even when JS drops its handle, as pump mode's does.
|
|
162
|
+
void CALPinHandle(Napi::Env env, uint64_t id, bool pin);
|
|
163
|
+
|
|
164
|
+
// What a verb captures for its command: the handle, or pump mode's object
|
|
165
|
+
// itself; nil for anything that is neither.
|
|
166
|
+
id CALHandleTarget(Napi::Value v);
|
|
167
|
+
|
|
168
|
+
// On the UI thread: the object a captured target names — nil before it is
|
|
169
|
+
// made and after it is gone.
|
|
170
|
+
id CALResolve(id target);
|
|
171
|
+
|
|
172
|
+
// A frame batch from a worker's outermost txCommit: queued like any
|
|
173
|
+
// command, tagged with the size it was painted at when the renderer said
|
|
174
|
+
// (txCommit({ width, height })) — what a resize handshake waits for.
|
|
175
|
+
void CALPostFrame(void (^block)(void), bool sized, double width, double height);
|
|
176
|
+
|
|
177
|
+
// On the UI thread, from windowDidResize: (windowkit/appkit#53): wait at
|
|
178
|
+
// most waitMs for a queued frame painted at width × height (not at all with
|
|
179
|
+
// nobody connected), then drain the queue inline. True when the frame came
|
|
180
|
+
// in time; *waitedMs is how long the wait took.
|
|
181
|
+
bool CALAwaitFrame(double width, double height, double waitMs, double* waitedMs);
|
|
182
|
+
|
|
183
|
+
// A layer verb's work (windowkit/appkit#52): run in the call on the main
|
|
184
|
+
// thread; from any other, recorded into that thread's open frame batch
|
|
185
|
+
// (txBegin … txCommit) or, outside one, queued as a command of its own.
|
|
186
|
+
void CALOnLayers(void (^op)(void));
|
|
187
|
+
|
|
188
|
+
// On the UI thread, before a layer's contents are replaced: while a worker's
|
|
189
|
+
// frame applies, an IOSurface taken off the layer is reported afterwards as
|
|
190
|
+
// `surface-released { id }`.
|
|
191
|
+
void CALNoteContentsReplaced(id layer, id next);
|
|
192
|
+
|
|
193
|
+
// A one-shot callback in the calling environment, answered from any thread:
|
|
194
|
+
// `make` runs there and builds the one argument cb gets. A pending reply
|
|
195
|
+
// holds that environment's loop open, like I/O in flight.
|
|
196
|
+
Napi::ThreadSafeFunction CALReplyTo(Napi::Env env, Napi::Function cb,
|
|
197
|
+
const char* name);
|
|
198
|
+
void CALReply(Napi::ThreadSafeFunction tsfn, CALValueBlock make);
|
|
199
|
+
|
|
200
|
+
// Whether JS can still be called in `env`. A worker that is ending (an
|
|
201
|
+
// uncaught error, its own process.exit) can still run a threadsafe
|
|
202
|
+
// function's callback from its loop's last spin, and there every property
|
|
203
|
+
// set fails — which node-addon-api, with C++ exceptions off, turns into a
|
|
204
|
+
// fatal error, since it cannot throw either. Every threadsafe function's
|
|
205
|
+
// callback asks first and, told no, frees what it carried and returns.
|
|
206
|
+
bool CALCanCallIntoJS(napi_env env);
|
|
207
|
+
|
|
208
|
+
// In a threadsafe function's callback, after calling into JS: an exception
|
|
209
|
+
// left pending is made that environment's uncaught exception
|
|
210
|
+
// (napi_fatal_exception). Left pending, Node's default N-API policy would
|
|
211
|
+
// only warn and drop it (windowkit/appkit#62). Not for a call made inside a
|
|
212
|
+
// JS call frame (pump2's inline delivery): there the exception rethrows to
|
|
213
|
+
// the caller, as it always has.
|
|
214
|
+
void CALRaiseUncaughtIfPending(napi_env env);
|
|
215
|
+
|
|
216
|
+
// cb(...args) with undefined as this, raw, so a failed call is a status
|
|
217
|
+
// rather than node-addon-api's fatal error — from a threadsafe function's
|
|
218
|
+
// callback, so an exception cb throws is then the environment's uncaught
|
|
219
|
+
// exception (CALRaiseUncaughtIfPending).
|
|
220
|
+
void CALCallJS(napi_env env, napi_value cb,
|
|
221
|
+
std::initializer_list<napi_value> args);
|
|
222
|
+
|
|
223
|
+
// A read of AppKit state, in either mode. Without a callback (the last
|
|
224
|
+
// argument, when it is a function) it answers synchronously — on the main
|
|
225
|
+
// thread only, as every read always has; off it that is a TypeError. With
|
|
226
|
+
// one, `compute` runs on the UI thread and what it returns builds cb's one
|
|
227
|
+
// argument in the caller's environment, a later tick even on the main
|
|
228
|
+
// thread. `compute` never returns nil. `nested`: compute spins a run loop of
|
|
229
|
+
// its own (a test hook waiting for a window), so a queued one runs through
|
|
230
|
+
// CALOnUIModal rather than inside a drain.
|
|
231
|
+
Napi::Value CALAnswer(const Napi::CallbackInfo& info, const char* name,
|
|
232
|
+
CALValueBlock (^compute)(void), bool nested = false);
|
package/src/notifications.mm
CHANGED
|
@@ -60,9 +60,8 @@
|
|
|
60
60
|
#include <string>
|
|
61
61
|
#include <vector>
|
|
62
62
|
|
|
63
|
-
// backend.mm: the one backend event
|
|
64
|
-
|
|
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";
|
|
@@ -161,58 +160,61 @@ using EventTsfn = Napi::TypedThreadSafeFunction<void, NotifEvent, CallJsEvent>;
|
|
|
161
160
|
static EventTsfn gEvents;
|
|
162
161
|
static std::vector<NotifEvent*> gHeld;
|
|
163
162
|
|
|
164
|
-
static
|
|
165
|
-
|
|
166
|
-
o.
|
|
167
|
-
o.
|
|
168
|
-
if (!ev.
|
|
169
|
-
if (
|
|
170
|
-
o.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (ev.hasUserInfo)
|
|
174
|
-
|
|
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);
|
|
163
|
+
static CALEvent EventRecord(const NotifEvent& ev) {
|
|
164
|
+
CALEvent o(ev.type.c_str());
|
|
165
|
+
o.Str("identifier", ev.identifier);
|
|
166
|
+
if (!ev.actionId.empty()) o.Str("actionId", ev.actionId);
|
|
167
|
+
if (!ev.reason.empty()) o.Str("reason", ev.reason);
|
|
168
|
+
if (ev.categoryId.empty()) o.Null("categoryId");
|
|
169
|
+
else o.Str("categoryId", ev.categoryId);
|
|
170
|
+
// parsed as it crosses; text that is not our own output after all is
|
|
171
|
+
// handed over as it is rather than fail
|
|
172
|
+
if (ev.hasUserInfo) o.Json("userInfo", ev.userInfoJson);
|
|
173
|
+
if (ev.hasUserText) o.Str("userText", ev.userText);
|
|
183
174
|
return o;
|
|
184
175
|
}
|
|
185
176
|
|
|
186
|
-
static void EmitOrHold(
|
|
187
|
-
if (!
|
|
177
|
+
static void EmitOrHold(NotifEvent* ev) {
|
|
178
|
+
if (!CALListening()) {
|
|
188
179
|
gHeld.push_back(ev);
|
|
189
180
|
return;
|
|
190
181
|
}
|
|
191
|
-
|
|
192
|
-
CALEmitBackendEvent(env, EventObject(env, *ev));
|
|
182
|
+
CALEmit(EventRecord(*ev));
|
|
193
183
|
delete ev;
|
|
194
184
|
}
|
|
195
185
|
|
|
196
|
-
// Called at the start of pump2
|
|
197
|
-
// goes out ahead of that
|
|
198
|
-
|
|
199
|
-
|
|
186
|
+
// Called at the start of pump2, and as runMain opens the channel
|
|
187
|
+
// (backend.mm): what arrived before a listener goes out ahead of that
|
|
188
|
+
// tick's input, in arrival order.
|
|
189
|
+
void CALNotificationsReplayHeld() {
|
|
190
|
+
if (gHeld.empty() || !CALListening()) return;
|
|
200
191
|
std::vector<NotifEvent*> held;
|
|
201
192
|
held.swap(gHeld);
|
|
202
|
-
for (NotifEvent* ev : held) EmitOrHold(
|
|
193
|
+
for (NotifEvent* ev : held) EmitOrHold(ev);
|
|
203
194
|
}
|
|
204
195
|
|
|
205
196
|
static void CallJsEvent(Napi::Env env, Napi::Function, void*, NotifEvent* ev) {
|
|
206
|
-
|
|
197
|
+
// the function is being torn down, or the environment is (channel.h)
|
|
198
|
+
if ((napi_env)env == nullptr || !CALCanCallIntoJS(env)) {
|
|
207
199
|
delete ev;
|
|
208
200
|
return;
|
|
209
201
|
}
|
|
210
|
-
CALNotificationsReplayHeld(
|
|
211
|
-
EmitOrHold(
|
|
202
|
+
CALNotificationsReplayHeld(); // keep order behind anything still held
|
|
203
|
+
EmitOrHold(ev);
|
|
204
|
+
// pump mode's inline delivery leaves a listener's exception pending; from
|
|
205
|
+
// a threadsafe function's callback it is the uncaught exception (#62)
|
|
206
|
+
CALRaiseUncaughtIfPending(env);
|
|
212
207
|
}
|
|
213
208
|
|
|
214
|
-
// From any thread: onto node's loop, then to the listener or the held list
|
|
209
|
+
// From any thread: onto node's loop, then to the listener or the held list
|
|
210
|
+
// — in pump mode. With threaded mode's channel open the record goes straight
|
|
211
|
+
// into it from the centre's own thread.
|
|
215
212
|
static void QueueEvent(NotifEvent* ev) {
|
|
213
|
+
if (CALChannelOpen()) {
|
|
214
|
+
CALEmit(EventRecord(*ev));
|
|
215
|
+
delete ev;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
216
218
|
if (gEvents.NonBlockingCall(ev) != napi_ok) delete ev;
|
|
217
219
|
}
|
|
218
220
|
|
|
@@ -416,7 +418,10 @@ static Napi::Value NotificationSettings(const Napi::CallbackInfo& info) {
|
|
|
416
418
|
if (!gCenter) {
|
|
417
419
|
tsfn.BlockingCall((void*)nullptr,
|
|
418
420
|
[](Napi::Env env, Napi::Function cb, void*) {
|
|
419
|
-
|
|
421
|
+
// an environment on its way out gets no answer
|
|
422
|
+
// (channel.h)
|
|
423
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
424
|
+
CALCallJS(env, cb, {UnavailableObject(env)});
|
|
420
425
|
});
|
|
421
426
|
tsfn.Release();
|
|
422
427
|
return env.Undefined();
|
|
@@ -426,7 +431,9 @@ static Napi::Value NotificationSettings(const Napi::CallbackInfo& info) {
|
|
|
426
431
|
void* p = (void*)CFBridgingRetain(s);
|
|
427
432
|
tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
428
433
|
UNNotificationSettings* s = CFBridgingRelease(p);
|
|
429
|
-
|
|
434
|
+
// an environment on its way out gets no answer (channel.h)
|
|
435
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
436
|
+
CALCallJS(env, cb, {SettingsObject(env, s)});
|
|
430
437
|
});
|
|
431
438
|
tsfn.Release();
|
|
432
439
|
}];
|
|
@@ -505,8 +512,16 @@ static Napi::Value RequestNotificationAuthorization(
|
|
|
505
512
|
if (out->ok && gCenter && gCategories) {
|
|
506
513
|
[gCenter setNotificationCategories:gCategories];
|
|
507
514
|
}
|
|
508
|
-
|
|
509
|
-
|
|
515
|
+
// an environment on its way out gets no
|
|
516
|
+
// answer (channel.h); the categories are the
|
|
517
|
+
// process's, re-applied either way
|
|
518
|
+
if (!CALCanCallIntoJS(env)) {
|
|
519
|
+
delete out;
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
CALCallJS(env, cb,
|
|
523
|
+
{Napi::Boolean::New(env, out->ok),
|
|
524
|
+
ErrorValue(env, out->error)});
|
|
510
525
|
delete out;
|
|
511
526
|
});
|
|
512
527
|
tsfn.Release();
|
|
@@ -646,10 +661,12 @@ static Napi::Value NotificationCategories(const Napi::CallbackInfo& info) {
|
|
|
646
661
|
void* p = (void*)CFBridgingRetain(cats);
|
|
647
662
|
tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
648
663
|
NSSet<UNNotificationCategory*>* cats = CFBridgingRelease(p);
|
|
664
|
+
// an environment on its way out gets no answer (channel.h)
|
|
665
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
649
666
|
Napi::Array out = Napi::Array::New(env);
|
|
650
667
|
uint32_t n = 0;
|
|
651
668
|
for (UNNotificationCategory* c in cats) out.Set(n++, CategoryObject(env, c));
|
|
652
|
-
cb
|
|
669
|
+
CALCallJS(env, cb, {out});
|
|
653
670
|
});
|
|
654
671
|
tsfn.Release();
|
|
655
672
|
}];
|
|
@@ -752,7 +769,13 @@ static Napi::Value Post(const Napi::CallbackInfo& info, size_t propsAt,
|
|
|
752
769
|
Outcome* out = new Outcome{error == nil, error};
|
|
753
770
|
tsfn.BlockingCall(
|
|
754
771
|
out, [](Napi::Env env, Napi::Function cb, Outcome* out) {
|
|
755
|
-
|
|
772
|
+
// an environment on its way out gets no answer
|
|
773
|
+
// (channel.h)
|
|
774
|
+
if (!CALCanCallIntoJS(env)) {
|
|
775
|
+
delete out;
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
CALCallJS(env, cb, {ErrorValue(env, out->error)});
|
|
756
779
|
delete out;
|
|
757
780
|
});
|
|
758
781
|
tsfn.Release();
|
|
@@ -826,6 +849,8 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
|
|
|
826
849
|
void* p = (void*)CFBridgingRetain(list);
|
|
827
850
|
tsfn.BlockingCall(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
828
851
|
NSArray<UNNotification*>* list = CFBridgingRelease(p);
|
|
852
|
+
// an environment on its way out gets no answer (channel.h)
|
|
853
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
829
854
|
Napi::Array out = Napi::Array::New(env);
|
|
830
855
|
uint32_t n = 0;
|
|
831
856
|
for (UNNotification* note in list) {
|
|
@@ -854,7 +879,7 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
|
|
|
854
879
|
o.Set("date", note.date.timeIntervalSince1970 * 1000.0);
|
|
855
880
|
out.Set(n++, o);
|
|
856
881
|
}
|
|
857
|
-
cb
|
|
882
|
+
CALCallJS(env, cb, {out});
|
|
858
883
|
});
|
|
859
884
|
tsfn.Release();
|
|
860
885
|
}];
|
package/src/permissions.mm
CHANGED
|
@@ -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.
|
|
@@ -299,8 +301,13 @@ static void Answer(Napi::ThreadSafeFunction tsfn, const char* status,
|
|
|
299
301
|
Reply* r = new Reply{status, granted};
|
|
300
302
|
napi_status st = tsfn.BlockingCall(
|
|
301
303
|
r, [](Napi::Env env, Napi::Function cb, Reply* r) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
+
// an environment on its way out gets no answer (channel.h)
|
|
305
|
+
if (!CALCanCallIntoJS(env)) {
|
|
306
|
+
delete r;
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
CALCallJS(env, cb, {Napi::Boolean::New(env, r->granted),
|
|
310
|
+
Napi::String::New(env, r->status)});
|
|
304
311
|
delete r;
|
|
305
312
|
});
|
|
306
313
|
if (st != napi_ok) delete r;
|
|
@@ -338,15 +345,20 @@ static NSMutableArray<CALLocationRequest*>* gLocationRequests = nil;
|
|
|
338
345
|
}
|
|
339
346
|
@end
|
|
340
347
|
|
|
348
|
+
// The manager reports on the run loop of the thread that made it, and a
|
|
349
|
+
// worker has none: it is made on the UI thread (inline in pump mode, a
|
|
350
|
+
// command in threaded mode), where the delegate call then arrives.
|
|
341
351
|
static void StartLocationRequest(Napi::ThreadSafeFunction tsfn) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
352
|
+
CALOnUI(^{
|
|
353
|
+
CALLocationRequest* r = [CALLocationRequest new];
|
|
354
|
+
r->tsfn_ = tsfn;
|
|
355
|
+
r->done_ = false;
|
|
356
|
+
r->mgr_ = [CLLocationManager new];
|
|
357
|
+
r->mgr_.delegate = r;
|
|
358
|
+
if (!gLocationRequests) gLocationRequests = [NSMutableArray new];
|
|
359
|
+
[gLocationRequests addObject:r];
|
|
360
|
+
[r->mgr_ requestWhenInUseAuthorization];
|
|
361
|
+
});
|
|
350
362
|
}
|
|
351
363
|
|
|
352
364
|
// --- the natives -----------------------------------------------------------
|