Sunset old state system finally.
This commit is contained in:
		@@ -5,28 +5,15 @@
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "assert/assert.hpp"
 | 
			
		||||
#include "event/Event.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class IStateEvent;
 | 
			
		||||
 | 
			
		||||
  template<typename...A>
 | 
			
		||||
  class StateEvent;
 | 
			
		||||
 | 
			
		||||
  template<typename ...A>
 | 
			
		||||
  class StateOwnerEventLegacy;
 | 
			
		||||
 | 
			
		||||
  class IStateOwnerEventLegacy {
 | 
			
		||||
    public:
 | 
			
		||||
      virtual void removeListener() = 0;
 | 
			
		||||
      virtual void teardown() = 0;
 | 
			
		||||
      virtual ~IStateOwnerEventLegacy() {}
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class IStateOwner {
 | 
			
		||||
    public:
 | 
			
		||||
      virtual void _stateEventDisposed(IStateEvent *evt) = 0;
 | 
			
		||||
      virtual void _stateLegacyEventDisposed(IStateOwnerEventLegacy *evt) = 0;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  template<typename...A>
 | 
			
		||||
 
 | 
			
		||||
@@ -8,42 +8,9 @@
 | 
			
		||||
#include "StateProperty.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  template<typename ...A>
 | 
			
		||||
  class StateOwnerEventLegacy : public IStateOwnerEventLegacy {
 | 
			
		||||
    public:
 | 
			
		||||
      IStateOwner *owner;
 | 
			
		||||
      Event<A...> *event;
 | 
			
		||||
      std::function<void(A...)> fn;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Function used to simply remove the event listener from the legacy
 | 
			
		||||
       * event, does not deal with the state owner. 
 | 
			
		||||
       */
 | 
			
		||||
      void removeListener() override {
 | 
			
		||||
        event->removeListener(this, &StateOwnerEventLegacy::callback);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Function that can be used to tear down this legacy event.
 | 
			
		||||
       */
 | 
			
		||||
      void teardown() override {
 | 
			
		||||
        this->removeListener();
 | 
			
		||||
        owner->_stateLegacyEventDisposed(this);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Callbaack method that is invoked by the legacy event.
 | 
			
		||||
       * @param args Arguments received by legacy event.
 | 
			
		||||
       */
 | 
			
		||||
      void callback(A... args) {
 | 
			
		||||
        this->fn(args...);
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class StateOwner : public IStateOwner {
 | 
			
		||||
    private:
 | 
			
		||||
      std::vector<IStateEvent*> eventsSubscribed;
 | 
			
		||||
      std::vector<IStateOwnerEventLegacy*> eventLegacyBridge;
 | 
			
		||||
 | 
			
		||||
    protected:
 | 
			
		||||
      /**
 | 
			
		||||
@@ -62,17 +29,9 @@ namespace Dawn {
 | 
			
		||||
          (*it)->_stateOwnerDestroyed(this);
 | 
			
		||||
          ++it;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        auto itBridge = this->eventLegacyBridge.begin();
 | 
			
		||||
        while(itBridge != this->eventLegacyBridge.end()) {
 | 
			
		||||
          (*itBridge)->removeListener();
 | 
			
		||||
          delete *itBridge;
 | 
			
		||||
          ++itBridge;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
        this->_stateProviderListeners.clear();
 | 
			
		||||
        this->eventsSubscribed.clear();
 | 
			
		||||
        this->eventLegacyBridge.clear();
 | 
			
		||||
      }
 | 
			
		||||
  
 | 
			
		||||
    public:
 | 
			
		||||
@@ -95,24 +54,6 @@ namespace Dawn {
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Called by legacy events when they are being disposed in a way that was
 | 
			
		||||
       * not called by this state owner disposing.
 | 
			
		||||
       * 
 | 
			
		||||
       * @param evt Event that is being disposed.
 | 
			
		||||
       */
 | 
			
		||||
      void _stateLegacyEventDisposed(IStateOwnerEventLegacy *evt) override {
 | 
			
		||||
        auto it = this->eventLegacyBridge.begin();
 | 
			
		||||
        while(it != this->eventLegacyBridge.end()) {
 | 
			
		||||
          if(*it == evt) {
 | 
			
		||||
            this->eventLegacyBridge.erase(it);
 | 
			
		||||
            break;
 | 
			
		||||
          } else {
 | 
			
		||||
            ++it;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Listen for changes to a state property and invoke the provided func
 | 
			
		||||
       * when the value is changed.
 | 
			
		||||
@@ -230,38 +171,6 @@ namespace Dawn {
 | 
			
		||||
 | 
			
		||||
        return listener.unsub;
 | 
			
		||||
      }
 | 
			
		||||
    
 | 
			
		||||
      /**
 | 
			
		||||
       * Listen for callback of a legacy styled event. This will be removed in
 | 
			
		||||
       * the future in favour of everything using State Events. This uses a lot
 | 
			
		||||
       * more memory than the new state event listener.
 | 
			
		||||
       * 
 | 
			
		||||
       * @deprecated In favour of StateEvent<>
 | 
			
		||||
       * @tparam F The type of the callback function.
 | 
			
		||||
       * @tparam A Argument types for the event.
 | 
			
		||||
       * @param fn Callback function to be invoked when the event is triggered.
 | 
			
		||||
       * @param event Event that will be listened to.
 | 
			
		||||
       * @return A method that, when invoked, will unsubscribe from the event.
 | 
			
		||||
       */
 | 
			
		||||
      template<typename F, typename... A>
 | 
			
		||||
      std::function<void()> useEventLegacy(
 | 
			
		||||
        F fn,
 | 
			
		||||
        Event<A...> &event
 | 
			
		||||
      ) {
 | 
			
		||||
        // This is a legacy feature to make upgrading to the new useEffect a bit
 | 
			
		||||
        // easier for me. For the time being I am just bodging this together to
 | 
			
		||||
        // do what I need here.
 | 
			
		||||
        auto bridge = new StateOwnerEventLegacy<A...>();
 | 
			
		||||
        bridge->owner = this;
 | 
			
		||||
        bridge->event = &event;
 | 
			
		||||
        bridge->fn = fn;
 | 
			
		||||
        event.addListener(bridge, &StateOwnerEventLegacy<A...>::callback);
 | 
			
		||||
        eventLegacyBridge.push_back(bridge);
 | 
			
		||||
 | 
			
		||||
        return std::bind([&](IStateOwnerEventLegacy *evt){
 | 
			
		||||
          evt->teardown();
 | 
			
		||||
        }, bridge);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * State Owner teardown function. Mostly just used to remove any lingering
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user