@nice2dev/game-engine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (519) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE +21 -0
  3. package/README.md +105 -0
  4. package/dist/cjs/accessibility/Accessibility.js +162 -0
  5. package/dist/cjs/accessibility/Accessibility.js.map +1 -0
  6. package/dist/cjs/ai/AI.js +321 -0
  7. package/dist/cjs/ai/AI.js.map +1 -0
  8. package/dist/cjs/animation/Animation.js +233 -0
  9. package/dist/cjs/animation/Animation.js.map +1 -0
  10. package/dist/cjs/audio/AudioAdvanced.js +262 -0
  11. package/dist/cjs/audio/AudioAdvanced.js.map +1 -0
  12. package/dist/cjs/audio/AudioManager.js +248 -0
  13. package/dist/cjs/audio/AudioManager.js.map +1 -0
  14. package/dist/cjs/core/EventBus.js +63 -0
  15. package/dist/cjs/core/EventBus.js.map +1 -0
  16. package/dist/cjs/core/GameClock.js +70 -0
  17. package/dist/cjs/core/GameClock.js.map +1 -0
  18. package/dist/cjs/core/GameConfig.js +86 -0
  19. package/dist/cjs/core/GameConfig.js.map +1 -0
  20. package/dist/cjs/core/GameLoop.js +93 -0
  21. package/dist/cjs/core/GameLoop.js.map +1 -0
  22. package/dist/cjs/core/ServiceLocator.js +73 -0
  23. package/dist/cjs/core/ServiceLocator.js.map +1 -0
  24. package/dist/cjs/core/Validation.js +119 -0
  25. package/dist/cjs/core/Validation.js.map +1 -0
  26. package/dist/cjs/core/math.js +116 -0
  27. package/dist/cjs/core/math.js.map +1 -0
  28. package/dist/cjs/devtools/DevTools.js +560 -0
  29. package/dist/cjs/devtools/DevTools.js.map +1 -0
  30. package/dist/cjs/devtools/DeveloperExperience.js +562 -0
  31. package/dist/cjs/devtools/DeveloperExperience.js.map +1 -0
  32. package/dist/cjs/docs/DocGenerator.js +357 -0
  33. package/dist/cjs/docs/DocGenerator.js.map +1 -0
  34. package/dist/cjs/ecs/World.js +280 -0
  35. package/dist/cjs/ecs/World.js.map +1 -0
  36. package/dist/cjs/editor/AdvancedEditor.js +149 -0
  37. package/dist/cjs/editor/AdvancedEditor.js.map +1 -0
  38. package/dist/cjs/editor/AssetManager.js +190 -0
  39. package/dist/cjs/editor/AssetManager.js.map +1 -0
  40. package/dist/cjs/editor/DebugTools.js +187 -0
  41. package/dist/cjs/editor/DebugTools.js.map +1 -0
  42. package/dist/cjs/editor/NiceGameEditor.js +361 -0
  43. package/dist/cjs/editor/NiceGameEditor.js.map +1 -0
  44. package/dist/cjs/editor/SceneEditor.js +223 -0
  45. package/dist/cjs/editor/SceneEditor.js.map +1 -0
  46. package/dist/cjs/engine/NiceGameEngine.js +172 -0
  47. package/dist/cjs/engine/NiceGameEngine.js.map +1 -0
  48. package/dist/cjs/enterprise/Enterprise.js +258 -0
  49. package/dist/cjs/enterprise/Enterprise.js.map +1 -0
  50. package/dist/cjs/i18n/I18n.js +634 -0
  51. package/dist/cjs/i18n/I18n.js.map +1 -0
  52. package/dist/cjs/i18n/useTranslation.js +100 -0
  53. package/dist/cjs/i18n/useTranslation.js.map +1 -0
  54. package/dist/cjs/index.js +526 -0
  55. package/dist/cjs/index.js.map +1 -0
  56. package/dist/cjs/input/GamepadNavigation.js +181 -0
  57. package/dist/cjs/input/GamepadNavigation.js.map +1 -0
  58. package/dist/cjs/input/InputManager.js +404 -0
  59. package/dist/cjs/input/InputManager.js.map +1 -0
  60. package/dist/cjs/input/useGamepads.js +86 -0
  61. package/dist/cjs/input/useGamepads.js.map +1 -0
  62. package/dist/cjs/kids/KidMode.js +670 -0
  63. package/dist/cjs/kids/KidMode.js.map +1 -0
  64. package/dist/cjs/kids/KidTools.js +533 -0
  65. package/dist/cjs/kids/KidTools.js.map +1 -0
  66. package/dist/cjs/monetization/Monetization.js +197 -0
  67. package/dist/cjs/monetization/Monetization.js.map +1 -0
  68. package/dist/cjs/multiplayer/LocalMultiplayer.js +288 -0
  69. package/dist/cjs/multiplayer/LocalMultiplayer.js.map +1 -0
  70. package/dist/cjs/multiplayer/MiniGameTypes.js +70 -0
  71. package/dist/cjs/multiplayer/MiniGameTypes.js.map +1 -0
  72. package/dist/cjs/network/MultiplayerTransport.js +109 -0
  73. package/dist/cjs/network/MultiplayerTransport.js.map +1 -0
  74. package/dist/cjs/network/Networking.js +569 -0
  75. package/dist/cjs/network/Networking.js.map +1 -0
  76. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AbortController.js +32 -0
  77. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -0
  78. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js +51 -0
  79. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -0
  80. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js +46 -0
  81. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -0
  82. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js +18 -0
  83. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -0
  84. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Errors.js +145 -0
  85. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -0
  86. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js +161 -0
  87. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -0
  88. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js +56 -0
  89. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -0
  90. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js +11 -0
  91. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -0
  92. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpClient.js +52 -0
  93. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -0
  94. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js +576 -0
  95. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -0
  96. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnection.js +956 -0
  97. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -0
  98. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js +149 -0
  99. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -0
  100. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js +25 -0
  101. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -0
  102. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ILogger.js +27 -0
  103. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -0
  104. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ITransport.js +26 -0
  105. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -0
  106. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js +124 -0
  107. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -0
  108. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Loggers.js +17 -0
  109. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -0
  110. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js +188 -0
  111. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -0
  112. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js +199 -0
  113. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -0
  114. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js +108 -0
  115. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -0
  116. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Subject.js +38 -0
  117. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -0
  118. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js +24 -0
  119. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -0
  120. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Utils.js +264 -0
  121. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -0
  122. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js +160 -0
  123. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -0
  124. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js +88 -0
  125. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -0
  126. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/index.js +50 -0
  127. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/index.js.map +1 -0
  128. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/pkg-version.js +6 -0
  129. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -0
  130. package/dist/cjs/pathfinding/Pathfinding.js +255 -0
  131. package/dist/cjs/pathfinding/Pathfinding.js.map +1 -0
  132. package/dist/cjs/performance/BenchmarkSuite.js +318 -0
  133. package/dist/cjs/performance/BenchmarkSuite.js.map +1 -0
  134. package/dist/cjs/performance/Performance.js +180 -0
  135. package/dist/cjs/performance/Performance.js.map +1 -0
  136. package/dist/cjs/performance/PerformanceAdvanced.js +628 -0
  137. package/dist/cjs/performance/PerformanceAdvanced.js.map +1 -0
  138. package/dist/cjs/physics/PhysicsAdvanced.js +432 -0
  139. package/dist/cjs/physics/PhysicsAdvanced.js.map +1 -0
  140. package/dist/cjs/physics/PhysicsEngine2D.js +445 -0
  141. package/dist/cjs/physics/PhysicsEngine2D.js.map +1 -0
  142. package/dist/cjs/plugins/PluginSDK.js +488 -0
  143. package/dist/cjs/plugins/PluginSDK.js.map +1 -0
  144. package/dist/cjs/plugins/PluginTestKit.js +368 -0
  145. package/dist/cjs/plugins/PluginTestKit.js.map +1 -0
  146. package/dist/cjs/procedural/Procedural.js +288 -0
  147. package/dist/cjs/procedural/Procedural.js.map +1 -0
  148. package/dist/cjs/procedural/WorldBuilding.js +246 -0
  149. package/dist/cjs/procedural/WorldBuilding.js.map +1 -0
  150. package/dist/cjs/release/MobileExport.js +149 -0
  151. package/dist/cjs/release/MobileExport.js.map +1 -0
  152. package/dist/cjs/release/Release.js +151 -0
  153. package/dist/cjs/release/Release.js.map +1 -0
  154. package/dist/cjs/rendering/Camera2D.js +129 -0
  155. package/dist/cjs/rendering/Camera2D.js.map +1 -0
  156. package/dist/cjs/rendering/Renderer2D.js +337 -0
  157. package/dist/cjs/rendering/Renderer2D.js.map +1 -0
  158. package/dist/cjs/runtime3d/Runtime3D.js +381 -0
  159. package/dist/cjs/runtime3d/Runtime3D.js.map +1 -0
  160. package/dist/cjs/runtime3d/SceneEditor3D.js +316 -0
  161. package/dist/cjs/runtime3d/SceneEditor3D.js.map +1 -0
  162. package/dist/cjs/scene/SceneManager.js +186 -0
  163. package/dist/cjs/scene/SceneManager.js.map +1 -0
  164. package/dist/cjs/scripting/NodeGraph.js +573 -0
  165. package/dist/cjs/scripting/NodeGraph.js.map +1 -0
  166. package/dist/cjs/social/Social.js +91 -0
  167. package/dist/cjs/social/Social.js.map +1 -0
  168. package/dist/cjs/templates/ActionTemplates.js +229 -0
  169. package/dist/cjs/templates/ActionTemplates.js.map +1 -0
  170. package/dist/cjs/templates/PartyTemplates.js +221 -0
  171. package/dist/cjs/templates/PartyTemplates.js.map +1 -0
  172. package/dist/cjs/templates/PuzzleTemplates.js +283 -0
  173. package/dist/cjs/templates/PuzzleTemplates.js.map +1 -0
  174. package/dist/cjs/templates/RPGTemplates.js +209 -0
  175. package/dist/cjs/templates/RPGTemplates.js.map +1 -0
  176. package/dist/cjs/templates/SportsTemplates.js +272 -0
  177. package/dist/cjs/templates/SportsTemplates.js.map +1 -0
  178. package/dist/cjs/templates/StrategyTemplates.js +173 -0
  179. package/dist/cjs/templates/StrategyTemplates.js.map +1 -0
  180. package/dist/cjs/templates/WaveDefense.js +470 -0
  181. package/dist/cjs/templates/WaveDefense.js.map +1 -0
  182. package/dist/cjs/tilemap/Tilemap.js +208 -0
  183. package/dist/cjs/tilemap/Tilemap.js.map +1 -0
  184. package/dist/cjs/tutorials/TutorialBranching.js +392 -0
  185. package/dist/cjs/tutorials/TutorialBranching.js.map +1 -0
  186. package/dist/cjs/tutorials/TutorialEngine.js +903 -0
  187. package/dist/cjs/tutorials/TutorialEngine.js.map +1 -0
  188. package/dist/cjs/tutorials/TutorialOverlay.js +602 -0
  189. package/dist/cjs/tutorials/TutorialOverlay.js.map +1 -0
  190. package/dist/cjs/tutorials/content/Tutorials_T05_T08.js +816 -0
  191. package/dist/cjs/tutorials/content/Tutorials_T05_T08.js.map +1 -0
  192. package/dist/cjs/tutorials/content/Tutorials_T09_T11.js +741 -0
  193. package/dist/cjs/tutorials/content/Tutorials_T09_T11.js.map +1 -0
  194. package/dist/cjs/tutorials/content/Tutorials_T12_T15.js +786 -0
  195. package/dist/cjs/tutorials/content/Tutorials_T12_T15.js.map +1 -0
  196. package/dist/cjs/ui/GameUI.js +312 -0
  197. package/dist/cjs/ui/GameUI.js.map +1 -0
  198. package/dist/cjs/xr/ARVR.js +197 -0
  199. package/dist/cjs/xr/ARVR.js.map +1 -0
  200. package/dist/esm/accessibility/Accessibility.js +153 -0
  201. package/dist/esm/accessibility/Accessibility.js.map +1 -0
  202. package/dist/esm/ai/AI.js +302 -0
  203. package/dist/esm/ai/AI.js.map +1 -0
  204. package/dist/esm/animation/Animation.js +227 -0
  205. package/dist/esm/animation/Animation.js.map +1 -0
  206. package/dist/esm/audio/AudioAdvanced.js +243 -0
  207. package/dist/esm/audio/AudioAdvanced.js.map +1 -0
  208. package/dist/esm/audio/AudioManager.js +246 -0
  209. package/dist/esm/audio/AudioManager.js.map +1 -0
  210. package/dist/esm/core/EventBus.js +61 -0
  211. package/dist/esm/core/EventBus.js.map +1 -0
  212. package/dist/esm/core/GameClock.js +68 -0
  213. package/dist/esm/core/GameClock.js.map +1 -0
  214. package/dist/esm/core/GameConfig.js +80 -0
  215. package/dist/esm/core/GameConfig.js.map +1 -0
  216. package/dist/esm/core/GameLoop.js +91 -0
  217. package/dist/esm/core/GameLoop.js.map +1 -0
  218. package/dist/esm/core/ServiceLocator.js +70 -0
  219. package/dist/esm/core/ServiceLocator.js.map +1 -0
  220. package/dist/esm/core/Validation.js +108 -0
  221. package/dist/esm/core/Validation.js.map +1 -0
  222. package/dist/esm/core/math.js +114 -0
  223. package/dist/esm/core/math.js.map +1 -0
  224. package/dist/esm/devtools/DevTools.js +555 -0
  225. package/dist/esm/devtools/DevTools.js.map +1 -0
  226. package/dist/esm/devtools/DeveloperExperience.js +547 -0
  227. package/dist/esm/devtools/DeveloperExperience.js.map +1 -0
  228. package/dist/esm/docs/DocGenerator.js +353 -0
  229. package/dist/esm/docs/DocGenerator.js.map +1 -0
  230. package/dist/esm/ecs/World.js +276 -0
  231. package/dist/esm/ecs/World.js.map +1 -0
  232. package/dist/esm/editor/AdvancedEditor.js +141 -0
  233. package/dist/esm/editor/AdvancedEditor.js.map +1 -0
  234. package/dist/esm/editor/AssetManager.js +188 -0
  235. package/dist/esm/editor/AssetManager.js.map +1 -0
  236. package/dist/esm/editor/DebugTools.js +179 -0
  237. package/dist/esm/editor/DebugTools.js.map +1 -0
  238. package/dist/esm/editor/NiceGameEditor.js +355 -0
  239. package/dist/esm/editor/NiceGameEditor.js.map +1 -0
  240. package/dist/esm/editor/SceneEditor.js +209 -0
  241. package/dist/esm/editor/SceneEditor.js.map +1 -0
  242. package/dist/esm/engine/NiceGameEngine.js +170 -0
  243. package/dist/esm/engine/NiceGameEngine.js.map +1 -0
  244. package/dist/esm/enterprise/Enterprise.js +246 -0
  245. package/dist/esm/enterprise/Enterprise.js.map +1 -0
  246. package/dist/esm/i18n/I18n.js +622 -0
  247. package/dist/esm/i18n/I18n.js.map +1 -0
  248. package/dist/esm/i18n/useTranslation.js +95 -0
  249. package/dist/esm/i18n/useTranslation.js.map +1 -0
  250. package/dist/esm/index.js +71 -0
  251. package/dist/esm/index.js.map +1 -0
  252. package/dist/esm/input/GamepadNavigation.js +178 -0
  253. package/dist/esm/input/GamepadNavigation.js.map +1 -0
  254. package/dist/esm/input/InputManager.js +399 -0
  255. package/dist/esm/input/InputManager.js.map +1 -0
  256. package/dist/esm/input/useGamepads.js +83 -0
  257. package/dist/esm/input/useGamepads.js.map +1 -0
  258. package/dist/esm/kids/KidMode.js +663 -0
  259. package/dist/esm/kids/KidMode.js.map +1 -0
  260. package/dist/esm/kids/KidTools.js +529 -0
  261. package/dist/esm/kids/KidTools.js.map +1 -0
  262. package/dist/esm/monetization/Monetization.js +186 -0
  263. package/dist/esm/monetization/Monetization.js.map +1 -0
  264. package/dist/esm/multiplayer/LocalMultiplayer.js +284 -0
  265. package/dist/esm/multiplayer/LocalMultiplayer.js.map +1 -0
  266. package/dist/esm/multiplayer/MiniGameTypes.js +60 -0
  267. package/dist/esm/multiplayer/MiniGameTypes.js.map +1 -0
  268. package/dist/esm/network/MultiplayerTransport.js +106 -0
  269. package/dist/esm/network/MultiplayerTransport.js.map +1 -0
  270. package/dist/esm/network/Networking.js +561 -0
  271. package/dist/esm/network/Networking.js.map +1 -0
  272. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AbortController.js +30 -0
  273. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -0
  274. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js +49 -0
  275. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -0
  276. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js +44 -0
  277. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -0
  278. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js +16 -0
  279. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -0
  280. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Errors.js +136 -0
  281. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -0
  282. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js +159 -0
  283. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -0
  284. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js +54 -0
  285. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -0
  286. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js +9 -0
  287. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -0
  288. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpClient.js +49 -0
  289. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -0
  290. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js +573 -0
  291. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -0
  292. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnection.js +954 -0
  293. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -0
  294. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js +147 -0
  295. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -0
  296. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js +25 -0
  297. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -0
  298. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ILogger.js +27 -0
  299. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -0
  300. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ITransport.js +26 -0
  301. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -0
  302. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js +122 -0
  303. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -0
  304. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Loggers.js +15 -0
  305. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -0
  306. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js +186 -0
  307. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -0
  308. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js +197 -0
  309. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -0
  310. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js +106 -0
  311. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -0
  312. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Subject.js +36 -0
  313. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -0
  314. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js +22 -0
  315. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -0
  316. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Utils.js +249 -0
  317. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -0
  318. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js +158 -0
  319. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -0
  320. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js +86 -0
  321. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -0
  322. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/index.js +13 -0
  323. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/index.js.map +1 -0
  324. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/pkg-version.js +4 -0
  325. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -0
  326. package/dist/esm/pathfinding/Pathfinding.js +250 -0
  327. package/dist/esm/pathfinding/Pathfinding.js.map +1 -0
  328. package/dist/esm/performance/BenchmarkSuite.js +315 -0
  329. package/dist/esm/performance/BenchmarkSuite.js.map +1 -0
  330. package/dist/esm/performance/Performance.js +171 -0
  331. package/dist/esm/performance/Performance.js.map +1 -0
  332. package/dist/esm/performance/PerformanceAdvanced.js +620 -0
  333. package/dist/esm/performance/PerformanceAdvanced.js.map +1 -0
  334. package/dist/esm/physics/PhysicsAdvanced.js +417 -0
  335. package/dist/esm/physics/PhysicsAdvanced.js.map +1 -0
  336. package/dist/esm/physics/PhysicsEngine2D.js +442 -0
  337. package/dist/esm/physics/PhysicsEngine2D.js.map +1 -0
  338. package/dist/esm/plugins/PluginSDK.js +484 -0
  339. package/dist/esm/plugins/PluginSDK.js.map +1 -0
  340. package/dist/esm/plugins/PluginTestKit.js +359 -0
  341. package/dist/esm/plugins/PluginTestKit.js.map +1 -0
  342. package/dist/esm/procedural/Procedural.js +282 -0
  343. package/dist/esm/procedural/Procedural.js.map +1 -0
  344. package/dist/esm/procedural/WorldBuilding.js +225 -0
  345. package/dist/esm/procedural/WorldBuilding.js.map +1 -0
  346. package/dist/esm/release/MobileExport.js +142 -0
  347. package/dist/esm/release/MobileExport.js.map +1 -0
  348. package/dist/esm/release/Release.js +141 -0
  349. package/dist/esm/release/Release.js.map +1 -0
  350. package/dist/esm/rendering/Camera2D.js +127 -0
  351. package/dist/esm/rendering/Camera2D.js.map +1 -0
  352. package/dist/esm/rendering/Renderer2D.js +334 -0
  353. package/dist/esm/rendering/Renderer2D.js.map +1 -0
  354. package/dist/esm/runtime3d/Runtime3D.js +371 -0
  355. package/dist/esm/runtime3d/Runtime3D.js.map +1 -0
  356. package/dist/esm/runtime3d/SceneEditor3D.js +299 -0
  357. package/dist/esm/runtime3d/SceneEditor3D.js.map +1 -0
  358. package/dist/esm/scene/SceneManager.js +184 -0
  359. package/dist/esm/scene/SceneManager.js.map +1 -0
  360. package/dist/esm/scripting/NodeGraph.js +568 -0
  361. package/dist/esm/scripting/NodeGraph.js.map +1 -0
  362. package/dist/esm/social/Social.js +85 -0
  363. package/dist/esm/social/Social.js.map +1 -0
  364. package/dist/esm/templates/ActionTemplates.js +217 -0
  365. package/dist/esm/templates/ActionTemplates.js.map +1 -0
  366. package/dist/esm/templates/PartyTemplates.js +204 -0
  367. package/dist/esm/templates/PartyTemplates.js.map +1 -0
  368. package/dist/esm/templates/PuzzleTemplates.js +267 -0
  369. package/dist/esm/templates/PuzzleTemplates.js.map +1 -0
  370. package/dist/esm/templates/RPGTemplates.js +196 -0
  371. package/dist/esm/templates/RPGTemplates.js.map +1 -0
  372. package/dist/esm/templates/SportsTemplates.js +257 -0
  373. package/dist/esm/templates/SportsTemplates.js.map +1 -0
  374. package/dist/esm/templates/StrategyTemplates.js +157 -0
  375. package/dist/esm/templates/StrategyTemplates.js.map +1 -0
  376. package/dist/esm/templates/WaveDefense.js +467 -0
  377. package/dist/esm/templates/WaveDefense.js.map +1 -0
  378. package/dist/esm/tilemap/Tilemap.js +205 -0
  379. package/dist/esm/tilemap/Tilemap.js.map +1 -0
  380. package/dist/esm/tutorials/TutorialBranching.js +388 -0
  381. package/dist/esm/tutorials/TutorialBranching.js.map +1 -0
  382. package/dist/esm/tutorials/TutorialEngine.js +897 -0
  383. package/dist/esm/tutorials/TutorialEngine.js.map +1 -0
  384. package/dist/esm/tutorials/TutorialOverlay.js +600 -0
  385. package/dist/esm/tutorials/TutorialOverlay.js.map +1 -0
  386. package/dist/esm/tutorials/content/Tutorials_T05_T08.js +810 -0
  387. package/dist/esm/tutorials/content/Tutorials_T05_T08.js.map +1 -0
  388. package/dist/esm/tutorials/content/Tutorials_T09_T11.js +736 -0
  389. package/dist/esm/tutorials/content/Tutorials_T09_T11.js.map +1 -0
  390. package/dist/esm/tutorials/content/Tutorials_T12_T15.js +780 -0
  391. package/dist/esm/tutorials/content/Tutorials_T12_T15.js.map +1 -0
  392. package/dist/esm/ui/GameUI.js +310 -0
  393. package/dist/esm/ui/GameUI.js.map +1 -0
  394. package/dist/esm/xr/ARVR.js +182 -0
  395. package/dist/esm/xr/ARVR.js.map +1 -0
  396. package/dist/types/__tests__/setup.d.ts +1 -0
  397. package/dist/types/accessibility/Accessibility.d.ts +82 -0
  398. package/dist/types/accessibility/index.d.ts +1 -0
  399. package/dist/types/ai/AI.d.ts +140 -0
  400. package/dist/types/ai/index.d.ts +1 -0
  401. package/dist/types/animation/Animation.d.ts +90 -0
  402. package/dist/types/animation/index.d.ts +1 -0
  403. package/dist/types/audio/AudioAdvanced.d.ts +190 -0
  404. package/dist/types/audio/AudioManager.d.ts +55 -0
  405. package/dist/types/audio/index.d.ts +1 -0
  406. package/dist/types/community/Community.d.ts +112 -0
  407. package/dist/types/community/index.d.ts +1 -0
  408. package/dist/types/core/EventBus.d.ts +16 -0
  409. package/dist/types/core/GameClock.d.ts +29 -0
  410. package/dist/types/core/GameConfig.d.ts +20 -0
  411. package/dist/types/core/GameLoop.d.ts +32 -0
  412. package/dist/types/core/ServiceLocator.d.ts +36 -0
  413. package/dist/types/core/TypeSafety.d.ts +190 -0
  414. package/dist/types/core/Validation.d.ts +24 -0
  415. package/dist/types/core/index.d.ts +7 -0
  416. package/dist/types/core/math.d.ts +32 -0
  417. package/dist/types/core/types.d.ts +130 -0
  418. package/dist/types/devtools/AssetStore.d.ts +133 -0
  419. package/dist/types/devtools/DevTools.d.ts +217 -0
  420. package/dist/types/devtools/DeveloperExperience.d.ts +123 -0
  421. package/dist/types/devtools/index.d.ts +3 -0
  422. package/dist/types/docs/APIPlayground.d.ts +26 -0
  423. package/dist/types/docs/Cookbook.d.ts +18 -0
  424. package/dist/types/docs/DocGenerator.d.ts +83 -0
  425. package/dist/types/docs/Guides.d.ts +18 -0
  426. package/dist/types/docs/index.d.ts +4 -0
  427. package/dist/types/ecs/World.d.ts +219 -0
  428. package/dist/types/ecs/index.d.ts +1 -0
  429. package/dist/types/editor/AdvancedEditor.d.ts +267 -0
  430. package/dist/types/editor/AssetManager.d.ts +45 -0
  431. package/dist/types/editor/DebugTools.d.ts +74 -0
  432. package/dist/types/editor/NiceGameEditor.d.ts +115 -0
  433. package/dist/types/editor/SceneEditor.d.ts +37 -0
  434. package/dist/types/editor/index.d.ts +8 -0
  435. package/dist/types/engine/NiceGameEngine.d.ts +53 -0
  436. package/dist/types/engine/index.d.ts +2 -0
  437. package/dist/types/enterprise/Enterprise.d.ts +217 -0
  438. package/dist/types/enterprise/index.d.ts +1 -0
  439. package/dist/types/epic/index.d.ts +10 -0
  440. package/dist/types/i18n/I18n.d.ts +107 -0
  441. package/dist/types/i18n/index.d.ts +2 -0
  442. package/dist/types/i18n/useTranslation.d.ts +74 -0
  443. package/dist/types/index.d.ts +127 -0
  444. package/dist/types/input/GamepadNavigation.d.ts +34 -0
  445. package/dist/types/input/InputManager.d.ts +77 -0
  446. package/dist/types/input/index.d.ts +3 -0
  447. package/dist/types/input/useGamepads.d.ts +39 -0
  448. package/dist/types/kids/KidMode.d.ts +207 -0
  449. package/dist/types/kids/KidTools.d.ts +135 -0
  450. package/dist/types/kids/index.d.ts +2 -0
  451. package/dist/types/legendary/index.d.ts +57 -0
  452. package/dist/types/monetization/Monetization.d.ts +224 -0
  453. package/dist/types/monetization/index.d.ts +1 -0
  454. package/dist/types/multiplayer/LocalMultiplayer.d.ts +61 -0
  455. package/dist/types/multiplayer/MiniGameTypes.d.ts +70 -0
  456. package/dist/types/multiplayer/index.d.ts +3 -0
  457. package/dist/types/network/MultiplayerTransport.d.ts +71 -0
  458. package/dist/types/network/Networking.d.ts +199 -0
  459. package/dist/types/network/index.d.ts +2 -0
  460. package/dist/types/pathfinding/Pathfinding.d.ts +42 -0
  461. package/dist/types/pathfinding/index.d.ts +1 -0
  462. package/dist/types/performance/AssetCompression.d.ts +137 -0
  463. package/dist/types/performance/BenchmarkSuite.d.ts +99 -0
  464. package/dist/types/performance/Performance.d.ts +87 -0
  465. package/dist/types/performance/PerformanceAdvanced.d.ts +220 -0
  466. package/dist/types/performance/WASMModules.d.ts +65 -0
  467. package/dist/types/performance/WebGPUCompute.d.ts +164 -0
  468. package/dist/types/performance/index.d.ts +1 -0
  469. package/dist/types/physics/PhysicsAdvanced.d.ts +148 -0
  470. package/dist/types/physics/PhysicsEngine2D.d.ts +66 -0
  471. package/dist/types/physics/index.d.ts +1 -0
  472. package/dist/types/plugins/PluginSDK.d.ts +134 -0
  473. package/dist/types/plugins/PluginTestKit.d.ts +107 -0
  474. package/dist/types/plugins/index.d.ts +2 -0
  475. package/dist/types/procedural/Procedural.d.ts +90 -0
  476. package/dist/types/procedural/WorldBuilding.d.ts +200 -0
  477. package/dist/types/procedural/index.d.ts +2 -0
  478. package/dist/types/release/MobileExport.d.ts +218 -0
  479. package/dist/types/release/Release.d.ts +81 -0
  480. package/dist/types/release/index.d.ts +1 -0
  481. package/dist/types/rendering/Camera2D.d.ts +39 -0
  482. package/dist/types/rendering/Renderer2D.d.ts +41 -0
  483. package/dist/types/rendering/index.d.ts +2 -0
  484. package/dist/types/runtime3d/Runtime3D.d.ts +259 -0
  485. package/dist/types/runtime3d/SceneEditor3D.d.ts +186 -0
  486. package/dist/types/runtime3d/index.d.ts +2 -0
  487. package/dist/types/scene/SceneManager.d.ts +59 -0
  488. package/dist/types/scene/index.d.ts +2 -0
  489. package/dist/types/scripting/NodeGraph.d.ts +84 -0
  490. package/dist/types/scripting/index.d.ts +2 -0
  491. package/dist/types/social/Social.d.ts +245 -0
  492. package/dist/types/social/index.d.ts +1 -0
  493. package/dist/types/templates/ActionTemplates.d.ts +173 -0
  494. package/dist/types/templates/PartyTemplates.d.ts +195 -0
  495. package/dist/types/templates/PuzzleTemplates.d.ts +127 -0
  496. package/dist/types/templates/RPGTemplates.d.ts +210 -0
  497. package/dist/types/templates/SportsTemplates.d.ts +162 -0
  498. package/dist/types/templates/StrategyTemplates.d.ts +264 -0
  499. package/dist/types/templates/WaveDefense.d.ts +92 -0
  500. package/dist/types/templates/index.d.ts +2 -0
  501. package/dist/types/tilemap/Tilemap.d.ts +66 -0
  502. package/dist/types/tilemap/index.d.ts +1 -0
  503. package/dist/types/tutorials/TutorialBranching.d.ts +132 -0
  504. package/dist/types/tutorials/TutorialEditor.d.ts +43 -0
  505. package/dist/types/tutorials/TutorialEngine.d.ts +237 -0
  506. package/dist/types/tutorials/TutorialOverlay.d.ts +79 -0
  507. package/dist/types/tutorials/VoiceNarration.d.ts +75 -0
  508. package/dist/types/tutorials/content/Tutorials_T04.d.ts +3 -0
  509. package/dist/types/tutorials/content/Tutorials_T05_T08.d.ts +6 -0
  510. package/dist/types/tutorials/content/Tutorials_T09_T11.d.ts +5 -0
  511. package/dist/types/tutorials/content/Tutorials_T12_T15.d.ts +6 -0
  512. package/dist/types/tutorials/content/UITutorials.d.ts +14 -0
  513. package/dist/types/tutorials/content/index.d.ts +5 -0
  514. package/dist/types/tutorials/index.d.ts +6 -0
  515. package/dist/types/ui/GameUI.d.ts +97 -0
  516. package/dist/types/ui/index.d.ts +2 -0
  517. package/dist/types/xr/ARVR.d.ts +252 -0
  518. package/dist/types/xr/index.d.ts +1 -0
  519. package/package.json +88 -0
