Skip to content

Commit

Permalink
feat: Support for context-free property subscribers
Browse files Browse the repository at this point in the history
Allows a subscriber to be registered for all events, regardless of context parameter value.
  • Loading branch information
jlacivita committed Aug 24, 2023
1 parent e59d461 commit 9809273
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,35 @@ const eventDefaults = event => {
return event
}

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

if (property.tags.find(t => (t.name == 'property' || t.name.startsWith('property:')) && (subscriberType === 'global'))) {
// wrap the existing result and the params in a new result object
const schema = {
title: property.name.charAt(0).toUpperCase() + property.name.substring(1) + 'ChangedInfo',
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 => {
const event = eventDefaults(JSON.parse(JSON.stringify(property)))
event.name = 'on' + event.name.charAt(0).toUpperCase() + event.name.substr(1) + 'Changed'
Expand All @@ -380,6 +409,36 @@ const createEventFromProperty = property => {
'x-subscriber-for': property.name
})

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 (old_tags.find(t => (t.name == 'property' || t.name.startsWith('property:')) && (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:'))
{
Expand Down Expand Up @@ -745,8 +804,20 @@ const generatePropertyEvents = json => {
const properties = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'property')) || []
const readonlies = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'property:readonly')) || []

properties.forEach(property => json.methods.push(createEventFromProperty(property)))
readonlies.forEach(property => json.methods.push(createEventFromProperty(property)))
properties.forEach(property => {
json.methods.push(createEventFromProperty(property))
const schema = createEventResultSchemaFromProperty(property)
if (schema) {
json.components.schemas[schema.title] = schema
}
})
readonlies.forEach(property => {
json.methods.push(createEventFromProperty(property))
const schema = createEventResultSchemaFromProperty(property)
if (schema) {
json.components.schemas[schema.title] = schema
}
})

return json
}
Expand Down

0 comments on commit 9809273

Please sign in to comment.