@invinite-org/chartlang-runtime 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +236 -0
- package/dist/barPoint.d.ts +20 -0
- package/dist/barPoint.d.ts.map +1 -0
- package/dist/barPoint.js +72 -0
- package/dist/barPoint.js.map +1 -0
- package/dist/bufferSnapshot.d.ts +102 -0
- package/dist/bufferSnapshot.d.ts.map +1 -0
- package/dist/bufferSnapshot.js +119 -0
- package/dist/bufferSnapshot.js.map +1 -0
- package/dist/createScriptRunner.d.ts +6 -3
- package/dist/createScriptRunner.d.ts.map +1 -1
- package/dist/createScriptRunner.js +29 -5
- package/dist/createScriptRunner.js.map +1 -1
- package/dist/dep/DepRunner.d.ts.map +1 -1
- package/dist/dep/DepRunner.js +1 -0
- package/dist/dep/DepRunner.js.map +1 -1
- package/dist/emit/draw/boxes/fillBetween.d.ts +45 -0
- package/dist/emit/draw/boxes/fillBetween.d.ts.map +1 -0
- package/dist/emit/draw/boxes/fillBetween.js +36 -0
- package/dist/emit/draw/boxes/fillBetween.js.map +1 -0
- package/dist/emit/draw/handle.d.ts +9 -0
- package/dist/emit/draw/handle.d.ts.map +1 -1
- package/dist/emit/draw/handle.js +65 -10
- package/dist/emit/draw/handle.js.map +1 -1
- package/dist/emit/draw/namespace.d.ts +4 -3
- package/dist/emit/draw/namespace.d.ts.map +1 -1
- package/dist/emit/draw/namespace.js +6 -3
- package/dist/emit/draw/namespace.js.map +1 -1
- package/dist/emit/plot.d.ts +7 -0
- package/dist/emit/plot.d.ts.map +1 -1
- package/dist/emit/plot.js +13 -0
- package/dist/emit/plot.js.map +1 -1
- package/dist/execution/dispose.d.ts.map +1 -1
- package/dist/execution/dispose.js +16 -0
- package/dist/execution/dispose.js.map +1 -1
- package/dist/execution/runComputeStep.d.ts.map +1 -1
- package/dist/execution/runComputeStep.js +10 -1
- package/dist/execution/runComputeStep.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/persistentStateStore.runtime.d.ts.map +1 -1
- package/dist/persistentStateStore.runtime.js +21 -7
- package/dist/persistentStateStore.runtime.js.map +1 -1
- package/dist/request/index.d.ts +2 -1
- package/dist/request/index.d.ts.map +1 -1
- package/dist/request/index.js +2 -1
- package/dist/request/index.js.map +1 -1
- package/dist/request/requestNamespace.d.ts.map +1 -1
- package/dist/request/requestNamespace.js +16 -3
- package/dist/request/requestNamespace.js.map +1 -1
- package/dist/request/security.d.ts +20 -1
- package/dist/request/security.d.ts.map +1 -1
- package/dist/request/security.js +62 -23
- package/dist/request/security.js.map +1 -1
- package/dist/request/securityExprRunner.d.ts +133 -0
- package/dist/request/securityExprRunner.d.ts.map +1 -0
- package/dist/request/securityExprRunner.js +235 -0
- package/dist/request/securityExprRunner.js.map +1 -0
- package/dist/request/streamBars.d.ts +14 -1
- package/dist/request/streamBars.d.ts.map +1 -1
- package/dist/request/streamBars.js +39 -1
- package/dist/request/streamBars.js.map +1 -1
- package/dist/runtimeContext.d.ts +48 -0
- package/dist/runtimeContext.d.ts.map +1 -1
- package/dist/runtimeContext.js.map +1 -1
- package/dist/seriesView.d.ts +42 -17
- package/dist/seriesView.d.ts.map +1 -1
- package/dist/seriesView.js +65 -42
- package/dist/seriesView.js.map +1 -1
- package/dist/state/index.d.ts +2 -1
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +2 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/lifecycle.d.ts +40 -0
- package/dist/state/lifecycle.d.ts.map +1 -1
- package/dist/state/lifecycle.js +53 -0
- package/dist/state/lifecycle.js.map +1 -1
- package/dist/state/seriesPersistence.d.ts +48 -0
- package/dist/state/seriesPersistence.d.ts.map +1 -0
- package/dist/state/seriesPersistence.js +87 -0
- package/dist/state/seriesPersistence.js.map +1 -0
- package/dist/state/seriesSlot.d.ts +105 -0
- package/dist/state/seriesSlot.d.ts.map +1 -0
- package/dist/state/seriesSlot.js +123 -0
- package/dist/state/seriesSlot.js.map +1 -0
- package/dist/state/stateNamespace.d.ts.map +1 -1
- package/dist/state/stateNamespace.js +28 -0
- package/dist/state/stateNamespace.js.map +1 -1
- package/dist/streamState.d.ts +25 -19
- package/dist/streamState.d.ts.map +1 -1
- package/dist/streamState.js +40 -66
- package/dist/streamState.js.map +1 -1
- package/dist/ta/adx.d.ts +3 -2
- package/dist/ta/adx.d.ts.map +1 -1
- package/dist/ta/adx.js +3 -2
- package/dist/ta/adx.js.map +1 -1
- package/dist/ta/alma.d.ts +6 -4
- package/dist/ta/alma.d.ts.map +1 -1
- package/dist/ta/alma.js +19 -6
- package/dist/ta/alma.js.map +1 -1
- package/dist/ta/atr.d.ts +3 -2
- package/dist/ta/atr.d.ts.map +1 -1
- package/dist/ta/atr.js +3 -2
- package/dist/ta/atr.js.map +1 -1
- package/dist/ta/bb.d.ts +3 -2
- package/dist/ta/bb.d.ts.map +1 -1
- package/dist/ta/bb.js +3 -2
- package/dist/ta/bb.js.map +1 -1
- package/dist/ta/chaikinOsc.d.ts +3 -2
- package/dist/ta/chaikinOsc.d.ts.map +1 -1
- package/dist/ta/chaikinOsc.js +3 -2
- package/dist/ta/chaikinOsc.js.map +1 -1
- package/dist/ta/crossover.d.ts +3 -2
- package/dist/ta/crossover.d.ts.map +1 -1
- package/dist/ta/crossover.js +3 -2
- package/dist/ta/crossover.js.map +1 -1
- package/dist/ta/crossunder.d.ts +3 -2
- package/dist/ta/crossunder.d.ts.map +1 -1
- package/dist/ta/crossunder.js +3 -2
- package/dist/ta/crossunder.js.map +1 -1
- package/dist/ta/dmi.d.ts +4 -3
- package/dist/ta/dmi.d.ts.map +1 -1
- package/dist/ta/dmi.js +4 -3
- package/dist/ta/dmi.js.map +1 -1
- package/dist/ta/ema.d.ts +3 -2
- package/dist/ta/ema.d.ts.map +1 -1
- package/dist/ta/ema.js +3 -2
- package/dist/ta/ema.js.map +1 -1
- package/dist/ta/eom.d.ts +3 -1
- package/dist/ta/eom.d.ts.map +1 -1
- package/dist/ta/eom.js +3 -1
- package/dist/ta/eom.js.map +1 -1
- package/dist/ta/highestbars.d.ts +25 -0
- package/dist/ta/highestbars.d.ts.map +1 -0
- package/dist/ta/highestbars.js +106 -0
- package/dist/ta/highestbars.js.map +1 -0
- package/dist/ta/historicalVolatility.d.ts +3 -2
- package/dist/ta/historicalVolatility.d.ts.map +1 -1
- package/dist/ta/historicalVolatility.js +3 -2
- package/dist/ta/historicalVolatility.js.map +1 -1
- package/dist/ta/ichimoku.d.ts +3 -1
- package/dist/ta/ichimoku.d.ts.map +1 -1
- package/dist/ta/ichimoku.js +3 -1
- package/dist/ta/ichimoku.js.map +1 -1
- package/dist/ta/lowestbars.d.ts +25 -0
- package/dist/ta/lowestbars.d.ts.map +1 -0
- package/dist/ta/lowestbars.js +102 -0
- package/dist/ta/lowestbars.js.map +1 -0
- package/dist/ta/macd.d.ts +3 -2
- package/dist/ta/macd.d.ts.map +1 -1
- package/dist/ta/macd.js +3 -2
- package/dist/ta/macd.js.map +1 -1
- package/dist/ta/massIndex.d.ts +3 -2
- package/dist/ta/massIndex.d.ts.map +1 -1
- package/dist/ta/massIndex.js +3 -2
- package/dist/ta/massIndex.js.map +1 -1
- package/dist/ta/mfi.d.ts +3 -1
- package/dist/ta/mfi.d.ts.map +1 -1
- package/dist/ta/mfi.js +3 -1
- package/dist/ta/mfi.js.map +1 -1
- package/dist/ta/netVolume.d.ts +3 -1
- package/dist/ta/netVolume.d.ts.map +1 -1
- package/dist/ta/netVolume.js +3 -1
- package/dist/ta/netVolume.js.map +1 -1
- package/dist/ta/nvi.d.ts +3 -1
- package/dist/ta/nvi.d.ts.map +1 -1
- package/dist/ta/nvi.js +3 -1
- package/dist/ta/nvi.js.map +1 -1
- package/dist/ta/persistence.d.ts.map +1 -1
- package/dist/ta/persistence.js +1 -40
- package/dist/ta/persistence.js.map +1 -1
- package/dist/ta/ppo.d.ts +3 -2
- package/dist/ta/ppo.d.ts.map +1 -1
- package/dist/ta/ppo.js +3 -2
- package/dist/ta/ppo.js.map +1 -1
- package/dist/ta/pvi.d.ts +3 -1
- package/dist/ta/pvi.d.ts.map +1 -1
- package/dist/ta/pvi.js +3 -1
- package/dist/ta/pvi.js.map +1 -1
- package/dist/ta/pvo.d.ts +3 -1
- package/dist/ta/pvo.d.ts.map +1 -1
- package/dist/ta/pvo.js +3 -1
- package/dist/ta/pvo.js.map +1 -1
- package/dist/ta/pvt.d.ts +3 -1
- package/dist/ta/pvt.d.ts.map +1 -1
- package/dist/ta/pvt.js +3 -1
- package/dist/ta/pvt.js.map +1 -1
- package/dist/ta/registry.d.ts +7 -1
- package/dist/ta/registry.d.ts.map +1 -1
- package/dist/ta/registry.js +4 -0
- package/dist/ta/registry.js.map +1 -1
- package/dist/ta/rsi.d.ts +3 -2
- package/dist/ta/rsi.d.ts.map +1 -1
- package/dist/ta/rsi.js +3 -2
- package/dist/ta/rsi.js.map +1 -1
- package/dist/ta/rvi.d.ts +3 -2
- package/dist/ta/rvi.d.ts.map +1 -1
- package/dist/ta/rvi.js +3 -2
- package/dist/ta/rvi.js.map +1 -1
- package/dist/ta/sma.d.ts +6 -3
- package/dist/ta/sma.d.ts.map +1 -1
- package/dist/ta/sma.js +6 -3
- package/dist/ta/sma.js.map +1 -1
- package/dist/ta/stdev.d.ts +3 -2
- package/dist/ta/stdev.d.ts.map +1 -1
- package/dist/ta/stdev.js +3 -2
- package/dist/ta/stdev.js.map +1 -1
- package/dist/ta/trendStrengthIndex.d.ts +3 -2
- package/dist/ta/trendStrengthIndex.d.ts.map +1 -1
- package/dist/ta/trendStrengthIndex.js +3 -2
- package/dist/ta/trendStrengthIndex.js.map +1 -1
- package/dist/ta/trix.d.ts +4 -3
- package/dist/ta/trix.d.ts.map +1 -1
- package/dist/ta/trix.js +4 -3
- package/dist/ta/trix.js.map +1 -1
- package/dist/ta/vortex.d.ts +3 -2
- package/dist/ta/vortex.d.ts.map +1 -1
- package/dist/ta/vortex.js +3 -2
- package/dist/ta/vortex.js.map +1 -1
- package/package.json +3 -3
- package/dist/ta/lib/applyOffset.d.ts +0 -19
- package/dist/ta/lib/applyOffset.d.ts.map +0 -1
- package/dist/ta/lib/applyOffset.js +0 -38
- package/dist/ta/lib/applyOffset.js.map +0 -1
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { NumberSeriesSlot } from "@invinite-org/chartlang-core";
|
|
2
|
+
import type { Float64RingBuffer } from "../ringBuffer.js";
|
|
3
|
+
/**
|
|
4
|
+
* Runtime slot behind a script-facing `state.series(init)` handle. The
|
|
5
|
+
* `buffer` is the history ring (index 0 = live head); the `view` is the
|
|
6
|
+
* identity-stable {@link NumberSeriesSlot} the script reads and writes;
|
|
7
|
+
* `committedHead` snapshots the head as of the last bar close so a tick
|
|
8
|
+
* can reset the live head before the script refines it.
|
|
9
|
+
*
|
|
10
|
+
* Unlike the scalar `StateSlot`, there is no tentative/committed value
|
|
11
|
+
* split — the head IS the tentative value and history IS committed (a bar
|
|
12
|
+
* advances the ring on close). `committedHead` exists only so a tick's
|
|
13
|
+
* `resetSeriesSlotHead` can undo a prior tick's `replaceHead`.
|
|
14
|
+
*
|
|
15
|
+
* @since 0.9
|
|
16
|
+
* @stable
|
|
17
|
+
* @example
|
|
18
|
+
* // const slot = createSeriesSlot(new Float64RingBuffer(8), 0);
|
|
19
|
+
* // slot.view.value = 42;
|
|
20
|
+
* // slot.view[0]; // 42
|
|
21
|
+
*/
|
|
22
|
+
export type SeriesSlot = {
|
|
23
|
+
readonly kind: "state.series";
|
|
24
|
+
readonly buffer: Float64RingBuffer;
|
|
25
|
+
readonly view: NumberSeriesSlot;
|
|
26
|
+
committedHead: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Build the identity-stable {@link NumberSeriesSlot} view over a ring
|
|
30
|
+
* buffer. Series reads (`[n]`, `current`, `length`, `valueOf`,
|
|
31
|
+
* `Symbol.toPrimitive`) delegate to a reused {@link makeSeriesView}; the
|
|
32
|
+
* `value` property is added on top — `get` → `buffer.at(0)` (live head),
|
|
33
|
+
* `set` → `buffer.replaceHead(v)` (write-through to the live head). The
|
|
34
|
+
* object's identity is stable across bars, so a script can keep
|
|
35
|
+
* `const s = state.series(0)` at the top of `compute`.
|
|
36
|
+
*
|
|
37
|
+
* @since 0.9
|
|
38
|
+
* @stable
|
|
39
|
+
* @example
|
|
40
|
+
* // const view = makeSeriesSlotView(buffer);
|
|
41
|
+
* // view.value = 7; // replaceHead
|
|
42
|
+
* // +view; // 7 (valueOf → buffer.at(0))
|
|
43
|
+
* // view[1]; // one committed bar back
|
|
44
|
+
*/
|
|
45
|
+
export declare function makeSeriesSlotView(buffer: Float64RingBuffer): NumberSeriesSlot;
|
|
46
|
+
/**
|
|
47
|
+
* Allocate a fresh {@link SeriesSlot}: seed the live head with `init`
|
|
48
|
+
* (matching `state.float(init)` — a never-written series reads `init` on
|
|
49
|
+
* its allocation bar), set `committedHead = init`, and build the
|
|
50
|
+
* identity-stable view. Later bars the script does not write become `NaN`
|
|
51
|
+
* gaps because the close hook advances the ring with `append(NaN)`.
|
|
52
|
+
*
|
|
53
|
+
* @since 0.9
|
|
54
|
+
* @stable
|
|
55
|
+
* @example
|
|
56
|
+
* // const slot = createSeriesSlot(new Float64RingBuffer(8), 0);
|
|
57
|
+
* // slot.view[0]; // 0 (the seeded init on the allocation bar)
|
|
58
|
+
*/
|
|
59
|
+
export declare function createSeriesSlot(buffer: Float64RingBuffer, init: number): SeriesSlot;
|
|
60
|
+
/**
|
|
61
|
+
* Rebuild a {@link SeriesSlot} over an already-restored ring buffer
|
|
62
|
+
* (snapshot path). The view identity is recreated — acceptable, same as
|
|
63
|
+
* `ta.*` restore.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.9
|
|
66
|
+
* @stable
|
|
67
|
+
* @example
|
|
68
|
+
* // const slot = restoreSeriesSlot(restoredBuffer, committedHead);
|
|
69
|
+
* // slot.view[1];
|
|
70
|
+
*/
|
|
71
|
+
export declare function restoreSeriesSlot(buffer: Float64RingBuffer, committedHead: number): SeriesSlot;
|
|
72
|
+
/**
|
|
73
|
+
* Advance the ring for a new close bar: append a fresh `NaN` head so the
|
|
74
|
+
* prior committed head slides to index 1. Runs once per close, before the
|
|
75
|
+
* script's compute, for every already-allocated slot.
|
|
76
|
+
*
|
|
77
|
+
* @since 0.9
|
|
78
|
+
* @stable
|
|
79
|
+
* @example
|
|
80
|
+
* // advanceSeriesSlot(slot); // slot.view[0] is now NaN until written
|
|
81
|
+
*/
|
|
82
|
+
export declare function advanceSeriesSlot(slot: SeriesSlot): void;
|
|
83
|
+
/**
|
|
84
|
+
* Commit the live head as the bar-close value, so the next close's
|
|
85
|
+
* `advanceSeriesSlot` retains it and a subsequent tick's
|
|
86
|
+
* `resetSeriesSlotHead` restores it. Runs once per close, after compute.
|
|
87
|
+
*
|
|
88
|
+
* @since 0.9
|
|
89
|
+
* @stable
|
|
90
|
+
* @example
|
|
91
|
+
* // commitSeriesSlot(slot); // slot.committedHead = slot.view[0]
|
|
92
|
+
*/
|
|
93
|
+
export declare function commitSeriesSlot(slot: SeriesSlot): void;
|
|
94
|
+
/**
|
|
95
|
+
* Reset the live head to the last committed value at the start of a tick,
|
|
96
|
+
* so a tick that re-writes refines from the committed baseline and a tick
|
|
97
|
+
* that does not write reads the committed head. Does NOT advance length.
|
|
98
|
+
*
|
|
99
|
+
* @since 0.9
|
|
100
|
+
* @stable
|
|
101
|
+
* @example
|
|
102
|
+
* // resetSeriesSlotHead(slot); // slot.view[0] = slot.committedHead
|
|
103
|
+
*/
|
|
104
|
+
export declare function resetSeriesSlotHead(slot: SeriesSlot): void;
|
|
105
|
+
//# sourceMappingURL=seriesSlot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seriesSlot.d.ts","sourceRoot":"","sources":["../../src/state/seriesSlot.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAErE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,UAAU,GAAG;IACrB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC;IACnC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,aAAa,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,GAAG,gBAAgB,CAmB9E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAQpF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,GAAG,UAAU,CAO9F;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAExD;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAEvD;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAE1D"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Copyright (c) 2026 Invinite. Licensed under the MIT License.
|
|
2
|
+
// See the LICENSE file in the repo root for full license text.
|
|
3
|
+
import { makeSeriesView } from "../seriesView.js";
|
|
4
|
+
/**
|
|
5
|
+
* Build the identity-stable {@link NumberSeriesSlot} view over a ring
|
|
6
|
+
* buffer. Series reads (`[n]`, `current`, `length`, `valueOf`,
|
|
7
|
+
* `Symbol.toPrimitive`) delegate to a reused {@link makeSeriesView}; the
|
|
8
|
+
* `value` property is added on top — `get` → `buffer.at(0)` (live head),
|
|
9
|
+
* `set` → `buffer.replaceHead(v)` (write-through to the live head). The
|
|
10
|
+
* object's identity is stable across bars, so a script can keep
|
|
11
|
+
* `const s = state.series(0)` at the top of `compute`.
|
|
12
|
+
*
|
|
13
|
+
* @since 0.9
|
|
14
|
+
* @stable
|
|
15
|
+
* @example
|
|
16
|
+
* // const view = makeSeriesSlotView(buffer);
|
|
17
|
+
* // view.value = 7; // replaceHead
|
|
18
|
+
* // +view; // 7 (valueOf → buffer.at(0))
|
|
19
|
+
* // view[1]; // one committed bar back
|
|
20
|
+
*/
|
|
21
|
+
export function makeSeriesSlotView(buffer) {
|
|
22
|
+
const reads = makeSeriesView(buffer);
|
|
23
|
+
return new Proxy(reads, {
|
|
24
|
+
get(target, prop, receiver) {
|
|
25
|
+
if (prop === "value")
|
|
26
|
+
return buffer.at(0);
|
|
27
|
+
return Reflect.get(target, prop, receiver);
|
|
28
|
+
},
|
|
29
|
+
set(_target, prop, value) {
|
|
30
|
+
if (prop === "value") {
|
|
31
|
+
buffer.replaceHead(value);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
},
|
|
36
|
+
has(target, prop) {
|
|
37
|
+
if (prop === "value")
|
|
38
|
+
return true;
|
|
39
|
+
return Reflect.has(target, prop);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Allocate a fresh {@link SeriesSlot}: seed the live head with `init`
|
|
45
|
+
* (matching `state.float(init)` — a never-written series reads `init` on
|
|
46
|
+
* its allocation bar), set `committedHead = init`, and build the
|
|
47
|
+
* identity-stable view. Later bars the script does not write become `NaN`
|
|
48
|
+
* gaps because the close hook advances the ring with `append(NaN)`.
|
|
49
|
+
*
|
|
50
|
+
* @since 0.9
|
|
51
|
+
* @stable
|
|
52
|
+
* @example
|
|
53
|
+
* // const slot = createSeriesSlot(new Float64RingBuffer(8), 0);
|
|
54
|
+
* // slot.view[0]; // 0 (the seeded init on the allocation bar)
|
|
55
|
+
*/
|
|
56
|
+
export function createSeriesSlot(buffer, init) {
|
|
57
|
+
buffer.append(init);
|
|
58
|
+
return {
|
|
59
|
+
kind: "state.series",
|
|
60
|
+
buffer,
|
|
61
|
+
view: makeSeriesSlotView(buffer),
|
|
62
|
+
committedHead: init,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Rebuild a {@link SeriesSlot} over an already-restored ring buffer
|
|
67
|
+
* (snapshot path). The view identity is recreated — acceptable, same as
|
|
68
|
+
* `ta.*` restore.
|
|
69
|
+
*
|
|
70
|
+
* @since 0.9
|
|
71
|
+
* @stable
|
|
72
|
+
* @example
|
|
73
|
+
* // const slot = restoreSeriesSlot(restoredBuffer, committedHead);
|
|
74
|
+
* // slot.view[1];
|
|
75
|
+
*/
|
|
76
|
+
export function restoreSeriesSlot(buffer, committedHead) {
|
|
77
|
+
return {
|
|
78
|
+
kind: "state.series",
|
|
79
|
+
buffer,
|
|
80
|
+
view: makeSeriesSlotView(buffer),
|
|
81
|
+
committedHead,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Advance the ring for a new close bar: append a fresh `NaN` head so the
|
|
86
|
+
* prior committed head slides to index 1. Runs once per close, before the
|
|
87
|
+
* script's compute, for every already-allocated slot.
|
|
88
|
+
*
|
|
89
|
+
* @since 0.9
|
|
90
|
+
* @stable
|
|
91
|
+
* @example
|
|
92
|
+
* // advanceSeriesSlot(slot); // slot.view[0] is now NaN until written
|
|
93
|
+
*/
|
|
94
|
+
export function advanceSeriesSlot(slot) {
|
|
95
|
+
slot.buffer.append(Number.NaN);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Commit the live head as the bar-close value, so the next close's
|
|
99
|
+
* `advanceSeriesSlot` retains it and a subsequent tick's
|
|
100
|
+
* `resetSeriesSlotHead` restores it. Runs once per close, after compute.
|
|
101
|
+
*
|
|
102
|
+
* @since 0.9
|
|
103
|
+
* @stable
|
|
104
|
+
* @example
|
|
105
|
+
* // commitSeriesSlot(slot); // slot.committedHead = slot.view[0]
|
|
106
|
+
*/
|
|
107
|
+
export function commitSeriesSlot(slot) {
|
|
108
|
+
slot.committedHead = slot.buffer.at(0);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reset the live head to the last committed value at the start of a tick,
|
|
112
|
+
* so a tick that re-writes refines from the committed baseline and a tick
|
|
113
|
+
* that does not write reads the committed head. Does NOT advance length.
|
|
114
|
+
*
|
|
115
|
+
* @since 0.9
|
|
116
|
+
* @stable
|
|
117
|
+
* @example
|
|
118
|
+
* // resetSeriesSlotHead(slot); // slot.view[0] = slot.committedHead
|
|
119
|
+
*/
|
|
120
|
+
export function resetSeriesSlotHead(slot) {
|
|
121
|
+
slot.buffer.replaceHead(slot.committedHead);
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=seriesSlot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seriesSlot.js","sourceRoot":"","sources":["../../src/state/seriesSlot.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAK/D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AA4BlD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAyB;IACxD,MAAM,KAAK,GAAG,cAAc,CAAS,MAAM,CAAC,CAAC;IAC7C,OAAO,IAAI,KAAK,CAAC,KAAyB,EAAE;QACxC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ;YACtB,IAAI,IAAI,KAAK,OAAO;gBAAE,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAC1C,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC/C,CAAC;QACD,GAAG,CAAC,OAAO,EAA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W,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAChD,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nimport type { NumberSeriesSlot } from \"@invinite-org/chartlang-core\";\n\nimport type { Float64RingBuffer } from \"../ringBuffer.js\";\nimport { makeSeriesView } from \"../seriesView.js\";\n\n/**\n * Runtime slot behind a script-facing `state.series(init)` handle. The\n * `buffer` is the history ring (index 0 = live head); the `view` is the\n * identity-stable {@link NumberSeriesSlot} the script reads and writes;\n * `committedHead` snapshots the head as of the last bar close so a tick\n * can reset the live head before the script refines it.\n *\n * Unlike the scalar `StateSlot`, there is no tentative/committed value\n * split — the head IS the tentative value and history IS committed (a bar\n * advances the ring on close). `committedHead` exists only so a tick's\n * `resetSeriesSlotHead` can undo a prior tick's `replaceHead`.\n *\n * @since 0.9\n * @stable\n * @example\n * // const slot = createSeriesSlot(new Float64RingBuffer(8), 0);\n * // slot.view.value = 42;\n * // slot.view[0]; // 42\n */\nexport type SeriesSlot = {\n readonly kind: \"state.series\";\n readonly buffer: Float64RingBuffer;\n readonly view: NumberSeriesSlot;\n committedHead: number;\n};\n\n/**\n * Build the identity-stable {@link NumberSeriesSlot} view over a ring\n * buffer. Series reads (`[n]`, `current`, `length`, `valueOf`,\n * `Symbol.toPrimitive`) delegate to a reused {@link makeSeriesView}; the\n * `value` property is added on top — `get` → `buffer.at(0)` (live head),\n * `set` → `buffer.replaceHead(v)` (write-through to the live head). The\n * object's identity is stable across bars, so a script can keep\n * `const s = state.series(0)` at the top of `compute`.\n *\n * @since 0.9\n * @stable\n * @example\n * // const view = makeSeriesSlotView(buffer);\n * // view.value = 7; // replaceHead\n * // +view; // 7 (valueOf → buffer.at(0))\n * // view[1]; // one committed bar back\n */\nexport function makeSeriesSlotView(buffer: Float64RingBuffer): NumberSeriesSlot {\n const reads = makeSeriesView<number>(buffer);\n return new Proxy(reads as NumberSeriesSlot, {\n get(target, prop, receiver) {\n if (prop === \"value\") return buffer.at(0);\n return Reflect.get(target, prop, receiver);\n },\n set(_target, prop, value) {\n if (prop === \"value\") {\n buffer.replaceHead(value as number);\n return true;\n }\n return false;\n },\n has(target, prop) {\n if (prop === \"value\") return true;\n return Reflect.has(target, prop);\n },\n });\n}\n\n/**\n * Allocate a fresh {@link SeriesSlot}: seed the live head with `init`\n * (matching `state.float(init)` — a never-written series reads `init` on\n * its allocation bar), set `committedHead = init`, and build the\n * identity-stable view. Later bars the script does not write become `NaN`\n * gaps because the close hook advances the ring with `append(NaN)`.\n *\n * @since 0.9\n * @stable\n * @example\n * // const slot = createSeriesSlot(new Float64RingBuffer(8), 0);\n * // slot.view[0]; // 0 (the seeded init on the allocation bar)\n */\nexport function createSeriesSlot(buffer: Float64RingBuffer, init: number): SeriesSlot {\n buffer.append(init);\n return {\n kind: \"state.series\",\n buffer,\n view: makeSeriesSlotView(buffer),\n committedHead: init,\n };\n}\n\n/**\n * Rebuild a {@link SeriesSlot} over an already-restored ring buffer\n * (snapshot path). The view identity is recreated — acceptable, same as\n * `ta.*` restore.\n *\n * @since 0.9\n * @stable\n * @example\n * // const slot = restoreSeriesSlot(restoredBuffer, committedHead);\n * // slot.view[1];\n */\nexport function restoreSeriesSlot(buffer: Float64RingBuffer, committedHead: number): SeriesSlot {\n return {\n kind: \"state.series\",\n buffer,\n view: makeSeriesSlotView(buffer),\n committedHead,\n };\n}\n\n/**\n * Advance the ring for a new close bar: append a fresh `NaN` head so the\n * prior committed head slides to index 1. Runs once per close, before the\n * script's compute, for every already-allocated slot.\n *\n * @since 0.9\n * @stable\n * @example\n * // advanceSeriesSlot(slot); // slot.view[0] is now NaN until written\n */\nexport function advanceSeriesSlot(slot: SeriesSlot): void {\n slot.buffer.append(Number.NaN);\n}\n\n/**\n * Commit the live head as the bar-close value, so the next close's\n * `advanceSeriesSlot` retains it and a subsequent tick's\n * `resetSeriesSlotHead` restores it. Runs once per close, after compute.\n *\n * @since 0.9\n * @stable\n * @example\n * // commitSeriesSlot(slot); // slot.committedHead = slot.view[0]\n */\nexport function commitSeriesSlot(slot: SeriesSlot): void {\n slot.committedHead = slot.buffer.at(0);\n}\n\n/**\n * Reset the live head to the last committed value at the start of a tick,\n * so a tick that re-writes refines from the committed baseline and a tick\n * that does not write reads the committed head. Does NOT advance length.\n *\n * @since 0.9\n * @stable\n * @example\n * // resetSeriesSlotHead(slot); // slot.view[0] = slot.committedHead\n */\nexport function resetSeriesSlotHead(slot: SeriesSlot): void {\n slot.buffer.replaceHead(slot.committedHead);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stateNamespace.d.ts","sourceRoot":"","sources":["../../src/state/stateNamespace.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"stateNamespace.d.ts","sourceRoot":"","sources":["../../src/state/stateNamespace.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAiC,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAoFlG;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,IAAI,cAAc,CA0BpD"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Copyright (c) 2026 Invinite. Licensed under the MIT License.
|
|
2
2
|
// See the LICENSE file in the repo root for full license text.
|
|
3
|
+
import { Float64RingBuffer } from "../ringBuffer.js";
|
|
3
4
|
import { ACTIVE_RUNTIME_CONTEXT } from "../runtimeContext.js";
|
|
5
|
+
import { createSeriesSlot } from "./seriesSlot.js";
|
|
4
6
|
import { asMutableSlot, StateSlot } from "./stateSlot.js";
|
|
5
7
|
/**
|
|
6
8
|
* Compose the runtime's `state.*` slot key from the compiler-injected
|
|
@@ -14,6 +16,16 @@ import { asMutableSlot, StateSlot } from "./stateSlot.js";
|
|
|
14
16
|
* @internal
|
|
15
17
|
*/
|
|
16
18
|
const stateKey = (ctx, slotId) => `${ctx.slotIdPrefix ?? ""}${slotId}:state`;
|
|
19
|
+
/**
|
|
20
|
+
* Compose the runtime's `state.series` slot key — the `:series` suffix
|
|
21
|
+
* (vs `:state`) lets the snapshot restore router tell a series slot from a
|
|
22
|
+
* scalar `state.*` slot. The `slotIdPrefix` isolation rule is identical to
|
|
23
|
+
* {@link stateKey}.
|
|
24
|
+
*
|
|
25
|
+
* @since 0.9
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
const seriesKey = (ctx, slotId) => `${ctx.slotIdPrefix ?? ""}${slotId}:series`;
|
|
17
29
|
function getCtx(name) {
|
|
18
30
|
const ctx = ACTIVE_RUNTIME_CONTEXT.current;
|
|
19
31
|
if (ctx === null) {
|
|
@@ -36,6 +48,21 @@ function getOrAllocate(name, slotId, init, tickPersistent) {
|
|
|
36
48
|
ctx.stateSlots.set(key, slot);
|
|
37
49
|
return asMutableSlot(slot);
|
|
38
50
|
}
|
|
51
|
+
function getOrAllocateSeries(slotId, init) {
|
|
52
|
+
const ctx = getCtx("state.series");
|
|
53
|
+
const key = seriesKey(ctx, slotId);
|
|
54
|
+
const existing = ctx.seriesSlots.get(key);
|
|
55
|
+
if (existing !== undefined) {
|
|
56
|
+
return existing.view;
|
|
57
|
+
}
|
|
58
|
+
// Size the ring to the runner's global capacity (`maxLookback + 1`, or
|
|
59
|
+
// the 5000-slot dynamic fallback). Warm restart rehydrates `seriesSlots`
|
|
60
|
+
// up front via `restoreSeriesSlots`, so a restored slot is found above
|
|
61
|
+
// and this seed path only runs for a genuinely first-seen callsite.
|
|
62
|
+
const slot = createSeriesSlot(new Float64RingBuffer(ctx.stream.ohlcv.close.capacity), init);
|
|
63
|
+
ctx.seriesSlots.set(key, slot);
|
|
64
|
+
return slot.view;
|
|
65
|
+
}
|
|
39
66
|
/**
|
|
40
67
|
* Build the runtime `state` namespace installed on `ComputeContext`.
|
|
41
68
|
* Each function accepts the compiler-injected `slotId` as its first
|
|
@@ -53,6 +80,7 @@ export function buildStateNamespace() {
|
|
|
53
80
|
int: (slotId, init) => getOrAllocate("state.int", slotId, init, false),
|
|
54
81
|
bool: (slotId, init) => getOrAllocate("state.bool", slotId, init, false),
|
|
55
82
|
string: (slotId, init) => getOrAllocate("state.string", slotId, init, false),
|
|
83
|
+
series: (slotId, init) => getOrAllocateSeries(slotId, init),
|
|
56
84
|
tick: {
|
|
57
85
|
float: (slotId, init) => getOrAllocate("state.tick.float", slotId, init, true),
|
|
58
86
|
int: (slotId, init) => getOrAllocate("state.tick.int", slotId, init, true),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stateNamespace.js","sourceRoot":"","sources":["../../src/state/stateNamespace.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAI/D,OAAO,EAAE,sBAAsB,EAAuB,MAAM,sBAAsB,CAAC;AACnF,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAO1D;;;;;;;;;;GAUG;AACH,MAAM,QAAQ,GAAG,CAAC,GAAmB,EAAE,MAAc,EAAU,EAAE,CAC7D,GAAG,GAAG,CAAC,YAAY,IAAI,EAAE,GAAG,MAAM,QAAQ,CAAC;AAE/C,SAAS,MAAM,CAAC,IAAY;IACxB,MAAM,GAAG,GAAG,sBAAsB,CAAC,OAAO,CAAC;IAC3C,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,uCAAuC,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC;AAED,SAAS,aAAa,CAClB,IAAY,EACZ,MAAc,EACd,IAAO,EACP,cAAuB;IAEvB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACzB,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,aAA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,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;YACvD,IAAI,EAAE,CAAC,MAAc,EAAE,IAAa,EAAwB,EAAE,CAC1D,aAAa,CAAC,iBAAiB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;YACxD,MAAM,EAAE,CAAC,MAAc,EAAE,IAAY,EAAuB,EAAE,CAC1D,aAAa,CAAC,mBAAmB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;SAC7D;KACJ,CAAC;IACF,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;IACvB,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAClB,OAAO,EAA+B,CAAC;AAC3C,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nimport type { MutableSlot, StateNamespace } from \"@invinite-org/chartlang-core\";\n\nimport { ACTIVE_RUNTIME_CONTEXT, type RuntimeContext } from \"../runtimeContext.js\";\nimport { asMutableSlot, StateSlot } from \"./stateSlot.js\";\n\ntype StoredStateSlot<T> = {\n readonly committed: T;\n readonly tentative: T;\n};\n\n/**\n * Compose the runtime's `state.*` slot key from the compiler-injected\n * `slotId` plus the active context's `slotIdPrefix`. The primary runner\n * has an absent / empty prefix — its keys stay byte-identical to the\n * Phase-1 `${slotId}:state` shape so single-script snapshots load\n * unchanged. `DepRunner` contexts carry `dep:<localId>/`,\n * `SiblingRunner` contexts carry `export:<exportName>/`.\n *\n * @since 0.7\n * @internal\n */\nconst stateKey = (ctx: RuntimeContext, slotId: string): string =>\n `${ctx.slotIdPrefix ?? \"\"}${slotId}:state`;\n\nfunction getCtx(name: string): RuntimeContext {\n const ctx = ACTIVE_RUNTIME_CONTEXT.current;\n if (ctx === null) {\n throw new Error(`${name} called outside an active script step`);\n }\n return ctx;\n}\n\nfunction getOrAllocate<T>(\n name: string,\n slotId: string,\n init: T,\n tickPersistent: boolean,\n): MutableSlot<T> {\n const ctx = getCtx(name);\n const key = stateKey(ctx, slotId);\n const existing = ctx.stateSlots.get(key);\n if (existing !== undefined) {\n return asMutableSlot(existing as StateSlot<T>);\n }\n\n const stored = ctx.stateStore.get<StoredStateSlot<T>>(key);\n const slot = new StateSlot<T>(stored?.committed ?? init, tickPersistent);\n if (stored !== undefined) {\n slot.tentative = stored.tentative;\n }\n ctx.stateSlots.set(key, slot as StateSlot<unknown>);\n return asMutableSlot(slot);\n}\n\n/**\n * Build the runtime `state` namespace installed on `ComputeContext`.\n * Each function accepts the compiler-injected `slotId` as its first\n * parameter, then the script-facing init value.\n *\n * @since 0.4\n * @stable\n * @example\n * const ns = buildStateNamespace();\n * void ns.float;\n */\nexport function buildStateNamespace(): StateNamespace {\n const ns = {\n float: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.float\", slotId, init, false),\n int: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.int\", slotId, init, false),\n bool: (slotId: string, init: boolean): MutableSlot<boolean> =>\n getOrAllocate(\"state.bool\", slotId, init, false),\n string: (slotId: string, init: string): MutableSlot<string> =>\n getOrAllocate(\"state.string\", slotId, init, false),\n tick: {\n float: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.tick.float\", slotId, init, true),\n int: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.tick.int\", slotId, init, true),\n bool: (slotId: string, init: boolean): MutableSlot<boolean> =>\n getOrAllocate(\"state.tick.bool\", slotId, init, true),\n string: (slotId: string, init: string): MutableSlot<string> =>\n getOrAllocate(\"state.tick.string\", slotId, init, true),\n },\n };\n Object.freeze(ns.tick);\n Object.freeze(ns);\n return ns as unknown as StateNamespace;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"stateNamespace.js","sourceRoot":"","sources":["../../src/state/stateNamespace.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAI/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBA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C;QACnD,IAAI,EAAE,CAAC,MAAc,EAAE,IAAa,EAAwB,EAAE,CAC1D,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC;QACpD,MAAM,EAAE,CAAC,MAAc,EAAE,IAAY,EAAuB,EAAE,CAC1D,aAAa,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC;QACtD,MAAM,EAAE,CAAC,MAAc,EAAE,IAAY,EAAoB,EAAE,CACvD,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAC;QACrC,IAAI,EAAE;YACF,KAAK,EAAE,CAAC,MAAc,EAAE,IAAY,EAAuB,EAAE,CACzD,aAAa,CAAC,kBAAkB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;YACzD,GAAG,EAAE,CAAC,MAAc,EAAE,IAAY,EAAuB,EAAE,CACvD,aAAa,CAAC,gBAAgB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;YACvD,IAAI,EAAE,CAAC,MAAc,EAAE,IAAa,EAAwB,EAAE,CAC1D,aAAa,CAAC,iBAAiB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;YACxD,MAAM,EAAE,CAAC,MAAc,EAAE,IAAY,EAAuB,EAAE,CAC1D,aAAa,CAAC,mBAAmB,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;SAC7D;KACJ,CAAC;IACF,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;IACvB,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAClB,OAAO,EAA+B,CAAC;AAC3C,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nimport type { MutableSlot, NumberSeriesSlot, StateNamespace } from \"@invinite-org/chartlang-core\";\n\nimport { Float64RingBuffer } from \"../ringBuffer.js\";\nimport { ACTIVE_RUNTIME_CONTEXT, type RuntimeContext } from \"../runtimeContext.js\";\nimport { createSeriesSlot } from \"./seriesSlot.js\";\nimport { asMutableSlot, StateSlot } from \"./stateSlot.js\";\n\ntype StoredStateSlot<T> = {\n readonly committed: T;\n readonly tentative: T;\n};\n\n/**\n * Compose the runtime's `state.*` slot key from the compiler-injected\n * `slotId` plus the active context's `slotIdPrefix`. The primary runner\n * has an absent / empty prefix — its keys stay byte-identical to the\n * Phase-1 `${slotId}:state` shape so single-script snapshots load\n * unchanged. `DepRunner` contexts carry `dep:<localId>/`,\n * `SiblingRunner` contexts carry `export:<exportName>/`.\n *\n * @since 0.7\n * @internal\n */\nconst stateKey = (ctx: RuntimeContext, slotId: string): string =>\n `${ctx.slotIdPrefix ?? \"\"}${slotId}:state`;\n\n/**\n * Compose the runtime's `state.series` slot key — the `:series` suffix\n * (vs `:state`) lets the snapshot restore router tell a series slot from a\n * scalar `state.*` slot. The `slotIdPrefix` isolation rule is identical to\n * {@link stateKey}.\n *\n * @since 0.9\n * @internal\n */\nconst seriesKey = (ctx: RuntimeContext, slotId: string): string =>\n `${ctx.slotIdPrefix ?? \"\"}${slotId}:series`;\n\nfunction getCtx(name: string): RuntimeContext {\n const ctx = ACTIVE_RUNTIME_CONTEXT.current;\n if (ctx === null) {\n throw new Error(`${name} called outside an active script step`);\n }\n return ctx;\n}\n\nfunction getOrAllocate<T>(\n name: string,\n slotId: string,\n init: T,\n tickPersistent: boolean,\n): MutableSlot<T> {\n const ctx = getCtx(name);\n const key = stateKey(ctx, slotId);\n const existing = ctx.stateSlots.get(key);\n if (existing !== undefined) {\n return asMutableSlot(existing as StateSlot<T>);\n }\n\n const stored = ctx.stateStore.get<StoredStateSlot<T>>(key);\n const slot = new StateSlot<T>(stored?.committed ?? init, tickPersistent);\n if (stored !== undefined) {\n slot.tentative = stored.tentative;\n }\n ctx.stateSlots.set(key, slot as StateSlot<unknown>);\n return asMutableSlot(slot);\n}\n\nfunction getOrAllocateSeries(slotId: string, init: number): NumberSeriesSlot {\n const ctx = getCtx(\"state.series\");\n const key = seriesKey(ctx, slotId);\n const existing = ctx.seriesSlots.get(key);\n if (existing !== undefined) {\n return existing.view;\n }\n // Size the ring to the runner's global capacity (`maxLookback + 1`, or\n // the 5000-slot dynamic fallback). Warm restart rehydrates `seriesSlots`\n // up front via `restoreSeriesSlots`, so a restored slot is found above\n // and this seed path only runs for a genuinely first-seen callsite.\n const slot = createSeriesSlot(new Float64RingBuffer(ctx.stream.ohlcv.close.capacity), init);\n ctx.seriesSlots.set(key, slot);\n return slot.view;\n}\n\n/**\n * Build the runtime `state` namespace installed on `ComputeContext`.\n * Each function accepts the compiler-injected `slotId` as its first\n * parameter, then the script-facing init value.\n *\n * @since 0.4\n * @stable\n * @example\n * const ns = buildStateNamespace();\n * void ns.float;\n */\nexport function buildStateNamespace(): StateNamespace {\n const ns = {\n float: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.float\", slotId, init, false),\n int: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.int\", slotId, init, false),\n bool: (slotId: string, init: boolean): MutableSlot<boolean> =>\n getOrAllocate(\"state.bool\", slotId, init, false),\n string: (slotId: string, init: string): MutableSlot<string> =>\n getOrAllocate(\"state.string\", slotId, init, false),\n series: (slotId: string, init: number): NumberSeriesSlot =>\n getOrAllocateSeries(slotId, init),\n tick: {\n float: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.tick.float\", slotId, init, true),\n int: (slotId: string, init: number): MutableSlot<number> =>\n getOrAllocate(\"state.tick.int\", slotId, init, true),\n bool: (slotId: string, init: boolean): MutableSlot<boolean> =>\n getOrAllocate(\"state.tick.bool\", slotId, init, true),\n string: (slotId: string, init: string): MutableSlot<string> =>\n getOrAllocate(\"state.tick.string\", slotId, init, true),\n },\n };\n Object.freeze(ns.tick);\n Object.freeze(ns);\n return ns as unknown as StateNamespace;\n}\n"]}
|
package/dist/streamState.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Bar, BarViewport, Series, StreamSnapshot } from "@invinite-org/chartlang-core";
|
|
1
|
+
import type { Bar, BarViewport, Price, PriceSeries, Series, StreamSnapshot, VolumeSeries, WorldPoint } from "@invinite-org/chartlang-core";
|
|
2
2
|
import { Float64RingBuffer } from "./ringBuffer.js";
|
|
3
3
|
/**
|
|
4
4
|
* The per-stream OHLCV ring-buffer set. Each field is a
|
|
@@ -31,13 +31,17 @@ export type OhlcvBuffers = {
|
|
|
31
31
|
readonly hlcc4: Float64RingBuffer;
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
* the run
|
|
36
|
-
*
|
|
37
|
-
*
|
|
34
|
+
* View of the current bar handed to `compute`. Identity stays stable across
|
|
35
|
+
* the run. The OHLCV + derived price/volume fields are the stream's cached
|
|
36
|
+
* number-coercible `Series` views (one identity per ring buffer) — they read
|
|
37
|
+
* the live buffer head, so a script can both use `bar.close` as a scalar
|
|
38
|
+
* (`bar.close * 2`) and index it (`bar.close[1]`) without the runtime copying
|
|
39
|
+
* scalars per bar. `time` stays a mutable scalar (the timestamp axis the
|
|
40
|
+
* emit/draw pipeline consumes as a raw number); `symbol` / `interval` are
|
|
41
|
+
* constant strings for a given `StreamState`.
|
|
38
42
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
43
|
+
* Before the first bar the buffers are empty, so `bar.close[0]` / `+bar.close`
|
|
44
|
+
* read `NaN` and `bar.close.length` is `0`.
|
|
41
45
|
*
|
|
42
46
|
* @since 0.1
|
|
43
47
|
* @example
|
|
@@ -47,24 +51,26 @@ export type OhlcvBuffers = {
|
|
|
47
51
|
* // capacity: 5,
|
|
48
52
|
* // symbol: "AAPL",
|
|
49
53
|
* // });
|
|
50
|
-
* // bar.symbol;
|
|
51
|
-
* // bar.interval;
|
|
52
|
-
* // bar.close;
|
|
54
|
+
* // bar.symbol; // "AAPL"
|
|
55
|
+
* // bar.interval; // "1D"
|
|
56
|
+
* // +bar.close; // NaN until the first bar
|
|
57
|
+
* // bar.close.length; // 0 until the first bar
|
|
53
58
|
*/
|
|
54
59
|
export type BarView = {
|
|
55
60
|
time: number;
|
|
56
|
-
open:
|
|
57
|
-
high:
|
|
58
|
-
low:
|
|
59
|
-
close:
|
|
60
|
-
volume:
|
|
61
|
-
hl2:
|
|
62
|
-
hlc3:
|
|
63
|
-
ohlc4:
|
|
64
|
-
hlcc4:
|
|
61
|
+
open: PriceSeries;
|
|
62
|
+
high: PriceSeries;
|
|
63
|
+
low: PriceSeries;
|
|
64
|
+
close: PriceSeries;
|
|
65
|
+
volume: VolumeSeries;
|
|
66
|
+
hl2: PriceSeries;
|
|
67
|
+
hlc3: PriceSeries;
|
|
68
|
+
ohlc4: PriceSeries;
|
|
69
|
+
hlcc4: PriceSeries;
|
|
65
70
|
symbol: string;
|
|
66
71
|
interval: string;
|
|
67
72
|
viewport: BarViewport;
|
|
73
|
+
point(offset: number, price: Price): WorldPoint;
|
|
68
74
|
};
|
|
69
75
|
/**
|
|
70
76
|
* Everything the runtime owns for a single interval stream — the OHLCV
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamState.d.ts","sourceRoot":"","sources":["../src/streamState.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"streamState.d.ts","sourceRoot":"","sources":["../src/streamState.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,GAAG,EACH,WAAW,EACX,KAAK,EACL,WAAW,EACX,MAAM,EACN,cAAc,EACd,YAAY,EACZ,UAAU,EACb,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAmBpD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,YAAY,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;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H,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,SAAM,GAAG,IAAI,CAa7E"}
|
package/dist/streamState.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Copyright (c) 2026 Invinite. Licensed under the MIT License.
|
|
2
2
|
// See the LICENSE file in the repo root for full license text.
|
|
3
|
+
import { resolveBarPoint } from "./barPoint.js";
|
|
3
4
|
import { Float64RingBuffer } from "./ringBuffer.js";
|
|
4
5
|
import { makeSeriesView } from "./seriesView.js";
|
|
5
6
|
function deriveBarSources(rawBar) {
|
|
@@ -90,21 +91,6 @@ export function createStreamState(args) {
|
|
|
90
91
|
ohlc4: new Float64RingBuffer(capacity),
|
|
91
92
|
hlcc4: new Float64RingBuffer(capacity),
|
|
92
93
|
};
|
|
93
|
-
const bar = {
|
|
94
|
-
time: 0,
|
|
95
|
-
open: Number.NaN,
|
|
96
|
-
high: Number.NaN,
|
|
97
|
-
low: Number.NaN,
|
|
98
|
-
close: Number.NaN,
|
|
99
|
-
volume: 0,
|
|
100
|
-
hl2: Number.NaN,
|
|
101
|
-
hlc3: Number.NaN,
|
|
102
|
-
ohlc4: Number.NaN,
|
|
103
|
-
hlcc4: Number.NaN,
|
|
104
|
-
symbol,
|
|
105
|
-
interval,
|
|
106
|
-
viewport: Object.freeze({ fromTime: 0, toTime: 0 }),
|
|
107
|
-
};
|
|
108
94
|
const seriesViews = {
|
|
109
95
|
time: makeSeriesView(ohlcv.time),
|
|
110
96
|
open: makeSeriesView(ohlcv.open),
|
|
@@ -117,6 +103,31 @@ export function createStreamState(args) {
|
|
|
117
103
|
ohlc4: makeSeriesView(ohlcv.ohlc4),
|
|
118
104
|
hlcc4: makeSeriesView(ohlcv.hlcc4),
|
|
119
105
|
};
|
|
106
|
+
// The OHLCV + derived bar fields ARE the cached series views (one identity
|
|
107
|
+
// per ring buffer): the views are number-coercible, so `bar.close` works
|
|
108
|
+
// as a scalar (`+bar.close` / arithmetic reads the live head) and as an
|
|
109
|
+
// index (`bar.close[1]` reads a prior bar). No per-bar scalar copy is
|
|
110
|
+
// needed — the views read the buffer head directly, including mid-tick.
|
|
111
|
+
const bar = {
|
|
112
|
+
time: 0,
|
|
113
|
+
open: seriesViews.open,
|
|
114
|
+
high: seriesViews.high,
|
|
115
|
+
low: seriesViews.low,
|
|
116
|
+
close: seriesViews.close,
|
|
117
|
+
volume: seriesViews.volume,
|
|
118
|
+
hl2: seriesViews.hl2,
|
|
119
|
+
hlc3: seriesViews.hlc3,
|
|
120
|
+
ohlc4: seriesViews.ohlc4,
|
|
121
|
+
hlcc4: seriesViews.hlcc4,
|
|
122
|
+
symbol,
|
|
123
|
+
interval,
|
|
124
|
+
viewport: Object.freeze({ fromTime: 0, toTime: 0 }),
|
|
125
|
+
// Closes over the stream's time history + the live scalar `bar.time` /
|
|
126
|
+
// `bar.interval` so offset-anchored drawings resolve against the real /
|
|
127
|
+
// extrapolated time at compute time. The `WorldPoint` it returns is the
|
|
128
|
+
// only persisted anchor frame — `bar.point` adds no new wire shape.
|
|
129
|
+
point: (offset, price) => resolveBarPoint(ohlcv.time, bar.interval, bar.time, offset, price),
|
|
130
|
+
};
|
|
120
131
|
const stream = {
|
|
121
132
|
interval,
|
|
122
133
|
ohlcv,
|
|
@@ -150,31 +161,13 @@ export function createStreamState(args) {
|
|
|
150
161
|
});
|
|
151
162
|
}
|
|
152
163
|
recomputeDerivedBuffers(ohlcv, snapshot);
|
|
164
|
+
// The OHLCV + derived `bar.*` fields are the series views over the
|
|
165
|
+
// ring buffers just restored above, so they reflect the restored
|
|
166
|
+
// head with no scalar copy. Only the scalar `time` / `interval`
|
|
167
|
+
// need writing.
|
|
153
168
|
const current = snapshot.headIndex;
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
bar.open = Number.NaN;
|
|
157
|
-
bar.high = Number.NaN;
|
|
158
|
-
bar.low = Number.NaN;
|
|
159
|
-
bar.close = Number.NaN;
|
|
160
|
-
bar.volume = 0;
|
|
161
|
-
bar.hl2 = Number.NaN;
|
|
162
|
-
bar.hlc3 = Number.NaN;
|
|
163
|
-
bar.ohlc4 = Number.NaN;
|
|
164
|
-
bar.hlcc4 = Number.NaN;
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
bar.time = valueAt(snapshot.buffers.time, current);
|
|
168
|
-
bar.open = valueAt(snapshot.buffers.open, current);
|
|
169
|
-
bar.high = valueAt(snapshot.buffers.high, current);
|
|
170
|
-
bar.low = valueAt(snapshot.buffers.low, current);
|
|
171
|
-
bar.close = valueAt(snapshot.buffers.close, current);
|
|
172
|
-
bar.volume = valueAt(snapshot.buffers.volume, current);
|
|
173
|
-
bar.hl2 = (bar.high + bar.low) / 2;
|
|
174
|
-
bar.hlc3 = (bar.high + bar.low + bar.close) / 3;
|
|
175
|
-
bar.ohlc4 = (bar.open + bar.high + bar.low + bar.close) / 4;
|
|
176
|
-
bar.hlcc4 = (bar.high + bar.low + bar.close + bar.close) / 4;
|
|
177
|
-
}
|
|
169
|
+
bar.time =
|
|
170
|
+
snapshot.filled === 0 || current < 0 ? 0 : valueAt(snapshot.buffers.time, current);
|
|
178
171
|
bar.interval = snapshot.interval;
|
|
179
172
|
},
|
|
180
173
|
};
|
|
@@ -203,16 +196,10 @@ export function appendBarToStream(stream, rawBar) {
|
|
|
203
196
|
ohlcv.hlc3.append(values.hlc3);
|
|
204
197
|
ohlcv.ohlc4.append(values.ohlc4);
|
|
205
198
|
ohlcv.hlcc4.append(values.hlcc4);
|
|
199
|
+
// OHLCV + derived bar fields read the buffer head live (they ARE the
|
|
200
|
+
// series views), so only the scalar `time` / `symbol` / `interval` are
|
|
201
|
+
// copied onto the `BarView`.
|
|
206
202
|
bar.time = rawBar.time;
|
|
207
|
-
bar.open = rawBar.open;
|
|
208
|
-
bar.high = rawBar.high;
|
|
209
|
-
bar.low = rawBar.low;
|
|
210
|
-
bar.close = rawBar.close;
|
|
211
|
-
bar.volume = rawBar.volume;
|
|
212
|
-
bar.hl2 = values.hl2;
|
|
213
|
-
bar.hlc3 = values.hlc3;
|
|
214
|
-
bar.ohlc4 = values.ohlc4;
|
|
215
|
-
bar.hlcc4 = values.hlcc4;
|
|
216
203
|
bar.symbol = rawBar.symbol;
|
|
217
204
|
bar.interval = rawBar.interval;
|
|
218
205
|
}
|
|
@@ -244,16 +231,8 @@ export function replaceStreamHead(stream, rawBar) {
|
|
|
244
231
|
ohlcv.hlc3.replaceHead(values.hlc3);
|
|
245
232
|
ohlcv.ohlc4.replaceHead(values.ohlc4);
|
|
246
233
|
ohlcv.hlcc4.replaceHead(values.hlcc4);
|
|
234
|
+
// See appendBarToStream — the OHLCV/derived views read the head live.
|
|
247
235
|
bar.time = rawBar.time;
|
|
248
|
-
bar.open = rawBar.open;
|
|
249
|
-
bar.high = rawBar.high;
|
|
250
|
-
bar.low = rawBar.low;
|
|
251
|
-
bar.close = rawBar.close;
|
|
252
|
-
bar.volume = rawBar.volume;
|
|
253
|
-
bar.hl2 = values.hl2;
|
|
254
|
-
bar.hlc3 = values.hlc3;
|
|
255
|
-
bar.ohlc4 = values.ohlc4;
|
|
256
|
-
bar.hlcc4 = values.hlcc4;
|
|
257
236
|
bar.symbol = rawBar.symbol;
|
|
258
237
|
bar.interval = rawBar.interval;
|
|
259
238
|
}
|
|
@@ -270,7 +249,7 @@ export function replaceStreamHead(stream, rawBar) {
|
|
|
270
249
|
*/
|
|
271
250
|
export function replaceTickHead(stream, rawBar) {
|
|
272
251
|
const values = deriveBarSources(rawBar);
|
|
273
|
-
const { ohlcv
|
|
252
|
+
const { ohlcv } = stream;
|
|
274
253
|
ohlcv.close.replaceHead(rawBar.close);
|
|
275
254
|
ohlcv.high.replaceHead(rawBar.high);
|
|
276
255
|
ohlcv.low.replaceHead(rawBar.low);
|
|
@@ -279,14 +258,9 @@ export function replaceTickHead(stream, rawBar) {
|
|
|
279
258
|
ohlcv.hlc3.replaceHead(values.hlc3);
|
|
280
259
|
ohlcv.ohlc4.replaceHead(values.ohlc4);
|
|
281
260
|
ohlcv.hlcc4.replaceHead(values.hlcc4);
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
bar.volume = rawBar.volume;
|
|
286
|
-
bar.hl2 = values.hl2;
|
|
287
|
-
bar.hlc3 = values.hlc3;
|
|
288
|
-
bar.ohlc4 = values.ohlc4;
|
|
289
|
-
bar.hlcc4 = values.hlcc4;
|
|
261
|
+
// The close-side / derived `bar.*` fields are the series views over these
|
|
262
|
+
// buffers, so replacing the buffer head is the whole update — no scalar
|
|
263
|
+
// copy. `time` / `open` are intentionally untouched (tick invariant).
|
|
290
264
|
}
|
|
291
265
|
/**
|
|
292
266
|
* Refresh the stream's fallback visible range to the latest `limit`
|