Skip to content

Commit

Permalink
fix(Events): Support for multiple context parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed May 9, 2023
1 parent 41cb22e commit 43d8318
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion languages/javascript/src/shared/Events/index.mjs
Expand Up @@ -201,7 +201,7 @@ const getClearArgs = function(...args) {
const event = args.shift() || '*'
const context = {}

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

Expand Down
2 changes: 1 addition & 1 deletion languages/javascript/src/shared/Prop/index.mjs
Expand Up @@ -12,7 +12,7 @@ function prop(moduleName, key, params, callbackOrValue = null, immutable, readon
if (immutable) {
throw new Error('Cannot subscribe to an immutable property')
}
return Events.listen(moduleName, key + 'Changed', callbackOrValue)
return Events.listen(moduleName, key + 'Changed', ...Object.values(params), callbackOrValue)
} else if (numArgs === (contextParameterCount) && callbackOrValue !== null) {
// setter
if (immutable) {
Expand Down
63 changes: 62 additions & 1 deletion test/openrpc/advanced.json
Expand Up @@ -17,7 +17,7 @@
"x-uses": ["xrn:firebolt:capability:test:test"]
}
],
"summary": "A temporal set method that lists Advanced objects.",
"summary": "An event with one context parameter.",
"params": [
{
"name": "appId",
Expand Down Expand Up @@ -56,6 +56,67 @@
}
]
},
{
"name": "onEventWithTwoContext",
"tags": [
{
"name": "event"
},
{
"name": "capabilities",
"x-uses": ["xrn:firebolt:capability:test:test"]
}
],
"summary": "An event with two context parameters.",
"params": [
{
"name": "appId",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "state",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "result",
"schema": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
},
"examples": [
{
"name": "Default Example",
"params": [
{
"name": "appId",
"value": "hulu"
},
{
"name": "state",
"value": "inactive"
}
],
"result": {
"name": "result",
"value": {
"foo": "bar"
}
}
}
]
},
{
"name": "propertyWithContext",
"summary":"",
Expand Down
15 changes: 14 additions & 1 deletion test/suite/properties-context.test.js
Expand Up @@ -27,10 +27,12 @@ let contextSentToGetter = false
let contextSentToSetter = false
let contextSentToSubscriber = false
let contextSentToEvent = false
let bothContextSentToEvent = false

beforeAll( () => {

transport.onSend(json => {
console.dir(json)
if (json.method === 'advanced.propertyWithContext') {
if (json.params.appId === 'some-app') {
contextSentToGetter = true
Expand Down Expand Up @@ -68,6 +70,11 @@ beforeAll( () => {
contextSentToEvent = true
}
}
else if (json.method === "advanced.onEventWithTwoContext") {
if (json.params.appId === 'some-app' && json.params.state === 'inactive') {
bothContextSentToEvent = true
}
}
})

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

test('Event with context', () => {
test('Event with single context param', () => {
Advanced.listen("eventWithContext", "some-app", (data) => {
expect(contextSentToEvent).toBe(true)
})
})

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

0 comments on commit 43d8318

Please sign in to comment.