-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable custom timer factory to be provided (#12856)
* Enable custom timer factory to be provided * Change files
- Loading branch information
1 parent
c2a2bd6
commit 34c5cd8
Showing
12 changed files
with
176 additions
and
13 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-286662e9-d377-48ce-8d82-95e92de04270.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "Enable custom timer factory to be provided", | ||
"packageName": "react-native-windows", | ||
"email": "30809111+acoates-ms@users.noreply.github.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "pch.h" | ||
#include "Timer.h" | ||
#include "Timer.g.cpp" | ||
|
||
#include <CppWinRTIncludes.h> | ||
#include "ReactCoreInjection.h" | ||
|
||
namespace winrt::Microsoft::ReactNative::implementation { | ||
|
||
// Implementation of ITimer based on Windows' DispatcherQueue | ||
struct ReactTimer : winrt::implements<ReactTimer, ITimer> { | ||
ReactTimer(const IReactPropertyBag &properties) { | ||
const auto queue = winrt::dispatching::DispatcherQueue::GetForCurrentThread(); | ||
m_dispatcherQueueTimer = queue.CreateTimer(); | ||
} | ||
|
||
winrt::Windows::Foundation::TimeSpan Interval() noexcept { | ||
return m_dispatcherQueueTimer.Interval(); | ||
} | ||
|
||
void Interval(winrt::Windows::Foundation::TimeSpan value) noexcept { | ||
m_dispatcherQueueTimer.Interval(value); | ||
} | ||
|
||
void Start() noexcept { | ||
return m_dispatcherQueueTimer.Start(); | ||
} | ||
|
||
void Stop() noexcept { | ||
return m_dispatcherQueueTimer.Stop(); | ||
} | ||
|
||
winrt::event_token Tick(const winrt::Windows::Foundation::EventHandler<IInspectable> &handler) { | ||
return m_dispatcherQueueTimer.Tick([handler](auto sender, auto args) { handler(sender, args); }); | ||
} | ||
|
||
void Tick(winrt::event_token const &token) { | ||
m_dispatcherQueueTimer.Tick(token); | ||
} | ||
|
||
private: | ||
winrt::dispatching::DispatcherQueueTimer m_dispatcherQueueTimer{nullptr}; | ||
}; | ||
|
||
Timer::Timer() noexcept {} | ||
|
||
ITimer Timer::Create(const IReactPropertyBag &properties) { | ||
auto dispatcher = properties.Get(ReactDispatcherHelper::UIDispatcherProperty()).try_as<IReactDispatcher>(); | ||
if (!dispatcher.HasThreadAccess()) { | ||
throw winrt::hresult_wrong_thread(); | ||
} | ||
|
||
if (auto customTimerFactory = implementation::ReactCoreInjection::GetTimerFactory(properties)) { | ||
return customTimerFactory(); | ||
} else { | ||
return winrt::make<ReactTimer>(properties); | ||
} | ||
} | ||
|
||
} // namespace winrt::Microsoft::ReactNative::implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "Timer.g.h" | ||
|
||
#include "winrt/Microsoft.ReactNative.h" | ||
|
||
namespace winrt::Microsoft::ReactNative::implementation { | ||
|
||
struct Timer : TimerT<Timer> { | ||
Timer() noexcept; | ||
|
||
static ITimer Create(const IReactPropertyBag &properties); | ||
}; | ||
|
||
} // namespace winrt::Microsoft::ReactNative::implementation | ||
|
||
namespace winrt::Microsoft::ReactNative::factory_implementation { | ||
struct Timer : TimerT<Timer, implementation::Timer> {}; | ||
} // namespace winrt::Microsoft::ReactNative::factory_implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "DocString.h" | ||
import "IJSValueReader.idl"; | ||
import "IJSValueWriter.idl"; | ||
import "Theme.idl"; | ||
|
||
namespace Microsoft.ReactNative { | ||
|
||
[webhosthidden] | ||
interface ITimer | ||
{ | ||
Windows.Foundation.TimeSpan Interval; | ||
void Start(); | ||
void Stop(); | ||
|
||
event Windows.Foundation.EventHandler<Object> Tick; | ||
} | ||
|
||
[default_interface] | ||
[webhosthidden] | ||
DOC_STRING( | ||
"Used to create timers.") | ||
runtimeclass Timer | ||
{ | ||
DOC_STRING( | ||
"Creates a UI timer. Must be called on the UI thread. Using this rather than " | ||
"System/Windows.DispatcherQueue.CreateTimer works when applications have provided " | ||
"custom Timer implementations using @ReactCoreInjection.SetTimerFactory") | ||
static ITimer Create(IReactPropertyBag properties); | ||
} | ||
|
||
} // namespace Microsoft.ReactNative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters