1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | #pragma once
|
8 |
|
9 | #include <memory>
|
10 | #include <mutex>
|
11 |
|
12 | #include <folly/dynamic.h>
|
13 | #include <react/events/EventDispatcher.h>
|
14 | #include <react/events/primitives.h>
|
15 |
|
16 | namespace facebook {
|
17 | namespace react {
|
18 |
|
19 | class EventEmitter;
|
20 |
|
21 | using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | class EventEmitter {
|
30 | |
31 |
|
32 |
|
33 |
|
34 | using Tag = int32_t;
|
35 |
|
36 | public:
|
37 | static std::mutex &DispatchMutex();
|
38 |
|
39 | static ValueFactory defaultPayloadFactory();
|
40 |
|
41 | EventEmitter(
|
42 | SharedEventTarget eventTarget,
|
43 | Tag tag,
|
44 | WeakEventDispatcher eventDispatcher);
|
45 |
|
46 | virtual ~EventEmitter() = default;
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | void setEnabled(bool enabled) const;
|
59 |
|
60 | protected:
|
61 | #ifdef ANDROID
|
62 |
|
63 |
|
64 | public:
|
65 | #endif
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 | void dispatchEvent(
|
72 | const std::string &type,
|
73 | const ValueFactory &payloadFactory =
|
74 | EventEmitter::defaultPayloadFactory(),
|
75 | const EventPriority &priority = EventPriority::AsynchronousBatched) const;
|
76 |
|
77 | void dispatchEvent(
|
78 | const std::string &type,
|
79 | const folly::dynamic &payload,
|
80 | const EventPriority &priority = EventPriority::AsynchronousBatched) const;
|
81 |
|
82 | private:
|
83 | void toggleEventTargetOwnership_() const;
|
84 |
|
85 | mutable SharedEventTarget eventTarget_;
|
86 | WeakEventDispatcher eventDispatcher_;
|
87 | mutable int enableCounter_{0};
|
88 | mutable bool isEnabled_{false};
|
89 | };
|
90 |
|
91 | }
|
92 | }
|