Created
November 30, 2021 20:40
-
-
Save jonasgrilleres/964d4d292502ed0482f47e3272badf59 to your computer and use it in GitHub Desktop.
Arolla Jam Code Kata Microservices - 30/11/2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MessageBus { | |
constructor() { | |
this.listeners = []; | |
} | |
subscribe(listener) { | |
this.listeners.push(listener); | |
} | |
send(message) { | |
this.listeners.forEach(function(listener) { | |
listener.onMessage(message); | |
}); | |
} | |
} | |
class Booking { | |
constructor(msgBus) { | |
this.msgBus = msgBus; | |
this.msgBus.subscribe(this); | |
} | |
onMessage(message) {} | |
book(seats_count) { | |
console.log("Booking.book(): seats_count='" + seats_count + "'"); | |
this.msgBus.send({ | |
name: "booking_placed", | |
seats_count: seats_count | |
}); | |
} | |
} | |
class Inventory { | |
constructor(msgBus, capacity) { | |
this.msgBus = msgBus; | |
this.seats_left = capacity; | |
this.msgBus.subscribe(this); | |
} | |
onMessage(message) { | |
if (message.name === "booking_placed") { | |
this.reserve_seats(message.seats_count); | |
} | |
} | |
reserve_seats(seats_count) { | |
console.log("Inventory.reserve_seats()"); | |
if (this.seats_left < seats_count) { | |
console.log("ERROR: Inventory.reserve_seats(): not enough seats left"); | |
this.msgBus.send({ | |
name: "capacity_exceeded" | |
}); | |
} | |
this.seats_left -= seats_count; | |
this.msgBus.send({ | |
name: "ticket_booked", | |
seats_count: seats_count | |
}); | |
} | |
} | |
class Ticketing { | |
constructor(msgBus) { | |
this.msgBus = msgBus; | |
this.msgBus.subscribe(this); | |
} | |
onMessage(message) { | |
if (message.name === "ticket_booked") { | |
this.send_ticket(message.seats_count) | |
} | |
} | |
send_ticket(seats_count) { | |
console.log("Ticketing.send_ticket(): seats_count='" + seats_count + "'"); | |
} | |
} | |
function main() { | |
msgBus = new MessageBus(); | |
inventory = new Inventory(msgBus, 30); | |
ticketing = new Ticketing(msgBus); | |
booking = new Booking(msgBus); | |
booking.book(2); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment