Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import { Component } from "@angular/core";
  2.  
  3. import { stockSignalRService } from "../services/stock.signalR.service";
  4. import { forEach } from "@angular/router/src/utils/collection";
  5.  
  6.  
  7. @Component({
  8. templateUrl: './stock.component.html',
  9. selector:"app-stock"
  10. })
  11.  
  12. export class StockComponent {
  13.  
  14. stocks = [];
  15. marketStatus: string;
  16.  
  17. constructor(private stockService: stockSignalRService) {
  18. this.stocks = [];
  19. this.marketStatus = 'closed';
  20. //subscribe for connection eastablish
  21. //fetch the stocks details
  22. stockService.connectionEstablished.subscribe(() => {
  23. stockService.getAllStocks().then((data) => {
  24. this.stocks = data;
  25. });
  26. });
  27.  
  28. //subscribe for market open
  29. stockService.marketOpened.subscribe(() => {
  30. this.marketStatus = 'open';
  31. this.startStrearming();
  32. });
  33.  
  34. //subscribe for market close
  35. stockService.marketClosed.subscribe(() => {
  36. this.marketStatus = 'closed';
  37. });
  38.  
  39. }
  40.  
  41. openMarketClicked() {
  42. this.stockService.openMarket();
  43. }
  44.  
  45. startStrearming() {
  46. this.stockService.startStreaming().subscribe({
  47. next: (data) => {
  48. this.displayStock(data);
  49. },
  50. error: function (err) {
  51. console.log('Error:' + err);
  52. },
  53. complete: function () {
  54. console.log('completed');
  55. }
  56. });
  57. }
  58.  
  59. closeMarketClicked() {
  60. this.stockService.CloseMarket();
  61. }
  62.  
  63. resetClicked() {
  64. this.stockService.ResetMarket();
  65. }
  66.  
  67. displayStock(stock) {
  68. console.log("stock updated:" + stock.symbol);
  69. for (let i in this.stocks) {
  70. //console.log(i);
  71. if (this.stocks[i].symbol == stock.symbol) {
  72. this.stocks[i] = stock;
  73. }
  74. }
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement