Skip to content

Commit

Permalink
fix: Get all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed Jun 10, 2024
1 parent 9305aa7 commit 131dbb1
Show file tree
Hide file tree
Showing 20 changed files with 394 additions and 513 deletions.
50 changes: 31 additions & 19 deletions languages/javascript/src/shared/Gateway/Client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,64 @@ win.__firebolt.idGenerator = idGenerator
const promises = {}
const deprecated = {}

// consider renaming `batch`

export async function bulk(requests) {
// request = { method: string, params: object, id: boolean }[]
// request with no `id` property are assumed to NOT be notifications, i.e. id must be set to false explicitly
export async function batch(requests) {
if (Array.isArray(requests)) {
const body = requests.map(req => processRequest(req.method, req.params))
Transport.send(body)
return await Promise.all(requests.map((req, i) => addPromiseToQueue(req.id, requests[i].transforms)))
const processed = requests.map(req => processRequest(req.method, req.params, req.id, req.id === false))

// filter requests exclude notifications, as they don't need promises
const promises = processed.filter(req => req.id).map(request => addPromiseToQueue(request.id))

Transport.send(processed)

// Using Promise.all get's us batch blocking for free
return Promise.all(promises)
}
throw `Bulk requests must be in an array`
}

// Request that the server provide fulfillment of an method
export async function request(method, params, transforms) {
export async function request(method, params) {
const json = processRequest(method, params)
const promise = addPromiseToQueue(json.id, transforms)
const promise = addPromiseToQueue(json.id)
Transport.send(json)
return promise
}

export async function notify(method, params) {
export function notify(method, params) {
Transport.send(processRequest(method, params, true))
}

export function response(id, result, error) {
if (result !== undefined) {
promises[id].resolve(result)
const promise = promises[id]

if (promise) {
if (result !== undefined) {
promises[id].resolve(result)
}
else if (error !== undefined) {
promises[id].reject(error)
}

// TODO make sure this works
delete promises[id]
}
else if (error !== undefined) {
promises[id].reject(error)
else {
throw `Received a response for an unidentified request ${id}`
}

// TODO make sure this works
delete promises[id]
}

export function deprecate(method, alternative) {
deprecated[method] = alternative
}

function addPromiseToQueue (id, transforms) {
function addPromiseToQueue (id) {
return new Promise((resolve, reject) => {
promises[id] = {}
promises[id].promise = this
promises[id].resolve = resolve
promises[id].reject = reject
promises[id].transforms = transforms
})
}

Expand All @@ -103,7 +115,7 @@ function processRequest(method, params, notification=false) {

export default {
request,
bulk,
batch,
response,
deprecate
}
2 changes: 1 addition & 1 deletion languages/javascript/src/shared/Gateway/Server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function registerProviderInterface(capability, _interface, method, parame
}


async function getProviderResult(method, params) {
async function getProviderResult(method, params={}) {
const split = method.split('.')
method = split.pop()
const interfaceName = split.join('.')
Expand Down
35 changes: 31 additions & 4 deletions languages/javascript/src/shared/Gateway/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ import Transport from "../Transport/index.mjs"
import Settings from "../Settings/index.mjs"

Transport.receive(async (json) => {
// console.debug('Received message from transport: \n' + JSON.stringify(json, { indent: '\t'}))
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'}))
}
Expand All @@ -38,17 +46,35 @@ Transport.receive(async (json) => {
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)) {
return await Client.bulk(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)
}
Expand All @@ -65,9 +91,10 @@ export function deprecate(method, alternative) {
Client.deprecate(method, alternative)
}


export default {
request,
notify,
batch,
subscribe,
unsubscribe,
provide,
Expand Down
28 changes: 10 additions & 18 deletions languages/javascript/src/shared/TemporalSet/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function stopSession(module, method) {
delete sessions[module.toLowerCase() + '.' + method]
}

function start(module, method, addName, removeName, params, add, remove, timeout, transforms) {
async function start(module, method, addName, removeName, params, add, remove, timeout, transforms) {
let session = getSession(module, method)

if (session) {
Expand Down Expand Up @@ -62,27 +62,23 @@ function start(module, method, addName, removeName, params, add, remove, timeout
})
}

const results = Gateway.request(requests)
const results = await Gateway.batch(requests)

session.id = results[0].id
session.add = add
session.remove = remove
session.addName = addName
session.removeName = removeName

results[0].promise.then( items => {
add && items && items.forEach(item => add(item))
})

if (add) {
results[0] && results[0].forEach(item => add(item))

return {
stop: () => {
const requests = [
{
method: `${module}.stop${method.charAt(0).toUpperCase() + method.substr(1)}`,
params: {
correlationId: session.id
}
params: {}
},
{
method: `${module}.${addName}`,
Expand All @@ -104,22 +100,18 @@ function start(module, method, addName, removeName, params, add, remove, timeout
}

Gateway.unsubscribe(`${module}.${removeName}`)
Gateway.request(requests)
Gateway.batch(requests)
stopSession(module, method)
}
}
}
else if (timeout) {
return results[0].promise.then(results => {
stopSession(module, method)
return results.shift()
})
stopSession(module, method)
return results[0].shift()
}
else {
return results[0].promise.then(results => {
stopSession(module, method)
return results
})
stopSession(module, method)
return Promise.resolve(results[0])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (win.__firebolt && win.__firebolt.testHarness) {
function send(json) {
// handle bulk sends
if (Array.isArray(json)) {
json.forEach(j => send(JSON.stringify(j)))
json.forEach(send)
return
}

Expand Down
4 changes: 3 additions & 1 deletion languages/javascript/templates/methods/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ function ${method.name}(${method.params.list}) {

const transforms = ${method.transforms}

return Gateway.request('${info.title}.${method.name}', { ${method.params.list} }, transforms)
return Gateway.request('${info.title}.${method.name}', { ${method.params.list} }).then( (result) => {
return Results.transform(result, transforms)
})
}
2 changes: 2 additions & 0 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,8 @@ function generateProviderSubscribe(server, client, templates, bidirectional) {
function generateProviderInterfaces(server, client, templates, codeblock, directory, bidirectional) {
const interfaces = getProvidedInterfaces(client || server)

console.dir(interfaces)

let template = getTemplate('/sections/provider-interfaces', templates)

const providers = reduce((acc, _interface) => {
Expand Down
8 changes: 5 additions & 3 deletions test/TransportHarness.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
let receiver

export const transport = {
send: function(message) {
const json = JSON.parse(message)
send: function(json) {
sendListener && sendListener(json)
},
receive: function(callback) {
Expand All @@ -53,7 +52,10 @@
id: id,
result: result
}
receiver && receiver(JSON.stringify(response))
receiver && receiver(response)
},
request: function(json) {
receiver && receiver(json)
}
}

Expand Down
Loading

0 comments on commit 131dbb1

Please sign in to comment.