Skip to content

Commit be86443

Browse files
Bidirectional events should come in as on<EventName> (#282)
* feat: Bidirectional events should come in as 'on<Method>' * fix: Fix unit tests
1 parent 3a102ea commit be86443

File tree

7 files changed

+21
-43
lines changed

7 files changed

+21
-43
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ const doListen = function(module, event, callback, context, once, internal=false
145145
if (Object.values(listeners.get(key)).length === 0) {
146146
const args = Object.assign({ listen: true }, context)
147147

148-
// TODO: Is subscriber -> notifer required to be a simple transform (drop 'on'?)
149-
const subscriber = module + '.on' + event[0].toUpperCase() + event.substring(1)
148+
const subscriber = module + '.' + event
150149
const notifier = module + '.' + event
151150

152151
Gateway.subscribe(notifier, (params) => {
@@ -252,7 +251,7 @@ export const prioritize = function(...args) {
252251
const unsubscribe = (key, context) => {
253252
const [module, event] = key.split('.').slice(0, 2)
254253
const args = Object.assign({ listen: false }, context)
255-
Gateway.request(module + '.on' + event[0].toUpperCase() + event.substr(1), args)
254+
Gateway.request(module + '.' + event, args)
256255
Gateway.unsubscribe(`${module}.${event}`)
257256
}
258257

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function prop(
2121
if (immutable) {
2222
throw new Error('Cannot subscribe to an immutable property');
2323
}
24-
return Events.listen(moduleName, key + 'Changed', ...Object.values(params), callbackOrValue);
24+
const subscriber = 'on' + key[0].toUpperCase() + key.substring(1) + 'Changed'
25+
return Events.listen(moduleName, subscriber, ...Object.values(params), callbackOrValue);
2526
} else if (type === 'setter') {
2627
// setter
2728
if (immutable) {

src/macrofier/engine.mjs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,6 @@ const rpcMethodsOrEmptyArray = compose(
346346
// Pick events out of the methods array
347347
const eventsOrEmptyArray = compose(
348348
option([]),
349-
map(filter(validEvent)),
350-
// Maintain the side effect of process.exit here if someone is violating the rules
351-
map(map(e => {
352-
if (!e.name.match(/on[A-Z]/)) {
353-
console.error(`ERROR: ${e.name} method is tagged as an event, but does not match the pattern "on[A-Z]"`)
354-
process.kill(process.pid) // Using process.kill so that other worspaces all exit (and don't bury this error w/ logs)
355-
}
356-
return e
357-
})),
358349
map(filter(isPublicEventMethod)),
359350
getMethods
360351
)
@@ -390,15 +381,6 @@ const providedCapabilitiesOrEmptyArray = compose(
390381
// Pick providers out of the methods array
391382
const providersOrEmptyArray = compose(
392383
option([]),
393-
map(filter(validEvent)),
394-
// Maintain the side effect of process.exit here if someone is violating the rules
395-
map(map(e => {
396-
if (!e.name.match(/on[A-Z]/)) {
397-
console.error(`ERROR: ${e.name} method is tagged as a provider, but does not match the pattern "on[A-Z]"`)
398-
process.exit(1) // Non-zero exit since we don't want to continue. Useful for CI/CD pipelines.
399-
}
400-
return e
401-
})),
402384
map(filter(isProviderInterfaceMethod)),
403385
getMethods
404386
)
@@ -420,7 +402,7 @@ const getModuleName = json => {
420402
return json ? (json.title || (json.info ? json.info.title : 'Unknown')) : 'Unknown'
421403
}
422404

423-
const makeEventName = x => methodName(x)[2].toLowerCase() + methodName(x).substr(3) // onFooBar becomes fooBar
405+
const makeEventName = x => methodName(x) // onFooBar remains onFooBar
424406
const makeProviderMethod = x => x.name["onRequest".length].toLowerCase() + x.name.substr("onRequest".length + 1) // onRequestChallenge becomes challenge
425407

426408
const generateAggregateMacros = (platformApi, appApi, additional, templates, library) => {
@@ -1785,7 +1767,7 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
17851767
.replace(/\$\{method\.context\.count}/g, method.context ? method.context.length : 0)
17861768
.replace(/\$\{method\.deprecation\}/g, deprecation)
17871769
.replace(/\$\{method\.Name\}/g, method.name[0].toUpperCase() + method.name.substr(1))
1788-
.replace(/\$\{event\.name\}/g, method.name.toLowerCase()[2] + method.name.substr(3))
1770+
.replace(/\$\{event\.name\}/g, method.name)
17891771
.replace(/\$\{event\.params\}/g, eventParams)
17901772
.replace(/\$\{event\.params\.table\.rows\}/g, eventParamsRows)
17911773
.replace(/\$\{if\.event\.params\}(.*?)\$\{end\.if\.event\.params\}/gms, event && event.params.length ? '$1' : '')

src/shared/modules.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,12 @@ const eventDefaults = event => {
432432
const createNotifierFromProperty = (property, type='Changed') => {
433433

434434
const notifier = JSON.parse(JSON.stringify(property))
435-
notifier.name = methodRename(notifier, name => name + type)
435+
notifier.name = methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1) + type)
436436

437437
Object.assign(notifier.tags.find(t => t.name.startsWith('property')), {
438438
name: 'notifier',
439439
'x-notifier-for': property.name,
440-
'x-event': methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1))
440+
'x-event': notifier.name
441441
})
442442

443443
notifier.params.push(notifier.result)
@@ -1110,13 +1110,12 @@ const generateEventSubscribers = json => {
11101110
const tag = notifier.tags.find(tag => tag.name === 'notifier')
11111111
// if there's an x-event extension, this denotes an editorially created subscriber
11121112
if (!tag['x-event']) {
1113-
tag['x-event'] = methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1))
1113+
tag['x-event'] = notifier.name
11141114
}
1115-
const subscriber = json.methods.find(method => method.name === tag['x-event'])
1115+
const subscriber = json.methods.find(method => method.tags.find(t => t['x-notifier'] === notifier.name))
11161116

11171117
if (!subscriber) {
11181118
const subscriber = JSON.parse(JSON.stringify(notifier))
1119-
subscriber.name = methodRename(subscriber, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1))
11201119
subscriber.params.pop()
11211120
subscriber.params.push({
11221121
name: 'listen',
@@ -2085,7 +2084,6 @@ export {
20852084
getEnums,
20862085
getTypes,
20872086
getEvents,
2088-
getPublicEvents,
20892087
getSchemas,
20902088
getParamsFromMethod,
20912089
fireboltize,

test_sdk/openrpc/advanced.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"name": "plainEvent",
1111
"tags": [
1212
{
13-
"name": "notifier",
14-
"x-event": "Advanced.onPlainEvent"
13+
"name": "notifier"
1514
},
1615
{
1716
"name": "capabilities",
@@ -43,7 +42,7 @@
4342
]
4443
},
4544
{
46-
"name": "eventWithContext",
45+
"name": "onEventWithContext",
4746
"tags": [
4847
{
4948
"name": "notifier",
@@ -96,11 +95,10 @@
9695
]
9796
},
9897
{
99-
"name": "eventWithTwoContext",
98+
"name": "onEventWithTwoContext",
10099
"tags": [
101100
{
102-
"name": "notifier",
103-
"x-event": "Advanced.onEventWithTwoContext"
101+
"name": "notifier"
104102
},
105103
{
106104
"name": "capabilities",

test_sdk/suite/properties-context.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ test('Context Property set', () => {
114114
});
115115

116116
test('Event with single context param', () => {
117-
Advanced.listen("eventWithContext", "some-app", (data) => {
117+
Advanced.listen("onEventWithContext", "some-app", (data) => {
118118
expect(contextSentToEvent).toBe(true)
119119
})
120120
})
121121

122122
test('Event with two context params', () => {
123-
Advanced.listen("eventWithTwoContext", "some-app", "inactive", (data) => {
123+
Advanced.listen("onEventWithTwoContext", "some-app", "inactive", (data) => {
124124
expect(bothContextSentToEvent).toBe(true)
125125
})
126126
})

test_sdk/suite/properties.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test('Basic Property subscribe', () => {
6969
let p = Simple.plainProperty(value => {
7070
expect(value.value).toBe("value 123")
7171
})
72-
MockTransport.event("Simple","plainPropertyChanged", "value 123");
72+
MockTransport.event("Simple","onPlainPropertyChanged", "value 123");
7373
return p;
7474
});
7575

@@ -90,12 +90,12 @@ test('Basic Property set with null', () => {
9090
expect(propertySetterWasTriggeredWithValue).toBe(true)
9191
});
9292

93-
//test listen to "plainPropertyChanged" event
93+
//test listen to "onPlainPropertyChanged" event
9494
test('Basic Property subscribe to event', () => {
95-
Simple.clear("plainPropertyChanged");
96-
let p = Simple.listen("plainPropertyChanged", value => {
95+
Simple.clear("onPlainPropertyChanged");
96+
let p = Simple.listen("onPlainPropertyChanged", value => {
9797
expect(value.value).toBe( "value 123")
9898
})
99-
MockTransport.event("Simple","plainPropertyChanged", "value 123");
99+
MockTransport.event("Simple","onPlainPropertyChanged", "value 123");
100100
return p;
101101
});

0 commit comments

Comments
 (0)