@metamask-previews/advanced-chart-core 0.0.0-preview-e58fe0640 → 0.0.0-preview-238cb7063

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 (321) hide show
  1. package/dist/core/bootstrap.cjs +185 -0
  2. package/dist/core/bootstrap.cjs.map +1 -0
  3. package/dist/core/bootstrap.d.cts +10 -0
  4. package/dist/core/bootstrap.d.cts.map +1 -0
  5. package/dist/core/bootstrap.d.mts +10 -0
  6. package/dist/core/bootstrap.d.mts.map +1 -0
  7. package/dist/core/bootstrap.mjs +181 -0
  8. package/dist/core/bootstrap.mjs.map +1 -0
  9. package/dist/core/bridge.cjs +115 -0
  10. package/dist/core/bridge.cjs.map +1 -0
  11. package/dist/core/bridge.d.cts +37 -0
  12. package/dist/core/bridge.d.cts.map +1 -0
  13. package/dist/core/bridge.d.mts +37 -0
  14. package/dist/core/bridge.d.mts.map +1 -0
  15. package/dist/core/bridge.mjs +108 -0
  16. package/dist/core/bridge.mjs.map +1 -0
  17. package/dist/core/dataLifecycle.cjs +52 -0
  18. package/dist/core/dataLifecycle.cjs.map +1 -0
  19. package/dist/core/dataLifecycle.d.cts +8 -0
  20. package/dist/core/dataLifecycle.d.cts.map +1 -0
  21. package/dist/core/dataLifecycle.d.mts +8 -0
  22. package/dist/core/dataLifecycle.d.mts.map +1 -0
  23. package/dist/core/dataLifecycle.mjs +46 -0
  24. package/dist/core/dataLifecycle.mjs.map +1 -0
  25. package/dist/core/loadLibrary.cjs +63 -0
  26. package/dist/core/loadLibrary.cjs.map +1 -0
  27. package/dist/core/loadLibrary.d.cts +16 -0
  28. package/dist/core/loadLibrary.d.cts.map +1 -0
  29. package/dist/core/loadLibrary.d.mts +16 -0
  30. package/dist/core/loadLibrary.d.mts.map +1 -0
  31. package/dist/core/loadLibrary.mjs +58 -0
  32. package/dist/core/loadLibrary.mjs.map +1 -0
  33. package/dist/core/resolution.cjs +58 -0
  34. package/dist/core/resolution.cjs.map +1 -0
  35. package/dist/core/resolution.d.cts +15 -0
  36. package/dist/core/resolution.d.cts.map +1 -0
  37. package/dist/core/resolution.d.mts +15 -0
  38. package/dist/core/resolution.d.mts.map +1 -0
  39. package/dist/core/resolution.mjs +54 -0
  40. package/dist/core/resolution.mjs.map +1 -0
  41. package/dist/core/state.cjs +343 -0
  42. package/dist/core/state.cjs.map +1 -0
  43. package/dist/core/state.d.cts +67 -0
  44. package/dist/core/state.d.cts.map +1 -0
  45. package/dist/core/state.d.mts +67 -0
  46. package/dist/core/state.d.mts.map +1 -0
  47. package/dist/core/state.mjs +283 -0
  48. package/dist/core/state.mjs.map +1 -0
  49. package/dist/core/timeUtils.cjs +72 -0
  50. package/dist/core/timeUtils.cjs.map +1 -0
  51. package/dist/core/timeUtils.d.cts +32 -0
  52. package/dist/core/timeUtils.d.cts.map +1 -0
  53. package/dist/core/timeUtils.d.mts +32 -0
  54. package/dist/core/timeUtils.d.mts.map +1 -0
  55. package/dist/core/timeUtils.mjs +66 -0
  56. package/dist/core/timeUtils.mjs.map +1 -0
  57. package/dist/core/timezone.cjs +135 -0
  58. package/dist/core/timezone.cjs.map +1 -0
  59. package/dist/core/timezone.d.cts +16 -0
  60. package/dist/core/timezone.d.cts.map +1 -0
  61. package/dist/core/timezone.d.mts +16 -0
  62. package/dist/core/timezone.d.mts.map +1 -0
  63. package/dist/core/timezone.mjs +131 -0
  64. package/dist/core/timezone.mjs.map +1 -0
  65. package/dist/core/types.cjs +20 -0
  66. package/dist/core/types.cjs.map +1 -0
  67. package/dist/core/types.d.cts +323 -0
  68. package/dist/core/types.d.cts.map +1 -0
  69. package/dist/core/types.d.mts +323 -0
  70. package/dist/core/types.d.mts.map +1 -0
  71. package/dist/core/types.mjs +17 -0
  72. package/dist/core/types.mjs.map +1 -0
  73. package/dist/features/indicators/index.cjs +183 -0
  74. package/dist/features/indicators/index.cjs.map +1 -0
  75. package/dist/features/indicators/index.d.cts +6 -0
  76. package/dist/features/indicators/index.d.cts.map +1 -0
  77. package/dist/features/indicators/index.d.mts +6 -0
  78. package/dist/features/indicators/index.d.mts.map +1 -0
  79. package/dist/features/indicators/index.mjs +177 -0
  80. package/dist/features/indicators/index.mjs.map +1 -0
  81. package/dist/features/indicators/legend.cjs +575 -0
  82. package/dist/features/indicators/legend.cjs.map +1 -0
  83. package/dist/features/indicators/legend.d.cts +40 -0
  84. package/dist/features/indicators/legend.d.cts.map +1 -0
  85. package/dist/features/indicators/legend.d.mts +40 -0
  86. package/dist/features/indicators/legend.d.mts.map +1 -0
  87. package/dist/features/indicators/legend.mjs +564 -0
  88. package/dist/features/indicators/legend.mjs.map +1 -0
  89. package/dist/features/indicators/resize.cjs +34 -0
  90. package/dist/features/indicators/resize.cjs.map +1 -0
  91. package/dist/features/indicators/resize.d.cts +2 -0
  92. package/dist/features/indicators/resize.d.cts.map +1 -0
  93. package/dist/features/indicators/resize.d.mts +2 -0
  94. package/dist/features/indicators/resize.d.mts.map +1 -0
  95. package/dist/features/indicators/resize.mjs +30 -0
  96. package/dist/features/indicators/resize.mjs.map +1 -0
  97. package/dist/features/indicators/studies.cjs +147 -0
  98. package/dist/features/indicators/studies.cjs.map +1 -0
  99. package/dist/features/indicators/studies.d.cts +36 -0
  100. package/dist/features/indicators/studies.d.cts.map +1 -0
  101. package/dist/features/indicators/studies.d.mts +36 -0
  102. package/dist/features/indicators/studies.d.mts.map +1 -0
  103. package/dist/features/indicators/studies.mjs +140 -0
  104. package/dist/features/indicators/studies.mjs.map +1 -0
  105. package/dist/features/indicators/subPane.cjs +78 -0
  106. package/dist/features/indicators/subPane.cjs.map +1 -0
  107. package/dist/features/indicators/subPane.d.cts +6 -0
  108. package/dist/features/indicators/subPane.d.cts.map +1 -0
  109. package/dist/features/indicators/subPane.d.mts +6 -0
  110. package/dist/features/indicators/subPane.d.mts.map +1 -0
  111. package/dist/features/indicators/subPane.mjs +72 -0
  112. package/dist/features/indicators/subPane.mjs.map +1 -0
  113. package/dist/features/volume/index.cjs +145 -0
  114. package/dist/features/volume/index.cjs.map +1 -0
  115. package/dist/features/volume/index.d.cts +4 -0
  116. package/dist/features/volume/index.d.cts.map +1 -0
  117. package/dist/features/volume/index.d.mts +4 -0
  118. package/dist/features/volume/index.d.mts.map +1 -0
  119. package/dist/features/volume/index.mjs +140 -0
  120. package/dist/features/volume/index.mjs.map +1 -0
  121. package/dist/index.cjs +20 -0
  122. package/dist/index.cjs.map +1 -0
  123. package/dist/index.d.cts +2 -0
  124. package/dist/index.d.cts.map +1 -0
  125. package/dist/index.d.mts +2 -0
  126. package/dist/index.d.mts.map +1 -0
  127. package/dist/index.mjs +18 -0
  128. package/dist/index.mjs.map +1 -0
  129. package/dist/interaction/crosshair.cjs +147 -0
  130. package/dist/interaction/crosshair.cjs.map +1 -0
  131. package/dist/interaction/crosshair.d.cts +21 -0
  132. package/dist/interaction/crosshair.d.cts.map +1 -0
  133. package/dist/interaction/crosshair.d.mts +21 -0
  134. package/dist/interaction/crosshair.d.mts.map +1 -0
  135. package/dist/interaction/crosshair.mjs +141 -0
  136. package/dist/interaction/crosshair.mjs.map +1 -0
  137. package/dist/interaction/visibleRange.cjs +102 -0
  138. package/dist/interaction/visibleRange.cjs.map +1 -0
  139. package/dist/interaction/visibleRange.d.cts +11 -0
  140. package/dist/interaction/visibleRange.d.cts.map +1 -0
  141. package/dist/interaction/visibleRange.d.mts +11 -0
  142. package/dist/interaction/visibleRange.d.mts.map +1 -0
  143. package/dist/interaction/visibleRange.mjs +97 -0
  144. package/dist/interaction/visibleRange.mjs.map +1 -0
  145. package/dist/messages/contract.cjs +27 -0
  146. package/dist/messages/contract.cjs.map +1 -0
  147. package/dist/messages/contract.d.cts +262 -0
  148. package/dist/messages/contract.d.cts.map +1 -0
  149. package/dist/messages/contract.d.mts +262 -0
  150. package/dist/messages/contract.d.mts.map +1 -0
  151. package/dist/messages/contract.mjs +26 -0
  152. package/dist/messages/contract.mjs.map +1 -0
  153. package/dist/messages/handler.cjs +54 -0
  154. package/dist/messages/handler.cjs.map +1 -0
  155. package/dist/messages/handler.d.cts +25 -0
  156. package/dist/messages/handler.d.cts.map +1 -0
  157. package/dist/messages/handler.d.mts +25 -0
  158. package/dist/messages/handler.d.mts.map +1 -0
  159. package/dist/messages/handler.mjs +48 -0
  160. package/dist/messages/handler.mjs.map +1 -0
  161. package/dist/overlays/focusTime/index.cjs +161 -0
  162. package/dist/overlays/focusTime/index.cjs.map +1 -0
  163. package/dist/overlays/focusTime/index.d.cts +6 -0
  164. package/dist/overlays/focusTime/index.d.cts.map +1 -0
  165. package/dist/overlays/focusTime/index.d.mts +6 -0
  166. package/dist/overlays/focusTime/index.d.mts.map +1 -0
  167. package/dist/overlays/focusTime/index.mjs +155 -0
  168. package/dist/overlays/focusTime/index.mjs.map +1 -0
  169. package/dist/overlays/positionLines/index.cjs +188 -0
  170. package/dist/overlays/positionLines/index.cjs.map +1 -0
  171. package/dist/overlays/positionLines/index.d.cts +4 -0
  172. package/dist/overlays/positionLines/index.d.cts.map +1 -0
  173. package/dist/overlays/positionLines/index.d.mts +4 -0
  174. package/dist/overlays/positionLines/index.d.mts.map +1 -0
  175. package/dist/overlays/positionLines/index.mjs +183 -0
  176. package/dist/overlays/positionLines/index.mjs.map +1 -0
  177. package/dist/overlays/positionLines/state.cjs +35 -0
  178. package/dist/overlays/positionLines/state.cjs.map +1 -0
  179. package/dist/overlays/positionLines/state.d.cts +8 -0
  180. package/dist/overlays/positionLines/state.d.cts.map +1 -0
  181. package/dist/overlays/positionLines/state.d.mts +8 -0
  182. package/dist/overlays/positionLines/state.d.mts.map +1 -0
  183. package/dist/overlays/positionLines/state.mjs +26 -0
  184. package/dist/overlays/positionLines/state.mjs.map +1 -0
  185. package/dist/overlays/socialLeaderboard/index.cjs +229 -0
  186. package/dist/overlays/socialLeaderboard/index.cjs.map +1 -0
  187. package/dist/overlays/socialLeaderboard/index.d.cts +48 -0
  188. package/dist/overlays/socialLeaderboard/index.d.cts.map +1 -0
  189. package/dist/overlays/socialLeaderboard/index.d.mts +48 -0
  190. package/dist/overlays/socialLeaderboard/index.d.mts.map +1 -0
  191. package/dist/overlays/socialLeaderboard/index.mjs +222 -0
  192. package/dist/overlays/socialLeaderboard/index.mjs.map +1 -0
  193. package/dist/overlays/tradeMarkers/animation.cjs +123 -0
  194. package/dist/overlays/tradeMarkers/animation.cjs.map +1 -0
  195. package/dist/overlays/tradeMarkers/animation.d.cts +5 -0
  196. package/dist/overlays/tradeMarkers/animation.d.cts.map +1 -0
  197. package/dist/overlays/tradeMarkers/animation.d.mts +5 -0
  198. package/dist/overlays/tradeMarkers/animation.d.mts.map +1 -0
  199. package/dist/overlays/tradeMarkers/animation.mjs +118 -0
  200. package/dist/overlays/tradeMarkers/animation.mjs.map +1 -0
  201. package/dist/overlays/tradeMarkers/index.cjs +334 -0
  202. package/dist/overlays/tradeMarkers/index.cjs.map +1 -0
  203. package/dist/overlays/tradeMarkers/index.d.cts +35 -0
  204. package/dist/overlays/tradeMarkers/index.d.cts.map +1 -0
  205. package/dist/overlays/tradeMarkers/index.d.mts +35 -0
  206. package/dist/overlays/tradeMarkers/index.d.mts.map +1 -0
  207. package/dist/overlays/tradeMarkers/index.mjs +324 -0
  208. package/dist/overlays/tradeMarkers/index.mjs.map +1 -0
  209. package/dist/overlays/tradeMarkers/markerHitTest.cjs +337 -0
  210. package/dist/overlays/tradeMarkers/markerHitTest.cjs.map +1 -0
  211. package/dist/overlays/tradeMarkers/markerHitTest.d.cts +15 -0
  212. package/dist/overlays/tradeMarkers/markerHitTest.d.cts.map +1 -0
  213. package/dist/overlays/tradeMarkers/markerHitTest.d.mts +15 -0
  214. package/dist/overlays/tradeMarkers/markerHitTest.d.mts.map +1 -0
  215. package/dist/overlays/tradeMarkers/markerHitTest.mjs +331 -0
  216. package/dist/overlays/tradeMarkers/markerHitTest.mjs.map +1 -0
  217. package/dist/overlays/tradeMarkers/state.cjs +75 -0
  218. package/dist/overlays/tradeMarkers/state.cjs.map +1 -0
  219. package/dist/overlays/tradeMarkers/state.d.cts +20 -0
  220. package/dist/overlays/tradeMarkers/state.d.cts.map +1 -0
  221. package/dist/overlays/tradeMarkers/state.d.mts +20 -0
  222. package/dist/overlays/tradeMarkers/state.d.mts.map +1 -0
  223. package/dist/overlays/tradeMarkers/state.mjs +60 -0
  224. package/dist/overlays/tradeMarkers/state.mjs.map +1 -0
  225. package/dist/pagination/priceApi.cjs +102 -0
  226. package/dist/pagination/priceApi.cjs.map +1 -0
  227. package/dist/pagination/priceApi.d.cts +43 -0
  228. package/dist/pagination/priceApi.d.cts.map +1 -0
  229. package/dist/pagination/priceApi.d.mts +43 -0
  230. package/dist/pagination/priceApi.d.mts.map +1 -0
  231. package/dist/pagination/priceApi.mjs +97 -0
  232. package/dist/pagination/priceApi.mjs.map +1 -0
  233. package/dist/pagination/rnBacked.cjs +108 -0
  234. package/dist/pagination/rnBacked.cjs.map +1 -0
  235. package/dist/pagination/rnBacked.d.cts +15 -0
  236. package/dist/pagination/rnBacked.d.cts.map +1 -0
  237. package/dist/pagination/rnBacked.d.mts +15 -0
  238. package/dist/pagination/rnBacked.d.mts.map +1 -0
  239. package/dist/pagination/rnBacked.mjs +100 -0
  240. package/dist/pagination/rnBacked.mjs.map +1 -0
  241. package/dist/widget/chartType.cjs +39 -0
  242. package/dist/widget/chartType.cjs.map +1 -0
  243. package/dist/widget/chartType.d.cts +4 -0
  244. package/dist/widget/chartType.d.cts.map +1 -0
  245. package/dist/widget/chartType.d.mts +4 -0
  246. package/dist/widget/chartType.d.mts.map +1 -0
  247. package/dist/widget/chartType.mjs +34 -0
  248. package/dist/widget/chartType.mjs.map +1 -0
  249. package/dist/widget/datafeed.cjs +228 -0
  250. package/dist/widget/datafeed.cjs.map +1 -0
  251. package/dist/widget/datafeed.d.cts +22 -0
  252. package/dist/widget/datafeed.d.cts.map +1 -0
  253. package/dist/widget/datafeed.d.mts +22 -0
  254. package/dist/widget/datafeed.d.mts.map +1 -0
  255. package/dist/widget/datafeed.mjs +223 -0
  256. package/dist/widget/datafeed.mjs.map +1 -0
  257. package/dist/widget/externalLinkBridge.cjs +134 -0
  258. package/dist/widget/externalLinkBridge.cjs.map +1 -0
  259. package/dist/widget/externalLinkBridge.d.cts +14 -0
  260. package/dist/widget/externalLinkBridge.d.cts.map +1 -0
  261. package/dist/widget/externalLinkBridge.d.mts +14 -0
  262. package/dist/widget/externalLinkBridge.d.mts.map +1 -0
  263. package/dist/widget/externalLinkBridge.mjs +127 -0
  264. package/dist/widget/externalLinkBridge.mjs.map +1 -0
  265. package/dist/widget/initChart.cjs +242 -0
  266. package/dist/widget/initChart.cjs.map +1 -0
  267. package/dist/widget/initChart.d.cts +53 -0
  268. package/dist/widget/initChart.d.cts.map +1 -0
  269. package/dist/widget/initChart.d.mts +53 -0
  270. package/dist/widget/initChart.d.mts.map +1 -0
  271. package/dist/widget/initChart.mjs +235 -0
  272. package/dist/widget/initChart.mjs.map +1 -0
  273. package/dist/widget/ohlcvIngestion.cjs +240 -0
  274. package/dist/widget/ohlcvIngestion.cjs.map +1 -0
  275. package/dist/widget/ohlcvIngestion.d.cts +16 -0
  276. package/dist/widget/ohlcvIngestion.d.cts.map +1 -0
  277. package/dist/widget/ohlcvIngestion.d.mts +16 -0
  278. package/dist/widget/ohlcvIngestion.d.mts.map +1 -0
  279. package/dist/widget/ohlcvIngestion.mjs +233 -0
  280. package/dist/widget/ohlcvIngestion.mjs.map +1 -0
  281. package/dist/widget/priceFormatter.cjs +142 -0
  282. package/dist/widget/priceFormatter.cjs.map +1 -0
  283. package/dist/widget/priceFormatter.d.cts +38 -0
  284. package/dist/widget/priceFormatter.d.cts.map +1 -0
  285. package/dist/widget/priceFormatter.d.mts +38 -0
  286. package/dist/widget/priceFormatter.d.mts.map +1 -0
  287. package/dist/widget/priceFormatter.mjs +134 -0
  288. package/dist/widget/priceFormatter.mjs.map +1 -0
  289. package/dist/widget/scaleLayout.cjs +89 -0
  290. package/dist/widget/scaleLayout.cjs.map +1 -0
  291. package/dist/widget/scaleLayout.d.cts +9 -0
  292. package/dist/widget/scaleLayout.d.cts.map +1 -0
  293. package/dist/widget/scaleLayout.d.mts +9 -0
  294. package/dist/widget/scaleLayout.d.mts.map +1 -0
  295. package/dist/widget/scaleLayout.mjs +85 -0
  296. package/dist/widget/scaleLayout.mjs.map +1 -0
  297. package/dist/widget/theme.cjs +286 -0
  298. package/dist/widget/theme.cjs.map +1 -0
  299. package/dist/widget/theme.d.cts +99 -0
  300. package/dist/widget/theme.d.cts.map +1 -0
  301. package/dist/widget/theme.d.mts +99 -0
  302. package/dist/widget/theme.d.mts.map +1 -0
  303. package/dist/widget/theme.mjs +270 -0
  304. package/dist/widget/theme.mjs.map +1 -0
  305. package/dist/widget/tvDomHelpers.cjs +65 -0
  306. package/dist/widget/tvDomHelpers.cjs.map +1 -0
  307. package/dist/widget/tvDomHelpers.d.cts +19 -0
  308. package/dist/widget/tvDomHelpers.d.cts.map +1 -0
  309. package/dist/widget/tvDomHelpers.d.mts +19 -0
  310. package/dist/widget/tvDomHelpers.d.mts.map +1 -0
  311. package/dist/widget/tvDomHelpers.mjs +60 -0
  312. package/dist/widget/tvDomHelpers.mjs.map +1 -0
  313. package/dist/widget/visualOverrides.cjs +63 -0
  314. package/dist/widget/visualOverrides.cjs.map +1 -0
  315. package/dist/widget/visualOverrides.d.cts +27 -0
  316. package/dist/widget/visualOverrides.d.cts.map +1 -0
  317. package/dist/widget/visualOverrides.d.mts +27 -0
  318. package/dist/widget/visualOverrides.d.mts.map +1 -0
  319. package/dist/widget/visualOverrides.mjs +58 -0
  320. package/dist/widget/visualOverrides.mjs.map +1 -0
  321. package/package.json +3 -1
