Guest User

Untitled

a guest
Dec 12th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. async function sleep(ms: number) {
  2. return new Promise((resolve, reject) => {
  3. setTimeout(() => resolve(), ms)
  4. })
  5. }
  6.  
  7. async function randomDelay() {
  8. const randomTime = Math.round(Math.random() * 1000)
  9. return sleep(randomTime)
  10. }
  11.  
  12. class ShipmentSearchIndex {
  13. async updateShipment(id: string, shipmentData: any) {
  14. const startTime = new Date()
  15. await randomDelay()
  16. const endTime = new Date()
  17. console.log(`update ${id}@${
  18. startTime.toISOString()
  19. } finished@${
  20. endTime.toISOString()
  21. }`
  22. )
  23.  
  24. return {startTime, endTime}
  25. }
  26. }
  27.  
  28.  
  29. interface ShipmentUpdateListenerInterface {
  30. receiveUpdate(id: string, shipmentData: any)
  31. }
  32.  
  33. class ShipmentUpdater implements ShipmentUpdateListenerInterface{
  34. private shipmentSearchIndex: ShipmentSearchIndex = new ShipmentSearchIndex();;
  35.  
  36. async receiveUpdate(id: string, shipmentData: any) {
  37. await this.shipmentSearchIndex.updateShipment(id, shipmentData);
  38. }
  39. }
  40.  
  41. async function testClass(){
  42.  
  43. let updater: ShipmentUpdater = new ShipmentUpdater();
  44.  
  45. await updater.receiveUpdate('1', {});
  46. await updater.receiveUpdate('2', {});
  47. }
  48.  
  49. testClass();
Add Comment
Please, Sign In to add comment