Skip to content

Commit

Permalink
Exposing env specific scheduler and passing it through Realm config
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Jul 19, 2024
1 parent 7c26045 commit d5719b4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/realm/bindgen/src/templates/node-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export function generate(context: TemplateContext): void {
// We know that node always has real WeakRefs so just use them.
export const WeakRef = global.WeakRef;
// Export a special function to get the env specific scheduler
export const getPlatformScheduler = nativeModule.getPlatformScheduler;
`);

generateNativeBigIntSupport(out);
Expand Down
15 changes: 14 additions & 1 deletion packages/realm/bindgen/src/templates/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class NodeAddon extends CppClass {
this.withCrtpBase("Napi::Addon");

this.members.push(new CppVar("std::deque<std::string>", "m_string_bufs"));
this.members.push(new CppVar("std::shared_ptr<NapiScheduler>", "m_scheduler"));
this.members.push(new CppVar("std::shared_ptr<util::Scheduler>", "m_scheduler"));
this.addMethod(
new CppMethod("wrapString", "const std::string&", [new CppVar("std::string", "str")], {
attributes: "inline",
Expand All @@ -105,6 +105,8 @@ class NodeAddon extends CppClass {
this.classes.forEach((t) =>
this.members.push(new CppVar("Napi::FunctionReference", NodeAddon.memberNameForExtractor(t))),
);

// Injectables
this.addMethod(
new CppMethod("injectInjectables", "void", [node_callback_info], {
body: `
Expand All @@ -123,6 +125,16 @@ class NodeAddon extends CppClass {
}),
);

// Env specific scheduler
this.addMethod(
new CppMethod("getPlatformScheduler", "Napi::Value", [node_callback_info], {
body: `
const auto env = info.Env();
return NODE_FROM_SHARED_Scheduler(env, env.GetInstanceData<RealmAddon>()->m_scheduler);
`,
}),
);

this.addMethod(
new CppCtor(this.name, [new CppVar("Napi::Env", env), new CppVar("Napi::Object", "exports")], {
mem_inits: [new CppMemInit("m_scheduler", `std::make_shared<NapiScheduler>(${env})`)],
Expand All @@ -132,6 +144,7 @@ class NodeAddon extends CppClass {
.map(([name, val]) => `InstanceValue("${name}", ${val}, napi_enumerable),`)
.join("\n")}
InstanceMethod<&${this.name}::injectInjectables>("injectInjectables"),
InstanceMethod<&${this.name}::getPlatformScheduler>("getPlatformScheduler"),
});
`,
}),
Expand Down
3 changes: 3 additions & 0 deletions packages/realm/bindgen/src/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ export function generate({ rawSpec, spec: boundSpec, file }: TemplateContext): v
out("export type AppError = Error & {code: number};");
out("export type CppErrorCode = Error & {code: number, category: string};");

out("// Special functions");
out("export const getPlatformScheduler: undefined | (() => binding.SharedScheduler);");

out(`
// WeakRef polyfill for Hermes.
export class WeakRef<T extends object> {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/binding/node/napi_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ NapiScheduler::NapiScheduler(Napi::Env& env)

bool NapiScheduler::is_on_thread() const noexcept
{
return false;
return m_id == std::this_thread::get_id();
}

bool NapiScheduler::is_same_as(const Scheduler* other) const noexcept
Expand Down
3 changes: 3 additions & 0 deletions packages/realm/binding/node/napi_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <napi.h>

#include <thread>

namespace realm::js::node {

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

} // namespace realm::js::node
2 changes: 2 additions & 0 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ export class Realm {
syncConfig: config.sync ? toBindingSyncConfig(config.sync) : undefined,
forceSyncHistory: config.openSyncedRealmLocally,
automaticallyHandleBacklinksInMigrations: config.migrationOptions?.resolveEmbeddedConstraints ?? false,
// Use a platform-specific scheduler if the platform expose one
scheduler: binding.getPlatformScheduler ? binding.getPlatformScheduler() : undefined,
},
};
}
Expand Down

0 comments on commit d5719b4

Please sign in to comment.