@@ -0,0 +1,229 @@
1
+ "use strict";
2
+ // Social Leaderboard (SLB) viewport centering and back-fill pagination.
3
+ //
4
+ // Implements **Strategy C — SLB bulk back-fill**, the third pagination
5
+ // strategy alongside Strategy A (Price API / Token Details) and Strategy B
6
+ // (RN-backed / Perps).
7
+ //
8
+ // ── How it works ──────────────────────────────────────────────────────────
9
+ //
10
+ // 1. RN pre-loads the complete OHLCV dataset that covers the trade window
11
+ // (visibleFromMs → visibleToMs) and sends it in one SET_OHLCV_DATA
12
+ // message with `slbMode: true`.
13
+ //
14
+ // 2. The WebView stores the data in core/state and marks viewport centering
15
+ // as pending (`slbCenteringPending = true`).
16
+ //
17
+ // 3. After the TradingView widget finishes its data load cycle, this module
18
+ // centers the viewport on the trade window so the user sees all relevant
19
+ // trades immediately (no manual scrolling needed).
20
+ //
21
+ // 4. getBars pagination is a no-op: because RN pre-loaded all data, any
22
+ // getBars call for a time range outside the in-memory dataset returns
23
+ // `{ noData: true }`. There is no second fetch — the dataset is complete.
24
+ //
25
+ // 5. When the user taps a different trade row, RN re-sends SET_OHLCV_DATA
26
+ // with updated visibleFromMs / visibleToMs. The centering flag resets,
27
+ // and step 3 fires again for the new window.
28
+ //
29
+ // ── Why a separate strategy? ──────────────────────────────────────────────
30
+ //
31
+ // Strategy A (Price API) and Strategy B (RN-backed) both paginate
32
+ // incrementally — fetching one page at a time as the user scrolls left.
33
+ // SLB needs the entire trade window visible from the start, so incremental
34
+ // pagination would cause visible "holes" while pages load. Bulk back-fill
35
+ // avoids this by having RN send all data upfront.
36
+ //
37
+ // ── Scoping via slbMode ──────────────────────────────────────────────────
38
+ //
39
+ // All SLB behavior is gated behind the `slbMode` flag so it cannot affect
40
+ // Token Details, Perps, or any other consumer. When `slbMode` is false
41
+ // (or omitted), this module is inert.
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.slbHandleGetBars = exports.slbScheduleInitialCentering = exports._resetSocialLeaderboardForTests = exports.slbCenterViewport = void 0;
44
+ const bridge_js_1 = require("../../core/bridge.cjs");
45
+ const state_js_1 = require("../../core/state.cjs");
46
+ const timeUtils_js_1 = require("../../core/timeUtils.cjs");
47
+ /**
48
+ * How long (ms) to keep re-asserting the centered visible range for a
49
+ * historical frame. A one-shot `setVisibleRange` issued during the post-load
50
+ * settle is overridden by TradingView's default "scroll to latest" positioning,
51
+ * so we re-apply every animation frame for this window — the same thing the
52
+ * animated focusTime slide does implicitly, which a single auto-center call did
53
+ * not. Kept short so it never fights a real user gesture.
54
+ */
55
+ const CENTER_HOLD_MS = 700;
56
+ /** Bumped on each hold so a newer center / fresh series cancels an in-flight hold. */
57
+ let centerHoldGeneration = 0;
58
+ /**
59
+ * Re-asserts `setVisibleRange({ from, to })` every animation frame for
60
+ * {@link CENTER_HOLD_MS}, defeating TradingView's post-load scroll-to-latest
61
+ * that clobbers a single call. Generation- and data-guarded so a newer center
62
+ * or a fresh series stops it; no-ops once the widget is torn down.
63
+ *
64
+ * Deliberately omits the `{ percentRightMargin: 0 }` option: passing it makes
65
+ * TradingView anchor to the latest candle and ignore an older from/to, so a
66
+ * historical frame (an old position's trades) would snap back to "today".
67
+ *
68
+ * @param chart - The active TradingView chart.
69
+ * @param fromSec - Left edge of the range, in unix seconds.
70
+ * @param toSec - Right edge of the range, in unix seconds.
71
+ */
72
+ function holdCenteredVisibleRange(chart, fromSec, toSec) {
73
+ centerHoldGeneration += 1;
74
+ const gen = centerHoldGeneration;
75
+ const dataGeneration = (0, state_js_1.getOhlcvGeneration)();
76
+ const startTs = Date.now();
77
+ const apply = () => {
78
+ if (gen !== centerHoldGeneration) {
79
+ return;
80
+ }
81
+ if (dataGeneration !== (0, state_js_1.getOhlcvGeneration)()) {
82
+ return;
83
+ }
84
+ if (!(0, state_js_1.getWidget)() || !(0, state_js_1.isChartReady)()) {
85
+ return;
86
+ }
87
+ try {
88
+ chart.setVisibleRange({ from: fromSec, to: toSec });
89
+ }
90
+ catch {
91
+ // setVisibleRange can throw every frame while the chart is mid-teardown;
92
+ // swallow silently so the hold window doesn't spam errors to RN.
93
+ }
94
+ if (Date.now() - startTs < CENTER_HOLD_MS) {
95
+ try {
96
+ requestAnimationFrame(apply);
97
+ }
98
+ catch {
99
+ setTimeout(apply, 16);
100
+ }
101
+ }
102
+ };
103
+ apply();
104
+ }
105
+ /**
106
+ * Centers the viewport on the SLB trade window after a data load.
107
+ *
108
+ * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.
109
+ * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar
110
+ * padding on each side so the edge candles aren't glued to the chart border.
111
+ *
112
+ * The centering flag is cleared after success so subsequent REALTIME_UPDATE
113
+ * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.
114
+ *
115
+ * When `options.immediate` is true, applies centering synchronously instead of
116
+ * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has
117
+ * already completed its data cycle by the time the callback fires.
118
+ *
119
+ * @param chart - The active TradingView chart.
120
+ * @param options - Optional centering options.
121
+ * @param options.immediate - When true, centers synchronously.
122
+ */
123
+ function slbCenterViewport(chart, options) {
124
+ if (!(0, state_js_1.getSlbMode)() || !(0, state_js_1.isSlbCenteringPending)()) {
125
+ return;
126
+ }
127
+ const fromMs = (0, state_js_1.getVisibleFromMs)();
128
+ const toMs = (0, state_js_1.getVisibleToMs)();
129
+ if (fromMs === null || toMs === null) {
130
+ return;
131
+ }
132
+ const capturedGeneration = (0, state_js_1.getOhlcvGeneration)();
133
+ const applyCenter = () => {
134
+ if (capturedGeneration !== (0, state_js_1.getOhlcvGeneration)()) {
135
+ return;
136
+ }
137
+ if (!(0, state_js_1.isSlbCenteringPending)()) {
138
+ return;
139
+ }
140
+ const data = (0, state_js_1.getOhlcvData)();
141
+ const barPadSec = (0, timeUtils_js_1.getApproxBarDurationSec)(data) * 2;
142
+ const fromSec = Math.floor(fromMs / 1000) - barPadSec;
143
+ const toSec = Math.ceil(toMs / 1000) + barPadSec;
144
+ // A frame whose right edge sits before the latest loaded bar is a historical
145
+ // position (an old, closed trade range). TradingView's post-load
146
+ // scroll-to-latest clobbers a single `setVisibleRange`, and
147
+ // `percentRightMargin: 0` re-anchors it to the latest candle — together they
148
+ // snap the viewport back to "today" and push the trades off-screen. Re-assert
149
+ // the range across the settle window (without that option) so the historical
150
+ // frame sticks. A frame ending at/after the latest bar is the trailing window:
151
+ // TV doesn't fight it, so a single anchored call is enough.
152
+ const lastBar = data.at(-1);
153
+ const lastBarSec = lastBar === undefined ? null : Math.floor(lastBar.time / 1000);
154
+ const isHistoricalFrame = lastBarSec !== null && Math.ceil(toMs / 1000) < lastBarSec;
155
+ try {
156
+ if (isHistoricalFrame) {
157
+ holdCenteredVisibleRange(chart, fromSec, toSec);
158
+ }
159
+ else {
160
+ chart.setVisibleRange({ from: fromSec, to: toSec }, { percentRightMargin: 0 });
161
+ }
162
+ (0, state_js_1.setSlbCenteringPending)(false);
163
+ }
164
+ catch (error) {
165
+ (0, bridge_js_1.reportErrorToRN)(error);
166
+ }
167
+ };
168
+ if (options?.immediate) {
169
+ applyCenter();
170
+ return;
171
+ }
172
+ const subscription = chart.onDataLoaded();
173
+ const onLoaded = () => {
174
+ subscription.unsubscribe(null, onLoaded);
175
+ applyCenter();
176
+ };
177
+ subscription.subscribe(null, onLoaded);
178
+ }
179
+ exports.slbCenterViewport = slbCenterViewport;
180
+ /** Test-only: reset the center-hold generation counter between cases. */
181
+ function _resetSocialLeaderboardForTests() {
182
+ centerHoldGeneration = 0;
183
+ }
184
+ exports._resetSocialLeaderboardForTests = _resetSocialLeaderboardForTests;
185
+ /**
186
+ * Schedules SLB viewport centering after the initial chart-ready event.
187
+ *
188
+ * Called from bootstrap's `onReady` callback. Only fires when the chart
189
+ * loaded with `slbMode` active — for all other consumers this is a no-op.
190
+ */
191
+ function slbScheduleInitialCentering() {
192
+ if (!(0, state_js_1.getSlbMode)() || !(0, state_js_1.isSlbCenteringPending)()) {
193
+ return;
194
+ }
195
+ const widget = (0, state_js_1.getWidget)();
196
+ if (!widget || !(0, state_js_1.isChartReady)()) {
197
+ return;
198
+ }
199
+ try {
200
+ const chart = widget.activeChart();
201
+ slbCenterViewport(chart);
202
+ }
203
+ catch (error) {
204
+ (0, bridge_js_1.reportErrorToRN)(error);
205
+ }
206
+ }
207
+ exports.slbScheduleInitialCentering = slbScheduleInitialCentering;
208
+ /**
209
+ * SLB back-fill pagination handler for datafeed.ts getBars.
210
+ *
211
+ * In SLB mode, RN has already sent the complete dataset — there is nothing
212
+ * to fetch. This function simply signals `noData: true` to TradingView,
213
+ * which stops it from requesting more bars.
214
+ *
215
+ * Returns `true` if it handled the request (caller should return early),
216
+ * or `false` if the caller should fall through to the other strategies.
217
+ *
218
+ * @param onResult - TradingView getBars result callback.
219
+ * @returns `true` when handled, `false` to fall through to other strategies.
220
+ */
221
+ function slbHandleGetBars(onResult) {
222
+ if (!(0, state_js_1.getSlbMode)()) {
223
+ return false;
224
+ }
225
+ onResult([], { noData: true });
226
+ return true;
227
+ }
228
+ exports.slbHandleGetBars = slbHandleGetBars;
229
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/overlays/socialLeaderboard/index.ts"],"names":[],"mappings":";AAAA,wEAAwE;AACxE,EAAE;AACF,uEAAuE;AACvE,2EAA2E;AAC3E,uBAAuB;AACvB,EAAE;AACF,6EAA6E;AAC7E,EAAE;AACF,4EAA4E;AAC5E,wEAAwE;AACxE,qCAAqC;AACrC,EAAE;AACF,8EAA8E;AAC9E,kDAAkD;AAClD,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,wDAAwD;AACxD,EAAE;AACF,0EAA0E;AAC1E,2EAA2E;AAC3E,+EAA+E;AAC/E,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,EAAE;AACF,oEAAoE;AACpE,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,oDAAoD;AACpD,EAAE;AACF,4EAA4E;AAC5E,EAAE;AACF,4EAA4E;AAC5E,yEAAyE;AACzE,wCAAwC;;;AAExC,qDAAuD;AACvD,mDAU6B;AAC7B,2DAAkE;AAGlE;;;;;;;GAOG;AACH,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B,sFAAsF;AACtF,IAAI,oBAA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AiB,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;IACzB,CAAC;AACH,CAAC;AAhBD,kEAgBC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,gBAAgB,CAC9B,QAA4D;IAE5D,IAAI,CAAC,IAAA,qBAAU,GAAE,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,QAAQ,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/B,OAAO,IAAI,CAAC;AACd,CAAC;AARD,4CAQC","sourcesContent":["// Social Leaderboard (SLB) viewport centering and back-fill pagination.\n//\n// Implements **Strategy C — SLB bulk back-fill**, the third pagination\n// strategy alongside Strategy A (Price API / Token Details) and Strategy B\n// (RN-backed / Perps).\n//\n// ── How it works ──────────────────────────────────────────────────────────\n//\n// 1. RN pre-loads the complete OHLCV dataset that covers the trade window\n// (visibleFromMs → visibleToMs) and sends it in one SET_OHLCV_DATA\n// message with `slbMode: true`.\n//\n// 2. The WebView stores the data in core/state and marks viewport centering\n// as pending (`slbCenteringPending = true`).\n//\n// 3. After the TradingView widget finishes its data load cycle, this module\n// centers the viewport on the trade window so the user sees all relevant\n// trades immediately (no manual scrolling needed).\n//\n// 4. getBars pagination is a no-op: because RN pre-loaded all data, any\n// getBars call for a time range outside the in-memory dataset returns\n// `{ noData: true }`. There is no second fetch — the dataset is complete.\n//\n// 5. When the user taps a different trade row, RN re-sends SET_OHLCV_DATA\n// with updated visibleFromMs / visibleToMs. The centering flag resets,\n// and step 3 fires again for the new window.\n//\n// ── Why a separate strategy? ──────────────────────────────────────────────\n//\n// Strategy A (Price API) and Strategy B (RN-backed) both paginate\n// incrementally — fetching one page at a time as the user scrolls left.\n// SLB needs the entire trade window visible from the start, so incremental\n// pagination would cause visible \"holes\" while pages load. Bulk back-fill\n// avoids this by having RN send all data upfront.\n//\n// ── Scoping via slbMode ──────────────────────────────────────────────────\n//\n// All SLB behavior is gated behind the `slbMode` flag so it cannot affect\n// Token Details, Perps, or any other consumer. When `slbMode` is false\n// (or omitted), this module is inert.\n\nimport { reportErrorToRN } from '../../core/bridge.js';\nimport {\n getOhlcvData,\n getOhlcvGeneration,\n getVisibleFromMs,\n getVisibleToMs,\n getSlbMode,\n isSlbCenteringPending,\n setSlbCenteringPending,\n getWidget,\n isChartReady,\n} from '../../core/state.js';\nimport { getApproxBarDurationSec } from '../../core/timeUtils.js';\nimport type { TVActiveChart } from '../../core/types.js';\n\n/**\n * How long (ms) to keep re-asserting the centered visible range for a\n * historical frame. A one-shot `setVisibleRange` issued during the post-load\n * settle is overridden by TradingView's default \"scroll to latest\" positioning,\n * so we re-apply every animation frame for this window — the same thing the\n * animated focusTime slide does implicitly, which a single auto-center call did\n * not. Kept short so it never fights a real user gesture.\n */\nconst CENTER_HOLD_MS = 700;\n\n/** Bumped on each hold so a newer center / fresh series cancels an in-flight hold. */\nlet centerHoldGeneration = 0;\n\n/**\n * Re-asserts `setVisibleRange({ from, to })` every animation frame for\n * {@link CENTER_HOLD_MS}, defeating TradingView's post-load scroll-to-latest\n * that clobbers a single call. Generation- and data-guarded so a newer center\n * or a fresh series stops it; no-ops once the widget is torn down.\n *\n * Deliberately omits the `{ percentRightMargin: 0 }` option: passing it makes\n * TradingView anchor to the latest candle and ignore an older from/to, so a\n * historical frame (an old position's trades) would snap back to \"today\".\n *\n * @param chart - The active TradingView chart.\n * @param fromSec - Left edge of the range, in unix seconds.\n * @param toSec - Right edge of the range, in unix seconds.\n */\nfunction holdCenteredVisibleRange(\n chart: TVActiveChart,\n fromSec: number,\n toSec: number,\n): void {\n centerHoldGeneration += 1;\n const gen = centerHoldGeneration;\n const dataGeneration = getOhlcvGeneration();\n const startTs = Date.now();\n\n const apply = (): void => {\n if (gen !== centerHoldGeneration) {\n return;\n }\n if (dataGeneration !== getOhlcvGeneration()) {\n return;\n }\n if (!getWidget() || !isChartReady()) {\n return;\n }\n try {\n chart.setVisibleRange({ from: fromSec, to: toSec });\n } catch {\n // setVisibleRange can throw every frame while the chart is mid-teardown;\n // swallow silently so the hold window doesn't spam errors to RN.\n }\n if (Date.now() - startTs < CENTER_HOLD_MS) {\n try {\n requestAnimationFrame(apply);\n } catch {\n setTimeout(apply, 16);\n }\n }\n };\n\n apply();\n}\n\n/**\n * Centers the viewport on the SLB trade window after a data load.\n *\n * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.\n * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar\n * padding on each side so the edge candles aren't glued to the chart border.\n *\n * The centering flag is cleared after success so subsequent REALTIME_UPDATE\n * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.\n *\n * When `options.immediate` is true, applies centering synchronously instead of\n * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has\n * already completed its data cycle by the time the callback fires.\n *\n * @param chart - The active TradingView chart.\n * @param options - Optional centering options.\n * @param options.immediate - When true, centers synchronously.\n */\nexport function slbCenterViewport(\n chart: TVActiveChart,\n options?: { immediate?: boolean },\n): void {\n if (!getSlbMode() || !isSlbCenteringPending()) {\n return;\n }\n\n const fromMs = getVisibleFromMs();\n const toMs = getVisibleToMs();\n if (fromMs === null || toMs === null) {\n return;\n }\n\n const capturedGeneration = getOhlcvGeneration();\n\n const applyCenter = (): void => {\n if (capturedGeneration !== getOhlcvGeneration()) {\n return;\n }\n if (!isSlbCenteringPending()) {\n return;\n }\n\n const data = getOhlcvData();\n const barPadSec = getApproxBarDurationSec(data) * 2;\n const fromSec = Math.floor(fromMs / 1000) - barPadSec;\n const toSec = Math.ceil(toMs / 1000) + barPadSec;\n\n // A frame whose right edge sits before the latest loaded bar is a historical\n // position (an old, closed trade range). TradingView's post-load\n // scroll-to-latest clobbers a single `setVisibleRange`, and\n // `percentRightMargin: 0` re-anchors it to the latest candle — together they\n // snap the viewport back to \"today\" and push the trades off-screen. Re-assert\n // the range across the settle window (without that option) so the historical\n // frame sticks. A frame ending at/after the latest bar is the trailing window:\n // TV doesn't fight it, so a single anchored call is enough.\n const lastBar = data.at(-1);\n const lastBarSec =\n lastBar === undefined ? null : Math.floor(lastBar.time / 1000);\n const isHistoricalFrame =\n lastBarSec !== null && Math.ceil(toMs / 1000) < lastBarSec;\n\n try {\n if (isHistoricalFrame) {\n holdCenteredVisibleRange(chart, fromSec, toSec);\n } else {\n chart.setVisibleRange(\n { from: fromSec, to: toSec },\n { percentRightMargin: 0 },\n );\n }\n setSlbCenteringPending(false);\n } catch (error) {\n reportErrorToRN(error);\n }\n };\n\n if (options?.immediate) {\n applyCenter();\n return;\n }\n\n const subscription = chart.onDataLoaded();\n const onLoaded = (): void => {\n subscription.unsubscribe(null, onLoaded);\n applyCenter();\n };\n subscription.subscribe(null, onLoaded);\n}\n\n/** Test-only: reset the center-hold generation counter between cases. */\nexport function _resetSocialLeaderboardForTests(): void {\n centerHoldGeneration = 0;\n}\n\n/**\n * Schedules SLB viewport centering after the initial chart-ready event.\n *\n * Called from bootstrap's `onReady` callback. Only fires when the chart\n * loaded with `slbMode` active — for all other consumers this is a no-op.\n */\nexport function slbScheduleInitialCentering(): void {\n if (!getSlbMode() || !isSlbCenteringPending()) {\n return;\n }\n\n const widget = getWidget();\n if (!widget || !isChartReady()) {\n return;\n }\n\n try {\n const chart = widget.activeChart();\n slbCenterViewport(chart);\n } catch (error) {\n reportErrorToRN(error);\n }\n}\n\n/**\n * SLB back-fill pagination handler for datafeed.ts getBars.\n *\n * In SLB mode, RN has already sent the complete dataset — there is nothing\n * to fetch. This function simply signals `noData: true` to TradingView,\n * which stops it from requesting more bars.\n *\n * Returns `true` if it handled the request (caller should return early),\n * or `false` if the caller should fall through to the other strategies.\n *\n * @param onResult - TradingView getBars result callback.\n * @returns `true` when handled, `false` to fall through to other strategies.\n */\nexport function slbHandleGetBars(\n onResult: (bars: never[], meta: { noData: boolean }) => void,\n): boolean {\n if (!getSlbMode()) {\n return false;\n }\n onResult([], { noData: true });\n return true;\n}\n"]}
@@ -0,0 +1,48 @@
1
+ import type { TVActiveChart } from "../../core/types.cjs";
2
+ /**
3
+ * Centers the viewport on the SLB trade window after a data load.
4
+ *
5
+ * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.
6
+ * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar
7
+ * padding on each side so the edge candles aren't glued to the chart border.
8
+ *
9
+ * The centering flag is cleared after success so subsequent REALTIME_UPDATE
10
+ * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.
11
+ *
12
+ * When `options.immediate` is true, applies centering synchronously instead of
13
+ * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has
14
+ * already completed its data cycle by the time the callback fires.
15
+ *
16
+ * @param chart - The active TradingView chart.
17
+ * @param options - Optional centering options.
18
+ * @param options.immediate - When true, centers synchronously.
19
+ */
20
+ export declare function slbCenterViewport(chart: TVActiveChart, options?: {
21
+ immediate?: boolean;
22
+ }): void;
23
+ /** Test-only: reset the center-hold generation counter between cases. */
24
+ export declare function _resetSocialLeaderboardForTests(): void;
25
+ /**
26
+ * Schedules SLB viewport centering after the initial chart-ready event.
27
+ *
28
+ * Called from bootstrap's `onReady` callback. Only fires when the chart
29
+ * loaded with `slbMode` active — for all other consumers this is a no-op.
30
+ */
31
+ export declare function slbScheduleInitialCentering(): void;
32
+ /**
33
+ * SLB back-fill pagination handler for datafeed.ts getBars.
34
+ *
35
+ * In SLB mode, RN has already sent the complete dataset — there is nothing
36
+ * to fetch. This function simply signals `noData: true` to TradingView,
37
+ * which stops it from requesting more bars.
38
+ *
39
+ * Returns `true` if it handled the request (caller should return early),
40
+ * or `false` if the caller should fall through to the other strategies.
41
+ *
42
+ * @param onResult - TradingView getBars result callback.
43
+ * @returns `true` when handled, `false` to fall through to other strategies.
44
+ */
45
+ export declare function slbHandleGetBars(onResult: (bars: never[], meta: {
46
+ noData: boolean;
47
+ }) => void): boolean;
48
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/overlays/socialLeaderboard/index.ts"],"names":[],"mappings":"AAsDA,OAAO,KAAK,EAAE,aAAa,EAAE,6BAA4B;AAmEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,aAAa,EACpB,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,GAChC,IAAI,CAkEN;AAED,yEAAyE;AACzE,wBAAgB,+BAA+B,IAAI,IAAI,CAEtD;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,IAAI,CAgBlD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,KAAK,IAAI,GAC3D,OAAO,CAMT"}
@@ -0,0 +1,48 @@
1
+ import type { TVActiveChart } from "../../core/types.mjs";
2
+ /**
3
+ * Centers the viewport on the SLB trade window after a data load.
4
+ *
5
+ * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.
6
+ * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar
7
+ * padding on each side so the edge candles aren't glued to the chart border.
8
+ *
9
+ * The centering flag is cleared after success so subsequent REALTIME_UPDATE
10
+ * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.
11
+ *
12
+ * When `options.immediate` is true, applies centering synchronously instead of
13
+ * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has
14
+ * already completed its data cycle by the time the callback fires.
15
+ *
16
+ * @param chart - The active TradingView chart.
17
+ * @param options - Optional centering options.
18
+ * @param options.immediate - When true, centers synchronously.
19
+ */
20
+ export declare function slbCenterViewport(chart: TVActiveChart, options?: {
21
+ immediate?: boolean;
22
+ }): void;
23
+ /** Test-only: reset the center-hold generation counter between cases. */
24
+ export declare function _resetSocialLeaderboardForTests(): void;
25
+ /**
26
+ * Schedules SLB viewport centering after the initial chart-ready event.
27
+ *
28
+ * Called from bootstrap's `onReady` callback. Only fires when the chart
29
+ * loaded with `slbMode` active — for all other consumers this is a no-op.
30
+ */
31
+ export declare function slbScheduleInitialCentering(): void;
32
+ /**
33
+ * SLB back-fill pagination handler for datafeed.ts getBars.
34
+ *
35
+ * In SLB mode, RN has already sent the complete dataset — there is nothing
36
+ * to fetch. This function simply signals `noData: true` to TradingView,
37
+ * which stops it from requesting more bars.
38
+ *
39
+ * Returns `true` if it handled the request (caller should return early),
40
+ * or `false` if the caller should fall through to the other strategies.
41
+ *
42
+ * @param onResult - TradingView getBars result callback.
43
+ * @returns `true` when handled, `false` to fall through to other strategies.
44
+ */
45
+ export declare function slbHandleGetBars(onResult: (bars: never[], meta: {
46
+ noData: boolean;
47
+ }) => void): boolean;
48
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/overlays/socialLeaderboard/index.ts"],"names":[],"mappings":"AAsDA,OAAO,KAAK,EAAE,aAAa,EAAE,6BAA4B;AAmEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,aAAa,EACpB,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,GAChC,IAAI,CAkEN;AAED,yEAAyE;AACzE,wBAAgB,+BAA+B,IAAI,IAAI,CAEtD;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,IAAI,CAgBlD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,KAAK,IAAI,GAC3D,OAAO,CAMT"}
@@ -0,0 +1,222 @@
1
+ // Social Leaderboard (SLB) viewport centering and back-fill pagination.
2
+ //
3
+ // Implements **Strategy C — SLB bulk back-fill**, the third pagination
4
+ // strategy alongside Strategy A (Price API / Token Details) and Strategy B
5
+ // (RN-backed / Perps).
6
+ //
7
+ // ── How it works ──────────────────────────────────────────────────────────
8
+ //
9
+ // 1. RN pre-loads the complete OHLCV dataset that covers the trade window
10
+ // (visibleFromMs → visibleToMs) and sends it in one SET_OHLCV_DATA
11
+ // message with `slbMode: true`.
12
+ //
13
+ // 2. The WebView stores the data in core/state and marks viewport centering
14
+ // as pending (`slbCenteringPending = true`).
15
+ //
16
+ // 3. After the TradingView widget finishes its data load cycle, this module
17
+ // centers the viewport on the trade window so the user sees all relevant
18
+ // trades immediately (no manual scrolling needed).
19
+ //
20
+ // 4. getBars pagination is a no-op: because RN pre-loaded all data, any
21
+ // getBars call for a time range outside the in-memory dataset returns
22
+ // `{ noData: true }`. There is no second fetch — the dataset is complete.
23
+ //
24
+ // 5. When the user taps a different trade row, RN re-sends SET_OHLCV_DATA
25
+ // with updated visibleFromMs / visibleToMs. The centering flag resets,
26
+ // and step 3 fires again for the new window.
27
+ //
28
+ // ── Why a separate strategy? ──────────────────────────────────────────────
29
+ //
30
+ // Strategy A (Price API) and Strategy B (RN-backed) both paginate
31
+ // incrementally — fetching one page at a time as the user scrolls left.
32
+ // SLB needs the entire trade window visible from the start, so incremental
33
+ // pagination would cause visible "holes" while pages load. Bulk back-fill
34
+ // avoids this by having RN send all data upfront.
35
+ //
36
+ // ── Scoping via slbMode ──────────────────────────────────────────────────
37
+ //
38
+ // All SLB behavior is gated behind the `slbMode` flag so it cannot affect
39
+ // Token Details, Perps, or any other consumer. When `slbMode` is false
40
+ // (or omitted), this module is inert.
41
+ import { reportErrorToRN } from "../../core/bridge.mjs";
42
+ import { getOhlcvData, getOhlcvGeneration, getVisibleFromMs, getVisibleToMs, getSlbMode, isSlbCenteringPending, setSlbCenteringPending, getWidget, isChartReady } from "../../core/state.mjs";
43
+ import { getApproxBarDurationSec } from "../../core/timeUtils.mjs";
44
+ /**
45
+ * How long (ms) to keep re-asserting the centered visible range for a
46
+ * historical frame. A one-shot `setVisibleRange` issued during the post-load
47
+ * settle is overridden by TradingView's default "scroll to latest" positioning,
48
+ * so we re-apply every animation frame for this window — the same thing the
49
+ * animated focusTime slide does implicitly, which a single auto-center call did
50
+ * not. Kept short so it never fights a real user gesture.
51
+ */
52
+ const CENTER_HOLD_MS = 700;
53
+ /** Bumped on each hold so a newer center / fresh series cancels an in-flight hold. */
54
+ let centerHoldGeneration = 0;
55
+ /**
56
+ * Re-asserts `setVisibleRange({ from, to })` every animation frame for
57
+ * {@link CENTER_HOLD_MS}, defeating TradingView's post-load scroll-to-latest
58
+ * that clobbers a single call. Generation- and data-guarded so a newer center
59
+ * or a fresh series stops it; no-ops once the widget is torn down.
60
+ *
61
+ * Deliberately omits the `{ percentRightMargin: 0 }` option: passing it makes
62
+ * TradingView anchor to the latest candle and ignore an older from/to, so a
63
+ * historical frame (an old position's trades) would snap back to "today".
64
+ *
65
+ * @param chart - The active TradingView chart.
66
+ * @param fromSec - Left edge of the range, in unix seconds.
67
+ * @param toSec - Right edge of the range, in unix seconds.
68
+ */
69
+ function holdCenteredVisibleRange(chart, fromSec, toSec) {
70
+ centerHoldGeneration += 1;
71
+ const gen = centerHoldGeneration;
72
+ const dataGeneration = getOhlcvGeneration();
73
+ const startTs = Date.now();
74
+ const apply = () => {
75
+ if (gen !== centerHoldGeneration) {
76
+ return;
77
+ }
78
+ if (dataGeneration !== getOhlcvGeneration()) {
79
+ return;
80
+ }
81
+ if (!getWidget() || !isChartReady()) {
82
+ return;
83
+ }
84
+ try {
85
+ chart.setVisibleRange({ from: fromSec, to: toSec });
86
+ }
87
+ catch {
88
+ // setVisibleRange can throw every frame while the chart is mid-teardown;
89
+ // swallow silently so the hold window doesn't spam errors to RN.
90
+ }
91
+ if (Date.now() - startTs < CENTER_HOLD_MS) {
92
+ try {
93
+ requestAnimationFrame(apply);
94
+ }
95
+ catch {
96
+ setTimeout(apply, 16);
97
+ }
98
+ }
99
+ };
100
+ apply();
101
+ }
102
+ /**
103
+ * Centers the viewport on the SLB trade window after a data load.
104
+ *
105
+ * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.
106
+ * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar
107
+ * padding on each side so the edge candles aren't glued to the chart border.
108
+ *
109
+ * The centering flag is cleared after success so subsequent REALTIME_UPDATE
110
+ * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.
111
+ *
112
+ * When `options.immediate` is true, applies centering synchronously instead of
113
+ * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has
114
+ * already completed its data cycle by the time the callback fires.
115
+ *
116
+ * @param chart - The active TradingView chart.
117
+ * @param options - Optional centering options.
118
+ * @param options.immediate - When true, centers synchronously.
119
+ */
120
+ export function slbCenterViewport(chart, options) {
121
+ if (!getSlbMode() || !isSlbCenteringPending()) {
122
+ return;
123
+ }
124
+ const fromMs = getVisibleFromMs();
125
+ const toMs = getVisibleToMs();
126
+ if (fromMs === null || toMs === null) {
127
+ return;
128
+ }
129
+ const capturedGeneration = getOhlcvGeneration();
130
+ const applyCenter = () => {
131
+ if (capturedGeneration !== getOhlcvGeneration()) {
132
+ return;
133
+ }
134
+ if (!isSlbCenteringPending()) {
135
+ return;
136
+ }
137
+ const data = getOhlcvData();
138
+ const barPadSec = getApproxBarDurationSec(data) * 2;
139
+ const fromSec = Math.floor(fromMs / 1000) - barPadSec;
140
+ const toSec = Math.ceil(toMs / 1000) + barPadSec;
141
+ // A frame whose right edge sits before the latest loaded bar is a historical
142
+ // position (an old, closed trade range). TradingView's post-load
143
+ // scroll-to-latest clobbers a single `setVisibleRange`, and
144
+ // `percentRightMargin: 0` re-anchors it to the latest candle — together they
145
+ // snap the viewport back to "today" and push the trades off-screen. Re-assert
146
+ // the range across the settle window (without that option) so the historical
147
+ // frame sticks. A frame ending at/after the latest bar is the trailing window:
148
+ // TV doesn't fight it, so a single anchored call is enough.
149
+ const lastBar = data.at(-1);
150
+ const lastBarSec = lastBar === undefined ? null : Math.floor(lastBar.time / 1000);
151
+ const isHistoricalFrame = lastBarSec !== null && Math.ceil(toMs / 1000) < lastBarSec;
152
+ try {
153
+ if (isHistoricalFrame) {
154
+ holdCenteredVisibleRange(chart, fromSec, toSec);
155
+ }
156
+ else {
157
+ chart.setVisibleRange({ from: fromSec, to: toSec }, { percentRightMargin: 0 });
158
+ }
159
+ setSlbCenteringPending(false);
160
+ }
161
+ catch (error) {
162
+ reportErrorToRN(error);
163
+ }
164
+ };
165
+ if (options?.immediate) {
166
+ applyCenter();
167
+ return;
168
+ }
169
+ const subscription = chart.onDataLoaded();
170
+ const onLoaded = () => {
171
+ subscription.unsubscribe(null, onLoaded);
172
+ applyCenter();
173
+ };
174
+ subscription.subscribe(null, onLoaded);
175
+ }
176
+ /** Test-only: reset the center-hold generation counter between cases. */
177
+ export function _resetSocialLeaderboardForTests() {
178
+ centerHoldGeneration = 0;
179
+ }
180
+ /**
181
+ * Schedules SLB viewport centering after the initial chart-ready event.
182
+ *
183
+ * Called from bootstrap's `onReady` callback. Only fires when the chart
184
+ * loaded with `slbMode` active — for all other consumers this is a no-op.
185
+ */
186
+ export function slbScheduleInitialCentering() {
187
+ if (!getSlbMode() || !isSlbCenteringPending()) {
188
+ return;
189
+ }
190
+ const widget = getWidget();
191
+ if (!widget || !isChartReady()) {
192
+ return;
193
+ }
194
+ try {
195
+ const chart = widget.activeChart();
196
+ slbCenterViewport(chart);
197
+ }
198
+ catch (error) {
199
+ reportErrorToRN(error);
200
+ }
201
+ }
202
+ /**
203
+ * SLB back-fill pagination handler for datafeed.ts getBars.
204
+ *
205
+ * In SLB mode, RN has already sent the complete dataset — there is nothing
206
+ * to fetch. This function simply signals `noData: true` to TradingView,
207
+ * which stops it from requesting more bars.
208
+ *
209
+ * Returns `true` if it handled the request (caller should return early),
210
+ * or `false` if the caller should fall through to the other strategies.
211
+ *
212
+ * @param onResult - TradingView getBars result callback.
213
+ * @returns `true` when handled, `false` to fall through to other strategies.
214
+ */
215
+ export function slbHandleGetBars(onResult) {
216
+ if (!getSlbMode()) {
217
+ return false;
218
+ }
219
+ onResult([], { noData: true });
220
+ return true;
221
+ }
222
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/overlays/socialLeaderboard/index.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,EAAE;AACF,uEAAuE;AACvE,2EAA2E;AAC3E,uBAAuB;AACvB,EAAE;AACF,6EAA6E;AAC7E,EAAE;AACF,4EAA4E;AAC5E,wEAAwE;AACxE,qCAAqC;AACrC,EAAE;AACF,8EAA8E;AAC9E,kDAAkD;AAClD,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,wDAAwD;AACxD,EAAE;AACF,0EAA0E;AAC1E,2EAA2E;AAC3E,+EAA+E;AAC/E,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,EAAE;AACF,oEAAoE;AACpE,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,oDAAoD;AACpD,EAAE;AACF,4EAA4E;AAC5E,EAAE;AACF,4EAA4E;AAC5E,yEAAyE;AACzE,wCAAwC;AAExC,OAAO,EAAE,eAAe,EAAE,8BAA6B;AACvD,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,qBAAqB,EACrB,sBAAsB,EACtB,SAAS,EACT,YAAY,EACb,6BAA4B;AAC7B,OAAO,EAAE,uBAAuB,EAAE,iCAAgC;AAGlE;;;;;;;GAOG;AACH,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B,sFAAsF;AACtF,IAAI,oBAAoB,GAAG,CAAC,CAAC;AAE7B;;;;;;;;;;;;;GAaG;AACH,SA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IAE5D,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,QAAQ,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["// Social Leaderboard (SLB) viewport centering and back-fill pagination.\n//\n// Implements **Strategy C — SLB bulk back-fill**, the third pagination\n// strategy alongside Strategy A (Price API / Token Details) and Strategy B\n// (RN-backed / Perps).\n//\n// ── How it works ──────────────────────────────────────────────────────────\n//\n// 1. RN pre-loads the complete OHLCV dataset that covers the trade window\n// (visibleFromMs → visibleToMs) and sends it in one SET_OHLCV_DATA\n// message with `slbMode: true`.\n//\n// 2. The WebView stores the data in core/state and marks viewport centering\n// as pending (`slbCenteringPending = true`).\n//\n// 3. After the TradingView widget finishes its data load cycle, this module\n// centers the viewport on the trade window so the user sees all relevant\n// trades immediately (no manual scrolling needed).\n//\n// 4. getBars pagination is a no-op: because RN pre-loaded all data, any\n// getBars call for a time range outside the in-memory dataset returns\n// `{ noData: true }`. There is no second fetch — the dataset is complete.\n//\n// 5. When the user taps a different trade row, RN re-sends SET_OHLCV_DATA\n// with updated visibleFromMs / visibleToMs. The centering flag resets,\n// and step 3 fires again for the new window.\n//\n// ── Why a separate strategy? ──────────────────────────────────────────────\n//\n// Strategy A (Price API) and Strategy B (RN-backed) both paginate\n// incrementally — fetching one page at a time as the user scrolls left.\n// SLB needs the entire trade window visible from the start, so incremental\n// pagination would cause visible \"holes\" while pages load. Bulk back-fill\n// avoids this by having RN send all data upfront.\n//\n// ── Scoping via slbMode ──────────────────────────────────────────────────\n//\n// All SLB behavior is gated behind the `slbMode` flag so it cannot affect\n// Token Details, Perps, or any other consumer. When `slbMode` is false\n// (or omitted), this module is inert.\n\nimport { reportErrorToRN } from '../../core/bridge.js';\nimport {\n getOhlcvData,\n getOhlcvGeneration,\n getVisibleFromMs,\n getVisibleToMs,\n getSlbMode,\n isSlbCenteringPending,\n setSlbCenteringPending,\n getWidget,\n isChartReady,\n} from '../../core/state.js';\nimport { getApproxBarDurationSec } from '../../core/timeUtils.js';\nimport type { TVActiveChart } from '../../core/types.js';\n\n/**\n * How long (ms) to keep re-asserting the centered visible range for a\n * historical frame. A one-shot `setVisibleRange` issued during the post-load\n * settle is overridden by TradingView's default \"scroll to latest\" positioning,\n * so we re-apply every animation frame for this window — the same thing the\n * animated focusTime slide does implicitly, which a single auto-center call did\n * not. Kept short so it never fights a real user gesture.\n */\nconst CENTER_HOLD_MS = 700;\n\n/** Bumped on each hold so a newer center / fresh series cancels an in-flight hold. */\nlet centerHoldGeneration = 0;\n\n/**\n * Re-asserts `setVisibleRange({ from, to })` every animation frame for\n * {@link CENTER_HOLD_MS}, defeating TradingView's post-load scroll-to-latest\n * that clobbers a single call. Generation- and data-guarded so a newer center\n * or a fresh series stops it; no-ops once the widget is torn down.\n *\n * Deliberately omits the `{ percentRightMargin: 0 }` option: passing it makes\n * TradingView anchor to the latest candle and ignore an older from/to, so a\n * historical frame (an old position's trades) would snap back to \"today\".\n *\n * @param chart - The active TradingView chart.\n * @param fromSec - Left edge of the range, in unix seconds.\n * @param toSec - Right edge of the range, in unix seconds.\n */\nfunction holdCenteredVisibleRange(\n chart: TVActiveChart,\n fromSec: number,\n toSec: number,\n): void {\n centerHoldGeneration += 1;\n const gen = centerHoldGeneration;\n const dataGeneration = getOhlcvGeneration();\n const startTs = Date.now();\n\n const apply = (): void => {\n if (gen !== centerHoldGeneration) {\n return;\n }\n if (dataGeneration !== getOhlcvGeneration()) {\n return;\n }\n if (!getWidget() || !isChartReady()) {\n return;\n }\n try {\n chart.setVisibleRange({ from: fromSec, to: toSec });\n } catch {\n // setVisibleRange can throw every frame while the chart is mid-teardown;\n // swallow silently so the hold window doesn't spam errors to RN.\n }\n if (Date.now() - startTs < CENTER_HOLD_MS) {\n try {\n requestAnimationFrame(apply);\n } catch {\n setTimeout(apply, 16);\n }\n }\n };\n\n apply();\n}\n\n/**\n * Centers the viewport on the SLB trade window after a data load.\n *\n * Called from ohlcvIngestion's `applyVisibleRange` when `slbMode` is active.\n * Uses `visibleFromMs` / `visibleToMs` to frame the viewport, with a 2-bar\n * padding on each side so the edge candles aren't glued to the chart border.\n *\n * The centering flag is cleared after success so subsequent REALTIME_UPDATE\n * messages don't re-trigger the slide. Only a fresh SET_OHLCV_DATA resets it.\n *\n * When `options.immediate` is true, applies centering synchronously instead of\n * waiting for `onDataLoaded`. Used after `setResolution` where TradingView has\n * already completed its data cycle by the time the callback fires.\n *\n * @param chart - The active TradingView chart.\n * @param options - Optional centering options.\n * @param options.immediate - When true, centers synchronously.\n */\nexport function slbCenterViewport(\n chart: TVActiveChart,\n options?: { immediate?: boolean },\n): void {\n if (!getSlbMode() || !isSlbCenteringPending()) {\n return;\n }\n\n const fromMs = getVisibleFromMs();\n const toMs = getVisibleToMs();\n if (fromMs === null || toMs === null) {\n return;\n }\n\n const capturedGeneration = getOhlcvGeneration();\n\n const applyCenter = (): void => {\n if (capturedGeneration !== getOhlcvGeneration()) {\n return;\n }\n if (!isSlbCenteringPending()) {\n return;\n }\n\n const data = getOhlcvData();\n const barPadSec = getApproxBarDurationSec(data) * 2;\n const fromSec = Math.floor(fromMs / 1000) - barPadSec;\n const toSec = Math.ceil(toMs / 1000) + barPadSec;\n\n // A frame whose right edge sits before the latest loaded bar is a historical\n // position (an old, closed trade range). TradingView's post-load\n // scroll-to-latest clobbers a single `setVisibleRange`, and\n // `percentRightMargin: 0` re-anchors it to the latest candle — together they\n // snap the viewport back to \"today\" and push the trades off-screen. Re-assert\n // the range across the settle window (without that option) so the historical\n // frame sticks. A frame ending at/after the latest bar is the trailing window:\n // TV doesn't fight it, so a single anchored call is enough.\n const lastBar = data.at(-1);\n const lastBarSec =\n lastBar === undefined ? null : Math.floor(lastBar.time / 1000);\n const isHistoricalFrame =\n lastBarSec !== null && Math.ceil(toMs / 1000) < lastBarSec;\n\n try {\n if (isHistoricalFrame) {\n holdCenteredVisibleRange(chart, fromSec, toSec);\n } else {\n chart.setVisibleRange(\n { from: fromSec, to: toSec },\n { percentRightMargin: 0 },\n );\n }\n setSlbCenteringPending(false);\n } catch (error) {\n reportErrorToRN(error);\n }\n };\n\n if (options?.immediate) {\n applyCenter();\n return;\n }\n\n const subscription = chart.onDataLoaded();\n const onLoaded = (): void => {\n subscription.unsubscribe(null, onLoaded);\n applyCenter();\n };\n subscription.subscribe(null, onLoaded);\n}\n\n/** Test-only: reset the center-hold generation counter between cases. */\nexport function _resetSocialLeaderboardForTests(): void {\n centerHoldGeneration = 0;\n}\n\n/**\n * Schedules SLB viewport centering after the initial chart-ready event.\n *\n * Called from bootstrap's `onReady` callback. Only fires when the chart\n * loaded with `slbMode` active — for all other consumers this is a no-op.\n */\nexport function slbScheduleInitialCentering(): void {\n if (!getSlbMode() || !isSlbCenteringPending()) {\n return;\n }\n\n const widget = getWidget();\n if (!widget || !isChartReady()) {\n return;\n }\n\n try {\n const chart = widget.activeChart();\n slbCenterViewport(chart);\n } catch (error) {\n reportErrorToRN(error);\n }\n}\n\n/**\n * SLB back-fill pagination handler for datafeed.ts getBars.\n *\n * In SLB mode, RN has already sent the complete dataset — there is nothing\n * to fetch. This function simply signals `noData: true` to TradingView,\n * which stops it from requesting more bars.\n *\n * Returns `true` if it handled the request (caller should return early),\n * or `false` if the caller should fall through to the other strategies.\n *\n * @param onResult - TradingView getBars result callback.\n * @returns `true` when handled, `false` to fall through to other strategies.\n */\nexport function slbHandleGetBars(\n onResult: (bars: never[], meta: { noData: boolean }) => void,\n): boolean {\n if (!getSlbMode()) {\n return false;\n }\n onResult([], { noData: true });\n return true;\n}\n"]}