Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef XENOLITH_SCENE_NODES_XLSUBSCRIPTIONLISTENER_H_ 24 : #define XENOLITH_SCENE_NODES_XLSUBSCRIPTIONLISTENER_H_ 25 : 26 : #include "XLComponent.h" 27 : #include "SPSubscription.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::xenolith { 30 : 31 : class Scheduler; 32 : 33 : class SubscriptionListener : public Component { 34 : public: 35 : using DirtyCallback = Function<void(SubscriptionFlags)>; 36 : 37 60 : virtual ~SubscriptionListener() { } 38 : 39 : virtual bool init(DirtyCallback &&cb = nullptr); 40 : 41 : virtual void onEnter(Scene *) override; 42 : virtual void onExit() override; 43 : 44 : virtual void setCallback(DirtyCallback &&cb); 45 2 : virtual const DirtyCallback &getCallback() const { return _callback; } 46 : 47 : virtual void setDirty(); 48 : virtual void update(UpdateTime); 49 : 50 : virtual void check(); 51 : 52 : protected: 53 : virtual void updateScheduler(); 54 : virtual void schedule(); 55 : virtual void unschedule(); 56 : 57 : Scheduler *_scheduler = nullptr; 58 : Subscription *_subscription = nullptr; 59 : DirtyCallback _callback; 60 : bool _dirty = false; 61 : bool _scheduled = false; 62 : }; 63 : 64 : template <typename T = Subscription> 65 : class DataListener : public SubscriptionListener { 66 : public: 67 480 : virtual ~DataListener() { } 68 : 69 240 : virtual bool init(DirtyCallback &&cb = nullptr, T *sub = nullptr) { 70 240 : if (!SubscriptionListener::init(move(cb))) { 71 0 : return false; 72 : } 73 : 74 240 : _binding.set(sub); 75 240 : _subscription = sub; 76 240 : return true; 77 : } 78 : 79 210 : virtual void setSubscription(T *sub) { 80 210 : if (_binding != sub) { 81 200 : _binding = Binding<T>(sub); 82 200 : _subscription = sub; 83 200 : updateScheduler(); 84 200 : setDirty(); 85 : } 86 210 : } 87 : 88 1756 : virtual T *getSubscription() const { return _binding.get(); } 89 : 90 17864 : virtual void update(UpdateTime dt) override { 91 17864 : if (_callback && _binding && _running) { 92 17864 : auto val = _binding.check(); 93 17864 : if (!val.empty() || _dirty) { 94 226 : _dirty = false; 95 226 : _callback(val); 96 : } 97 : } 98 17864 : } 99 : 100 : protected: 101 : Binding<T> _binding; 102 : }; 103 : 104 : } 105 : 106 : #endif /* XENOLITH_SCENE_NODES_XLSUBSCRIPTIONLISTENER_H_ */