@@ -0,0 +1,956 @@
1
+ 'use strict';
2
+
3
+ var HandshakeProtocol = require('./HandshakeProtocol.js');
4
+ var Errors = require('./Errors.js');
5
+ var IHubProtocol = require('./IHubProtocol.js');
6
+ var ILogger = require('./ILogger.js');
7
+ var Subject = require('./Subject.js');
8
+ var Utils = require('./Utils.js');
9
+ var MessageBuffer = require('./MessageBuffer.js');
10
+
11
+ // Licensed to the .NET Foundation under one or more agreements.
12
+ // The .NET Foundation licenses this file to you under the MIT license.
13
+ const DEFAULT_TIMEOUT_IN_MS = 30 * 1000;
14
+ const DEFAULT_PING_INTERVAL_IN_MS = 15 * 1000;
15
+ const DEFAULT_STATEFUL_RECONNECT_BUFFER_SIZE = 100000;
16
+ /** Describes the current state of the {@link HubConnection} to the server. */
17
+ exports.HubConnectionState = void 0;
18
+ (function (HubConnectionState) {
19
+ /** The hub connection is disconnected. */
20
+ HubConnectionState["Disconnected"] = "Disconnected";
21
+ /** The hub connection is connecting. */
22
+ HubConnectionState["Connecting"] = "Connecting";
23
+ /** The hub connection is connected. */
24
+ HubConnectionState["Connected"] = "Connected";
25
+ /** The hub connection is disconnecting. */
26
+ HubConnectionState["Disconnecting"] = "Disconnecting";
27
+ /** The hub connection is reconnecting. */
28
+ HubConnectionState["Reconnecting"] = "Reconnecting";
29
+ })(exports.HubConnectionState || (exports.HubConnectionState = {}));
30
+ /** Represents a connection to a SignalR Hub. */
31
+ class HubConnection {
32
+ /** @internal */
33
+ // Using a public static factory method means we can have a private constructor and an _internal_
34
+ // create method that can be used by HubConnectionBuilder. An "internal" constructor would just
35
+ // be stripped away and the '.d.ts' file would have no constructor, which is interpreted as a
36
+ // public parameter-less constructor.
37
+ static create(connection, logger, protocol, reconnectPolicy, serverTimeoutInMilliseconds, keepAliveIntervalInMilliseconds, statefulReconnectBufferSize) {
38
+ return new HubConnection(connection, logger, protocol, reconnectPolicy, serverTimeoutInMilliseconds, keepAliveIntervalInMilliseconds, statefulReconnectBufferSize);
39
+ }
40
+ constructor(connection, logger, protocol, reconnectPolicy, serverTimeoutInMilliseconds, keepAliveIntervalInMilliseconds, statefulReconnectBufferSize) {
41
+ this._nextKeepAlive = 0;
42
+ this._freezeEventListener = () => {
43
+ this._logger.log(ILogger.LogLevel.Warning, "The page is being frozen, this will likely lead to the connection being closed and messages being lost. For more information see the docs at https://learn.microsoft.com/aspnet/core/signalr/javascript-client#bsleep");
44
+ };
45
+ Utils.Arg.isRequired(connection, "connection");
46
+ Utils.Arg.isRequired(logger, "logger");
47
+ Utils.Arg.isRequired(protocol, "protocol");
48
+ this.serverTimeoutInMilliseconds = serverTimeoutInMilliseconds !== null && serverTimeoutInMilliseconds !== void 0 ? serverTimeoutInMilliseconds : DEFAULT_TIMEOUT_IN_MS;
49
+ this.keepAliveIntervalInMilliseconds = keepAliveIntervalInMilliseconds !== null && keepAliveIntervalInMilliseconds !== void 0 ? keepAliveIntervalInMilliseconds : DEFAULT_PING_INTERVAL_IN_MS;
50
+ this._statefulReconnectBufferSize = statefulReconnectBufferSize !== null && statefulReconnectBufferSize !== void 0 ? statefulReconnectBufferSize : DEFAULT_STATEFUL_RECONNECT_BUFFER_SIZE;
51
+ this._logger = logger;
52
+ this._protocol = protocol;
53
+ this.connection = connection;
54
+ this._reconnectPolicy = reconnectPolicy;
55
+ this._handshakeProtocol = new HandshakeProtocol.HandshakeProtocol();
56
+ this.connection.onreceive = (data) => this._processIncomingData(data);
57
+ this.connection.onclose = (error) => this._connectionClosed(error);
58
+ this._callbacks = {};
59
+ this._methods = {};
60
+ this._closedCallbacks = [];
61
+ this._reconnectingCallbacks = [];
62
+ this._reconnectedCallbacks = [];
63
+ this._invocationId = 0;
64
+ this._receivedHandshakeResponse = false;
65
+ this._connectionState = exports.HubConnectionState.Disconnected;
66
+ this._connectionStarted = false;
67
+ this._cachedPingMessage = this._protocol.writeMessage({ type: IHubProtocol.MessageType.Ping });
68
+ }
69
+ /** Indicates the state of the {@link HubConnection} to the server. */
70
+ get state() {
71
+ return this._connectionState;
72
+ }
73
+ /** Represents the connection id of the {@link HubConnection} on the server. The connection id will be null when the connection is either
74
+ * in the disconnected state or if the negotiation step was skipped.
75
+ */
76
+ get connectionId() {
77
+ return this.connection ? (this.connection.connectionId || null) : null;
78
+ }
79
+ /** Indicates the url of the {@link HubConnection} to the server. */
80
+ get baseUrl() {
81
+ return this.connection.baseUrl || "";
82
+ }
83
+ /**
84
+ * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
85
+ * Reconnecting states.
86
+ * @param {string} url The url to connect to.
87
+ */
88
+ set baseUrl(url) {
89
+ if (this._connectionState !== exports.HubConnectionState.Disconnected && this._connectionState !== exports.HubConnectionState.Reconnecting) {
90
+ throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
91
+ }
92
+ if (!url) {
93
+ throw new Error("The HubConnection url must be a valid url.");
94
+ }
95
+ this.connection.baseUrl = url;
96
+ }
97
+ /** Starts the connection.
98
+ *
99
+ * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
100
+ */
101
+ start() {
102
+ this._startPromise = this._startWithStateTransitions();
103
+ return this._startPromise;
104
+ }
105
+ async _startWithStateTransitions() {
106
+ if (this._connectionState !== exports.HubConnectionState.Disconnected) {
107
+ return Promise.reject(new Error("Cannot start a HubConnection that is not in the 'Disconnected' state."));
108
+ }
109
+ this._connectionState = exports.HubConnectionState.Connecting;
110
+ this._logger.log(ILogger.LogLevel.Debug, "Starting HubConnection.");
111
+ try {
112
+ await this._startInternal();
113
+ if (Utils.Platform.isBrowser) {
114
+ // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
115
+ window.document.addEventListener("freeze", this._freezeEventListener);
116
+ }
117
+ this._connectionState = exports.HubConnectionState.Connected;
118
+ this._connectionStarted = true;
119
+ this._logger.log(ILogger.LogLevel.Debug, "HubConnection connected successfully.");
120
+ }
121
+ catch (e) {
122
+ this._connectionState = exports.HubConnectionState.Disconnected;
123
+ this._logger.log(ILogger.LogLevel.Debug, `HubConnection failed to start successfully because of error '${e}'.`);
124
+ return Promise.reject(e);
125
+ }
126
+ }
127
+ async _startInternal() {
128
+ this._stopDuringStartError = undefined;
129
+ this._receivedHandshakeResponse = false;
130
+ // Set up the promise before any connection is (re)started otherwise it could race with received messages
131
+ const handshakePromise = new Promise((resolve, reject) => {
132
+ this._handshakeResolver = resolve;
133
+ this._handshakeRejecter = reject;
134
+ });
135
+ await this.connection.start(this._protocol.transferFormat);
136
+ try {
137
+ let version = this._protocol.version;
138
+ if (!this.connection.features.reconnect) {
139
+ // Stateful Reconnect starts with HubProtocol version 2, newer clients connecting to older servers will fail to connect due to
140
+ // the handshake only supporting version 1, so we will try to send version 1 during the handshake to keep old servers working.
141
+ version = 1;
142
+ }
143
+ const handshakeRequest = {
144
+ protocol: this._protocol.name,
145
+ version,
146
+ };
147
+ this._logger.log(ILogger.LogLevel.Debug, "Sending handshake request.");
148
+ await this._sendMessage(this._handshakeProtocol.writeHandshakeRequest(handshakeRequest));
149
+ this._logger.log(ILogger.LogLevel.Information, `Using HubProtocol '${this._protocol.name}'.`);
150
+ // defensively cleanup timeout in case we receive a message from the server before we finish start
151
+ this._cleanupTimeout();
152
+ this._resetTimeoutPeriod();
153
+ this._resetKeepAliveInterval();
154
+ await handshakePromise;
155
+ // It's important to check the stopDuringStartError instead of just relying on the handshakePromise
156
+ // being rejected on close, because this continuation can run after both the handshake completed successfully
157
+ // and the connection was closed.
158
+ if (this._stopDuringStartError) {
159
+ // It's important to throw instead of returning a rejected promise, because we don't want to allow any state
160
+ // transitions to occur between now and the calling code observing the exceptions. Returning a rejected promise
161
+ // will cause the calling continuation to get scheduled to run later.
162
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
163
+ throw this._stopDuringStartError;
164
+ }
165
+ const useStatefulReconnect = this.connection.features.reconnect || false;
166
+ if (useStatefulReconnect) {
167
+ this._messageBuffer = new MessageBuffer.MessageBuffer(this._protocol, this.connection, this._statefulReconnectBufferSize);
168
+ this.connection.features.disconnected = this._messageBuffer._disconnected.bind(this._messageBuffer);
169
+ this.connection.features.resend = () => {
170
+ if (this._messageBuffer) {
171
+ return this._messageBuffer._resend();
172
+ }
173
+ };
174
+ }
175
+ if (!this.connection.features.inherentKeepAlive) {
176
+ await this._sendMessage(this._cachedPingMessage);
177
+ }
178
+ }
179
+ catch (e) {
180
+ this._logger.log(ILogger.LogLevel.Debug, `Hub handshake failed with error '${e}' during start(). Stopping HubConnection.`);
181
+ this._cleanupTimeout();
182
+ this._cleanupPingTimer();
183
+ // HttpConnection.stop() should not complete until after the onclose callback is invoked.
184
+ // This will transition the HubConnection to the disconnected state before HttpConnection.stop() completes.
185
+ await this.connection.stop(e);
186
+ throw e;
187
+ }
188
+ }
189
+ /** Stops the connection.
190
+ *
191
+ * @returns {Promise<void>} A Promise that resolves when the connection has been successfully terminated, or rejects with an error.
192
+ */
193
+ async stop() {
194
+ // Capture the start promise before the connection might be restarted in an onclose callback.
195
+ const startPromise = this._startPromise;
196
+ this.connection.features.reconnect = false;
197
+ this._stopPromise = this._stopInternal();
198
+ await this._stopPromise;
199
+ try {
200
+ // Awaiting undefined continues immediately
201
+ await startPromise;
202
+ }
203
+ catch (e) {
204
+ // This exception is returned to the user as a rejected Promise from the start method.
205
+ }
206
+ }
207
+ _stopInternal(error) {
208
+ if (this._connectionState === exports.HubConnectionState.Disconnected) {
209
+ this._logger.log(ILogger.LogLevel.Debug, `Call to HubConnection.stop(${error}) ignored because it is already in the disconnected state.`);
210
+ return Promise.resolve();
211
+ }
212
+ if (this._connectionState === exports.HubConnectionState.Disconnecting) {
213
+ this._logger.log(ILogger.LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnecting state.`);
214
+ return this._stopPromise;
215
+ }
216
+ const state = this._connectionState;
217
+ this._connectionState = exports.HubConnectionState.Disconnecting;
218
+ this._logger.log(ILogger.LogLevel.Debug, "Stopping HubConnection.");
219
+ if (this._reconnectDelayHandle) {
220
+ // We're in a reconnect delay which means the underlying connection is currently already stopped.
221
+ // Just clear the handle to stop the reconnect loop (which no one is waiting on thankfully) and
222
+ // fire the onclose callbacks.
223
+ this._logger.log(ILogger.LogLevel.Debug, "Connection stopped during reconnect delay. Done reconnecting.");
224
+ clearTimeout(this._reconnectDelayHandle);
225
+ this._reconnectDelayHandle = undefined;
226
+ this._completeClose();
227
+ return Promise.resolve();
228
+ }
229
+ if (state === exports.HubConnectionState.Connected) {
230
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
231
+ this._sendCloseMessage();
232
+ }
233
+ this._cleanupTimeout();
234
+ this._cleanupPingTimer();
235
+ this._stopDuringStartError = error || new Errors.AbortError("The connection was stopped before the hub handshake could complete.");
236
+ // HttpConnection.stop() should not complete until after either HttpConnection.start() fails
237
+ // or the onclose callback is invoked. The onclose callback will transition the HubConnection
238
+ // to the disconnected state if need be before HttpConnection.stop() completes.
239
+ return this.connection.stop(error);
240
+ }
241
+ async _sendCloseMessage() {
242
+ try {
243
+ await this._sendWithProtocol(this._createCloseMessage());
244
+ }
245
+ catch {
246
+ // Ignore, this is a best effort attempt to let the server know the client closed gracefully.
247
+ }
248
+ }
249
+ /** Invokes a streaming hub method on the server using the specified name and arguments.
250
+ *
251
+ * @typeparam T The type of the items returned by the server.
252
+ * @param {string} methodName The name of the server method to invoke.
253
+ * @param {any[]} args The arguments used to invoke the server method.
254
+ * @returns {IStreamResult<T>} An object that yields results from the server as they are received.
255
+ */
256
+ stream(methodName, ...args) {
257
+ const [streams, streamIds] = this._replaceStreamingParams(args);
258
+ const invocationDescriptor = this._createStreamInvocation(methodName, args, streamIds);
259
+ // eslint-disable-next-line prefer-const
260
+ let promiseQueue;
261
+ const subject = new Subject.Subject();
262
+ subject.cancelCallback = () => {
263
+ const cancelInvocation = this._createCancelInvocation(invocationDescriptor.invocationId);
264
+ delete this._callbacks[invocationDescriptor.invocationId];
265
+ return promiseQueue.then(() => {
266
+ return this._sendWithProtocol(cancelInvocation);
267
+ });
268
+ };
269
+ this._callbacks[invocationDescriptor.invocationId] = (invocationEvent, error) => {
270
+ if (error) {
271
+ subject.error(error);
272
+ return;
273
+ }
274
+ else if (invocationEvent) {
275
+ // invocationEvent will not be null when an error is not passed to the callback
276
+ if (invocationEvent.type === IHubProtocol.MessageType.Completion) {
277
+ if (invocationEvent.error) {
278
+ subject.error(new Error(invocationEvent.error));
279
+ }
280
+ else {
281
+ subject.complete();
282
+ }
283
+ }
284
+ else {
285
+ subject.next((invocationEvent.item));
286
+ }
287
+ }
288
+ };
289
+ promiseQueue = this._sendWithProtocol(invocationDescriptor)
290
+ .catch((e) => {
291
+ subject.error(e);
292
+ delete this._callbacks[invocationDescriptor.invocationId];
293
+ });
294
+ this._launchStreams(streams, promiseQueue);
295
+ return subject;
296
+ }
297
+ _sendMessage(message) {
298
+ this._resetKeepAliveInterval();
299
+ return this.connection.send(message);
300
+ }
301
+ /**
302
+ * Sends a js object to the server.
303
+ * @param message The js object to serialize and send.
304
+ */
305
+ _sendWithProtocol(message) {
306
+ if (this._messageBuffer) {
307
+ return this._messageBuffer._send(message);
308
+ }
309
+ else {
310
+ return this._sendMessage(this._protocol.writeMessage(message));
311
+ }
312
+ }
313
+ /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver.
314
+ *
315
+ * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still
316
+ * be processing the invocation.
317
+ *
318
+ * @param {string} methodName The name of the server method to invoke.
319
+ * @param {any[]} args The arguments used to invoke the server method.
320
+ * @returns {Promise<void>} A Promise that resolves when the invocation has been successfully sent, or rejects with an error.
321
+ */
322
+ send(methodName, ...args) {
323
+ const [streams, streamIds] = this._replaceStreamingParams(args);
324
+ const sendPromise = this._sendWithProtocol(this._createInvocation(methodName, args, true, streamIds));
325
+ this._launchStreams(streams, sendPromise);
326
+ return sendPromise;
327
+ }
328
+ /** Invokes a hub method on the server using the specified name and arguments.
329
+ *
330
+ * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise
331
+ * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of
332
+ * resolving the Promise.
333
+ *
334
+ * @typeparam T The expected return type.
335
+ * @param {string} methodName The name of the server method to invoke.
336
+ * @param {any[]} args The arguments used to invoke the server method.
337
+ * @returns {Promise<T>} A Promise that resolves with the result of the server method (if any), or rejects with an error.
338
+ */
339
+ invoke(methodName, ...args) {
340
+ const [streams, streamIds] = this._replaceStreamingParams(args);
341
+ const invocationDescriptor = this._createInvocation(methodName, args, false, streamIds);
342
+ const p = new Promise((resolve, reject) => {
343
+ // invocationId will always have a value for a non-blocking invocation
344
+ this._callbacks[invocationDescriptor.invocationId] = (invocationEvent, error) => {
345
+ if (error) {
346
+ reject(error);
347
+ return;
348
+ }
349
+ else if (invocationEvent) {
350
+ // invocationEvent will not be null when an error is not passed to the callback
351
+ if (invocationEvent.type === IHubProtocol.MessageType.Completion) {
352
+ if (invocationEvent.error) {
353
+ reject(new Error(invocationEvent.error));
354
+ }
355
+ else {
356
+ resolve(invocationEvent.result);
357
+ }
358
+ }
359
+ else {
360
+ reject(new Error(`Unexpected message type: ${invocationEvent.type}`));
361
+ }
362
+ }
363
+ };
364
+ const promiseQueue = this._sendWithProtocol(invocationDescriptor)
365
+ .catch((e) => {
366
+ reject(e);
367
+ // invocationId will always have a value for a non-blocking invocation
368
+ delete this._callbacks[invocationDescriptor.invocationId];
369
+ });
370
+ this._launchStreams(streams, promiseQueue);
371
+ });
372
+ return p;
373
+ }
374
+ on(methodName, newMethod) {
375
+ if (!methodName || !newMethod) {
376
+ return;
377
+ }
378
+ methodName = methodName.toLowerCase();
379
+ if (!this._methods[methodName]) {
380
+ this._methods[methodName] = [];
381
+ }
382
+ // Preventing adding the same handler multiple times.
383
+ if (this._methods[methodName].indexOf(newMethod) !== -1) {
384
+ return;
385
+ }
386
+ this._methods[methodName].push(newMethod);
387
+ }
388
+ off(methodName, method) {
389
+ if (!methodName) {
390
+ return;
391
+ }
392
+ methodName = methodName.toLowerCase();
393
+ const handlers = this._methods[methodName];
394
+ if (!handlers) {
395
+ return;
396
+ }
397
+ if (method) {
398
+ const removeIdx = handlers.indexOf(method);
399
+ if (removeIdx !== -1) {
400
+ handlers.splice(removeIdx, 1);
401
+ if (handlers.length === 0) {
402
+ delete this._methods[methodName];
403
+ }
404
+ }
405
+ }
406
+ else {
407
+ delete this._methods[methodName];
408
+ }
409
+ }
410
+ /** Registers a handler that will be invoked when the connection is closed.
411
+ *
412
+ * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).
413
+ */
414
+ onclose(callback) {
415
+ if (callback) {
416
+ this._closedCallbacks.push(callback);
417
+ }
418
+ }
419
+ /** Registers a handler that will be invoked when the connection starts reconnecting.
420
+ *
421
+ * @param {Function} callback The handler that will be invoked when the connection starts reconnecting. Optionally receives a single argument containing the error that caused the connection to start reconnecting (if any).
422
+ */
423
+ onreconnecting(callback) {
424
+ if (callback) {
425
+ this._reconnectingCallbacks.push(callback);
426
+ }
427
+ }
428
+ /** Registers a handler that will be invoked when the connection successfully reconnects.
429
+ *
430
+ * @param {Function} callback The handler that will be invoked when the connection successfully reconnects.
431
+ */
432
+ onreconnected(callback) {
433
+ if (callback) {
434
+ this._reconnectedCallbacks.push(callback);
435
+ }
436
+ }
437
+ _processIncomingData(data) {
438
+ this._cleanupTimeout();
439
+ if (!this._receivedHandshakeResponse) {
440
+ data = this._processHandshakeResponse(data);
441
+ this._receivedHandshakeResponse = true;
442
+ }
443
+ // Data may have all been read when processing handshake response
444
+ if (data) {
445
+ // Parse the messages
446
+ const messages = this._protocol.parseMessages(data, this._logger);
447
+ for (const message of messages) {
448
+ if (this._messageBuffer && !this._messageBuffer._shouldProcessMessage(message)) {
449
+ // Don't process the message, we are either waiting for a SequenceMessage or received a duplicate message
450
+ continue;
451
+ }
452
+ switch (message.type) {
453
+ case IHubProtocol.MessageType.Invocation:
454
+ this._invokeClientMethod(message)
455
+ .catch((e) => {
456
+ this._logger.log(ILogger.LogLevel.Error, `Invoke client method threw error: ${Utils.getErrorString(e)}`);
457
+ });
458
+ break;
459
+ case IHubProtocol.MessageType.StreamItem:
460
+ case IHubProtocol.MessageType.Completion: {
461
+ const callback = this._callbacks[message.invocationId];
462
+ if (callback) {
463
+ if (message.type === IHubProtocol.MessageType.Completion) {
464
+ delete this._callbacks[message.invocationId];
465
+ }
466
+ try {
467
+ callback(message);
468
+ }
469
+ catch (e) {
470
+ this._logger.log(ILogger.LogLevel.Error, `Stream callback threw error: ${Utils.getErrorString(e)}`);
471
+ }
472
+ }
473
+ break;
474
+ }
475
+ case IHubProtocol.MessageType.Ping:
476
+ // Don't care about pings
477
+ break;
478
+ case IHubProtocol.MessageType.Close: {
479
+ this._logger.log(ILogger.LogLevel.Information, "Close message received from server.");
480
+ const error = message.error ? new Error("Server returned an error on close: " + message.error) : undefined;
481
+ if (message.allowReconnect === true) {
482
+ // It feels wrong not to await connection.stop() here, but processIncomingData is called as part of an onreceive callback which is not async,
483
+ // this is already the behavior for serverTimeout(), and HttpConnection.Stop() should catch and log all possible exceptions.
484
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
485
+ this.connection.stop(error);
486
+ }
487
+ else {
488
+ // We cannot await stopInternal() here, but subsequent calls to stop() will await this if stopInternal() is still ongoing.
489
+ this._stopPromise = this._stopInternal(error);
490
+ }
491
+ break;
492
+ }
493
+ case IHubProtocol.MessageType.Ack:
494
+ if (this._messageBuffer) {
495
+ this._messageBuffer._ack(message);
496
+ }
497
+ break;
498
+ case IHubProtocol.MessageType.Sequence:
499
+ if (this._messageBuffer) {
500
+ this._messageBuffer._resetSequence(message);
501
+ }
502
+ break;
503
+ default:
504
+ this._logger.log(ILogger.LogLevel.Warning, `Invalid message type: ${message.type}.`);
505
+ break;
506
+ }
507
+ }
508
+ }
509
+ this._resetTimeoutPeriod();
510
+ }
511
+ _processHandshakeResponse(data) {
512
+ let responseMessage;
513
+ let remainingData;
514
+ try {
515
+ [remainingData, responseMessage] = this._handshakeProtocol.parseHandshakeResponse(data);
516
+ }
517
+ catch (e) {
518
+ const message = "Error parsing handshake response: " + e;
519
+ this._logger.log(ILogger.LogLevel.Error, message);
520
+ const error = new Error(message);
521
+ this._handshakeRejecter(error);
522
+ throw error;
523
+ }
524
+ if (responseMessage.error) {
525
+ const message = "Server returned handshake error: " + responseMessage.error;
526
+ this._logger.log(ILogger.LogLevel.Error, message);
527
+ const error = new Error(message);
528
+ this._handshakeRejecter(error);
529
+ throw error;
530
+ }
531
+ else {
532
+ this._logger.log(ILogger.LogLevel.Debug, "Server handshake complete.");
533
+ }
534
+ this._handshakeResolver();
535
+ return remainingData;
536
+ }
537
+ _resetKeepAliveInterval() {
538
+ if (this.connection.features.inherentKeepAlive) {
539
+ return;
540
+ }
541
+ // Set the time we want the next keep alive to be sent
542
+ // Timer will be setup on next message receive
543
+ this._nextKeepAlive = new Date().getTime() + this.keepAliveIntervalInMilliseconds;
544
+ this._cleanupPingTimer();
545
+ }
546
+ _resetTimeoutPeriod() {
547
+ if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
548
+ // Set the timeout timer
549
+ this._timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);
550
+ // Immediately fire Keep-Alive ping if nextPing is overdue to avoid dependency on JS timers
551
+ let nextPing = this._nextKeepAlive - new Date().getTime();
552
+ if (nextPing < 0) {
553
+ if (this._connectionState === exports.HubConnectionState.Connected) {
554
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
555
+ this._trySendPingMessage();
556
+ }
557
+ return;
558
+ }
559
+ // Set keepAlive timer if there isn't one
560
+ if (this._pingServerHandle === undefined) {
561
+ if (nextPing < 0) {
562
+ nextPing = 0;
563
+ }
564
+ // The timer needs to be set from a networking callback to avoid Chrome timer throttling from causing timers to run once a minute
565
+ this._pingServerHandle = setTimeout(async () => {
566
+ if (this._connectionState === exports.HubConnectionState.Connected) {
567
+ await this._trySendPingMessage();
568
+ }
569
+ }, nextPing);
570
+ }
571
+ }
572
+ }
573
+ // eslint-disable-next-line @typescript-eslint/naming-convention
574
+ serverTimeout() {
575
+ // The server hasn't talked to us in a while. It doesn't like us anymore ... :(
576
+ // Terminate the connection, but we don't need to wait on the promise. This could trigger reconnecting.
577
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
578
+ this.connection.stop(new Error("Server timeout elapsed without receiving a message from the server."));
579
+ }
580
+ async _invokeClientMethod(invocationMessage) {
581
+ const methodName = invocationMessage.target.toLowerCase();
582
+ const methods = this._methods[methodName];
583
+ if (!methods) {
584
+ this._logger.log(ILogger.LogLevel.Warning, `No client method with the name '${methodName}' found.`);
585
+ // No handlers provided by client but the server is expecting a response still, so we send an error
586
+ if (invocationMessage.invocationId) {
587
+ this._logger.log(ILogger.LogLevel.Warning, `No result given for '${methodName}' method and invocation ID '${invocationMessage.invocationId}'.`);
588
+ await this._sendWithProtocol(this._createCompletionMessage(invocationMessage.invocationId, "Client didn't provide a result.", null));
589
+ }
590
+ return;
591
+ }
592
+ // Avoid issues with handlers removing themselves thus modifying the list while iterating through it
593
+ const methodsCopy = methods.slice();
594
+ // Server expects a response
595
+ const expectsResponse = invocationMessage.invocationId ? true : false;
596
+ // We preserve the last result or exception but still call all handlers
597
+ let res;
598
+ let exception;
599
+ let completionMessage;
600
+ for (const m of methodsCopy) {
601
+ try {
602
+ const prevRes = res;
603
+ res = await m.apply(this, invocationMessage.arguments);
604
+ if (expectsResponse && res && prevRes) {
605
+ this._logger.log(ILogger.LogLevel.Error, `Multiple results provided for '${methodName}'. Sending error to server.`);
606
+ completionMessage = this._createCompletionMessage(invocationMessage.invocationId, `Client provided multiple results.`, null);
607
+ }
608
+ // Ignore exception if we got a result after, the exception will be logged
609
+ exception = undefined;
610
+ }
611
+ catch (e) {
612
+ exception = e;
613
+ this._logger.log(ILogger.LogLevel.Error, `A callback for the method '${methodName}' threw error '${e}'.`);
614
+ }
615
+ }
616
+ if (completionMessage) {
617
+ await this._sendWithProtocol(completionMessage);
618
+ }
619
+ else if (expectsResponse) {
620
+ // If there is an exception that means either no result was given or a handler after a result threw
621
+ if (exception) {
622
+ completionMessage = this._createCompletionMessage(invocationMessage.invocationId, `${exception}`, null);
623
+ }
624
+ else if (res !== undefined) {
625
+ completionMessage = this._createCompletionMessage(invocationMessage.invocationId, null, res);
626
+ }
627
+ else {
628
+ this._logger.log(ILogger.LogLevel.Warning, `No result given for '${methodName}' method and invocation ID '${invocationMessage.invocationId}'.`);
629
+ // Client didn't provide a result or throw from a handler, server expects a response so we send an error
630
+ completionMessage = this._createCompletionMessage(invocationMessage.invocationId, "Client didn't provide a result.", null);
631
+ }
632
+ await this._sendWithProtocol(completionMessage);
633
+ }
634
+ else {
635
+ if (res) {
636
+ this._logger.log(ILogger.LogLevel.Error, `Result given for '${methodName}' method but server is not expecting a result.`);
637
+ }
638
+ }
639
+ }
640
+ _connectionClosed(error) {
641
+ this._logger.log(ILogger.LogLevel.Debug, `HubConnection.connectionClosed(${error}) called while in state ${this._connectionState}.`);
642
+ // Triggering this.handshakeRejecter is insufficient because it could already be resolved without the continuation having run yet.
643
+ this._stopDuringStartError = this._stopDuringStartError || error || new Errors.AbortError("The underlying connection was closed before the hub handshake could complete.");
644
+ // If the handshake is in progress, start will be waiting for the handshake promise, so we complete it.
645
+ // If it has already completed, this should just noop.
646
+ if (this._handshakeResolver) {
647
+ this._handshakeResolver();
648
+ }
649
+ this._cancelCallbacksWithError(error || new Error("Invocation canceled due to the underlying connection being closed."));
650
+ this._cleanupTimeout();
651
+ this._cleanupPingTimer();
652
+ if (this._connectionState === exports.HubConnectionState.Disconnecting) {
653
+ this._completeClose(error);
654
+ }
655
+ else if (this._connectionState === exports.HubConnectionState.Connected && this._reconnectPolicy) {
656
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
657
+ this._reconnect(error);
658
+ }
659
+ else if (this._connectionState === exports.HubConnectionState.Connected) {
660
+ this._completeClose(error);
661
+ }
662
+ // If none of the above if conditions were true were called the HubConnection must be in either:
663
+ // 1. The Connecting state in which case the handshakeResolver will complete it and stopDuringStartError will fail it.
664
+ // 2. The Reconnecting state in which case the handshakeResolver will complete it and stopDuringStartError will fail the current reconnect attempt
665
+ // and potentially continue the reconnect() loop.
666
+ // 3. The Disconnected state in which case we're already done.
667
+ }
668
+ _completeClose(error) {
669
+ if (this._connectionStarted) {
670
+ this._connectionState = exports.HubConnectionState.Disconnected;
671
+ this._connectionStarted = false;
672
+ if (this._messageBuffer) {
673
+ this._messageBuffer._dispose(error !== null && error !== void 0 ? error : new Error("Connection closed."));
674
+ this._messageBuffer = undefined;
675
+ }
676
+ if (Utils.Platform.isBrowser) {
677
+ window.document.removeEventListener("freeze", this._freezeEventListener);
678
+ }
679
+ try {
680
+ this._closedCallbacks.forEach((c) => c.apply(this, [error]));
681
+ }
682
+ catch (e) {
683
+ this._logger.log(ILogger.LogLevel.Error, `An onclose callback called with error '${error}' threw error '${e}'.`);
684
+ }
685
+ }
686
+ }
687
+ async _reconnect(error) {
688
+ const reconnectStartTime = Date.now();
689
+ let previousReconnectAttempts = 0;
690
+ let retryError = error !== undefined ? error : new Error("Attempting to reconnect due to a unknown error.");
691
+ let nextRetryDelay = this._getNextRetryDelay(previousReconnectAttempts, 0, retryError);
692
+ if (nextRetryDelay === null) {
693
+ this._logger.log(ILogger.LogLevel.Debug, "Connection not reconnecting because the IRetryPolicy returned null on the first reconnect attempt.");
694
+ this._completeClose(error);
695
+ return;
696
+ }
697
+ this._connectionState = exports.HubConnectionState.Reconnecting;
698
+ if (error) {
699
+ this._logger.log(ILogger.LogLevel.Information, `Connection reconnecting because of error '${error}'.`);
700
+ }
701
+ else {
702
+ this._logger.log(ILogger.LogLevel.Information, "Connection reconnecting.");
703
+ }
704
+ if (this._reconnectingCallbacks.length !== 0) {
705
+ try {
706
+ this._reconnectingCallbacks.forEach((c) => c.apply(this, [error]));
707
+ }
708
+ catch (e) {
709
+ this._logger.log(ILogger.LogLevel.Error, `An onreconnecting callback called with error '${error}' threw error '${e}'.`);
710
+ }
711
+ // Exit early if an onreconnecting callback called connection.stop().
712
+ if (this._connectionState !== exports.HubConnectionState.Reconnecting) {
713
+ this._logger.log(ILogger.LogLevel.Debug, "Connection left the reconnecting state in onreconnecting callback. Done reconnecting.");
714
+ return;
715
+ }
716
+ }
717
+ while (nextRetryDelay !== null) {
718
+ this._logger.log(ILogger.LogLevel.Information, `Reconnect attempt number ${previousReconnectAttempts + 1} will start in ${nextRetryDelay} ms.`);
719
+ await new Promise((resolve) => {
720
+ this._reconnectDelayHandle = setTimeout(resolve, nextRetryDelay);
721
+ });
722
+ this._reconnectDelayHandle = undefined;
723
+ if (this._connectionState !== exports.HubConnectionState.Reconnecting) {
724
+ this._logger.log(ILogger.LogLevel.Debug, "Connection left the reconnecting state during reconnect delay. Done reconnecting.");
725
+ return;
726
+ }
727
+ try {
728
+ await this._startInternal();
729
+ this._connectionState = exports.HubConnectionState.Connected;
730
+ this._logger.log(ILogger.LogLevel.Information, "HubConnection reconnected successfully.");
731
+ if (this._reconnectedCallbacks.length !== 0) {
732
+ try {
733
+ this._reconnectedCallbacks.forEach((c) => c.apply(this, [this.connection.connectionId]));
734
+ }
735
+ catch (e) {
736
+ this._logger.log(ILogger.LogLevel.Error, `An onreconnected callback called with connectionId '${this.connection.connectionId}; threw error '${e}'.`);
737
+ }
738
+ }
739
+ return;
740
+ }
741
+ catch (e) {
742
+ this._logger.log(ILogger.LogLevel.Information, `Reconnect attempt failed because of error '${e}'.`);
743
+ if (this._connectionState !== exports.HubConnectionState.Reconnecting) {
744
+ this._logger.log(ILogger.LogLevel.Debug, `Connection moved to the '${this._connectionState}' from the reconnecting state during reconnect attempt. Done reconnecting.`);
745
+ // The TypeScript compiler thinks that connectionState must be Connected here. The TypeScript compiler is wrong.
746
+ if (this._connectionState === exports.HubConnectionState.Disconnecting) {
747
+ this._completeClose();
748
+ }
749
+ return;
750
+ }
751
+ previousReconnectAttempts++;
752
+ retryError = e instanceof Error ? e : new Error(e.toString());
753
+ nextRetryDelay = this._getNextRetryDelay(previousReconnectAttempts, Date.now() - reconnectStartTime, retryError);
754
+ }
755
+ }
756
+ this._logger.log(ILogger.LogLevel.Information, `Reconnect retries have been exhausted after ${Date.now() - reconnectStartTime} ms and ${previousReconnectAttempts} failed attempts. Connection disconnecting.`);
757
+ this._completeClose();
758
+ }
759
+ _getNextRetryDelay(previousRetryCount, elapsedMilliseconds, retryReason) {
760
+ try {
761
+ return this._reconnectPolicy.nextRetryDelayInMilliseconds({
762
+ elapsedMilliseconds,
763
+ previousRetryCount,
764
+ retryReason,
765
+ });
766
+ }
767
+ catch (e) {
768
+ this._logger.log(ILogger.LogLevel.Error, `IRetryPolicy.nextRetryDelayInMilliseconds(${previousRetryCount}, ${elapsedMilliseconds}) threw error '${e}'.`);
769
+ return null;
770
+ }
771
+ }
772
+ _cancelCallbacksWithError(error) {
773
+ const callbacks = this._callbacks;
774
+ this._callbacks = {};
775
+ Object.keys(callbacks)
776
+ .forEach((key) => {
777
+ const callback = callbacks[key];
778
+ try {
779
+ callback(null, error);
780
+ }
781
+ catch (e) {
782
+ this._logger.log(ILogger.LogLevel.Error, `Stream 'error' callback called with '${error}' threw error: ${Utils.getErrorString(e)}`);
783
+ }
784
+ });
785
+ }
786
+ _cleanupPingTimer() {
787
+ if (this._pingServerHandle) {
788
+ clearTimeout(this._pingServerHandle);
789
+ this._pingServerHandle = undefined;
790
+ }
791
+ }
792
+ _cleanupTimeout() {
793
+ if (this._timeoutHandle) {
794
+ clearTimeout(this._timeoutHandle);
795
+ }
796
+ }
797
+ _createInvocation(methodName, args, nonblocking, streamIds) {
798
+ if (nonblocking) {
799
+ if (streamIds.length !== 0) {
800
+ return {
801
+ target: methodName,
802
+ arguments: args,
803
+ streamIds,
804
+ type: IHubProtocol.MessageType.Invocation,
805
+ };
806
+ }
807
+ else {
808
+ return {
809
+ target: methodName,
810
+ arguments: args,
811
+ type: IHubProtocol.MessageType.Invocation,
812
+ };
813
+ }
814
+ }
815
+ else {
816
+ const invocationId = this._invocationId;
817
+ this._invocationId++;
818
+ if (streamIds.length !== 0) {
819
+ return {
820
+ target: methodName,
821
+ arguments: args,
822
+ invocationId: invocationId.toString(),
823
+ streamIds,
824
+ type: IHubProtocol.MessageType.Invocation,
825
+ };
826
+ }
827
+ else {
828
+ return {
829
+ target: methodName,
830
+ arguments: args,
831
+ invocationId: invocationId.toString(),
832
+ type: IHubProtocol.MessageType.Invocation,
833
+ };
834
+ }
835
+ }
836
+ }
837
+ _launchStreams(streams, promiseQueue) {
838
+ if (streams.length === 0) {
839
+ return;
840
+ }
841
+ // Synchronize stream data so they arrive in-order on the server
842
+ if (!promiseQueue) {
843
+ promiseQueue = Promise.resolve();
844
+ }
845
+ // We want to iterate over the keys, since the keys are the stream ids
846
+ // eslint-disable-next-line guard-for-in
847
+ for (const streamId in streams) {
848
+ streams[streamId].subscribe({
849
+ complete: () => {
850
+ promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createCompletionMessage(streamId)));
851
+ },
852
+ error: (err) => {
853
+ let message;
854
+ if (err instanceof Error) {
855
+ message = err.message;
856
+ }
857
+ else if (err && err.toString) {
858
+ message = err.toString();
859
+ }
860
+ else {
861
+ message = "Unknown error";
862
+ }
863
+ promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createCompletionMessage(streamId, message)));
864
+ },
865
+ next: (item) => {
866
+ promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createStreamItemMessage(streamId, item)));
867
+ },
868
+ });
869
+ }
870
+ }
871
+ _replaceStreamingParams(args) {
872
+ const streams = [];
873
+ const streamIds = [];
874
+ for (let i = 0; i < args.length; i++) {
875
+ const argument = args[i];
876
+ if (this._isObservable(argument)) {
877
+ const streamId = this._invocationId;
878
+ this._invocationId++;
879
+ // Store the stream for later use
880
+ streams[streamId] = argument;
881
+ streamIds.push(streamId.toString());
882
+ // remove stream from args
883
+ args.splice(i, 1);
884
+ }
885
+ }
886
+ return [streams, streamIds];
887
+ }
888
+ _isObservable(arg) {
889
+ // This allows other stream implementations to just work (like rxjs)
890
+ return arg && arg.subscribe && typeof arg.subscribe === "function";
891
+ }
892
+ _createStreamInvocation(methodName, args, streamIds) {
893
+ const invocationId = this._invocationId;
894
+ this._invocationId++;
895
+ if (streamIds.length !== 0) {
896
+ return {
897
+ target: methodName,
898
+ arguments: args,
899
+ invocationId: invocationId.toString(),
900
+ streamIds,
901
+ type: IHubProtocol.MessageType.StreamInvocation,
902
+ };
903
+ }
904
+ else {
905
+ return {
906
+ target: methodName,
907
+ arguments: args,
908
+ invocationId: invocationId.toString(),
909
+ type: IHubProtocol.MessageType.StreamInvocation,
910
+ };
911
+ }
912
+ }
913
+ _createCancelInvocation(id) {
914
+ return {
915
+ invocationId: id,
916
+ type: IHubProtocol.MessageType.CancelInvocation,
917
+ };
918
+ }
919
+ _createStreamItemMessage(id, item) {
920
+ return {
921
+ invocationId: id,
922
+ item,
923
+ type: IHubProtocol.MessageType.StreamItem,
924
+ };
925
+ }
926
+ _createCompletionMessage(id, error, result) {
927
+ if (error) {
928
+ return {
929
+ error,
930
+ invocationId: id,
931
+ type: IHubProtocol.MessageType.Completion,
932
+ };
933
+ }
934
+ return {
935
+ invocationId: id,
936
+ result,
937
+ type: IHubProtocol.MessageType.Completion,
938
+ };
939
+ }
940
+ _createCloseMessage() {
941
+ return { type: IHubProtocol.MessageType.Close };
942
+ }
943
+ async _trySendPingMessage() {
944
+ try {
945
+ await this._sendMessage(this._cachedPingMessage);
946
+ }
947
+ catch {
948
+ // We don't care about the error. It should be seen elsewhere in the client.
949
+ // The connection is probably in a bad or closed state now, cleanup the timer so it stops triggering
950
+ this._cleanupPingTimer();
951
+ }
952
+ }
953
+ }
954
+
955
+ exports.HubConnection = HubConnection;
956
+ //# sourceMappingURL=HubConnection.js.map