Skip to content

Commit 43d8318

Browse files
committed
fix(Events): Support for multiple context parameters
#91
1 parent 41cb22e commit 43d8318

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const getClearArgs = function(...args) {
201201
const event = args.shift() || '*'
202202
const context = {}
203203

204-
for (let i = 0; i<args.length; i++) {
204+
for (let i = 0; args.length; i++) {
205205
context[validContext[module][event][i]] = args.shift()
206206
}
207207

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function prop(moduleName, key, params, callbackOrValue = null, immutable, readon
1212
if (immutable) {
1313
throw new Error('Cannot subscribe to an immutable property')
1414
}
15-
return Events.listen(moduleName, key + 'Changed', callbackOrValue)
15+
return Events.listen(moduleName, key + 'Changed', ...Object.values(params), callbackOrValue)
1616
} else if (numArgs === (contextParameterCount) && callbackOrValue !== null) {
1717
// setter
1818
if (immutable) {

test/openrpc/advanced.json

+62-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"x-uses": ["xrn:firebolt:capability:test:test"]
1818
}
1919
],
20-
"summary": "A temporal set method that lists Advanced objects.",
20+
"summary": "An event with one context parameter.",
2121
"params": [
2222
{
2323
"name": "appId",
@@ -56,6 +56,67 @@
5656
}
5757
]
5858
},
59+
{
60+
"name": "onEventWithTwoContext",
61+
"tags": [
62+
{
63+
"name": "event"
64+
},
65+
{
66+
"name": "capabilities",
67+
"x-uses": ["xrn:firebolt:capability:test:test"]
68+
}
69+
],
70+
"summary": "An event with two context parameters.",
71+
"params": [
72+
{
73+
"name": "appId",
74+
"required": true,
75+
"schema": {
76+
"type": "string"
77+
}
78+
},
79+
{
80+
"name": "state",
81+
"required": true,
82+
"schema": {
83+
"type": "string"
84+
}
85+
}
86+
],
87+
"result": {
88+
"name": "result",
89+
"schema": {
90+
"type": "object",
91+
"properties": {
92+
"foo": {
93+
"type": "string"
94+
}
95+
}
96+
}
97+
},
98+
"examples": [
99+
{
100+
"name": "Default Example",
101+
"params": [
102+
{
103+
"name": "appId",
104+
"value": "hulu"
105+
},
106+
{
107+
"name": "state",
108+
"value": "inactive"
109+
}
110+
],
111+
"result": {
112+
"name": "result",
113+
"value": {
114+
"foo": "bar"
115+
}
116+
}
117+
}
118+
]
119+
},
59120
{
60121
"name": "propertyWithContext",
61122
"summary":"",

test/suite/properties-context.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ let contextSentToGetter = false
2727
let contextSentToSetter = false
2828
let contextSentToSubscriber = false
2929
let contextSentToEvent = false
30+
let bothContextSentToEvent = false
3031

3132
beforeAll( () => {
3233

3334
transport.onSend(json => {
35+
console.dir(json)
3436
if (json.method === 'advanced.propertyWithContext') {
3537
if (json.params.appId === 'some-app') {
3638
contextSentToGetter = true
@@ -68,6 +70,11 @@ beforeAll( () => {
6870
contextSentToEvent = true
6971
}
7072
}
73+
else if (json.method === "advanced.onEventWithTwoContext") {
74+
if (json.params.appId === 'some-app' && json.params.state === 'inactive') {
75+
bothContextSentToEvent = true
76+
}
77+
}
7178
})
7279

7380
Advanced.propertyWithContext('some-app', true)
@@ -97,8 +104,14 @@ test('Context Property set', () => {
97104
expect(contextSentToSetter).toBe(true)
98105
});
99106

100-
test('Event with context', () => {
107+
test('Event with single context param', () => {
101108
Advanced.listen("eventWithContext", "some-app", (data) => {
102109
expect(contextSentToEvent).toBe(true)
103110
})
111+
})
112+
113+
test('Event with two context params', () => {
114+
Advanced.listen("eventWithTwoContext", "some-app", "inactive", (data) => {
115+
expect(bothContextSentToEvent).toBe(true)
116+
})
104117
})

0 commit comments

Comments
 (0)