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 : #include "XLSubscriptionListener.h" 24 : #include "XLDirector.h" 25 : #include "XLScheduler.h" 26 : #include "XLScene.h" 27 : 28 : namespace STAPPLER_VERSIONIZED stappler::xenolith { 29 : 30 533 : bool SubscriptionListener::init(DirtyCallback &&cb) { 31 533 : if (!Component::init()) { 32 0 : return false; 33 : } 34 : 35 533 : _callback = move(cb); 36 533 : return true; 37 : } 38 : 39 533 : void SubscriptionListener::onEnter(Scene *scene) { 40 533 : Component::onEnter(scene); 41 : 42 533 : _scheduler = scene->getDirector()->getScheduler(); 43 : 44 533 : updateScheduler(); 45 533 : } 46 : 47 533 : void SubscriptionListener::onExit() { 48 533 : if (_scheduled) { 49 449 : unschedule(); 50 : } 51 : 52 533 : _scheduler = nullptr; 53 : 54 533 : Component::onExit(); 55 533 : } 56 : 57 21 : void SubscriptionListener::setCallback(DirtyCallback &&cb) { 58 21 : _callback = move(cb); 59 21 : } 60 : 61 428 : void SubscriptionListener::setDirty() { 62 428 : _dirty = true; 63 428 : } 64 : 65 21 : void SubscriptionListener::update(UpdateTime dt) { } 66 : 67 21 : void SubscriptionListener::check() { 68 21 : update(UpdateTime()); 69 21 : } 70 : 71 961 : void SubscriptionListener::updateScheduler() { 72 961 : if (_subscription && !_scheduled) { 73 604 : schedule(); 74 357 : } else if (!_subscription && _scheduled) { 75 0 : unschedule(); 76 : } 77 961 : } 78 : 79 604 : void SubscriptionListener::schedule() { 80 604 : if (_subscription && !_scheduled && _scheduler) { 81 449 : _scheduler->scheduleUpdate(this, 0, false); 82 449 : _scheduled = true; 83 : } 84 604 : } 85 : 86 449 : void SubscriptionListener::unschedule() { 87 449 : if (_scheduled && _scheduler) { 88 449 : _scheduler->unschedule(this); 89 449 : _scheduled = false; 90 : } 91 449 : } 92 : 93 : }