From 95d1ef476f47ab0748584bcd5bf5afb0ce6c9623 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 10 Mar 2023 09:59:47 -0800 Subject: [PATCH] Did a bit of provider cleanup --- src/dawn/state/State.hpp | 9 +++++++++ src/dawn/state/StateProvider.hpp | 9 +++++---- src/dawn/time/ITimeManager.hpp | 10 ++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/dawn/state/State.hpp b/src/dawn/state/State.hpp index f21c68d1..757156c3 100644 --- a/src/dawn/state/State.hpp +++ b/src/dawn/state/State.hpp @@ -46,6 +46,8 @@ namespace Dawn { std::vector eventLegacyBridge; public: + std::vector> _stateProviderListeners; + /** * Called by the state event when it is disposing (before the StateOwner * itself is). @@ -228,6 +230,13 @@ namespace Dawn { * useEffects or useEvents. */ virtual ~StateOwner() { + auto providers = this->_stateProviderListeners; + auto itProvider = providers.begin(); + while(itProvider != providers.end()) { + (*itProvider)(); + ++itProvider; + } + auto it = this->eventsSubscribed.begin(); while(it != this->eventsSubscribed.end()) { (*it)->_stateOwnerDestroyed(this); diff --git a/src/dawn/state/StateProvider.hpp b/src/dawn/state/StateProvider.hpp index f6355534..4b953ce1 100644 --- a/src/dawn/state/StateProvider.hpp +++ b/src/dawn/state/StateProvider.hpp @@ -38,12 +38,13 @@ namespace Dawn { l.data = data; this->listeners.push_back(l); - return std::bind([&](struct StateListener listener) { + auto unsub = std::bind([&](struct StateListener listener) { + // Not yet implemented assertUnreachable(); - // auto it = std::find(listeners.begin(), listeners.end(), listener); - // assertFalse(it == listeners.end()); - // listeners.erase(it); }, l); + + context->_stateProviderListeners.push_back(unsub); + return unsub; } }; } \ No newline at end of file diff --git a/src/dawn/time/ITimeManager.hpp b/src/dawn/time/ITimeManager.hpp index ef37d980..5b5d1392 100644 --- a/src/dawn/time/ITimeManager.hpp +++ b/src/dawn/time/ITimeManager.hpp @@ -77,6 +77,16 @@ namespace Dawn { return context->getGame()->timeManager.timeoutProvider.addEffect(someCallback, timeout, context); } + /** + * Use interval provider method. Invokes your callback at a consistant and + * common time based on the interval you provide. + * + * @tparam T Your context type (usually SceneItemComponent). + * @param someCallback Callback to be invoked. + * @param timeout Interval to invoke your callback. + * @param context Context of the component, just use (this). + * @return Method that when invoked will unsubscribe from the interval. + */ template std::function useInterval( std::function callback,