@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.
- package/README.md +236 -4
- package/binding.gyp +2 -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 +570 -224
- package/src/backend.mm +2394 -1110
- package/src/calendars.mm +69 -46
- package/src/channel.h +292 -0
- package/src/notifications.mm +99 -72
- package/src/permissions.mm +30 -21
- package/src/screencolor.mm +35 -25
- package/src/threaded.mm +1072 -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
|
|
@@ -741,76 +740,85 @@ static Napi::Value ErrorJs(Napi::Env env, const CalAnswer& a) {
|
|
|
741
740
|
// cb(err) or cb(null, ...) on the JS thread, from a background queue, then
|
|
742
741
|
// the thread-safe function goes — which is what was holding the loop open
|
|
743
742
|
// for the length of the fetch or the write.
|
|
744
|
-
static void Deliver(
|
|
745
|
-
|
|
743
|
+
static void Deliver(const CALTsfn& tsfn, CalAnswer* a) {
|
|
744
|
+
bool queued = tsfn.Answer(
|
|
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
|
});
|
|
772
|
-
if (
|
|
773
|
-
tsfn.Release();
|
|
776
|
+
if (!queued) delete a;
|
|
774
777
|
}
|
|
775
778
|
|
|
776
779
|
// --- the store's change notification ----------------------------------------
|
|
777
780
|
|
|
778
781
|
static void CallJsChanged(Napi::Env env, Napi::Function, void*, void*);
|
|
779
782
|
using ChangedTsfn = Napi::TypedThreadSafeFunction<void, void, CallJsChanged>;
|
|
780
|
-
|
|
783
|
+
// The main thread's environment's (InitCalendars), called from whichever
|
|
784
|
+
// thread EventKit posts on; null until made. Leaked with the process.
|
|
785
|
+
static std::atomic<CALTsfn*> gChanged{nullptr};
|
|
781
786
|
// One change waiting for a listener. The event carries nothing and the
|
|
782
787
|
// framework coalesces, so what is held is that a change happened at all.
|
|
783
788
|
static bool gHeldChange = false;
|
|
784
789
|
|
|
785
|
-
static void EmitOrHoldChange(
|
|
786
|
-
if (!
|
|
790
|
+
static void EmitOrHoldChange() {
|
|
791
|
+
if (!CALListening()) {
|
|
787
792
|
gHeldChange = true;
|
|
788
793
|
return;
|
|
789
794
|
}
|
|
790
|
-
|
|
791
|
-
Napi::Object ev = Napi::Object::New(env);
|
|
792
|
-
ev.Set("type", "calendar-store-changed");
|
|
793
|
-
CALEmitBackendEvent(env, ev);
|
|
795
|
+
CALEmit(CALEvent("calendar-store-changed"));
|
|
794
796
|
}
|
|
795
797
|
|
|
796
|
-
// Called at the start of pump2
|
|
797
|
-
// listener existed goes out ahead of
|
|
798
|
-
|
|
799
|
-
|
|
798
|
+
// Called at the start of pump2, and as runMain opens the channel
|
|
799
|
+
// (backend.mm): a change from before the listener existed goes out ahead of
|
|
800
|
+
// that tick's input.
|
|
801
|
+
void CALCalendarsReplayHeld() {
|
|
802
|
+
if (!gHeldChange || !CALListening()) return;
|
|
800
803
|
gHeldChange = false;
|
|
801
|
-
EmitOrHoldChange(
|
|
804
|
+
EmitOrHoldChange();
|
|
802
805
|
}
|
|
803
806
|
|
|
804
807
|
static void CallJsChanged(Napi::Env env, Napi::Function, void*, void*) {
|
|
805
808
|
if ((napi_env)env == nullptr) return; // the function is being torn down
|
|
806
|
-
|
|
809
|
+
if (!CALCanCallIntoJS(env)) return; // or the environment is (channel.h)
|
|
810
|
+
EmitOrHoldChange();
|
|
811
|
+
// pump mode's inline delivery leaves a listener's exception pending; from
|
|
812
|
+
// a threadsafe function's callback it is the uncaught exception (#62)
|
|
813
|
+
CALRaiseUncaughtIfPending(env);
|
|
807
814
|
}
|
|
808
815
|
|
|
809
816
|
// permissions.mm calls this the once, as it creates the process's store: the
|
|
810
817
|
// observer is in place from the store's first moment, whichever verb made it.
|
|
811
818
|
// EKEventStoreChangedNotification carries no guarantee about its thread, so
|
|
812
819
|
// the crossing to node's loop is the same hand-off a notification response
|
|
813
|
-
// takes.
|
|
820
|
+
// takes — in pump mode. With threaded mode's channel open the record goes
|
|
821
|
+
// straight into it from whichever thread this is.
|
|
814
822
|
void CALCalendarsObserveStore(EKEventStore* store) {
|
|
815
823
|
static id observer = nil;
|
|
816
824
|
if (observer) return;
|
|
@@ -818,7 +826,15 @@ void CALCalendarsObserveStore(EKEventStore* store) {
|
|
|
818
826
|
addObserverForName:EKEventStoreChangedNotification
|
|
819
827
|
object:store
|
|
820
828
|
queue:nil
|
|
821
|
-
usingBlock:^(NSNotification*) {
|
|
829
|
+
usingBlock:^(NSNotification*) {
|
|
830
|
+
if (CALChannelOpen()) {
|
|
831
|
+
CALEmit(CALEvent("calendar-store-changed"));
|
|
832
|
+
} else if (CALTsfn* changed = gChanged.load()) {
|
|
833
|
+
changed->Use(false, [](napi_threadsafe_function f) {
|
|
834
|
+
return ChangedTsfn(f).NonBlockingCall();
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
}];
|
|
822
838
|
}
|
|
823
839
|
|
|
824
840
|
// --- the natives ------------------------------------------------------------
|
|
@@ -893,8 +909,8 @@ static Napi::Value Calendars(const Napi::CallbackInfo& info) {
|
|
|
893
909
|
}
|
|
894
910
|
@autoreleasepool {
|
|
895
911
|
EKEventStore* store = CALEventStore(); // JS thread: it is a singleton
|
|
896
|
-
|
|
897
|
-
|
|
912
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
913
|
+
"appkit:calendars");
|
|
898
914
|
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
899
915
|
@autoreleasepool {
|
|
900
916
|
CalAnswer* a = new CalAnswer;
|
|
@@ -919,8 +935,8 @@ static Napi::Value EventsBetween(const Napi::CallbackInfo& info) {
|
|
|
919
935
|
}
|
|
920
936
|
@autoreleasepool {
|
|
921
937
|
EKEventStore* store = CALEventStore();
|
|
922
|
-
|
|
923
|
-
|
|
938
|
+
CALTsfn tsfn = CALTsfn::New(env, info[1].As<Napi::Function>(),
|
|
939
|
+
"appkit:eventsBetween");
|
|
924
940
|
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
925
941
|
@autoreleasepool {
|
|
926
942
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1536,8 +1552,8 @@ static Napi::Value DefaultCalendar(const Napi::CallbackInfo& info) {
|
|
|
1536
1552
|
}
|
|
1537
1553
|
@autoreleasepool {
|
|
1538
1554
|
EKEventStore* store = CALEventStore();
|
|
1539
|
-
|
|
1540
|
-
|
|
1555
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
1556
|
+
"appkit:defaultCalendar");
|
|
1541
1557
|
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
1542
1558
|
@autoreleasepool {
|
|
1543
1559
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1571,8 +1587,8 @@ static Napi::Value SaveEvent(const Napi::CallbackInfo& info) {
|
|
|
1571
1587
|
}
|
|
1572
1588
|
@autoreleasepool {
|
|
1573
1589
|
EKEventStore* store = CALEventStore();
|
|
1574
|
-
|
|
1575
|
-
|
|
1590
|
+
CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
|
|
1591
|
+
"appkit:saveEvent");
|
|
1576
1592
|
dispatch_async(WriteQueue(), ^{
|
|
1577
1593
|
@autoreleasepool {
|
|
1578
1594
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1607,8 +1623,8 @@ static Napi::Value RemoveEvent(const Napi::CallbackInfo& info) {
|
|
|
1607
1623
|
}
|
|
1608
1624
|
@autoreleasepool {
|
|
1609
1625
|
EKEventStore* store = CALEventStore();
|
|
1610
|
-
|
|
1611
|
-
|
|
1626
|
+
CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
|
|
1627
|
+
"appkit:removeEvent");
|
|
1612
1628
|
dispatch_async(WriteQueue(), ^{
|
|
1613
1629
|
@autoreleasepool {
|
|
1614
1630
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1631,8 +1647,8 @@ static Napi::Value CommitCalendarStore(const Napi::CallbackInfo& info) {
|
|
|
1631
1647
|
}
|
|
1632
1648
|
@autoreleasepool {
|
|
1633
1649
|
EKEventStore* store = CALEventStore();
|
|
1634
|
-
|
|
1635
|
-
|
|
1650
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
1651
|
+
"appkit:commitCalendarStore");
|
|
1636
1652
|
dispatch_async(WriteQueue(), ^{
|
|
1637
1653
|
@autoreleasepool {
|
|
1638
1654
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1655,8 +1671,8 @@ static Napi::Value ResetCalendarStore(const Napi::CallbackInfo& info) {
|
|
|
1655
1671
|
}
|
|
1656
1672
|
@autoreleasepool {
|
|
1657
1673
|
EKEventStore* store = CALEventStore();
|
|
1658
|
-
|
|
1659
|
-
|
|
1674
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
1675
|
+
"appkit:resetCalendarStore");
|
|
1660
1676
|
dispatch_async(WriteQueue(), ^{
|
|
1661
1677
|
@autoreleasepool {
|
|
1662
1678
|
CalAnswer* a = new CalAnswer;
|
|
@@ -1690,8 +1706,15 @@ static Napi::Value PostCalendarStoreChanged(const Napi::CallbackInfo& info) {
|
|
|
1690
1706
|
// ---------------------------------------------------------------------------
|
|
1691
1707
|
|
|
1692
1708
|
void InitCalendars(Napi::Env env, Napi::Object exports) {
|
|
1693
|
-
|
|
1694
|
-
|
|
1709
|
+
// Pump mode delivers on the main thread only (CALEmit), so a change
|
|
1710
|
+
// crosses to the main thread's environment: made there, the once. Each
|
|
1711
|
+
// environment used to make its own and replace the last, so a worker's —
|
|
1712
|
+
// gone with the worker — could be the one EventKit's thread then called.
|
|
1713
|
+
if (pthread_main_np() && !gChanged.load()) {
|
|
1714
|
+
ChangedTsfn changed = ChangedTsfn::New(env, "appkit:calendars-changed", 0, 1);
|
|
1715
|
+
changed.Unref(env); // a change never holds the loop open by itself
|
|
1716
|
+
gChanged.store(new CALTsfn(CALTsfn::Watch(env, changed)));
|
|
1717
|
+
}
|
|
1695
1718
|
exports.Set("calendars", Napi::Function::New(env, Calendars));
|
|
1696
1719
|
exports.Set("eventsBetween", Napi::Function::New(env, EventsBetween));
|
|
1697
1720
|
exports.Set("defaultCalendar", Napi::Function::New(env, DefaultCalendar));
|
package/src/channel.h
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
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 <functional>
|
|
21
|
+
#include <initializer_list>
|
|
22
|
+
#include <memory>
|
|
23
|
+
#include <string>
|
|
24
|
+
#include <vector>
|
|
25
|
+
|
|
26
|
+
// A JS value the producer's environment already holds (a status item's own
|
|
27
|
+
// handle), looked up when the event is materialized. Gets the environment
|
|
28
|
+
// the event is being delivered to and answers null when the value is not
|
|
29
|
+
// that environment's.
|
|
30
|
+
typedef Napi::Value (^CALValueBlock)(Napi::Env env);
|
|
31
|
+
|
|
32
|
+
// A backend event as plain data. Built on whatever thread AppKit calls in
|
|
33
|
+
// on, never as a Napi::Object, since no JS value may be made off its
|
|
34
|
+
// environment's thread; materialized there, key order as added.
|
|
35
|
+
class CALEvent {
|
|
36
|
+
public:
|
|
37
|
+
CALEvent() = default;
|
|
38
|
+
explicit CALEvent(const char* type) : type_(type) { Str("type", type); }
|
|
39
|
+
|
|
40
|
+
CALEvent& Str(const char* key, const std::string& v);
|
|
41
|
+
CALEvent& Str(const char* key, const char* v); // nullptr reads as ""
|
|
42
|
+
CALEvent& Num(const char* key, double v);
|
|
43
|
+
CALEvent& Bool(const char* key, bool v);
|
|
44
|
+
CALEvent& Null(const char* key);
|
|
45
|
+
CALEvent& Strs(const char* key, std::vector<std::string> v);
|
|
46
|
+
// JSON text, parsed as it crosses; the text itself if it does not parse
|
|
47
|
+
CALEvent& Json(const char* key, const std::string& v);
|
|
48
|
+
CALEvent& Handle(const char* key, CALValueBlock v);
|
|
49
|
+
// the JS handle CALWrapHandle registered for this id, or null when the
|
|
50
|
+
// delivering environment is not the one holding it (or it was collected)
|
|
51
|
+
CALEvent& HandleRef(const char* key, uint64_t id);
|
|
52
|
+
// In threaded mode an event of the same type and key queued right behind
|
|
53
|
+
// this one replaces it: consecutive motion crosses as its latest position.
|
|
54
|
+
CALEvent& FoldBy(double key) {
|
|
55
|
+
foldable_ = true;
|
|
56
|
+
foldKey_ = key;
|
|
57
|
+
return *this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Napi::Object ToObject(Napi::Env env) const;
|
|
61
|
+
bool FoldsOnto(const CALEvent& prev) const {
|
|
62
|
+
return foldable_ && prev.foldable_ && foldKey_ == prev.foldKey_ &&
|
|
63
|
+
type_ == prev.type_;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private:
|
|
67
|
+
struct Field {
|
|
68
|
+
enum Kind { kStr, kNum, kBool, kNull, kStrs, kJson, kHandle } kind;
|
|
69
|
+
std::string key;
|
|
70
|
+
std::string s;
|
|
71
|
+
double n = 0;
|
|
72
|
+
bool b = false;
|
|
73
|
+
std::vector<std::string> list;
|
|
74
|
+
CALValueBlock handle = nil;
|
|
75
|
+
};
|
|
76
|
+
Field& Add(Field::Kind kind, const char* key);
|
|
77
|
+
|
|
78
|
+
std::string type_;
|
|
79
|
+
std::vector<Field> fields_;
|
|
80
|
+
bool foldable_ = false;
|
|
81
|
+
double foldKey_ = 0;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// The event goes out: into the channel's queue in threaded mode (from any
|
|
85
|
+
// thread), to the backend callback inline in pump mode (the JS main thread
|
|
86
|
+
// only; dropped when no callback is installed, as before).
|
|
87
|
+
void CALEmit(CALEvent&& ev);
|
|
88
|
+
|
|
89
|
+
// Somebody will receive an event: the channel is open (runMain entered or an
|
|
90
|
+
// environment connected), or a pump-mode callback is installed. The test
|
|
91
|
+
// producers use before doing work, and the test for holding one back.
|
|
92
|
+
bool CALListening();
|
|
93
|
+
|
|
94
|
+
// The channel is open: CALEmit queues, from any thread. A producer that
|
|
95
|
+
// runs off the main thread (a UNUserNotificationCenter delegate, EventKit's
|
|
96
|
+
// change notification) emits directly when this holds, and takes its own
|
|
97
|
+
// hop to the JS main thread for pump mode when it does not.
|
|
98
|
+
bool CALChannelOpen();
|
|
99
|
+
|
|
100
|
+
// runMain has been entered and not returned: the main thread is AppKit's.
|
|
101
|
+
bool CALThreaded();
|
|
102
|
+
|
|
103
|
+
// Runs `block` on the UI thread: inline when called there (pump mode, as
|
|
104
|
+
// every verb does today), queued to the command source otherwise. Arguments
|
|
105
|
+
// are parsed on the calling thread and captured by value; no napi_value may
|
|
106
|
+
// cross. Queued blocks apply in order, a whole drain inside one
|
|
107
|
+
// CATransaction with implicit actions disabled.
|
|
108
|
+
void CALOnUI(void (^block)(void));
|
|
109
|
+
|
|
110
|
+
// The same for a block that starts a modal loop — a pop-up menu, an
|
|
111
|
+
// app-modal panel, a drag session. Queued, it runs from a run-loop callout
|
|
112
|
+
// of its own in the default mode once the drain it arrived in is done, so
|
|
113
|
+
// the rest of that batch never waits behind the gesture; called on the UI
|
|
114
|
+
// thread outside a drain it runs inline, as today.
|
|
115
|
+
void CALOnUIModal(void (^block)(void));
|
|
116
|
+
|
|
117
|
+
// On the UI thread: an application-defined event for a loop waiting in
|
|
118
|
+
// nextEventMatchingMask: to wake on — after a stopModal from a command, say,
|
|
119
|
+
// which the modal loop only notices on its next event.
|
|
120
|
+
void CALPostWakeEvent();
|
|
121
|
+
|
|
122
|
+
// Windows and status items alive — the objects a person can act on. The
|
|
123
|
+
// channel's threadsafe function holds the connected environment's loop open
|
|
124
|
+
// while any exist and lets it go when none do, so an app with nothing on
|
|
125
|
+
// screen can exit. Called on the connected environment's own thread (a
|
|
126
|
+
// worker's createWindow2) the reference changes at once, so a worker whose
|
|
127
|
+
// script ends right after making a window is still held.
|
|
128
|
+
void CALUIObjectsChanged(int delta);
|
|
129
|
+
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
// handles allocated at the call (windowkit/appkit#51)
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
//
|
|
134
|
+
// JS never waits on the UI thread, so in threaded mode a verb that makes an
|
|
135
|
+
// AppKit object answers before the object exists: it allocates a CALHandle
|
|
136
|
+
// on the calling thread, hands JS an External over it, and queues the
|
|
137
|
+
// making; the UI thread binds the object into the handle as it makes it.
|
|
138
|
+
// Every later verb captures the handle and resolves it inside its own
|
|
139
|
+
// command, which runs after the making because commands apply in order.
|
|
140
|
+
// Pump mode still hands out the object itself, as it always has; the two
|
|
141
|
+
// helpers below take either.
|
|
142
|
+
|
|
143
|
+
@interface CALHandle : NSObject {
|
|
144
|
+
@public
|
|
145
|
+
uint64_t id_; // what an event's `handle` field names
|
|
146
|
+
id object_; // the UI thread's: nil until made, nil once gone
|
|
147
|
+
CALHandle* part_; // a window's root layer, allocated with it
|
|
148
|
+
std::atomic<long> number_; // a window's number once made (0 before); any thread
|
|
149
|
+
std::atomic<bool> released_; // destroyed / removed through a verb (counted once)
|
|
150
|
+
}
|
|
151
|
+
@end
|
|
152
|
+
|
|
153
|
+
CALHandle* CALNewHandle(); // any thread
|
|
154
|
+
|
|
155
|
+
// The External JS holds. `registered`: events may name it (a window, a
|
|
156
|
+
// status item), so a weak reference is kept for CALEvent::HandleRef; the
|
|
157
|
+
// External's finalizer drops it. The object is let go on the UI thread
|
|
158
|
+
// whichever thread the handle dies on.
|
|
159
|
+
Napi::Value CALWrapHandle(Napi::Env env, CALHandle* h, bool registered);
|
|
160
|
+
|
|
161
|
+
// Hold a registered handle's External strongly (pin) or let it go again,
|
|
162
|
+
// on the environment's own thread: a worker's status item stays in the bar
|
|
163
|
+
// until removeStatusItem even when JS drops its handle, as pump mode's does.
|
|
164
|
+
void CALPinHandle(Napi::Env env, uint64_t id, bool pin);
|
|
165
|
+
|
|
166
|
+
// What a verb captures for its command: the handle, or pump mode's object
|
|
167
|
+
// itself; nil for anything that is neither.
|
|
168
|
+
id CALHandleTarget(Napi::Value v);
|
|
169
|
+
|
|
170
|
+
// On the UI thread: the object a captured target names — nil before it is
|
|
171
|
+
// made and after it is gone.
|
|
172
|
+
id CALResolve(id target);
|
|
173
|
+
|
|
174
|
+
// A frame batch from a worker's outermost txCommit: queued like any
|
|
175
|
+
// command, tagged with the size it was painted at when the renderer said
|
|
176
|
+
// (txCommit({ width, height })) — what a resize handshake waits for.
|
|
177
|
+
void CALPostFrame(void (^block)(void), bool sized, double width, double height);
|
|
178
|
+
|
|
179
|
+
// On the UI thread, from windowDidResize: (windowkit/appkit#53): wait at
|
|
180
|
+
// most waitMs for a queued frame painted at width × height (not at all with
|
|
181
|
+
// nobody connected), then drain the queue inline. True when the frame came
|
|
182
|
+
// in time; *waitedMs is how long the wait took.
|
|
183
|
+
bool CALAwaitFrame(double width, double height, double waitMs, double* waitedMs);
|
|
184
|
+
|
|
185
|
+
// A layer verb's work (windowkit/appkit#52): run in the call on the main
|
|
186
|
+
// thread; from any other, recorded into that thread's open frame batch
|
|
187
|
+
// (txBegin … txCommit) or, outside one, queued as a command of its own.
|
|
188
|
+
void CALOnLayers(void (^op)(void));
|
|
189
|
+
|
|
190
|
+
// On the UI thread, before a layer's contents are replaced: while a worker's
|
|
191
|
+
// frame applies, an IOSurface taken off the layer is reported afterwards as
|
|
192
|
+
// `surface-released { id }`.
|
|
193
|
+
void CALNoteContentsReplaced(id layer, id next);
|
|
194
|
+
|
|
195
|
+
// A threadsafe function called from a thread other than its environment's —
|
|
196
|
+
// a dispatch queue, a framework's completion handler, the UI thread — which
|
|
197
|
+
// must not be touched once that environment has begun to end. Node without
|
|
198
|
+
// nodejs/node#55877 (every 18, 20 and 22; 24 before 24.14; 25 before 25.4)
|
|
199
|
+
// frees a threadsafe function in its environment's teardown whether or not a
|
|
200
|
+
// thread still holds it: a call that lands afterwards locks a freed mutex
|
|
201
|
+
// inside napi_call_threadsafe_function and aborts the process, before any
|
|
202
|
+
// callback runs, where CALCanCallIntoJS cannot help. Node with it keeps the
|
|
203
|
+
// function for the thread, but a call answered napi_closing has let the
|
|
204
|
+
// thread's reference go and may have freed it: a release after that is the
|
|
205
|
+
// same abort.
|
|
206
|
+
//
|
|
207
|
+
// So the function is used through a record of whether it still may be,
|
|
208
|
+
// under the record's lock. An environment cleanup hook, registered after the
|
|
209
|
+
// function is made and so run before the function's own teardown (hooks run
|
|
210
|
+
// in reverse order), lets the reference go on the holder's behalf and marks
|
|
211
|
+
// the record; a call after that finds it marked and does nothing. Copies
|
|
212
|
+
// share the record.
|
|
213
|
+
class CALTsfn {
|
|
214
|
+
public:
|
|
215
|
+
CALTsfn() = default;
|
|
216
|
+
|
|
217
|
+
// One answer's function for cb, made in env: holds env's loop open until
|
|
218
|
+
// it answers or is released.
|
|
219
|
+
static CALTsfn New(Napi::Env env, Napi::Function cb, const char* name);
|
|
220
|
+
|
|
221
|
+
// A long-lived function made elsewhere, whose one reference is never let
|
|
222
|
+
// go — an event source's — watched from here on: on env's thread, right
|
|
223
|
+
// after the function is made.
|
|
224
|
+
static CALTsfn Watch(napi_env env, napi_threadsafe_function f);
|
|
225
|
+
|
|
226
|
+
// From any thread: callback(env, cb, data) queued for env's thread, then
|
|
227
|
+
// the reference let go. False when nothing was queued — env is ending, or
|
|
228
|
+
// the function was answered or released already — and data is still the
|
|
229
|
+
// caller's to free.
|
|
230
|
+
template <typename DataType, typename Callback>
|
|
231
|
+
bool Answer(DataType* data, Callback callback) const {
|
|
232
|
+
return Use(true, [&](napi_threadsafe_function f) {
|
|
233
|
+
return Napi::ThreadSafeFunction(f).BlockingCall(data, callback);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// From any thread: the reference let go without an answer.
|
|
238
|
+
void Release() const { Use(true, nullptr); }
|
|
239
|
+
|
|
240
|
+
// The general form, from any thread: `call` gets the function, under the
|
|
241
|
+
// record's lock, while its environment has not begun to end and the
|
|
242
|
+
// reference is held; with `release` the reference goes after it. True when
|
|
243
|
+
// call answered napi_ok.
|
|
244
|
+
bool Use(bool release,
|
|
245
|
+
const std::function<napi_status(napi_threadsafe_function)>& call) const;
|
|
246
|
+
|
|
247
|
+
private:
|
|
248
|
+
struct Life;
|
|
249
|
+
static void EnvEnding(void* box);
|
|
250
|
+
static void Finalized(napi_env env, void* box);
|
|
251
|
+
std::shared_ptr<Life> life_;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// A one-shot callback in the calling environment, answered from any thread:
|
|
255
|
+
// `make` runs there and builds the one argument cb gets. A pending reply
|
|
256
|
+
// holds that environment's loop open, like I/O in flight.
|
|
257
|
+
CALTsfn CALReplyTo(Napi::Env env, Napi::Function cb, const char* name);
|
|
258
|
+
void CALReply(const CALTsfn& tsfn, CALValueBlock make);
|
|
259
|
+
|
|
260
|
+
// Whether JS can still be called in `env`. A worker that is ending (an
|
|
261
|
+
// uncaught error, its own process.exit) can still run a threadsafe
|
|
262
|
+
// function's callback from its loop's last spin, and there every property
|
|
263
|
+
// set fails — which node-addon-api, with C++ exceptions off, turns into a
|
|
264
|
+
// fatal error, since it cannot throw either. Every threadsafe function's
|
|
265
|
+
// callback asks first and, told no, frees what it carried and returns.
|
|
266
|
+
bool CALCanCallIntoJS(napi_env env);
|
|
267
|
+
|
|
268
|
+
// In a threadsafe function's callback, after calling into JS: an exception
|
|
269
|
+
// left pending is made that environment's uncaught exception
|
|
270
|
+
// (napi_fatal_exception). Left pending, Node's default N-API policy would
|
|
271
|
+
// only warn and drop it (windowkit/appkit#62). Not for a call made inside a
|
|
272
|
+
// JS call frame (pump2's inline delivery): there the exception rethrows to
|
|
273
|
+
// the caller, as it always has.
|
|
274
|
+
void CALRaiseUncaughtIfPending(napi_env env);
|
|
275
|
+
|
|
276
|
+
// cb(...args) with undefined as this, raw, so a failed call is a status
|
|
277
|
+
// rather than node-addon-api's fatal error — from a threadsafe function's
|
|
278
|
+
// callback, so an exception cb throws is then the environment's uncaught
|
|
279
|
+
// exception (CALRaiseUncaughtIfPending).
|
|
280
|
+
void CALCallJS(napi_env env, napi_value cb,
|
|
281
|
+
std::initializer_list<napi_value> args);
|
|
282
|
+
|
|
283
|
+
// A read of AppKit state, in either mode. Without a callback (the last
|
|
284
|
+
// argument, when it is a function) it answers synchronously — on the main
|
|
285
|
+
// thread only, as every read always has; off it that is a TypeError. With
|
|
286
|
+
// one, `compute` runs on the UI thread and what it returns builds cb's one
|
|
287
|
+
// argument in the caller's environment, a later tick even on the main
|
|
288
|
+
// thread. `compute` never returns nil. `nested`: compute spins a run loop of
|
|
289
|
+
// its own (a test hook waiting for a window), so a queued one runs through
|
|
290
|
+
// CALOnUIModal rather than inside a drain.
|
|
291
|
+
Napi::Value CALAnswer(const Napi::CallbackInfo& info, const char* name,
|
|
292
|
+
CALValueBlock (^compute)(void), bool nested = false);
|