oakscriptjs 0.15.1 → 0.15.3

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 (103) hide show
  1. package/dist/cjs/callsite/index.d.ts.map +1 -1
  2. package/dist/cjs/callsite/index.js +61 -7
  3. package/dist/cjs/callsite/index.js.map +2 -2
  4. package/dist/cjs/drawing/registry.d.ts +31 -3
  5. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  6. package/dist/cjs/drawing/registry.js +188 -68
  7. package/dist/cjs/drawing/registry.js.map +2 -2
  8. package/dist/cjs/index.d.ts +1 -1
  9. package/dist/cjs/index.js +1 -1
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/linefill/index.d.ts.map +1 -1
  12. package/dist/cjs/linefill/index.js +14 -5
  13. package/dist/cjs/linefill/index.js.map +2 -2
  14. package/dist/cjs/session/bars.d.ts +3 -0
  15. package/dist/cjs/session/bars.d.ts.map +1 -1
  16. package/dist/cjs/session/bars.js +10 -3
  17. package/dist/cjs/session/bars.js.map +2 -2
  18. package/dist/cjs/session/calendar.d.ts +19 -6
  19. package/dist/cjs/session/calendar.d.ts.map +1 -1
  20. package/dist/cjs/session/calendar.js +57 -18
  21. package/dist/cjs/session/calendar.js.map +2 -2
  22. package/dist/cjs/session/context.d.ts.map +1 -1
  23. package/dist/cjs/session/context.js +25 -8
  24. package/dist/cjs/session/context.js.map +2 -2
  25. package/dist/cjs/str/floatingdecimal.d.ts +3 -0
  26. package/dist/cjs/str/floatingdecimal.d.ts.map +1 -1
  27. package/dist/cjs/str/floatingdecimal.js +25 -12
  28. package/dist/cjs/str/floatingdecimal.js.map +2 -2
  29. package/dist/cjs/str/messageformat.d.ts.map +1 -1
  30. package/dist/cjs/str/messageformat.js +27 -8
  31. package/dist/cjs/str/messageformat.js.map +2 -2
  32. package/dist/cjs/str/numberformat.d.ts.map +1 -1
  33. package/dist/cjs/str/numberformat.js +9 -16
  34. package/dist/cjs/str/numberformat.js.map +3 -3
  35. package/dist/cjs/ta/index.d.ts.map +1 -1
  36. package/dist/cjs/ta/index.js +27 -0
  37. package/dist/cjs/ta/index.js.map +2 -2
  38. package/dist/cjs/ta/running-sum.d.ts +8 -1
  39. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  40. package/dist/cjs/ta/running-sum.js +13 -1
  41. package/dist/cjs/ta/running-sum.js.map +2 -2
  42. package/dist/cjs/time/timezone.d.ts +13 -1
  43. package/dist/cjs/time/timezone.d.ts.map +1 -1
  44. package/dist/cjs/time/timezone.js +64 -12
  45. package/dist/cjs/time/timezone.js.map +2 -2
  46. package/dist/esm/callsite/index.d.ts.map +1 -1
  47. package/dist/esm/callsite/index.js +76 -7
  48. package/dist/esm/callsite/index.js.map +1 -1
  49. package/dist/esm/drawing/registry.d.ts +31 -3
  50. package/dist/esm/drawing/registry.d.ts.map +1 -1
  51. package/dist/esm/drawing/registry.js +237 -80
  52. package/dist/esm/drawing/registry.js.map +1 -1
  53. package/dist/esm/index.d.ts +1 -1
  54. package/dist/esm/index.js +1 -1
  55. package/dist/esm/linefill/index.d.ts.map +1 -1
  56. package/dist/esm/linefill/index.js +17 -7
  57. package/dist/esm/linefill/index.js.map +1 -1
  58. package/dist/esm/session/bars.d.ts +3 -0
  59. package/dist/esm/session/bars.d.ts.map +1 -1
  60. package/dist/esm/session/bars.js +11 -3
  61. package/dist/esm/session/bars.js.map +1 -1
  62. package/dist/esm/session/calendar.d.ts +19 -6
  63. package/dist/esm/session/calendar.d.ts.map +1 -1
  64. package/dist/esm/session/calendar.js +74 -21
  65. package/dist/esm/session/calendar.js.map +1 -1
  66. package/dist/esm/session/context.d.ts.map +1 -1
  67. package/dist/esm/session/context.js +34 -9
  68. package/dist/esm/session/context.js.map +1 -1
  69. package/dist/esm/str/floatingdecimal.d.ts +3 -0
  70. package/dist/esm/str/floatingdecimal.d.ts.map +1 -1
  71. package/dist/esm/str/floatingdecimal.js +27 -11
  72. package/dist/esm/str/floatingdecimal.js.map +1 -1
  73. package/dist/esm/str/messageformat.d.ts.map +1 -1
  74. package/dist/esm/str/messageformat.js +35 -9
  75. package/dist/esm/str/messageformat.js.map +1 -1
  76. package/dist/esm/str/numberformat.d.ts.map +1 -1
  77. package/dist/esm/str/numberformat.js +12 -18
  78. package/dist/esm/str/numberformat.js.map +1 -1
  79. package/dist/esm/ta/index.d.ts.map +1 -1
  80. package/dist/esm/ta/index.js +35 -0
  81. package/dist/esm/ta/index.js.map +1 -1
  82. package/dist/esm/ta/running-sum.d.ts +8 -1
  83. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  84. package/dist/esm/ta/running-sum.js +16 -1
  85. package/dist/esm/ta/running-sum.js.map +1 -1
  86. package/dist/esm/time/timezone.d.ts +13 -1
  87. package/dist/esm/time/timezone.d.ts.map +1 -1
  88. package/dist/esm/time/timezone.js +97 -16
  89. package/dist/esm/time/timezone.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/callsite/index.ts +65 -7
  92. package/src/drawing/registry.ts +249 -79
  93. package/src/index.ts +1 -1
  94. package/src/linefill/index.ts +16 -7
  95. package/src/session/bars.ts +11 -3
  96. package/src/session/calendar.ts +84 -21
  97. package/src/session/context.ts +36 -9
  98. package/src/str/floatingdecimal.ts +27 -11
  99. package/src/str/messageformat.ts +40 -9
  100. package/src/str/numberformat.ts +13 -17
  101. package/src/ta/index.ts +41 -0
  102. package/src/ta/running-sum.ts +13 -1
  103. package/src/time/timezone.ts +99 -16
