Skip to content

Commit 8d3b71c

Browse files
committed
refactor(runtime): improve refresh control and libomp integration
1 parent a7dde15 commit 8d3b71c

File tree

18 files changed

+385
-128
lines changed

18 files changed

+385
-128
lines changed

api/conduit.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@ export const DEFAULT_MAX_RECONNECT_TIMEOUT = 256
3838
export const pool = new Set()
3939

4040
// reconnect when application resumes
41-
hooks.onApplicationResume(() => {
41+
hooks.onApplicationResume(async () => {
4242
isApplicationPaused = false
43+
let result
44+
result = await ipc.request('internal.conduit.stop')
45+
if (result.err) {
46+
console.warn(result.err)
47+
}
48+
49+
result = await ipc.request('internal.conduit.start')
50+
if (result.err) {
51+
console.warn(result.err)
52+
}
53+
4354
for (const conduit of pool) {
4455
// @ts-ignore
4556
if (conduit?.shouldReconnect) {
@@ -51,8 +62,13 @@ hooks.onApplicationResume(() => {
5162
}
5263
})
5364

54-
hooks.onApplicationPause(() => {
65+
hooks.onApplicationPause(async () => {
5566
isApplicationPaused = true
67+
const result = await ipc.request('internal.conduit.stop')
68+
if (result.err) {
69+
console.warn(result.err)
70+
}
71+
5672
for (const conduit of pool) {
5773
if (conduit) {
5874
// @ts-ignore

api/index.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16478,7 +16478,10 @@ declare module "socket:navigation/navigation" {
1647816478
}
1647916479

1648016480
declare module "socket:navigation" {
16481-
export {};
16481+
export const Navigation: any;
16482+
export const NavigationHistoryEntry: any;
16483+
const _default: any;
16484+
export default _default;
1648216485
}
1648316486

1648416487
declare module "socket:node-esm-loader" {
@@ -17587,7 +17590,7 @@ declare module "socket:service-worker/notification" {
1758717590
}
1758817591

1758917592
declare module "socket:service-worker/registration" {
17590-
export class ServiceWorkerRegistration {
17593+
export class ServiceWorkerRegistration extends EventTarget {
1759117594
constructor(info: any, serviceWorker: any);
1759217595
get scope(): any;
1759317596
get updateViaCache(): string;
@@ -17599,7 +17602,7 @@ declare module "socket:service-worker/registration" {
1759917602
get navigationPreload(): any;
1760017603
getNotifications(): Promise<any>;
1760117604
showNotification(title: any, options: any): Promise<void>;
17602-
unregister(): Promise<void>;
17605+
unregister(): Promise<boolean>;
1760317606
update(): Promise<void>;
1760417607
#private;
1760517608
}

api/internal/primitives.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,6 @@ export function init () {
309309
// @ts-ignore
310310
install({ geolocation }, globalThis.navigator, 'geolocation')
311311

312-
if (!globalThis.navigation) {
313-
navigation.apply
314-
}
315-
316312
install({ close })
317313

318314
async function close () {

api/service-worker/registration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { showNotification, getNotifications } from './notification.js'
22
import ipc from '../ipc.js'
33

4-
export class ServiceWorkerRegistration {
4+
export class ServiceWorkerRegistration extends EventTarget {
55
#info = null
66
#active = null
77
#waiting = null
88
#installing = null
99
#onupdatefound = null
1010

1111
constructor (info, serviceWorker) {
12+
super()
1213
this.#info = info
1314

1415
serviceWorker[Symbol.for('socket.runtime.serviceWorker.state')] = info.registration.state
@@ -110,6 +111,8 @@ export class ServiceWorkerRegistration {
110111
if (result.err) {
111112
throw result.err
112113
}
114+
115+
return true
113116
}
114117

115118
async update () {}

bin/install.sh

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ if [ "$host" == "Darwin" ]; then
165165
die $? "not ok - missing build tools, try \"$(advice "libtool")\""
166166
quiet command -v curl
167167
die $? "not ok - missing curl, try \"$(advice "curl")\""
168-
if ! brew list | grep libomp >/dev/null 2>&1; then
169-
die $? "not ok - missing libomp, try \"$(advice "libomp")\""
170-
fi
171168
fi
172169

173170
if [ "$host" == "Linux" ]; then
@@ -598,11 +595,47 @@ function _install {
598595
cp -rfp "$BUILD_DIR/$arch-$platform"/lib$_d/*.a "$SOCKET_HOME/lib$_d/$arch-$platform"
599596
fi
600597

601-
if [[ "$host" == "Darwin" ]] && [[ "$platform" != "desktop" ]]; then
602-
if (( do_link == 1 )); then
603-
ln -sf "$BUILD_DIR/$arch-$platform"/lib/*.metallib "$SOCKET_HOME/lib/$arch-$platform"
598+
if [[ "$host" == "Darwin" ]]; then
599+
if [[ "$platform" == "desktop" ]]; then
600+
echo "# locating 'libomp.dylib...'"
601+
local llvms=()
602+
local libomp_path=""
603+
604+
llvms+=($(find /opt/homebrew/opt/llvm* 2>/dev/null))
605+
llvms+=($(echo $LLVM_PATHS | tr ':' ' '))
606+
607+
for path in ${llvms[@]}; do
608+
local libomp="$(find -L "$path" -path '*/lib/libomp.dylib' 2>/dev/null | head -n1)"
609+
if [ -n "$libomp" ] && [ -f "$libomp" ]; then
610+
libomp_path="$libomp"
611+
echo "# found LLVM libomp at: '$libomp_path'"
612+
break
613+
fi
614+
done
615+
616+
if [ -z "$libomp_path" ] || ! [ -f "$libomp_path" ]; then
617+
# Fallback: try to locate a libomp from the standalone libomp package via Homebrew
618+
local fallback_prefix=$(brew --prefix libomp 2>/dev/null || true)
619+
if [ -n "$fallback_prefix" ] && [ -f "$fallback_prefix/lib/libomp.dylib" ]; then
620+
libomp_path="$fallback_prefix/lib/libomp.dylib"
621+
echo "# found standalone libomp at: '$libomp_path'"
622+
else
623+
die 1 "not ok - could not locate 'libomp.dylib'. Please install an LLVM package (preferred) or the libomp package: \"$(advice "libomp")\""
624+
fi
625+
fi
626+
627+
cp -f "$libomp" "$SOCKET_HOME/lib/$arch-desktop/$(basename "$libomp")"
628+
echo "# copied '$libomp_path'"
629+
630+
echo "# modifying the install name of the copied 'libomp.dylib'"
631+
quiet install_name_tool -id "@rpath/$(basename "$libomp_path")" "$SOCKET_HOME/lib/$arch-desktop/$(basename "$libomp")"
632+
die $? "not ok - failed to modify the install name of copied 'libomp.dylib'"
604633
else
605-
cp -rfp "$BUILD_DIR/$arch-$platform"/lib/*.metallib "$SOCKET_HOME/lib/$arch-$platform"
634+
if (( do_link == 1 )); then
635+
ln -sf "$BUILD_DIR/$arch-$platform"/lib/*.metallib "$SOCKET_HOME/lib/$arch-$platform"
636+
else
637+
cp -rfp "$BUILD_DIR/$arch-$platform"/lib/*.metallib "$SOCKET_HOME/lib/$arch-$platform"
638+
fi
606639
fi
607640
fi
608641
fi
@@ -934,6 +967,9 @@ function _compile_llama {
934967
if [[ "$host" != "Win32" ]]; then
935968
quiet command -v cmake
936969
die $? "not ok - missing cmake, \"$(advice 'cmake')\""
970+
local cflags="-fPIC"
971+
export CFLAGS="$cflags"
972+
export CXXFLAGS="$cflags"
937973

938974
quiet cmake -S . -B build -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$target-$platform" ${cmake_args[@]}
939975
die $? "not ok - libllama.a (desktop)"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"standard": {
3434
"ignore": [
3535
"/api/external/",
36+
"/api/navigation/navigation.js",
3637
"/api/test/fast-deep-equal.js",
3738
"/api/crypto/sodium.js",
3839
"/api/url/urlpattern/urlpattern.js",

src/cli/main.cc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,10 @@ int main (int argc, char* argv[]) {
30693069
if (platform.mac && isForDesktop) {
30703070
log("preparing build for mac");
30713071

3072-
flags = "-std=c++2a -ObjC++ -v -fopenmp";
3072+
flags = "-std=c++2a -ObjC++ -v";
3073+
if (!flagCodeSign) {
3074+
flags += " -fopenmp";
3075+
}
30733076
flags += " -framework UniformTypeIdentifiers";
30743077
flags += " -framework CoreBluetooth";
30753078
flags += " -framework CoreLocation";
@@ -3091,6 +3094,10 @@ int main (int argc, char* argv[]) {
30913094
flags += " -I" + prefixFile();
30923095
flags += " -I" + prefixFile("include");
30933096
flags += " -L" + prefixFile("lib/" + platform.arch + "-desktop");
3097+
if (flagCodeSign) {
3098+
flags += " -Wl,-rpath,@executable_path";
3099+
}
3100+
flags += " -fPIC";
30943101
flags += " -lsocket-runtime";
30953102
flags += " -lomp";
30963103
flags += " -luv";
@@ -3243,6 +3250,13 @@ int main (int argc, char* argv[]) {
32433250
auto credits = tmpl(gCredits, defaultTemplateAttrs);
32443251

32453252
writeFile(paths.pathResourcesRelativeToUserBuild / "Credits.html", credits);
3253+
3254+
fs::create_directories(paths.pathPackage / pathBase / "MacOS");
3255+
fs::copy(
3256+
trim(prefixFile("lib/" + platform.arch + "-desktop/libomp.dylib")),
3257+
paths.pathPackage / pathBase / "MacOS" / "libomp.dylib",
3258+
fs::copy_options::overwrite_existing
3259+
);
32463260
}
32473261

32483262
if (platform.mac && isForDesktop) {
@@ -5243,6 +5257,13 @@ int main (int argc, char* argv[]) {
52435257

52445258
entitlementSettings["configured_entitlements"] = "";
52455259

5260+
if (settings["permissions_allow_unvalidated_native_libraries"] == "true") {
5261+
entitlementSettings["configured_entitlements"] += (
5262+
" <key>com.apple.security.cs.disable-library-validation</key>\n"
5263+
" <true/>\n"
5264+
);
5265+
}
5266+
52465267
if (settings["permissions_allow_push_notifications"] == "true") {
52475268
entitlementSettings["configured_entitlements"] += (
52485269
" <key>com.apple.developer.usernotifications.filtering</key>\n"
@@ -6623,9 +6644,13 @@ int main (int argc, char* argv[]) {
66236644

66246645
<< " && codesign"
66256646
<< commonFlags.str()
6626-
<< paths.pathPackage.string();
6647+
<< paths.pathPackage.string()
66276648

6628-
if (flagDebugMode) {
6649+
<< " && codesign"
6650+
<< commonFlags.str()
6651+
<< (paths.pathPackage / "Contents" / "MacOS" / "libomp.dylib").string();
6652+
6653+
if (flagDebugMode || flagVerboseMode) {
66296654
log(signCommand.str());
66306655
}
66316656

@@ -6634,7 +6659,7 @@ int main (int argc, char* argv[]) {
66346659
if (r.output.size() > 0) {
66356660
if (r.exitCode != 0) {
66366661
log("ERROR: Unable to sign application with 'codesign'");
6637-
if (flagDebugMode) {
6662+
if (flagDebugMode || flagVerboseMode) {
66386663
log(r.output);
66396664
}
66406665
exit(r.exitCode);

src/runtime/app/apple.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (void) menuWillOpen: (NSMenu*) menu {
4949
if (!w) return;
5050

5151
[w makeKeyAndOrderFront: nil];
52-
[NSApp activateIgnoringOtherApps:YES];
52+
[NSApp activateIgnoringOtherApps: YES];
5353

5454
if (app != nullptr) {
5555
for (auto window : self.app->runtime.windowManager.windows) {

src/runtime/bridge.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ namespace ssc::runtime::bridge {
9292
Manager& operator = (Manager&&) = delete;
9393

9494
SharedPointer<Bridge> get (int, const BridgeOptions&);
95+
SharedPointer<Bridge> get (int);
9596
bool has (int) const;
9697
bool remove (int);
9798
};

src/runtime/bridge/manager.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ namespace ssc::runtime::bridge {
2222
});
2323
}
2424

25-
return this->entries[index];
25+
return this->entries.at(index);
26+
}
27+
28+
SharedPointer<Bridge> Manager::get (int index) {
29+
Lock lock(this->mutex);
30+
31+
if (index >= this->entries.size()) {
32+
return nullptr;
33+
}
34+
35+
return this->entries.at(index);
2636
}
2737

2838
bool Manager::has (int index) const {

0 commit comments

Comments
 (0)