ixfx
Version:
A framework for programming interactivity
183 lines (180 loc) • 4.66 kB
JavaScript
import {
getOrGenerate
} from "./chunk-N6YIY4CM.js";
// src/trackers/TrackerBase.ts
var TrackerBase = class {
constructor(opts = {}) {
this.id = opts.id ?? `tracker`;
this.debug = opts.debug ?? false;
this.sampleLimit = opts.sampleLimit ?? -1;
this.resetAfterSamples = opts.resetAfterSamples ?? -1;
this.storeIntermediate = opts.storeIntermediate ?? (this.sampleLimit > -1 || this.resetAfterSamples > -1);
this.seenCount = 0;
if (this.debug) {
console.log(`TrackerBase: sampleLimit: ${this.sampleLimit} resetAfter: ${this.resetAfterSamples} store: ${this.storeIntermediate}`);
}
}
/**
* Reset tracker
*/
reset() {
this.seenCount = 0;
this.onReset();
}
/**
* Adds a value, returning computed result.
*
* At this point, we check if the buffer is larger than `resetAfterSamples`. If so, `reset()` is called.
* If not, we check `sampleLimit`. If the buffer is twice as large as sample limit, `trimStore()` is
* called to take it down to sample limit, and `onTrimmed()` is called.
* @param p
* @returns
*/
seen(...p) {
if (this.resetAfterSamples > 0 && this.seenCount > this.resetAfterSamples) {
this.reset();
} else if (this.sampleLimit > 0 && this.seenCount > this.sampleLimit * 2) {
this.seenCount = this.trimStore(this.sampleLimit);
this.onTrimmed(`resize`);
}
this.seenCount += p.length;
const t = this.filterData(p);
return this.computeResults(t);
}
};
// src/trackers/TrackedValue.ts
var TrackedValueMap = class {
constructor(creator) {
this.store = /* @__PURE__ */ new Map();
this.gog = getOrGenerate(this.store, creator);
}
/**
* Number of named values being tracked
*/
get size() {
return this.store.size;
}
/**
* Returns _true_ if `id` is stored
* @param id
* @returns
*/
has(id) {
return this.store.has(id);
}
/**
* For a given id, note that we have seen one or more values.
* @param id Id
* @param values Values(s)
* @returns Information about start to last value
*/
//eslint-disable-next-line @typescript-eslint/no-explicit-any
//eslint-disable-next-line functional/prefer-immutable-types
async seen(id, ...values) {
const trackedValue = await this.getTrackedValue(id, ...values);
const result = trackedValue.seen(...values);
return result;
}
/**
* Creates or returns a TrackedValue instance for `id`.
* @param id
* @param values
* @returns
*/
//eslint-disable-next-line functional/prefer-immutable-types
async getTrackedValue(id, ...values) {
if (id === null) throw new Error(`id parameter cannot be null`);
if (id === void 0) throw new Error(`id parameter cannot be undefined`);
const trackedValue = await this.gog(id, values[0]);
return trackedValue;
}
/**
* Remove a tracked value by id.
* Use {@link reset} to clear them all.
* @param id
*/
delete(id) {
this.store.delete(id);
}
/**
* Remove all tracked values.
* Use {@link delete} to remove a single value by id.
*/
reset() {
this.store = /* @__PURE__ */ new Map();
}
/**
* Enumerate ids
*/
*ids() {
yield* this.store.keys();
}
/**
* Enumerate tracked values
*/
*tracked() {
yield* this.store.values();
}
/**
* Iterates TrackedValues ordered with oldest first
* @returns
*/
*trackedByAge() {
const tp = [...this.store.values()];
tp.sort((a, b) => {
const aa = a.elapsed;
const bb = b.elapsed;
if (aa === bb) return 0;
if (aa > bb) return -1;
return 1;
});
for (const t of tp) {
yield t;
}
}
/**
* Iterates underlying values, ordered by age (oldest first)
* First the named values are sorted by their `elapsed` value, and then
* we return the last value for that group.
*/
*valuesByAge() {
for (const tb of this.trackedByAge()) {
yield tb.last;
}
}
/**
* Enumerate last received values
*
* @example Calculate centroid of latest-received values
* ```js
* const pointers = pointTracker();
* const c = Points.centroid(...Array.from(pointers.lastPoints()));
* ```
*/
*last() {
for (const p of this.store.values()) {
yield p.last;
}
}
/**
* Enumerate starting values
*/
*initialValues() {
for (const p of this.store.values()) {
yield p.initial;
}
}
/**
* Returns a tracked value by id, or undefined if not found
* @param id
* @returns
*/
get(id) {
return this.store.get(id);
}
};
export {
TrackedValueMap,
TrackerBase
};
//# sourceMappingURL=chunk-7ICNCHYJ.js.map