Skip to content

Instantly share code, notes, and snippets.

@supertrens
Created November 26, 2018 18:10
Show Gist options
  • Save supertrens/ccba627f69422a540a24255d5282b695 to your computer and use it in GitHub Desktop.
Save supertrens/ccba627f69422a540a24255d5282b695 to your computer and use it in GitHub Desktop.
FreightHub Coding Challenge
1) I had first implemented the requirement with both classes in the same file. however, when I split them into dedifferent modules,
I had some issue running it on browser since both `commonJS` and `amd` could import/define to bundle the module.
So, I have mae some google search and I came across `systemJs` module loader, that fixed my issue in browser.
2) About the following requirement `Also make sure that the executions of updateShipment with the same id never run concurrently`.
I wasn't to share if the requirement was to pause the execution of the function and wait until the promise is returned ( I implemented that with async await).
Or it was a request to check the `id value` of the coming update (which I dont implement).
Should you have any technical questions about my submittion, please let me know.
Thank you!!!
async function sleep(ms: number) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms)
})
}
async function randomDelay() {
const randomTime = Math.round(Math.random() * 1000)
return sleep(randomTime)
}
export default class ShipmentSearchIndex {
async updateShipment(id: string, shipmentData: any) {
const startTime = new Date()
await randomDelay()
const endTime = new Date()
console.log(`update ${id}@${
startTime.toISOString()
} finished@${
endTime.toISOString()
}`
)
return { startTime, endTime }
}
}
import ShipmentSearchIndex from './shipmentSearchIndex'
interface ShipmentUpdateListenerInterface {
receiveUpdate(id: string, shipmentData: any): void;
}
class ShipmentUpdateListener extends ShipmentSearchIndex implements ShipmentUpdateListenerInterface {
async receiveUpdate(id: string, shipmentData: any) {
await this.updateShipment(id, shipmentData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment