Skip to content

Commit

Permalink
feat: Cpp global subscriber type (#218)
Browse files Browse the repository at this point in the history
* feat: Global subscribe for cpp

* feat: Add global subscriber templates for cpp
  • Loading branch information
ksentak authored Sep 16, 2024
1 parent dc42313 commit 45cbf0a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions languages/cpp/templates/declarations-override/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
// method result properties : ${method.result.properties}
void subscribe( ${event.signature.params}${if.event.params}, ${end.if.event.params}I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
void unsubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
${if.globalsubscriber}
void globalSubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
void globalUnsubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
${end.if.globalsubscriber}
4 changes: 4 additions & 0 deletions languages/cpp/templates/declarations/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
// method result properties : ${method.result.properties}
virtual void subscribe( ${event.signature.params}${if.event.params}, ${end.if.event.params}I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
virtual void unsubscribe( I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
${if.globalsubscriber}
virtual void globalSubscribe( I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
virtual void globalUnsubscribe( I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
${end.if.globalsubscriber}
38 changes: 38 additions & 0 deletions languages/cpp/templates/methods/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,41 @@
*err = status;
}
}

${if.globalsubscriber}
static void ${method.name}GlobalCallback( void* notification, const void* userData, void* jsonResponse)
{
${event.callback.serialization}
ASSERT(proxyResponse.IsValid() == true);

if (proxyResponse.IsValid() == true) {
${event.callback.initialization}

${event.callback.instantiation}
proxyResponse.Release();

I${info.Title}::I${method.Name}Notification& notifier = *(reinterpret_cast<I${info.Title}::I${method.Name}Notification*>(notification));
notifier.${method.rpc.name}(${event.callback.response.instantiation});
}
}
void ${info.Title}Impl::globalSubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err )
{
const string eventName = _T("${info.title.lowercase}.${method.rpc.name}");
Firebolt::Error status = Firebolt::Error::None;

JsonObject jsonParameters;
status = FireboltSDK::Event::Instance().Subscribe<${event.result.json.type}>(eventName, jsonParameters, ${method.name}GlobalCallback, reinterpret_cast<void*>(&notification), nullptr);

if (err != nullptr) {
*err = status;
}
}
void ${info.Title}Impl::globalUnsubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err )
{
Firebolt::Error status = FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title.lowercase}.${method.rpc.name}"), reinterpret_cast<void*>(&notification));

if (err != nullptr) {
*err = status;
}
}
${end.if.globalsubscriber}
21 changes: 21 additions & 0 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ const eventHasOptionalParam = (event) => {
return event.params.length && event.params.find(param => !(param.required && param.required === true))
}

const isGlobalSubscriber = (method) => {
return method.tags && method.tags.some(tag => tag['x-subscriber-type'] === 'global');
}

const isOptionalParam = (param) => {
return (!(param.required && param.required === true))
}
Expand Down Expand Up @@ -347,6 +351,12 @@ const deprecatedOrEmptyArray = compose(
getMethods
)

const getGlobalSubscribers = compose(
option([]),
map(filter(isGlobalSubscriber)),
getMethods
)

const props = compose(
option([]),
map(filter(m => isPropertyMethod(m))),
Expand Down Expand Up @@ -1299,7 +1309,17 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
const result = JSON.parse(JSON.stringify(methodObj.result))
const event = isEventMethod(methodObj) ? JSON.parse(JSON.stringify(methodObj)) : ''

// Keep track of any global subscribers to insert into templates
const globalSubscribersArr = getGlobalSubscribers(json);
let isGlobalSubscriberEvent = false

if (event) {
isGlobalSubscriberEvent = globalSubscribersArr.some(subscriber => {
const strippedEventName = event.name.replace(/^on/, '').replace(/Changed$/, '').toLowerCase();
const subscriberName = subscriber.name.toLowerCase();
return subscriberName && strippedEventName === subscriberName;
})

result.schema = JSON.parse(JSON.stringify(getPayloadFromEvent(methodObj)))
event.result.schema = getPayloadFromEvent(event)
event.params = event.params.filter(p => p.name !== 'listen')
Expand Down Expand Up @@ -1440,6 +1460,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
.replace(/\$\{event\.params\}/g, eventParams)
.replace(/\$\{event\.params\.table\.rows\}/g, eventParamsRows)
.replace(/\$\{if\.event\.params\}(.*?)\$\{end\.if\.event\.params\}/gms, event && event.params.length ? '$1' : '')
.replace(/\$\{if\.globalsubscriber\}(.*?)\$\{end\.if\.globalsubscriber\}/gms, (isGlobalSubscriberEvent) ? '$1' : '')
.replace(/\$\{if\.event\.callback\.params\}(.*?)\$\{end\.if\.event\.callback\.params\}/gms, event && eventHasOptionalParam(event) ? '$1' : '')
.replace(/\$\{event\.signature\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section }) : '')
.replace(/\$\{event\.signature\.callback\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section, callback: true }) : '')
Expand Down

0 comments on commit 45cbf0a

Please sign in to comment.