promisedemo2.lpr 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. library promisedemo;
  2. {$mode objfpc}
  3. {$h+}
  4. uses nothreads, sysutils, job.js, variants;
  5. Type
  6. TFunction = function (const arguments: Variant): Variant of object;
  7. IJSWindow = interface(IJSObject)
  8. function setTimeout(const aHandler: TFunction; aTimeout: LongInt; const aArguments: Variant): LongInt{; ToDo:varargs};
  9. function setTimeout(const aHandler: TFunction): LongInt{; ToDo:varargs};
  10. function setTimeout(const aHandler: UnicodeString; aTimeout: LongInt; const aUnused: Variant): LongInt{; ToDo:varargs};
  11. function setTimeout(const aHandler: UnicodeString): LongInt{; ToDo:varargs};
  12. end;
  13. TJSWindow = class(TJSObject,IJSWindow)
  14. function setTimeout(const aHandler: TFunction; aTimeout: LongInt; const aArguments: Variant): LongInt{; ToDo:varargs};
  15. function setTimeout(const aHandler: TFunction): LongInt{; ToDo:varargs};
  16. function setTimeout(const aHandler: UnicodeString; aTimeout: LongInt; const aUnused: Variant): LongInt{; ToDo:varargs};
  17. function setTimeout(const aHandler: UnicodeString): LongInt{; ToDo:varargs};
  18. end;
  19. TApp = Class(TObject)
  20. private
  21. FOnResolve: TJSPromiseResolver;
  22. FOnReject: TJSPromiseResolver;
  23. public
  24. function DoTimeout(const arguments: Variant): Variant;
  25. function DoResolve(const aValue: Variant): Variant;
  26. procedure DoPromiseExecutor(const OnResolve, OnReject:TJSPromiseResolver);
  27. function ResolveTest: TJSPromise;
  28. procedure Run;
  29. end;
  30. var
  31. JSWindow : IJSWindow;
  32. function JOBCallFunction_(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
  33. var
  34. arguments: Variant;
  35. begin
  36. arguments:=H.GetVariant;
  37. Result:=H.AllocVariant(TFunction(aMethod)(arguments));
  38. end;
  39. function TJSWindow.setTimeout(const aHandler: TFunction; aTimeout: LongInt; const aArguments: Variant): LongInt{; ToDo:varargs};
  40. var
  41. m: TJOB_Method;
  42. begin
  43. m:=TJOB_Method.Create(TMethod(aHandler),@JOBCallFunction_);
  44. try
  45. Result:=InvokeJSLongIntResult('setTimeout',[m,aTimeout,aArguments]);
  46. finally
  47. m.free;
  48. end;
  49. end;
  50. function TJSWindow.setTimeout(const aHandler: TFunction): LongInt{; ToDo:varargs};
  51. var
  52. m: TJOB_Method;
  53. begin
  54. m:=TJOB_Method.Create(TMethod(aHandler),@JOBCallFunction_);
  55. try
  56. Result:=InvokeJSLongIntResult('setTimeout',[m]);
  57. finally
  58. m.free;
  59. end;
  60. end;
  61. function TJSWindow.setTimeout(const aHandler: UnicodeString; aTimeout: LongInt; const aUnused: Variant): LongInt{; ToDo:varargs};
  62. begin
  63. Result:=InvokeJSLongIntResult('setTimeout',[aHandler,aTimeout,aUnused]);
  64. end;
  65. function TJSWindow.setTimeout(const aHandler: UnicodeString): LongInt{; ToDo:varargs};
  66. begin
  67. Result:=InvokeJSLongIntResult('setTimeout',[aHandler]);
  68. end;
  69. { TApp }
  70. function TApp.DoTimeout(const arguments: Variant): Variant;
  71. begin
  72. if not Assigned(FOnResolve) then
  73. Writeln('Wasm ERROR: no resolve callback');
  74. if not Assigned(FOnReject) then
  75. Writeln('Wasm ERROR: no reject callback');
  76. if Assigned(FOnResolve) then
  77. FOnResolve('resolved');
  78. end;
  79. function TApp.DoResolve(const aValue: Variant): Variant;
  80. begin
  81. Writeln('Wasm: in DoResolve: success. Argument vartype: ', vartype(aValue));
  82. if vartype(aValue)=varOleStr then
  83. Writeln('Wasm: DoResolve received value: ', VarToStr(aValue));
  84. result:=unassigned;
  85. end;
  86. procedure TApp.DoPromiseExecutor(const OnResolve, OnReject: TJSPromiseResolver);
  87. begin
  88. FOnResolve := OnResolve;
  89. FOnReject := OnReject;
  90. JSWindow.setTimeout(@DoTimeout, 1000, nil);
  91. end;
  92. function TApp.ResolveTest: TJSPromise;
  93. begin
  94. Result:=TJSPromise.Create(@DoPromiseExecutor);
  95. end;
  96. procedure TApp.Run;
  97. Var
  98. P : TJSPromise;
  99. begin
  100. try
  101. P:=ResolveTest;
  102. P._then(@DoResolve);
  103. except
  104. on E: Exception do
  105. Writeln(e.Message);
  106. end;
  107. end;
  108. var
  109. App : TApp;
  110. begin
  111. JSWindow:=TJSWindow.JOBCreateGlobal('window');
  112. App:=TApp.Create;
  113. App.Run;
  114. end.