Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion languages/javascript/src/shared/Prop/Router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function (params, callbackOrValue, contextParameterCount) {
// subscribe
return "subscriber"
} else if (numArgs === 0 && typeof callbackOrValue === 'function') {
// for x-subscriber-type: global
// subscribe
return "subscriber"
} else if (numArgs === (contextParameterCount) && callbackOrValue !== undefined) {
// setter
Expand Down
2 changes: 1 addition & 1 deletion languages/javascript/templates/methods/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function ${method.name}(${method.params.list}) {
let callbackOrValue = arguments[${method.params.count}]
let params = { ${method.params.list} }

// x-subscriber-type: global
// If there is only one parameter, it must be the callback.
if (arguments.length === 1 && (typeof arguments[0] === 'function')) {
callbackOrValue = arguments[0]
params = {}
Expand Down
2 changes: 0 additions & 2 deletions languages/markdown/templates/methods/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ See also: [listen()](#listen), [once()](#listen), [clear()](#listen).

${event.params}

Event value:

${method.result}

${method.capabilities}
Expand Down
21 changes: 1 addition & 20 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const getNotifierForMethod = (method, appApi) => {
if (appApiMethod.tags) {
//iterate over tags to find the event tag with x-event
const eventTag = appApiMethod.tags.find(tag => tag.name === "notifier" && tag["x-event"])
if (eventTag && eventTag["x-event"].includes(method.name))
if (eventTag && eventTag["x-event"].includes('.' + method.name))
return appApiMethod
}
return null
Expand Down Expand Up @@ -331,9 +331,6 @@ 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 @@ -413,12 +410,6 @@ 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 @@ -1597,16 +1588,7 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
})
}

// Keep track of any global subscribers to insert into templates
const globalSubscribersArr = getGlobalSubscribers(platformApi);
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;
})

if (!appApi) {
result.schema = JSON.parse(JSON.stringify(getPayloadFromEvent(methodObj)))
Expand Down Expand Up @@ -1807,7 +1789,6 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
.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, currentModuleApiForEvent, { namespace: !config.copySchemasIntoModules }) : '')
.replace(/\$\{event\.result\.schema\.params\}/g, eventResultSchemaPropParams ? eventResultSchemaPropParams : '')
Expand Down
154 changes: 5 additions & 149 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -428,100 +428,8 @@ const eventDefaults = event => {
return event
}

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

const caps = property.tags.find(t => t.name === 'capabilities')
let name = caps['x-provided-by'] ? caps['x-provided-by'].split('.').pop().replace('onRequest', '') : property.name
name = name.charAt(0).toUpperCase() + name.substring(1)

if ( subscriberType === 'global') {
// wrap the existing result and the params in a new result object
const schema = {
title: methodRename(property, name => name.charAt(0).toUpperCase() + name.substring(1) + type + 'Info').split('.').pop(),
type: "object",
properties: {

},
required: []
}

// add all of the params
property.params.filter(p => p.name !== 'listen').forEach(p => {
schema.properties[p.name] = p.schema
schema.required.push(p.name)
})

// add the result (which might override a param of the same name)
schema.properties[property.result.name] = property.result.schema
!schema.required.includes(property.result.name) && schema.required.push(property.result.name)

return schema
}
}

const createEventFromProperty = (property, type='', alternative, json) => {
const provider = (property.tags.find(t => t['x-provided-by']) || {})['x-provided-by']
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
const event = eventDefaults(JSON.parse(JSON.stringify(property)))
// event.name = (module ? module + '.' : '') + 'on' + event.name.charAt(0).toUpperCase() + event.name.substr(1) + type
event.name = provider ? provider.split('.').pop().replace('onRequest', '') : event.name.charAt(0).toUpperCase() + event.name.substr(1) + type
event.name = event.name.split('.').map((x, i, arr) => (i === arr.length-1) ? 'on' + x.charAt(0).toUpperCase() + x.substr(1) : x).join('.')
const subscriberFor = pusher || (json.info.title + '.' + property.name)

const old_tags = JSON.parse(JSON.stringify(property.tags))

alternative && (event.tags[0]['x-alternative'] = alternative)

!provider && event.tags.unshift({
name: "subscriber",
'x-subscriber-for': subscriberFor
})

const subscriberType = property.tags.map(t => t['x-subscriber-type']).find(t => typeof t === 'string') || 'context'

// if the subscriber type is global, zap all of the parameters and change the result type to the schema that includes them
if (subscriberType === 'global') {

// wrap the existing result and the params in a new result object
const result = {
name: "data",
schema: {
$ref: "#/components/schemas/" + event.name.substring(2) + 'Info'
}
}

event.examples.map(example => {
const result = {}
example.params.filter(p => p.name !== 'listen').forEach(p => {
result[p.name] = p.value
})
result[example.result.name] = example.result.value
example.params = example.params.filter(p => p.name === 'listen')
example.result.name = "data"
example.result.value = result
})

event.result = result

// remove the params
event.params = event.params.filter(p => p.name === 'listen')
}

old_tags.forEach(t => {
if (t.name !== 'property' && !t.name.startsWith('property:') && t.name !== 'push-pull')
{
event.tags.push(t)
}
})

provider && (event.tags.find(t => t.name === 'capabilities')['x-provided-by'] = subscriberFor)

return event
}

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

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

if (subscriberType === 'global') {
notifier.params = [
{
name: "info",
schema: {
"$ref": "#/components/schemas/" + methodRename(notifier, name => name.charAt(0).toUpperCase() + name.substr(1) + 'Info')
}
}
]
}
else {
notifier.params.push(notifier.result)
}

notifier.params.push(notifier.result)

delete notifier.result

if (subscriberType === 'global') {
notifier.examples = property.examples.map(example => ({
name: example.name,
params: [
{
name: "info",
value: Object.assign(Object.fromEntries(example.params.map(p => [p.name, p.value])), Object.fromEntries([[example.result.name, example.result.value]]))
}
]
}))
}
else {
notifier.examples.forEach(example => {
notifier.examples.forEach(example => {
example.params.push(example.result)
delete example.result
})
}

return notifier
}

Expand Down Expand Up @@ -602,10 +483,6 @@ const createNotifierFromEvent = (event, json) => {
return push
}

const createPushEvent = (requestor, json) => {
return createEventFromProperty(requestor, '', undefined, json)
}

const createPullEventFromPush = (pusher, json) => {
const event = JSON.parse(JSON.stringify(pusher))
event.params = []
Expand Down Expand Up @@ -1042,17 +919,11 @@ const generatePropertyEvents = json => {

properties.forEach(property => {
json.methods.push(createNotifierFromProperty(property))
const schema = createEventResultSchemaFromProperty(property)
if (schema) {
json.components.schemas[property.name.split('.').shift() + '.' + schema.title] = schema
}

})
readonlies.forEach(property => {
json.methods.push(createNotifierFromProperty(property))
const schema = createEventResultSchemaFromProperty(property)
if (schema) {
json.components.schemas[property.name.split('.').shift() + '.' + schema.title] = schema
}

})

return json
Expand All @@ -1074,21 +945,6 @@ const generatePolymorphicPullEvents = json => {
return json
}

const generatePushPullMethods = json => {
const requestors = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'push-pull')) || []
requestors.forEach(requestor => {
json.methods.push(createPushEvent(requestor, json))

const schema = createEventResultSchemaFromProperty(requestor)
if (schema) {
json.components = json.components || {}
json.components.schemas = json.components.schemas || {}
json.components.schemas[schema.title] = schema
}
})

return json
}

const generateProvidedByMethods = json => {
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'])) || []
Expand Down
Loading