Skip to content

Commit 845c307

Browse files
refactor: Code clean up
1 parent 54e293e commit 845c307

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed

languages/javascript/src/shared/Events/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ const getClearArgs = function(...args) {
221221

222222
// populate context based on registered context for this module/event
223223
// if an argument is not registered in validContext, that is ok meaning it is not a context argument
224+
// Needed after introducing x-contextual-params
224225
for (let i = 0; args.length; i++) {
225226
let currentArg = args.shift()
226227
if (validContext[module] && validContext[module][event] && validContext[module][event][i]) {

src/shared/modules.mjs

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,28 @@ const eventDefaults = event => {
429429
}
430430

431431

432-
const createNotifierFromProperty = (property, json) => {
432+
const createNotifierFromProperty = (property) => {
433+
434+
const notifier = JSON.parse(JSON.stringify(property))
435+
notifier.name = methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1) + 'Changed')
436+
437+
Object.assign(notifier.tags.find(t => t.name.startsWith('property')), {
438+
name: 'notifier',
439+
'x-notifier-for': property.name,
440+
'x-event': notifier.name
441+
})
442+
443+
notifier.params.push(notifier.result)
444+
445+
delete notifier.result
446+
notifier.examples.forEach(example => {
447+
example.params.push(example.result)
448+
delete example.result
449+
})
450+
return notifier
451+
}
452+
453+
const createNotifierFromPropertyFlatteningParams = (property, json) => {
433454

434455
const notifier = JSON.parse(JSON.stringify(property))
435456
notifier.name = methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1) + 'Changed')
@@ -734,7 +755,56 @@ const createTemporalStopMethod = (method, jsoname) => {
734755
return stop
735756
}
736757

737-
const createSetterFromProperty = (property, json) => {
758+
const createSetterFromProperty = property => {
759+
const setter = JSON.parse(JSON.stringify(property))
760+
setter.name = methodRename(setter, name => 'set' + name.charAt(0).toUpperCase() + name.substr(1))
761+
const old_tags = setter.tags
762+
setter.tags = [
763+
{
764+
'name': 'setter',
765+
'x-setter-for': property.name
766+
}
767+
]
768+
769+
const param = setter.result
770+
param.name = 'value'
771+
param.required = true
772+
setter.params.push(param)
773+
774+
setter.result = {
775+
name: 'result',
776+
schema: {
777+
type: "null"
778+
}
779+
}
780+
781+
setter.examples && setter.examples.forEach(example => {
782+
example.params.push({
783+
name: 'value',
784+
value: example.result.value
785+
})
786+
787+
example.result.value = null
788+
})
789+
790+
old_tags.forEach(t => {
791+
if (t.name !== 'property' && !t.name.startsWith('property:'))
792+
{
793+
if (t.name === 'capabilities') {
794+
setter.tags.push({
795+
name: 'capabilities',
796+
'x-manages': t['x-uses'] || t['x-manages']
797+
})
798+
} else {
799+
setter.tags.push(t)
800+
}
801+
}
802+
})
803+
804+
return setter
805+
}
806+
807+
const createSetterFromPropertyFlatteningParams = (property, json) => {
738808
const setter = JSON.parse(JSON.stringify(property))
739809
setter.name = methodRename(setter, name => 'set' + name.charAt(0).toUpperCase() + name.substr(1))
740810
const old_tags = setter.tags
@@ -1019,12 +1089,24 @@ const generatePropertyEvents = (json) => {
10191089
const readonlies = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'property:readonly')) || []
10201090

10211091
properties.forEach(property => {
1022-
json.methods.push(createNotifierFromProperty(property, json))
1023-
1092+
//check if the property has a tag to flatten the params or not ("x-notifier-params-flattening" )
1093+
const xNotifierParamsFlattening = property.tags.find(t => t['x-notifier-params-flattening']) ? property.tags.find(t => t['x-notifier-params-flattening'])['x-notifier-params-flattening'] : false
1094+
if (xNotifierParamsFlattening) {
1095+
json.methods.push(createNotifierFromPropertyFlatteningParams(property, json))
1096+
}
1097+
else {
1098+
json.methods.push(createNotifierFromProperty(property))
1099+
}
10241100
})
10251101
readonlies.forEach(property => {
1026-
json.methods.push(createNotifierFromProperty(property, json))
1027-
1102+
//check if the property has a tag to flatten the params or not ("x-notifier-params-flattening" )
1103+
const xNotifierParamsFlattening = property.tags.find(t => t['x-notifier-params-flattening']) ? property.tags.find(t => t['x-notifier-params-flattening'])['x-notifier-params-flattening'] : false
1104+
if (xNotifierParamsFlattening) {
1105+
json.methods.push(createNotifierFromPropertyFlatteningParams(property, json))
1106+
}
1107+
else {
1108+
json.methods.push(createNotifierFromProperty(property))
1109+
}
10281110
})
10291111

10301112
return json
@@ -1033,7 +1115,16 @@ const generatePropertyEvents = (json) => {
10331115
const generatePropertySetters = json => {
10341116
const properties = json.methods.filter( m => m.tags && m.tags.find( t => t.name == 'property')) || []
10351117

1036-
properties.forEach(property => json.methods.push(createSetterFromProperty(property, json)))
1118+
properties.forEach(property => {
1119+
//check if the property has a tag to flatten the params or not ("x-setter-params-flattening" )
1120+
const xSetterParamsFlattening = property.tags.find(t => t['x-setter-params-flattening']) ? property.tags.find(t => t['x-setter-params-flattening'])['x-setter-params-flattening'] : false
1121+
if (xSetterParamsFlattening) {
1122+
json.methods.push(createSetterFromPropertyFlatteningParams(property, json))
1123+
}
1124+
else {
1125+
json.methods.push(createSetterFromProperty(property))
1126+
}
1127+
})
10371128

10381129
return json
10391130
}
@@ -1254,8 +1345,8 @@ const generateEventSubscribers = json => {
12541345
subscriber.params.pop()
12551346
}
12561347
}
1257-
//subscriber.params.push({
1258-
subscriber.params.unshift({
1348+
subscriber.params.push({
1349+
//subscriber.params.unshift({
12591350
name: 'listen',
12601351
schema: {
12611352
type: 'boolean'
@@ -1287,8 +1378,8 @@ const generateEventSubscribers = json => {
12871378
}
12881379
}
12891380

1290-
//example.params.push({
1291-
example.params.unshift({
1381+
example.params.push({
1382+
//example.params.unshift({
12921383
name: "listen",
12931384
value: true
12941385
})
@@ -1406,8 +1497,8 @@ const generateEventListenerParameters = json => {
14061497
events.forEach(event => {
14071498
event.params = event.params || []
14081499
//push in front to keep existing params order
1409-
event.params.unshift({
1410-
//example.params.push({
1500+
//event.params.unshift({
1501+
example.params.push({
14111502
"name": "listen",
14121503
"required": true,
14131504
"schema": {
@@ -1419,8 +1510,8 @@ const generateEventListenerParameters = json => {
14191510

14201511
event.examples.forEach(example => {
14211512
example.params = example.params || []
1422-
event.params.unshift({
1423-
//example.params.push({
1513+
//event.params.unshift({
1514+
example.params.push({
14241515
"name": "listen",
14251516
"value": true
14261517
})

0 commit comments

Comments
 (0)