Skip to content

Commit 9c0faa8

Browse files
committed
Exposing env specific scheduler and passing it through Realm config
1 parent 4c0e32f commit 9c0faa8

File tree

6 files changed

+25
-2
lines changed

6 files changed

+25
-2
lines changed

packages/realm/bindgen/src/templates/node-wrapper.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function generate(context: TemplateContext): void {
3636
3737
// We know that node always has real WeakRefs so just use them.
3838
export const WeakRef = global.WeakRef;
39+
// Export a special function to get the env specific scheduler
40+
export const getPlatformScheduler = nativeModule.getPlatformScheduler;
3941
`);
4042

4143
generateNativeBigIntSupport(out);

packages/realm/bindgen/src/templates/node.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class NodeAddon extends CppClass {
8383
this.withCrtpBase("Napi::Addon");
8484

8585
this.members.push(new CppVar("std::deque<std::string>", "m_string_bufs"));
86-
this.members.push(new CppVar("std::shared_ptr<NapiScheduler>", "m_scheduler"));
86+
this.members.push(new CppVar("std::shared_ptr<util::Scheduler>", "m_scheduler"));
8787
this.addMethod(
8888
new CppMethod("wrapString", "const std::string&", [new CppVar("std::string", "str")], {
8989
attributes: "inline",
@@ -105,6 +105,8 @@ class NodeAddon extends CppClass {
105105
this.classes.forEach((t) =>
106106
this.members.push(new CppVar("Napi::FunctionReference", NodeAddon.memberNameForExtractor(t))),
107107
);
108+
109+
// Injectables
108110
this.addMethod(
109111
new CppMethod("injectInjectables", "void", [node_callback_info], {
110112
body: `
@@ -123,6 +125,16 @@ class NodeAddon extends CppClass {
123125
}),
124126
);
125127

128+
// Env specific scheduler
129+
this.addMethod(
130+
new CppMethod("getPlatformScheduler", "Napi::Value", [node_callback_info], {
131+
body: `
132+
const auto env = info.Env();
133+
return NODE_FROM_SHARED_Scheduler(env, env.GetInstanceData<RealmAddon>()->m_scheduler);
134+
`,
135+
}),
136+
);
137+
126138
this.addMethod(
127139
new CppCtor(this.name, [new CppVar("Napi::Env", env), new CppVar("Napi::Object", "exports")], {
128140
mem_inits: [new CppMemInit("m_scheduler", `std::make_shared<NapiScheduler>(${env})`)],
@@ -132,6 +144,7 @@ class NodeAddon extends CppClass {
132144
.map(([name, val]) => `InstanceValue("${name}", ${val}, napi_enumerable),`)
133145
.join("\n")}
134146
InstanceMethod<&${this.name}::injectInjectables>("injectInjectables"),
147+
InstanceMethod<&${this.name}::getPlatformScheduler>("getPlatformScheduler"),
135148
});
136149
`,
137150
}),

packages/realm/bindgen/src/templates/typescript.ts

+3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ export function generate({ rawSpec, spec: boundSpec, file }: TemplateContext): v
187187
out("export type AppError = Error & {code: number};");
188188
out("export type CppErrorCode = Error & {code: number, category: string};");
189189

190+
out("// Special functions");
191+
out("export const getPlatformScheduler: undefined | (() => binding.SharedScheduler);");
192+
190193
out(`
191194
// WeakRef polyfill for Hermes.
192195
export class WeakRef<T extends object> {

packages/realm/binding/node/napi_scheduler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ NapiScheduler::NapiScheduler(Napi::Env& env)
5757

5858
bool NapiScheduler::is_on_thread() const noexcept
5959
{
60-
return false;
60+
return m_id == std::this_thread::get_id();
6161
}
6262

6363
bool NapiScheduler::is_same_as(const Scheduler* other) const noexcept

packages/realm/binding/node/napi_scheduler.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include <napi.h>
2525

26+
#include <thread>
27+
2628
namespace realm::js::node {
2729

2830
using VoidUniqueFunctionImpl = std::remove_pointer_t<decltype(realm::util::UniqueFunction<void()>().release())>;
@@ -38,6 +40,7 @@ class NapiScheduler : public realm::util::Scheduler {
3840
private:
3941
Napi::Env m_env;
4042
Napi::TypedThreadSafeFunction<std::nullptr_t, VoidUniqueFunctionImpl> m_tsf;
43+
std::thread::id m_id = std::this_thread::get_id();
4144
};
4245

4346
} // namespace realm::js::node

packages/realm/src/Realm.ts

+2
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ export class Realm {
455455
syncConfig: config.sync ? toBindingSyncConfig(config.sync) : undefined,
456456
forceSyncHistory: config.openSyncedRealmLocally,
457457
automaticallyHandleBacklinksInMigrations: config.migrationOptions?.resolveEmbeddedConstraints ?? false,
458+
// Use a platform-specific scheduler if the platform expose one
459+
scheduler: binding.getPlatformScheduler ? binding.getPlatformScheduler() : undefined,
458460
},
459461
};
460462
}

0 commit comments

Comments
 (0)