Platform-Node.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. define(["amber/boot", "require", "amber/core/Kernel-Objects"], function($boot,requirejs){"use strict";
  2. var $core=$boot.api,nil=$boot.nilAsValue,$nil=$boot.nilAsReceiver,$recv=$boot.asReceiver,$globals=$boot.globals;
  3. var $pkg = $core.addPackage("Platform-Node");
  4. $pkg.innerEval = function (expr) { return eval(expr); };
  5. $pkg.imports = ["amber/core/Platform-Services"];
  6. //>>excludeStart("imports", pragmas.excludeImports);
  7. $pkg.isReady = new Promise(function (resolve, reject) { requirejs(["amber/core/Platform-Services"], function () {resolve();}, reject); });
  8. //>>excludeEnd("imports");
  9. $pkg.transport = {"type":"amd","amdNamespace":"amber/core"};
  10. $core.addClass("NodePlatform", $globals.Object, [], "Platform-Node");
  11. //>>excludeStart("ide", pragmas.excludeIdeData);
  12. $globals.NodePlatform.comment="I am `Platform` service implementation for node-like environment.";
  13. //>>excludeEnd("ide");
  14. $core.addMethod(
  15. $core.method({
  16. selector: "fetch:",
  17. protocol: "public API",
  18. //>>excludeStart("ide", pragmas.excludeIdeData);
  19. args: ["aStringOrObject"],
  20. source: "fetch: aStringOrObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
  21. referencedClasses: ["Promise"],
  22. //>>excludeEnd("ide");
  23. pragmas: [],
  24. messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "signal:"]
  25. }, function ($methodClass){ return function (aStringOrObject){
  26. var self=this,$self=this;
  27. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  28. return $core.withContext(function($ctx1) {
  29. //>>excludeEnd("ctx");
  30. return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
  31. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  32. return $core.withContext(function($ctx2) {
  33. //>>excludeEnd("ctx");
  34. return $recv(fetch)._value_(aStringOrObject);
  35. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  36. }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
  37. //>>excludeEnd("ctx");
  38. }),(function(){
  39. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  40. return $core.withContext(function($ctx2) {
  41. //>>excludeEnd("ctx");
  42. return $recv($globals.Promise)._signal_("fetch not available.");
  43. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  44. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
  45. //>>excludeEnd("ctx");
  46. }));
  47. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  48. }, function($ctx1) {$ctx1.fill(self,"fetch:",{aStringOrObject:aStringOrObject})});
  49. //>>excludeEnd("ctx");
  50. }; }),
  51. $globals.NodePlatform);
  52. $core.addMethod(
  53. $core.method({
  54. selector: "fetchUrl:options:",
  55. protocol: "public API",
  56. //>>excludeStart("ide", pragmas.excludeIdeData);
  57. args: ["aString", "anObject"],
  58. source: "fetchUrl: aString options: anObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
  59. referencedClasses: ["Promise"],
  60. //>>excludeEnd("ide");
  61. pragmas: [],
  62. messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "signal:"]
  63. }, function ($methodClass){ return function (aString,anObject){
  64. var self=this,$self=this;
  65. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  66. return $core.withContext(function($ctx1) {
  67. //>>excludeEnd("ctx");
  68. return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
  69. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  70. return $core.withContext(function($ctx2) {
  71. //>>excludeEnd("ctx");
  72. return $recv(fetch)._value_value_(aString,anObject);
  73. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  74. }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
  75. //>>excludeEnd("ctx");
  76. }),(function(){
  77. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  78. return $core.withContext(function($ctx2) {
  79. //>>excludeEnd("ctx");
  80. return $recv($globals.Promise)._signal_("fetch not available.");
  81. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  82. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
  83. //>>excludeEnd("ctx");
  84. }));
  85. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  86. }, function($ctx1) {$ctx1.fill(self,"fetchUrl:options:",{aString:aString,anObject:anObject})});
  87. //>>excludeEnd("ctx");
  88. }; }),
  89. $globals.NodePlatform);
  90. $core.addMethod(
  91. $core.method({
  92. selector: "globals",
  93. protocol: "accessing",
  94. //>>excludeStart("ide", pragmas.excludeIdeData);
  95. args: [],
  96. source: "globals\x0a\x09^ global",
  97. referencedClasses: [],
  98. //>>excludeEnd("ide");
  99. pragmas: [],
  100. messageSends: []
  101. }, function ($methodClass){ return function (){
  102. var self=this,$self=this;
  103. return global;
  104. }; }),
  105. $globals.NodePlatform);
  106. $core.addMethod(
  107. $core.method({
  108. selector: "newXhr",
  109. protocol: "public API",
  110. //>>excludeStart("ide", pragmas.excludeIdeData);
  111. args: [],
  112. source: "newXhr\x0a\x09XMLHttpRequest\x0a\x09\x09ifNotNil: [ ^ XMLHttpRequest new ]\x0a\x09\x09ifNil: [ self error: 'XMLHttpRequest not available.' ]",
  113. referencedClasses: ["XMLHttpRequest"],
  114. //>>excludeEnd("ide");
  115. pragmas: [],
  116. messageSends: ["ifNotNil:ifNil:", "new", "error:"]
  117. }, function ($methodClass){ return function (){
  118. var self=this,$self=this;
  119. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  120. return $core.withContext(function($ctx1) {
  121. //>>excludeEnd("ctx");
  122. var $1;
  123. $1=$globals.XMLHttpRequest;
  124. if($1 == null || $1.a$nil){
  125. $self._error_("XMLHttpRequest not available.");
  126. } else {
  127. return $recv($globals.XMLHttpRequest)._new();
  128. }
  129. return self;
  130. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  131. }, function($ctx1) {$ctx1.fill(self,"newXhr",{})});
  132. //>>excludeEnd("ctx");
  133. }; }),
  134. $globals.NodePlatform);
  135. $core.addMethod(
  136. $core.method({
  137. selector: "initialize",
  138. protocol: "testing",
  139. //>>excludeStart("ide", pragmas.excludeIdeData);
  140. args: [],
  141. source: "initialize\x0a\x09self isFeasible ifTrue: [ Platform registerIfNone: self new ]",
  142. referencedClasses: ["Platform"],
  143. //>>excludeEnd("ide");
  144. pragmas: [],
  145. messageSends: ["ifTrue:", "isFeasible", "registerIfNone:", "new"]
  146. }, function ($methodClass){ return function (){
  147. var self=this,$self=this;
  148. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  149. return $core.withContext(function($ctx1) {
  150. //>>excludeEnd("ctx");
  151. if($core.assert($self._isFeasible())){
  152. $recv($globals.Platform)._registerIfNone_($self._new());
  153. }
  154. return self;
  155. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  156. }, function($ctx1) {$ctx1.fill(self,"initialize",{})});
  157. //>>excludeEnd("ctx");
  158. }; }),
  159. $globals.NodePlatform.a$cls);
  160. $core.addMethod(
  161. $core.method({
  162. selector: "isFeasible",
  163. protocol: "testing",
  164. //>>excludeStart("ide", pragmas.excludeIdeData);
  165. args: [],
  166. source: "isFeasible\x0a<inlineJS: 'return typeof process !== \x22undefined\x22 && process && process.versions && process.versions.node != null'>",
  167. referencedClasses: [],
  168. //>>excludeEnd("ide");
  169. pragmas: [["inlineJS:", ["return typeof process !== \x22undefined\x22 && process && process.versions && process.versions.node != null"]]],
  170. messageSends: []
  171. }, function ($methodClass){ return function (){
  172. var self=this,$self=this;
  173. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  174. return $core.withContext(function($ctx1) {
  175. //>>excludeEnd("ctx");
  176. return typeof process !== "undefined" && process && process.versions && process.versions.node != null;
  177. return self;
  178. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  179. }, function($ctx1) {$ctx1.fill(self,"isFeasible",{})});
  180. //>>excludeEnd("ctx");
  181. }; }),
  182. $globals.NodePlatform.a$cls);
  183. });