From 5088f5fff231023a722a4626e73661ff6b9ad9fd Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 25 Jun 2024 16:45:04 -0300 Subject: [PATCH] feat: expose __dateTimeConfigurationChangeNotification (#220) --- NativeScript/runtime/Runtime.h | 3 ++- NativeScript/runtime/Runtime.mm | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/NativeScript/runtime/Runtime.h b/NativeScript/runtime/Runtime.h index 0b86b32f..e339836d 100644 --- a/NativeScript/runtime/Runtime.h +++ b/NativeScript/runtime/Runtime.h @@ -69,7 +69,8 @@ class Runtime { void DefinePerformanceObject(v8::Isolate* isolate, v8::Local globalTemplate); void DefineTimeMethod(v8::Isolate* isolate, v8::Local globalTemplate); void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local globalTemplate); - + void DefineDateTimeConfigurationChangeNotificationMethod(v8::Isolate* isolate, v8::Local globalTemplate); + static void PerformanceNowCallback(const v8::FunctionCallbackInfo& args); v8::Isolate* isolate_; std::unique_ptr moduleInternal_; diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index 2e837d8a..ca7423da 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -38,6 +38,25 @@ SimpleAllocator allocator_; NSDictionary* AppPackageJson = nil; +// TODO: consider listening to timezone changes and automatically reseting the DateTime. Probably makes more sense to move it to its own file +//void UpdateTimezoneNotificationCallback(CFNotificationCenterRef center, +// void *observer, +// CFStringRef name, +// const void *object, +// CFDictionaryRef userInfo) { +// Runtime* r = (Runtime*)observer; +// auto isolate = r->GetIsolate(); +// +// CFRunLoopPerformBlock(r->RuntimeLoop(), kCFRunLoopDefaultMode, ^() { +// TODO: lock isolate here? +// isolate->DateTimeConfigurationChangeNotification(Isolate::TimeZoneDetection::kRedetect); +// }); +//} +// add this to register (most likely on setting up isolate +//CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), this, &UpdateTimezoneNotificationCallback, kCFTimeZoneSystemTimeZoneDidChangeNotification, nullptr, CFNotificationSuspensionBehaviorDeliverImmediately); +// add this to remove the observer +//CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), this, kCFTimeZoneSystemTimeZoneDidChangeNotification, NULL); + void DisposeIsolateWhenPossible(Isolate* isolate) { // most of the time, this will never delay disposal // occasionally this can happen when the runtime is destroyed by actions of its own isolate @@ -379,6 +398,13 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { globalTemplate->Set(ToV8String(isolate, "__drainMicrotaskQueue"), drainMicrotaskTemplate); } +void Runtime::DefineDateTimeConfigurationChangeNotificationMethod(v8::Isolate* isolate, v8::Local globalTemplate) { + Local drainMicrotaskTemplate = FunctionTemplate::New(isolate, [](const FunctionCallbackInfo& info) { + info.GetIsolate()->DateTimeConfigurationChangeNotification(Isolate::TimeZoneDetection::kRedetect); + }); + globalTemplate->Set(ToV8String(isolate, "__dateTimeConfigurationChangeNotification"), drainMicrotaskTemplate); +} + bool Runtime::IsAlive(const Isolate* isolate) { // speedup lookup by avoiding locking if thread locals match // note: this can be a problem when the Runtime is deleted in a different thread that it was created