@@ -71,13 +71,87 @@ const SLACK = 5;
71
71
 
72
72
  export type DrawingLimits = Partial<Record<Exclude<DrawingKind, 'linefill' | 'table'>, number>>;
73
73
 
74
- const live: { [K in DrawingKind]: Array<DrawingTypes[K]> } = {
75
- line: [],
76
- label: [],
77
- box: [],
78
- polyline: [],
79
- linefill: [],
80
- table: [],
74
+ /**
75
+ * The live objects of a kind, in creation order. The maximum count deletes the oldest ones: they leave by moving the
76
+ * head, and the array drops its dead front once it is as long as the live part, so a creation at the maximum costs
77
+ * O(1) amortised instead of a move of the whole list on every cut.
78
+ */
79
+ class LiveList<T> {
80
+ private items: T[] = [];
81
+ private head = 0;
82
+
83
+ get length(): number {
84
+ return this.items.length - this.head;
85
+ }
86
+
87
+ /** The `k`-th live object (0: the oldest) */
88
+ at(k: number): T {
89
+ return this.items[this.head + k]!;
90
+ }
91
+
92
+ push(obj: T): void {
93
+ this.items.push(obj);
94
+ }
95
+
96
+ /** Position of `obj` among the live objects, -1 when it is not one */
97
+ lastIndexOf(obj: T): number {
98
+ const index = this.items.lastIndexOf(obj);
99
+ return index >= this.head ? index - this.head : -1;
100
+ }
101
+
102
+ removeAt(k: number): void {
103
+ if (k === 0) this.dropFront(1);
104
+ else this.items.splice(this.head + k, 1);
105
+ }
106
+
107
+ /** Removes the live objects at positions `gone` (increasing); the others keep their order */
108
+ removeAll(gone: readonly number[]): void {
109
+ if (gone.length === 0) return;
110
+ if (gone[0] === 0 && gone[gone.length - 1] === gone.length - 1) {
111
+ this.dropFront(gone.length);
112
+ return;
113
+ }
114
+ const items = this.items;
115
+ let to = this.head + gone[0]!;
116
+ for (let from = to, g = 0; from < items.length; from++) {
117
+ if (from - this.head === gone[g]) g++;
118
+ else items[to++] = items[from]!;
119
+ }
120
+ items.length = to;
121
+ }
122
+
123
+ /** Removes the `n` oldest objects and returns them */
124
+ takeFront(n: number): T[] {
125
+ const out = this.items.slice(this.head, this.head + n);
126
+ this.dropFront(n);
127
+ return out;
128
+ }
129
+
130
+ private dropFront(n: number): void {
131
+ this.head += n;
132
+ if (this.head >= this.length) {
133
+ this.items = this.items.slice(this.head);
134
+ this.head = 0;
135
+ }
136
+ }
137
+
138
+ toArray(): T[] {
139
+ return this.items.slice(this.head);
140
+ }
141
+
142
+ replace(objs: readonly T[]): void {
143
+ this.items = [...objs];
144
+ this.head = 0;
145
+ }
146
+ }
147
+
148
+ const live: { [K in DrawingKind]: LiveList<DrawingTypes[K]> } = {
149
+ line: new LiveList(),
150
+ label: new LiveList(),
151
+ box: new LiveList(),
152
+ polyline: new LiveList(),
153
+ linefill: new LiveList(),
154
+ table: new LiveList(),
81
155
  };
82
156
  let limits: Required<DrawingLimits> = {
83
157
  line: DEFAULT_MAX_COUNT,
@@ -85,16 +159,78 @@ let limits: Required<DrawingLimits> = {
85
159
  box: DEFAULT_MAX_COUNT,
86
160
  polyline: DEFAULT_MAX_COUNT,
87
161
  };
88
- const deleted = new WeakSet<object>();
89
- /** Objects held by a `var` variable, with the number of variables that hold them. */
90
- let held = new WeakMap<object, number>();
91
- /** Bar on which the last `var` variable released an object (linefill rule). */
92
- let releasedAt = new WeakMap<object, number>();
162
+ /**
163
+ * Base of the classes that add a private field to an existing object (their constructor returns the object they are
164
+ * given). Such a field replaces a WeakMap entry: a study that creates hundreds of thousands of objects spends most of
165
+ * its drawing time adding WeakMap keys. The field is invisible (not a key, not copied by a spread) and stays for the
166
+ * life of the object, as the entry did.
167
+ */
168
+ export class Stamped {
169
+ constructor(obj: object) {
170
+ return obj;
171
+ }
172
+ }
173
+
174
+ /** The deleted objects. */
175
+ class Deleted extends Stamped {
176
+ #deleted = true;
177
+ static mark(obj: object): void {
178
+ if (!Deleted.has(obj)) new Deleted(obj);
179
+ }
180
+ static has(obj: object): boolean {
181
+ return #deleted in obj;
182
+ }
183
+ }
184
+
185
+ /** The `var` records of a run: a reset starts new ones, a nested run saves and puts back those of its run. */
186
+ interface Holds {
187
+ /** The records of the objects whose private record belongs to another run */
188
+ readonly others: WeakMap<object, HoldRecord>;
189
+ /** Records made in the run. None: no object has one, nothing to look up for each object the maximum count may
190
+ * delete */
191
+ records: number;
192
+ }
193
+
194
+ /** What the `var` variables of a run did with an object. */
195
+ interface HoldRecord {
196
+ readonly run: Holds;
197
+ /** Number of `var` variables that hold the object (0: not held) */
198
+ count: number;
199
+ /** Bar on which a `var` variable got the object: on that bar the maximum count deletes it only when no other object
200
+ * can go; from the next bar on, never. Undefined outside a bar loop */
201
+ heldAt: number | undefined;
202
+ /** Bar on which the last `var` variable released the object (linefill rule) */
203
+ releasedAt: number | undefined;
204
+ }
205
+ let holds: Holds = { others: new WeakMap(), records: 0 };
206
+
207
+ /**
208
+ * The record of the first run that holds an object, on the object itself (a study holding a new object on every bar
209
+ * would otherwise add a WeakMap key per bar); the records of other runs go to their {@link Holds.others}.
210
+ */
211
+ class Held extends Stamped {
212
+ #record: HoldRecord;
213
+ private constructor(obj: object, record: HoldRecord) {
214
+ super(obj);
215
+ this.#record = record;
216
+ }
217
+ /** The record of `obj` in the current run, if any. */
218
+ static get(obj: object): HoldRecord | undefined {
219
+ if (holds.records === 0) return undefined;
220
+ const own = #record in obj ? (obj as Held).#record : undefined;
221
+ return own?.run === holds ? own : holds.others.get(obj);
222
+ }
223
+ /** A new record of `obj` in the current run (it has none). */
224
+ static create(obj: object): HoldRecord {
225
+ const record: HoldRecord = { run: holds, count: 0, heldAt: undefined, releasedAt: undefined };
226
+ holds.records++;
227
+ if (#record in obj) holds.others.set(obj, record);
228
+ else new Held(obj, record);
229
+ return record;
230
+ }
231
+ }
93
232
  /** Bars during which a released linefill is still skipped by the maximum count. */
94
233
  const LINEFILL_RELEASE_BARS = 5000;
95
- /** Bar on which a `var` variable got an object: on that bar the maximum count deletes it only when no other object can
96
- * go; from the next bar on, never. */
97
- let heldAt = new WeakMap<object, number>();
98
234
  /** Kinds with a maximum count of their own. */
99
235
  type CountedKind = 'line' | 'label' | 'box' | 'polyline';
100
236
  const COUNTED_KINDS: readonly CountedKind[] = ['line', 'label', 'box', 'polyline'];
@@ -109,26 +245,26 @@ const PENDING_RESET: Record<CountedKind, number> = { line: 5, label: 5, box: 5,
109
245
  * variable): the maximum count never deletes it, and it still counts. Returns the object.
110
246
  */
111
247
  export function holdDrawing<T extends object>(obj: T): T {
112
- if (!held.has(obj)) {
113
- if (currentBar === null) heldAt.delete(obj);
114
- else heldAt.set(obj, currentBar);
115
- }
116
- held.set(obj, (held.get(obj) ?? 0) + 1);
248
+ const record = Held.get(obj) ?? Held.create(obj);
249
+ if (record.count === 0) record.heldAt = currentBar ?? undefined;
250
+ record.count++;
117
251
  return obj;
118
252
  }
119
253
 
120
254
  /** The `var` variable no longer refers to the object (it got another object or `na`). */
121
255
  export function releaseDrawing(obj: object | null | undefined): void {
122
256
  if (!obj) return;
123
- const count = held.get(obj) ?? 0;
124
- if (count > 1) held.set(obj, count - 1);
125
- else if (held.delete(obj) && currentBar !== null) releasedAt.set(obj, currentBar);
257
+ const record = Held.get(obj);
258
+ if (!record || record.count === 0) return;
259
+ record.count--;
260
+ if (record.count === 0 && currentBar !== null) record.releasedAt = currentBar;
126
261
  }
127
262
 
128
263
  /** Held by a `var` variable since a bar before the current one (or outside a bar loop). */
129
264
  function heldBefore(obj: object): boolean {
130
- if (!held.has(obj)) return false;
131
- const bar = heldAt.get(obj);
265
+ const record = Held.get(obj);
266
+ if (!record || record.count === 0) return false;
267
+ const bar = record.heldAt;
132
268
  return bar === undefined || currentBar === null || bar < currentBar;
133
269
  }
134
270
 
@@ -139,13 +275,17 @@ function heldBefore(obj: object): boolean {
139
275
  */
140
276
  function heldOnThisBar(obj: object): boolean {
141
277
  if (currentBar === null) return false;
142
- return held.has(obj) ? heldAt.get(obj) === currentBar : releasedAt.get(obj) === currentBar;
278
+ const record = Held.get(obj);
279
+ if (!record) return false;
280
+ return record.count > 0 ? record.heldAt === currentBar : record.releasedAt === currentBar;
143
281
  }
144
282
 
145
283
  /** Linefill rule: held, or released less than 5,000 bars ago. */
146
284
  function fillProtected(obj: object): boolean {
147
- if (held.has(obj)) return true;
148
- const bar = releasedAt.get(obj);
285
+ const record = Held.get(obj);
286
+ if (!record) return false;
287
+ if (record.count > 0) return true;
288
+ const bar = record.releasedAt;
149
289
  return bar !== undefined && currentBar !== null && currentBar - bar < LINEFILL_RELEASE_BARS;
150
290
  }
151
291
 
@@ -225,7 +365,7 @@ export function setDrawingBar(barIndex: number | null, last: boolean = false, la
225
365
  const start = (kind: AllowanceKind): number => (pending[kind] >= PENDING_RESET[kind] ? 0 : pending[kind]);
226
366
  lastBar = {
227
367
  bar: barIndex,
228
- old: new Set<object>([...live.line, ...live.label, ...live.box, ...live.polyline]),
368
+ old: new Set<object>([...live.line.toArray(), ...live.label.toArray(), ...live.box.toArray(), ...live.polyline.toArray()]),
229
369
  allowance: { line: start('line'), label: start('label'), box: start('box') },
230
370
  carried: pending.polyline,
231
371
  polyline: false,
@@ -271,11 +411,38 @@ const COORDINATES: { [K in DrawingKind]: Array<keyof DrawingTypes[K]> } = {
271
411
  table: [],
272
412
  };
273
413
 
414
+ /** The live linefills of each line, in creation order: deleting a line or making a fill does not scan all the fills. */
415
+ let fillsOfLine = new Map<Line, Linefill[]>();
416
+
417
+ function trackFill(fill: Linefill): void {
418
+ for (const l of fill.line1 === fill.line2 ? [fill.line1] : [fill.line1, fill.line2]) {
419
+ const fills = fillsOfLine.get(l);
420
+ if (fills) fills.push(fill);
421
+ else fillsOfLine.set(l, [fill]);
422
+ }
423
+ }
424
+
425
+ function untrackFill(fill: Linefill): void {
426
+ for (const l of fill.line1 === fill.line2 ? [fill.line1] : [fill.line1, fill.line2]) {
427
+ const fills = fillsOfLine.get(l);
428
+ if (!fills) continue;
429
+ const k = fills.indexOf(fill);
430
+ if (k !== -1) fills.splice(k, 1);
431
+ if (fills.length === 0) fillsOfLine.delete(l);
432
+ }
433
+ }
434
+
435
+ /** The live linefills that use `line`, in creation order (do not modify). */
436
+ export function liveFillsOf(line: Line): readonly Linefill[] {
437
+ return fillsOfLine.get(line) ?? [];
438
+ }
439
+
274
440
  function markDeleted<K extends DrawingKind>(kind: K, obj: DrawingTypes[K], explicit: boolean): void {
275
- deleted.add(obj);
441
+ Deleted.mark(obj);
276
442
  for (const field of COORDINATES[kind]) (obj as unknown as Record<string, unknown>)[field as string] = NaN;
277
443
  if (kind === 'line' && explicit) {
278
- for (const fill of live.linefill.filter((f) => f.line1 === obj || f.line2 === obj)) remove('linefill', fill);
444
+ const fills = fillsOfLine.get(obj as Line);
445
+ if (fills) for (const fill of [...fills]) remove('linefill', fill);
279
446
  }
280
447
  }
281
448
 
@@ -291,33 +458,34 @@ function allowance(kind: DrawingKind): number {
291
458
  */
292
459
  function cutOldest<K extends DrawingKind>(
293
460
  kind: K,
294
- list: Array<DrawingTypes[K]>,
461
+ list: LiveList<DrawingTypes[K]>,
295
462
  obj: DrawingTypes[K],
296
463
  only?: (old: object) => boolean
297
464
  ): Array<DrawingTypes[K]> {
298
465
  if (kind === 'table') return []; // tables have no maximum count
299
466
  const max = limits[kind === 'linefill' ? 'line' : (kind as keyof DrawingLimits)] + allowance(kind);
300
467
  if (list.length <= max + SLACK) return [];
301
- const gone = new Set<object>();
302
468
  let excess = list.length - max;
303
- const candidate = (old: DrawingTypes[K]): boolean =>
304
- old !== obj && (!only || only(old)) && !(kind === 'linefill' ? fillProtected(old) : heldBefore(old));
305
- // the oldest first; the objects a `var` variable got (or released) on this bar only when the others are not enough
306
- for (const weak of kind === 'linefill' ? [false] : [false, true]) {
307
- for (const old of list) {
308
- if (excess <= 0) break;
309
- if (!gone.has(old) && candidate(old) && (kind === 'linefill' || heldOnThisBar(old) === weak)) {
310
- excess--;
311
- gone.add(old);
312
- }
469
+ const fill = kind === 'linefill';
470
+ // positions of the objects to delete. The oldest first; the objects a `var` variable got (or released) on this bar
471
+ // only when the others are not enough (the two passes pick different objects)
472
+ const gone: number[] = [];
473
+ for (let pass = 0; pass < (fill ? 1 : 2); pass++) {
474
+ const before = gone.length;
475
+ for (let k = 0; k < list.length && excess > 0; k++) {
476
+ const old = list.at(k);
477
+ if (old === obj || (only && !only(old))) continue;
478
+ if (fill ? fillProtected(old) : heldBefore(old) || heldOnThisBar(old) !== (pass === 1)) continue;
479
+ excess--;
480
+ gone.push(k);
313
481
  }
482
+ if (before > 0 && gone.length > before) gone.sort((a, b) => a - b);
314
483
  }
315
- const kept = list.filter((old) => !gone.has(old));
316
- const cut = list.filter((old) => gone.has(old));
484
+ const cut = gone.map((k) => list.at(k));
485
+ // the others keep their order; usually the cut is the oldest run of objects, which leaves by moving the head
486
+ list.removeAll(gone);
317
487
  // linefills: still above the maximum + 5, the oldest go, the held ones too
318
- if (kind === 'linefill' && kept.length > max + SLACK) cut.push(...kept.splice(0, kept.length - max));
319
- list.length = 0;
320
- list.push(...kept);
488
+ if (fill && list.length > max + SLACK) cut.push(...list.takeFront(list.length - max));
321
489
  return cut;
322
490
  }
323
491
 
@@ -336,9 +504,13 @@ export function register<K extends DrawingKind>(kind: K, obj: DrawingTypes[K]):
336
504
  if (lastBar && isCounted(kind)) return registerLast(lastBar, kind, obj);
337
505
  if (isCounted(kind) && pending[kind] >= PENDING_RESET[kind]) pending[kind] = 0;
338
506
  list.push(obj);
507
+ if (kind === 'linefill') trackFill(obj as Linefill);
339
508
  const cut = cutOldest(kind, list, obj);
340
509
  if (kind === 'polyline') pending.polyline += cut.length;
341
- for (const old of cut) markDeleted(kind, old, false);
510
+ for (const old of cut) {
511
+ if (kind === 'linefill') untrackFill(old as Linefill);
512
+ markDeleted(kind, old, false);
513
+ }
342
514
  return obj;
343
515
  }
344
516
 
@@ -352,7 +524,12 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
352
524
  const list = live[kind];
353
525
  list.push(obj);
354
526
  const isOld = (old: object): boolean => bar.old.has(old);
355
- if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old) && !heldOnThisBar(old))) {
527
+ let oldCandidate = false;
528
+ for (let k = 0; k < list.length && !oldCandidate; k++) {
529
+ const old = list.at(k);
530
+ oldCandidate = old !== obj && isOld(old) && !heldBefore(old) && !heldOnThisBar(old);
531
+ }
532
+ if (oldCandidate) {
356
533
  // the objects of the bars before only; a line takes its linefills with it
357
534
  const cut = cutOldest(kind, list, obj, isOld);
358
535
  if (kind === 'polyline') {
@@ -373,32 +550,33 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
373
550
  /** Deletes an object (no effect when it is already deleted). */
374
551
  export function remove<K extends DrawingKind>(kind: K, obj: DrawingTypes[K]): void {
375
552
  const list = live[kind];
376
- const index = list.indexOf(obj);
553
+ // from the end: an object is in the list once, and scripts mostly delete their newest objects
554
+ const index = list.lastIndexOf(obj);
377
555
  if (index !== -1) {
378
- list.splice(index, 1);
556
+ list.removeAt(index);
557
+ if (kind === 'linefill') untrackFill(obj as Linefill);
379
558
  if (isCounted(kind)) pending[kind]++;
380
559
  if (kind === 'polyline' && lastBar?.old.has(obj)) lastBar.oldPolylinesGone++;
381
560
  }
382
- if (!deleted.has(obj)) markDeleted(kind, obj, true);
561
+ if (!Deleted.has(obj)) markDeleted(kind, obj, true);
383
562
  }
384
563
 
385
564
  /** True when the object was deleted (by `delete` or by the maximum count). */
386
565
  export function isDeleted(obj: unknown): boolean {
387
- return typeof obj === 'object' && obj !== null && deleted.has(obj);
566
+ return typeof obj === 'object' && obj !== null && Deleted.has(obj);
388
567
  }
389
568
 
390
569
  /** The live objects of a kind, in creation order (a new array). */
391
570
  export function all<K extends DrawingKind>(kind: K): Array<DrawingTypes[K]> {
392
- return [...live[kind]] as Array<DrawingTypes[K]>;
571
+ return live[kind].toArray() as Array<DrawingTypes[K]>;
393
572
  }
394
573
 
395
574
  /** Forgets all objects (without deleting them) and sets the maximum counts (default 50). */
396
575
  export function resetDrawings(newLimits: DrawingLimits = {}): void {
397
- for (const kind of Object.keys(live) as DrawingKind[]) live[kind].length = 0;
576
+ for (const kind of Object.keys(live) as DrawingKind[]) live[kind].replace([]);
577
+ fillsOfLine = new Map();
398
578
  created.clear();
399
- held = new WeakMap();
400
- releasedAt = new WeakMap();
401
- heldAt = new WeakMap();
579
+ holds = { others: new WeakMap(), records: 0 };
402
580
  pending = { line: 0, label: 0, box: 0, polyline: 0 };
403
581
  currentBar = null;
404
582
  lastBarIndex = null;
@@ -420,9 +598,7 @@ export function setDrawingLimits(newLimits: DrawingLimits): void {
420
598
  export interface DrawingState {
421
599
  live: { [K in DrawingKind]: Array<DrawingTypes[K]> };
422
600
  created: DrawingKind[];
423
- held: WeakMap<object, number>;
424
- releasedAt: WeakMap<object, number>;
425
- heldAt: WeakMap<object, number>;
601
+ holds: Holds;
426
602
  pending: Record<CountedKind, number>;
427
603
  currentBar: number | null;
428
604
  lastBarIndex: number | null;
@@ -434,17 +610,15 @@ export interface DrawingState {
434
610
  export function saveDrawingState(): DrawingState {
435
611
  return {
436
612
  live: {
437
- line: [...live.line],
438
- label: [...live.label],
439
- box: [...live.box],
440
- polyline: [...live.polyline],
441
- linefill: [...live.linefill],
442
- table: [...live.table],
613
+ line: live.line.toArray(),
614
+ label: live.label.toArray(),
615
+ box: live.box.toArray(),
616
+ polyline: live.polyline.toArray(),
617
+ linefill: live.linefill.toArray(),
618
+ table: live.table.toArray(),
443
619
  },
444
620
  created: [...created],
445
- held,
446
- releasedAt,
447
- heldAt,
621
+ holds,
448
622
  pending: { ...pending },
449
623
  currentBar,
450
624
  lastBarIndex,
@@ -455,16 +629,12 @@ export function saveDrawingState(): DrawingState {
455
629
 
456
630
  /** Puts back the registry saved by {@link saveDrawingState} after the nested run. */
457
631
  export function restoreDrawingState(s: DrawingState): void {
458
- for (const kind of Object.keys(live) as DrawingKind[]) {
459
- const list = live[kind] as object[];
460
- list.length = 0;
461
- list.push(...(s.live[kind] as object[]));
462
- }
632
+ for (const kind of Object.keys(live) as DrawingKind[]) (live[kind] as LiveList<object>).replace(s.live[kind]);
633
+ fillsOfLine = new Map();
634
+ for (const fill of s.live.linefill) trackFill(fill);
463
635
  created.clear();
464
636
  for (const kind of s.created) created.add(kind);
465
- held = s.held;
466
- releasedAt = s.releasedAt;
467
- heldAt = s.heldAt;
637
+ holds = s.holds;
468
638
  pending = { ...s.pending };
469
639
  currentBar = s.currentBar;
470
640
  lastBarIndex = s.lastBarIndex;
package/src/index.ts CHANGED
@@ -225,7 +225,7 @@ export type { LightweightSeriesDefinitions } from './runtime/adapters/Lightweigh
225
225
  export { SimpleInputAdapter } from './runtime/adapters/SimpleInputAdapter.js';
226
226
 
227
227
  // Version
228
- export const VERSION = '0.15.1';
228
+ export const VERSION = '0.15.3';
229
229
 
230
230
  /**
231
231
  * Library information
@@ -10,7 +10,7 @@
10
10
  * @version 6
11
11
  */
12
12
 
13
- import { all as allDrawings, isDeleted, register, remove } from '../drawing/registry.js';
13
+ import { Stamped, all as allDrawings, isDeleted, liveFillsOf, register, remove } from '../drawing/registry.js';
14
14
  import type { Linefill, Line, color } from '../types/index.js';
15
15
 
16
16
  /**
@@ -46,13 +46,13 @@ export function new_linefill(line1: Line, line2: Line, fillColor?: color): Linef
46
46
  // PineScript: an na line gives an na linefill
47
47
  if (line1 === null || line1 === undefined || line2 === null || line2 === undefined) return null as unknown as Linefill;
48
48
  let found: Linefill | undefined;
49
- for (const fill of allDrawings('linefill')) {
49
+ for (const fill of liveFillsOf(line1)) {
50
50
  const has = (l: Line): boolean => fill.line1 === l || fill.line2 === l;
51
- if (has(line1) && has(line2) && (!found || (touched.get(fill) ?? 0) > (touched.get(found) ?? 0))) found = fill;
51
+ if (has(line2) && (!found || Touched.get(fill) > Touched.get(found))) found = fill;
52
52
  }
53
53
  if (found) {
54
54
  found.color = fillColor;
55
- touched.set(found, ++touchCount);
55
+ Touched.set(found, ++touchCount);
56
56
  return found;
57
57
  }
58
58
  const fill = register('linefill', {
@@ -60,12 +60,21 @@ export function new_linefill(line1: Line, line2: Line, fillColor?: color): Linef
60
60
  line2,
61
61
  color: fillColor
62
62
  });
63
- touched.set(fill, ++touchCount);
63
+ Touched.set(fill, ++touchCount);
64
64
  return fill;
65
65
  }
66
66
 
67
- /** Order in which the linefills were created or returned by `linefill.new` (the last one wins a lookup). */
68
- const touched = new WeakMap<Linefill, number>();
67
+ /** Order in which the linefills were created or returned by `linefill.new` (the last one wins a lookup); 0 before. */
68
+ class Touched extends Stamped {
69
+ #touch = 0;
70
+ static get(fill: Linefill): number {
71
+ return #touch in fill ? fill.#touch : 0;
72
+ }
73
+ static set(fill: Linefill, order: number): void {
74
+ if (!(#touch in fill)) new Touched(fill);
75
+ (fill as unknown as Touched).#touch = order;
76
+ }
77
+ }
69
78
  let touchCount = 0;
70
79
 
71
80
  /**
@@ -51,6 +51,8 @@ export class SessionBars {
51
51
  readonly chart: TimeframeInfo;
52
52
 
53
53
  private readonly yearDays = new Map<number, DateNum[]>();
54
+ /** Parsed timeframes: `timeOf` / `closeOf` run on every bar */
55
+ private readonly timeframes = new Map<string, TimeframeInfo>();
54
56
 
55
57
  constructor(
56
58
  readonly calendar: TradingCalendar,
@@ -102,6 +104,12 @@ export class SessionBars {
102
104
  return [monthStart(year * 10000 + firstMonth * 100 + 1), monthEnd(year * 10000 + lastMonth * 100 + 1)];
103
105
  }
104
106
 
107
+ private timeframe(timeframe: string): TimeframeInfo {
108
+ let tf = this.timeframes.get(timeframe);
109
+ if (!tf) this.timeframes.set(timeframe, (tf = info(timeframe)));
110
+ return tf;
111
+ }
112
+
105
113
  private dayStart(date: DateNum): number {
106
114
  return this.calendar.periods(date)[0]![0];
107
115
  }
@@ -117,7 +125,7 @@ export class SessionBars {
117
125
  * NaN on it (measured; `time("W")` keeps its value).
118
126
  */
119
127
  timeOf(time: number, timeframe: string): number {
120
- const tf = info(timeframe);
128
+ const tf = this.timeframe(timeframe);
121
129
  const at = this.calendar.locate(time);
122
130
  if (!at) return NaN;
123
131
  if (tf.isintraday) {
@@ -137,7 +145,7 @@ export class SessionBars {
137
145
 
138
146
  /** PineScript `time_close(tf)` for a bar that opens at `time`. */
139
147
  closeOf(time: number, timeframe: string): number {
140
- const tf = info(timeframe);
148
+ const tf = this.timeframe(timeframe);
141
149
  const at = this.calendar.locate(time);
142
150
  if (!at) return NaN;
143
151
  if (tf.isintraday) {
@@ -178,7 +186,7 @@ export class SessionBars {
178
186
  * bar completes a higher-timeframe period (request.security).
179
187
  */
180
188
  periodEnd(time: number, timeframe: string): number {
181
- const tf = info(timeframe);
189
+ const tf = this.timeframe(timeframe);
182
190
  const at = this.calendar.locate(time);
183
191
  if (!at) return NaN;
184
192
  if (tf.isintraday) {