summaryrefslogtreecommitdiff
path: root/src/helpers/signal/Signal.cpp
blob: fd2d11c8b3fd2d1c20eb3c892e1c8066d65a8f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "Signal.hpp"
#include <algorithm>

void CSignal::emit(std::any data) {
    bool                             dirty = false;

    std::vector<SP<CSignalListener>> listeners;
    for (auto& l : m_vListeners) {
        if (l.expired()) {
            dirty = true;
            continue;
        }

        listeners.emplace_back(l.lock());
    }

    std::vector<CStaticSignalListener*> statics;
    for (auto& l : m_vStaticListeners) {
        statics.emplace_back(l.get());
    }

    for (auto& l : listeners) {
        // if there is only one lock, it means the event is only held by the listeners
        // vector and was removed during our iteration
        if (l.strongRef() == 1) {
            dirty = true;
            continue;
        }
        l->emit(data);
    }

    for (auto& l : statics) {
        l->emit(data);
    }

    // release SPs
    listeners.clear();

    if (dirty)
        std::erase_if(m_vListeners, [](const auto& other) { return other.expired(); });
}

CHyprSignalListener CSignal::registerListener(std::function<void(std::any)> handler) {
    CHyprSignalListener listener = makeShared<CSignalListener>(handler);
    m_vListeners.emplace_back(WP<CSignalListener>(listener));
    return listener;
}

void CSignal::registerStaticListener(std::function<void(void*, std::any)> handler, void* owner) {
    m_vStaticListeners.emplace_back(std::make_unique<CStaticSignalListener>(handler, owner));
}