Skip to content

Commit 225a9c3

Browse files
feat: Remove global subscriber tag
1 parent 402c017 commit 225a9c3

File tree

5 files changed

+8
-173
lines changed

5 files changed

+8
-173
lines changed

languages/javascript/src/shared/Prop/Router.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function (params, callbackOrValue, contextParameterCount) {
88
// subscribe
99
return "subscriber"
1010
} else if (numArgs === 0 && typeof callbackOrValue === 'function') {
11-
// for x-subscriber-type: global
11+
// subscribe
1212
return "subscriber"
1313
} else if (numArgs === (contextParameterCount) && callbackOrValue !== undefined) {
1414
// setter

languages/javascript/templates/methods/property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function ${method.name}(${method.params.list}) {
22
let callbackOrValue = arguments[${method.params.count}]
33
let params = { ${method.params.list} }
44

5-
// x-subscriber-type: global
5+
// If there is only one parameter, it must be the callback.
66
if (arguments.length === 1 && (typeof arguments[0] === 'function')) {
77
callbackOrValue = arguments[0]
88
params = {}

languages/markdown/templates/methods/event.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ See also: [listen()](#listen), [once()](#listen), [clear()](#listen).
1313

1414
${event.params}
1515

16-
Event value:
17-
1816
${method.result}
1917

2018
${method.capabilities}

src/macrofier/engine.mjs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const getNotifierForMethod = (method, appApi) => {
112112
if (appApiMethod.tags) {
113113
//iterate over tags to find the event tag with x-event
114114
const eventTag = appApiMethod.tags.find(tag => tag.name === "notifier" && tag["x-event"])
115-
if (eventTag && eventTag["x-event"].includes(method.name))
115+
if (eventTag && eventTag["x-event"].includes('.' + method.name))
116116
return appApiMethod
117117
}
118118
return null
@@ -331,9 +331,6 @@ const eventHasOptionalParam = (event) => {
331331
return event.params.length && event.params.find(param => !(param.required && param.required === true))
332332
}
333333

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

338335
const isOptionalParam = (param) => {
339336
return (!(param.required && param.required === true))
@@ -413,12 +410,6 @@ const deprecatedOrEmptyArray = compose(
413410
getMethods
414411
)
415412

416-
const getGlobalSubscribers = compose(
417-
option([]),
418-
map(filter(isGlobalSubscriber)),
419-
getMethods
420-
)
421-
422413
const props = compose(
423414
option([]),
424415
map(filter(m => isPropertyMethod(m))),
@@ -1597,16 +1588,7 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
15971588
})
15981589
}
15991590

1600-
// Keep track of any global subscribers to insert into templates
1601-
const globalSubscribersArr = getGlobalSubscribers(platformApi);
1602-
let isGlobalSubscriberEvent = false
1603-
16041591
if (event) {
1605-
isGlobalSubscriberEvent = globalSubscribersArr.some(subscriber => {
1606-
const strippedEventName = event.name.replace(/^on/, '').replace(/Changed$/, '').toLowerCase();
1607-
const subscriberName = subscriber.name.toLowerCase();
1608-
return subscriberName && strippedEventName === subscriberName;
1609-
})
16101592

16111593
if (!appApi) {
16121594
result.schema = JSON.parse(JSON.stringify(getPayloadFromEvent(methodObj)))
@@ -1807,7 +1789,6 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
18071789
.replace(/\$\{event\.params\}/g, eventParams)
18081790
.replace(/\$\{event\.params\.table\.rows\}/g, eventParamsRows)
18091791
.replace(/\$\{if\.event\.params\}(.*?)\$\{end\.if\.event\.params\}/gms, event && event.params.length ? '$1' : '')
1810-
.replace(/\$\{if\.globalsubscriber\}(.*?)\$\{end\.if\.globalsubscriber\}/gms, (isGlobalSubscriberEvent) ? '$1' : '')
18111792
.replace(/\$\{if\.event\.callback\.params\}(.*?)\$\{end\.if\.event\.callback\.params\}/gms, event && eventHasOptionalParam(event) ? '$1' : '')
18121793
.replace(/\$\{event\.signature\.params\}/g, event ? Types.getMethodSignatureParams(event, currentModuleApiForEvent, { namespace: !config.copySchemasIntoModules }) : '')
18131794
.replace(/\$\{event\.result\.schema\.params\}/g, eventResultSchemaPropParams ? eventResultSchemaPropParams : '')

src/shared/modules.mjs

Lines changed: 5 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -428,100 +428,8 @@ const eventDefaults = event => {
428428
return event
429429
}
430430

431-
const createEventResultSchemaFromProperty = (property, type='Changed') => {
432-
const subscriberType = property.tags.map(t => t['x-subscriber-type']).find(t => typeof t === 'string') || 'context'
433-
434-
const caps = property.tags.find(t => t.name === 'capabilities')
435-
let name = caps['x-provided-by'] ? caps['x-provided-by'].split('.').pop().replace('onRequest', '') : property.name
436-
name = name.charAt(0).toUpperCase() + name.substring(1)
437-
438-
if ( subscriberType === 'global') {
439-
// wrap the existing result and the params in a new result object
440-
const schema = {
441-
title: methodRename(property, name => name.charAt(0).toUpperCase() + name.substring(1) + type + 'Info').split('.').pop(),
442-
type: "object",
443-
properties: {
444-
445-
},
446-
required: []
447-
}
448-
449-
// add all of the params
450-
property.params.filter(p => p.name !== 'listen').forEach(p => {
451-
schema.properties[p.name] = p.schema
452-
schema.required.push(p.name)
453-
})
454-
455-
// add the result (which might override a param of the same name)
456-
schema.properties[property.result.name] = property.result.schema
457-
!schema.required.includes(property.result.name) && schema.required.push(property.result.name)
458-
459-
return schema
460-
}
461-
}
462-
463-
const createEventFromProperty = (property, type='', alternative, json) => {
464-
const provider = (property.tags.find(t => t['x-provided-by']) || {})['x-provided-by']
465-
const pusher = provider ? provider.replace('onRequest', '').split('.').map((x, i, arr) => (i === arr.length-1) ? x.charAt(0).toLowerCase() + x.substr(1) : x).join('.') : undefined
466-
const event = eventDefaults(JSON.parse(JSON.stringify(property)))
467-
// event.name = (module ? module + '.' : '') + 'on' + event.name.charAt(0).toUpperCase() + event.name.substr(1) + type
468-
event.name = provider ? provider.split('.').pop().replace('onRequest', '') : event.name.charAt(0).toUpperCase() + event.name.substr(1) + type
469-
event.name = event.name.split('.').map((x, i, arr) => (i === arr.length-1) ? 'on' + x.charAt(0).toUpperCase() + x.substr(1) : x).join('.')
470-
const subscriberFor = pusher || (json.info.title + '.' + property.name)
471-
472-
const old_tags = JSON.parse(JSON.stringify(property.tags))
473-
474-
alternative && (event.tags[0]['x-alternative'] = alternative)
475-
476-
!provider && event.tags.unshift({
477-
name: "subscriber",
478-
'x-subscriber-for': subscriberFor
479-
})
480-
481-
const subscriberType = property.tags.map(t => t['x-subscriber-type']).find(t => typeof t === 'string') || 'context'
482-
483-
// if the subscriber type is global, zap all of the parameters and change the result type to the schema that includes them
484-
if (subscriberType === 'global') {
485-
486-
// wrap the existing result and the params in a new result object
487-
const result = {
488-
name: "data",
489-
schema: {
490-
$ref: "#/components/schemas/" + event.name.substring(2) + 'Info'
491-
}
492-
}
493-
494-
event.examples.map(example => {
495-
const result = {}
496-
example.params.filter(p => p.name !== 'listen').forEach(p => {
497-
result[p.name] = p.value
498-
})
499-
result[example.result.name] = example.result.value
500-
example.params = example.params.filter(p => p.name === 'listen')
501-
example.result.name = "data"
502-
example.result.value = result
503-
})
504-
505-
event.result = result
506-
507-
// remove the params
508-
event.params = event.params.filter(p => p.name === 'listen')
509-
}
510-
511-
old_tags.forEach(t => {
512-
if (t.name !== 'property' && !t.name.startsWith('property:') && t.name !== 'push-pull')
513-
{
514-
event.tags.push(t)
515-
}
516-
})
517-
518-
provider && (event.tags.find(t => t.name === 'capabilities')['x-provided-by'] = subscriberFor)
519-
520-
return event
521-
}
522431

523432
const createNotifierFromProperty = (property, type='Changed') => {
524-
const subscriberType = property.tags.map(t => t['x-subscriber-type']).find(t => typeof t === 'string') || 'context'
525433

526434
const notifier = JSON.parse(JSON.stringify(property))
527435
notifier.name = methodRename(notifier, name => name + type)
@@ -532,40 +440,13 @@ const createNotifierFromProperty = (property, type='Changed') => {
532440
'x-event': methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1))
533441
})
534442

535-
if (subscriberType === 'global') {
536-
notifier.params = [
537-
{
538-
name: "info",
539-
schema: {
540-
"$ref": "#/components/schemas/" + methodRename(notifier, name => name.charAt(0).toUpperCase() + name.substr(1) + 'Info')
541-
}
542-
}
543-
]
544-
}
545-
else {
546-
notifier.params.push(notifier.result)
547-
}
548-
443+
notifier.params.push(notifier.result)
444+
549445
delete notifier.result
550-
551-
if (subscriberType === 'global') {
552-
notifier.examples = property.examples.map(example => ({
553-
name: example.name,
554-
params: [
555-
{
556-
name: "info",
557-
value: Object.assign(Object.fromEntries(example.params.map(p => [p.name, p.value])), Object.fromEntries([[example.result.name, example.result.value]]))
558-
}
559-
]
560-
}))
561-
}
562-
else {
563-
notifier.examples.forEach(example => {
446+
notifier.examples.forEach(example => {
564447
example.params.push(example.result)
565448
delete example.result
566449
})
567-
}
568-
569450
return notifier
570451
}
571452

@@ -602,10 +483,6 @@ const createNotifierFromEvent = (event, json) => {
602483
return push
603484
}
604485

605-
const createPushEvent = (requestor, json) => {
606-
return createEventFromProperty(requestor, '', undefined, json)
607-
}
608-
609486
const createPullEventFromPush = (pusher, json) => {
610487
const event = JSON.parse(JSON.stringify(pusher))
611488
event.params = []
@@ -1042,17 +919,11 @@ const generatePropertyEvents = json => {
1042919

1043920
properties.forEach(property => {
1044921
json.methods.push(createNotifierFromProperty(property))
1045-
const schema = createEventResultSchemaFromProperty(property)
1046-
if (schema) {
1047-
json.components.schemas[property.name.split('.').shift() + '.' + schema.title] = schema
1048-
}
922+
1049923
})
1050924
readonlies.forEach(property => {
1051925
json.methods.push(createNotifierFromProperty(property))
1052-
const schema = createEventResultSchemaFromProperty(property)
1053-
if (schema) {
1054-
json.components.schemas[property.name.split('.').shift() + '.' + schema.title] = schema
1055-
}
926+
1056927
})
1057928

1058929
return json
@@ -1074,21 +945,6 @@ const generatePolymorphicPullEvents = json => {
1074945
return json
1075946
}
1076947

1077-
const generatePushPullMethods = json => {
1078-
const requestors = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'push-pull')) || []
1079-
requestors.forEach(requestor => {
1080-
json.methods.push(createPushEvent(requestor, json))
1081-
1082-
const schema = createEventResultSchemaFromProperty(requestor)
1083-
if (schema) {
1084-
json.components = json.components || {}
1085-
json.components.schemas = json.components.schemas || {}
1086-
json.components.schemas[schema.title] = schema
1087-
}
1088-
})
1089-
1090-
return json
1091-
}
1092948

1093949
const generateProvidedByMethods = json => {
1094950
const requestors = json.methods.filter(m => !m.tags.find(t => t.name === 'notifier')).filter( m => m.tags && m.tags.find( t => t['x-provided-by'])) || []

0 commit comments

Comments
 (0)