Repository: electron/electron
File: shell/browser/event_emitter_mixin.h (C/C++)

Code before:

 protected:
  EventEmitterMixin() = default;

  gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
    gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
    auto* wrapper_info = &(static_cast<T*>(this)->kWrapperInfo);

    // DeprecatedWrapperInfo support will be removed as part of
    // https://github.com/electron/electron/issues/47922
    constexpr bool is_deprecated_wrapper =
        std::is_same_v<decltype(wrapper_info), gin::DeprecatedWrapperInfo*>;

    v8::Local<v8::FunctionTemplate> constructor;
    if constexpr (is_deprecated_wrapper) {
      constructor = data->DeprecatedGetFunctionTemplate(wrapper_info);
    } else {
      constructor = data->GetFunctionTemplate(wrapper_info);
    }

    const char* class_name = "";
    if constexpr (is_deprecated_wrapper) {
      class_name = static_cast<T*>(this)->GetTypeName();
    } else {
      class_name = static_cast<T*>(this)->GetHumanReadableName();
    }

    if (constructor.IsEmpty()) {
      constructor = v8::FunctionTemplate::New(isolate);
      constructor->SetClassName(gin::StringToV8(isolate, class_name));
      constructor->Inherit(internal::GetEventEmitterTemplate(isolate));
      if constexpr (is_deprecated_wrapper) {
        data->DeprecatedSetFunctionTemplate(wrapper_info, constructor);
      } else {
        data->SetFunctionTemplate(wrapper_info, constructor);
      }
    }
    return gin::ObjectTemplateBuilder(isolate, class_name,
                                      constructor->InstanceTemplate());
  }
};

}  // namespace gin_helper

#endif  // ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_


Review comment (suggestion):
We are using `GetHumanReadableName` specifically to show up in snapshots and have prefixed them with `Electron / <ClassName>` to keep them unique. It cannot be used directly as class constructor names. We can have a `GetClassName` member that can be used here instead, an example is https://github.com/electron/electron/blob/1f322ebce3cccc65b32b653ea15bc1511a0b5993/shell/browser/api/electron_api_session.h#L92

Code after:

 protected:
  EventEmitterMixin() = default;

  gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
    gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
    auto* wrapper_info = &(static_cast<T*>(this)->kWrapperInfo);

    // DeprecatedWrapperInfo support will be removed as part of
    // https://github.com/electron/electron/issues/47922
    constexpr bool is_deprecated_wrapper =
        std::is_same_v<decltype(wrapper_info), gin::DeprecatedWrapperInfo*>;

    v8::Local<v8::FunctionTemplate> constructor;
    if constexpr (is_deprecated_wrapper) {
      constructor = data->DeprecatedGetFunctionTemplate(wrapper_info);
    } else {
      constructor = data->GetFunctionTemplate(wrapper_info);
    }

    const char* class_name = "";
    if constexpr (is_deprecated_wrapper) {
      class_name = static_cast<T*>(this)->GetTypeName();
    } else {
      class_name = static_cast<T*>(this)->GetClassName();
    }

    if (constructor.IsEmpty()) {
      constructor = v8::FunctionTemplate::New(isolate);
      constructor->SetClassName(gin::StringToV8(isolate, class_name));
      constructor->Inherit(internal::GetEventEmitterTemplate(isolate));
      if constexpr (is_deprecated_wrapper) {
        data->DeprecatedSetFunctionTemplate(wrapper_info, constructor);
      } else {
        data->SetFunctionTemplate(wrapper_info, constructor);
      }
    }
    return gin::ObjectTemplateBuilder(isolate, class_name,
                                      constructor->InstanceTemplate());
  }
};

}  // namespace gin_helper

#endif  // ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_