refactor: make TrackableObjectBase::weak_map_id_ const (#46557)

refactor: make TrackableObjectBase::weak_map_id_ const

simplify declaration and initialization

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-04-08 07:08:15 -05:00 committed by GitHub
parent 1046842f73
commit de933233e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -42,11 +42,12 @@ class TrackableObjectBase : public CleanedUpAtExit {
// Returns a closure that can destroy the native class.
base::OnceClosure GetDestroyClosure();
int32_t weak_map_id_ = 0;
private:
void Destroy();
static inline int32_t next_id_ = 0;
const int32_t weak_map_id_ = ++next_id_;
base::WeakPtrFactory<TrackableObjectBase> weak_factory_{this};
};
@ -111,26 +112,21 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
}
protected:
TrackableObject() { weak_map_id_ = ++next_id_; }
TrackableObject() = default;
~TrackableObject() override { RemoveFromWeakMap(); }
void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override {
if (!weak_map_) {
weak_map_ = new electron::KeyWeakMap<int32_t>;
}
weak_map_->Set(isolate, weak_map_id_, wrapper);
weak_map_->Set(isolate, weak_map_id(), wrapper);
gin_helper::WrappableBase::InitWith(isolate, wrapper);
}
private:
static int32_t next_id_;
static electron::KeyWeakMap<int32_t>* weak_map_; // leaked on purpose
};
template <typename T>
int32_t TrackableObject<T>::next_id_ = 0;
template <typename T>
electron::KeyWeakMap<int32_t>* TrackableObject<T>::weak_map_ = nullptr;