Guest User

Untitled

a guest
Jan 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. class CounterComponent {
  2. public currentCount: any;
  3. public dictionary = {};
  4. public proxy: any;
  5. constructor() { this.SetProxy(); }
  6.  
  7. private GetPromise(name: PropertyKey,args)
  8. {
  9.  
  10. let key = Guid.newGuid();
  11. let promise = new Promise((resolve, reject) => {
  12. let item = new Item(resolve, args, name);
  13. this.dictionary[key] = item;
  14.  
  15. });
  16.  
  17.  
  18. return promise;
  19. }
  20. private SetProxy(): void {
  21.  
  22. let self=this;
  23. this.proxy = new Proxy({}, {
  24.  
  25. get: function(rcvr: any, name: PropertyKey)
  26. {
  27. // Для свойства нужно вернуть Promise
  28. if (name == "AllHello3")
  29. return self.GetPromise(name, []);
  30. // А для Метода нужно вернуть функцию создающую Promise
  31.  
  32. return (...args) => {
  33. return self.GetPromise(name,args)
  34.  
  35. };
  36.  
  37.  
  38. }
  39.  
  40.  
  41. });
  42. }
  43.  
  44.  
  45.  
  46. public async CallMethodAsync() {
  47.  
  48.  
  49. this.currentCount = await this.proxy.AllHello("Всем Привет");
  50. }
  51.  
  52.  
  53. //
  54. public setResult(): void {
  55.  
  56. let key: string;
  57. for (let name in this.dictionary) {
  58. key = name;
  59.  
  60.  
  61. // item.resolve("Всем привет");
  62. }
  63.  
  64. let item = <Item>this.dictionary[key];
  65. delete this.dictionary[key];
  66. item.resolve(item.value);
  67. }
  68. }
  69.  
  70. console.log('before async result ');
  71. let res = await this.counter.proxy.AllHello1("Всем Привет");
  72. console.log('Всем привет ' + res);
  73. res = await this.counter.proxy.AllHello2();
  74. console.log('func ' + res);
  75. res = await this.counter.proxy.AllHello3;
  76. console.log('property ' + res);
  77. });
Add Comment
Please, Sign In to add comment