Skip to content

Commit

Permalink
Move: Make NativeHandler private.
Browse files Browse the repository at this point in the history
This ensures that users call `newNativeHandler()` rather than constructing it directly.
  • Loading branch information
kentonv committed Feb 9, 2024
1 parent 4a29548 commit 1d8ede4
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions src/workerd/api/basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,36 +231,6 @@ class CustomEvent: public Event {
// An implementation of the Web Platform Standard EventTarget API
class EventTarget: public jsg::Object {
public:

// RAII-style listener that can be attached to an EventTarget.
class NativeHandler {
public:
using Signature = void(jsg::Ref<Event>);
NativeHandler(jsg::Lock& js, jsg::Ref<EventTarget> target, kj::String type,
jsg::Function<Signature> func, bool once = false);
~NativeHandler() noexcept(false);
KJ_DISALLOW_COPY_AND_MOVE(NativeHandler);

void operator()(jsg::Lock&js, jsg::Ref<Event> event);

uint hashCode() const;

void visitForGc(jsg::GcVisitor& visitor);
private:
void detach();

kj::String type;
struct State {
jsg::Ref<EventTarget> target;
jsg::Function<Signature> func;
};

kj::Maybe<State> state;
bool once;

friend class EventTarget;
};

~EventTarget() noexcept(false);

size_t getHandlerCount(kj::StringPtr type) const;
Expand Down Expand Up @@ -349,6 +319,35 @@ class EventTarget: public jsg::Object {
void visitForMemoryInfo(jsg::MemoryTracker& tracker) const;

private:
// RAII-style listener that can be attached to an EventTarget.
class NativeHandler {
public:
using Signature = void(jsg::Ref<Event>);
NativeHandler(jsg::Lock& js, jsg::Ref<EventTarget> target, kj::String type,
jsg::Function<Signature> func, bool once = false);
~NativeHandler() noexcept(false);
KJ_DISALLOW_COPY_AND_MOVE(NativeHandler);

void operator()(jsg::Lock&js, jsg::Ref<Event> event);

uint hashCode() const;

void visitForGc(jsg::GcVisitor& visitor);
private:
void detach();

kj::String type;
struct State {
jsg::Ref<EventTarget> target;
jsg::Function<Signature> func;
};

kj::Maybe<State> state;
bool once;

friend class EventTarget;
};

void addNativeListener(jsg::Lock& js, NativeHandler& handler);
bool removeNativeListener(NativeHandler& handler);

Expand Down Expand Up @@ -603,8 +602,6 @@ class Scheduler final: public jsg::Object {
private:
};

KJ_DECLARE_NON_POLYMORPHIC(workerd::api::EventTarget::NativeHandler);

#define EW_BASICS_ISOLATE_TYPES \
api::Event, \
api::Event::Init, \
Expand Down

0 comments on commit 1d8ede4

Please sign in to comment.