Skip to content

Commit

Permalink
feat: Support for both Bi- and Uni-directional SDKs
Browse files Browse the repository at this point in the history
also added support for `firebolt-openrpc.config.json` file to override language settings.
  • Loading branch information
jlacivita committed Jun 18, 2024
1 parent b13d4cd commit 7bedf0b
Show file tree
Hide file tree
Showing 31 changed files with 505 additions and 302 deletions.
4 changes: 2 additions & 2 deletions languages/javascript/src/shared/Events/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ const doListen = function(module, event, callback, context, once, internal=false
const subscriber = module + '.on' + event[0].toUpperCase() + event.substring(1)
const notifier = module + '.' + event

const promise = Gateway.request(subscriber, args)
Gateway.subscribe(notifier, (params) => {
callCallbacks(key, params[Object.keys(params).pop()])
callCallbacks(key, params)
})
const promise = Gateway.request(subscriber, args)
promises.push(promise)
}

Expand Down
108 changes: 108 additions & 0 deletions languages/javascript/src/shared/Gateway/Bidirectional.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import Server from "./Server.mjs"
import Client from "./Client.mjs"
import Transport from "../Transport/index.mjs"
import Settings from "../Settings/index.mjs"

Transport.receive(async (message) => {
const json = JSON.parse(message)
if (Array.isArray(json)) {
json.forEach(message => processMessage(message))
}
else {
processMessage(json)
}
})

function processMessage(json) {
if (Settings.getLogLevel() === 'DEBUG') {
console.debug('Receiving message from transport: \n' + JSON.stringify(json, { indent: '\t'}))
}

if (json.method !== undefined) {
if (json.id !== undefined) {
Server.request(json.id, json.method, json.params)
}
else {
Server.notify(json.method, json.params)
}
}
else if (json.id !== undefined) {
Client.response(json.id, json.result, json.error)
}
}

export async function batch(requests) {
if (Array.isArray(requests)) {
return await Client.batch(requests)
}
else {
throw "Gateway.batch() requires an array of requests: { method: String, params: Object, id: Boolean }"
}
}

export async function request(method, params) {
if (Array.isArray(method)) {
throw "Use Gateway.batch() for batch requests."
}
else {
return await Client.request(method, params)
}
}

export async function notify(method, params) {
if (Array.isArray(method)) {
throw "Use Gateway.batch() for batch requests."
}
else {
return await Client.notify(method, params)
}
}

export function subscribe(event, callback) {
Server.subscribe(event, callback)
}

export function unsubscribe(event) {
Server.unsubscribe(event)
}

export function simulate(event, value) {
Server.simulate(event, value)
}

export function provide(interfaceName, provider) {
Server.provide(interfaceName, provider)
}

export function deprecate(method, alternative) {
Client.deprecate(method, alternative)
}

export default {
request,
notify,
batch,
subscribe,
unsubscribe,
simulate,
provide,
deprecate
}
15 changes: 12 additions & 3 deletions languages/javascript/src/shared/Gateway/Server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ export async function request(id, method, params, transforms) {
Transport.send(response)
}

// TODO: How do we know what order the params are in!?
// Need to implement this spec:
// https://github.com/rdkcentral/firebolt-apis/blob/feature/protocol/requirements/specifications/general/context-parameters.md
// Which ensures that we'll only have one (any name) or two (data & context) parameters.
export async function notify(method, params) {
if (listeners[method]) {
listeners[method](params)
listeners[method](...Object.values(params))
return
}
throw `Notification not implemented: ${method}`
}
}

// Register a provider implementation with an interface name
export function provide(interfaceName, provider) {
Expand All @@ -76,6 +80,10 @@ export function unsubscribe(event) {
delete listeners[event]
}

export function simulate(event, value) {
listeners[event](value)
}

// TODO: consider renaming
export function registerProviderInterface(capability, _interface, method, parameters, response, focusable) {
interfaces[_interface] = interfaces[_interface] || {
Expand Down Expand Up @@ -115,5 +123,6 @@ export default {
notify,
provide,
subscribe,
unsubscribe
unsubscribe,
simulate
}
Loading

0 comments on commit 7bedf0b

Please sign in to comment.