aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/helpers/signal/Signal.cpp
blob: fdd2cc239dc0be26d721cacbd9b90ec7edf36461 (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
#include "Signal.hpp"
#include <algorithm>

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

    for (auto& l : m_vListeners) {
        if (const CHyprSignalListener L = l.lock())
            L->emit(data);
        else
            dirty = true;
    }

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

    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));
}