Skip to content

Commit a7497a4

Browse files
committed
feat: add function to extract pushed events from action strings
1 parent eee91ee commit a7497a4

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

src/cli/generators.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <vector>
77
#include <cctype>
88
#include <set>
9+
#include <regex>
910
#include <sys/stat.h>
1011

1112
namespace webcc
@@ -659,6 +660,23 @@ namespace webcc
659660
return maps;
660661
}
661662

663+
std::set<std::string> get_pushed_events_from_action(const std::string &action)
664+
{
665+
std::set<std::string> events;
666+
static const std::regex push_event_regex(R"(push_event_([a-zA-Z0-9]+)_([A-Z0-9_]+)\s*\()");
667+
668+
auto begin = std::sregex_iterator(action.begin(), action.end(), push_event_regex);
669+
auto end = std::sregex_iterator();
670+
for (auto it = begin; it != end; ++it)
671+
{
672+
std::string ns = (*it)[1].str();
673+
std::string name = (*it)[2].str();
674+
events.insert(ns + "::" + name);
675+
}
676+
677+
return events;
678+
}
679+
662680
JsGenResult generate_js_runtime(const SchemaDefs &defs, const std::string &user_code, const std::string &out_dir)
663681
{
664682
CodeWriter w;
@@ -674,6 +692,7 @@ namespace webcc
674692
std::set<std::string> used_namespaces;
675693
std::set<std::string> used_maps;
676694
std::set<std::string> used_event_listeners; // Track which event types need delegation
695+
std::set<std::string> used_event_helpers; // Track which push_event helpers must exist
677696
std::vector<std::string> generated_js_imports;
678697
CodeWriter cases_w;
679698
cases_w.set_indent(4);
@@ -784,6 +803,10 @@ namespace webcc
784803
for (const auto &m : maps)
785804
used_maps.insert(m);
786805

806+
auto pushed_events = get_pushed_events_from_action(d.action);
807+
for (const auto &e : pushed_events)
808+
used_event_helpers.insert(e);
809+
787810
// Track event listener types for delegation
788811
if (d.func_name == "add_click_listener")
789812
used_event_listeners.insert("click");
@@ -858,11 +881,13 @@ namespace webcc
858881
// Generate push_event helpers in JS only for event types that are actually used.
859882
for (const auto &d : defs.events)
860883
{
884+
bool helper_needed = used_event_helpers.count(d.ns + "::" + d.name) > 0;
885+
861886
// Convert event name to lowercase for matching against used_event_listeners
862887
std::string event_lower = d.name;
863888
for (auto &c : event_lower) c = std::tolower(c);
864-
865-
if (used_event_listeners.find(event_lower) == used_event_listeners.end())
889+
890+
if (!helper_needed && used_event_listeners.find(event_lower) == used_event_listeners.end())
866891
continue;
867892

868893
std::stringstream sig;

0 commit comments

Comments
 (0)