Skip to content

Commit

Permalink
An alternative to a Proxy: return an array with a custom property tra…
Browse files Browse the repository at this point in the history
…nsform, that needs to be invoked when we want to use the data. That means that downstream consumers of this need to know how to handle it. In the tests there is only one case, and it goes through arrayify, which is where I "decode" the structure. Not sure if it's any better.
  • Loading branch information
Fil committed Mar 22, 2024
1 parent 9448278 commit 23a29fe
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function groupn(
x, // optionally group on x
y, // optionally group on y
{
data: reduceData = reduceIdentity,
data: reduceData = reduceIdentityLazy,
filter,
sort,
reverse,
Expand Down Expand Up @@ -153,6 +153,7 @@ function groupn(
groupFacets.push(groupFacet);
}
maybeSort(groupFacets, sort, reverse);
if (reduceData === reduceIdentityLazy) groupData.transform = () => groupData.map((d) => d());
return {data: groupData, facets: groupFacets};
}),
...(!hasOutput(outputs, "x") && (GX ? {x: GX} : {x1, x2})),
Expand Down Expand Up @@ -236,6 +237,7 @@ export function maybeGroup(I, X) {
export function maybeReduce(reduce, value, fallback = invalidReduce) {
if (reduce == null) return fallback(reduce);
if (typeof reduce.reduceIndex === "function") return reduce;
if (typeof reduce.reduceIndexLazy === "function") return reduce;
if (typeof reduce.reduce === "function" && isObject(reduce)) return reduceReduce(reduce); // N.B. array.reduce
if (typeof reduce === "function") return reduceFunction(reduce);
if (/^p\d{2}$/i.test(reduce)) return reduceAccessor(percentile(reduce));
Expand Down Expand Up @@ -362,8 +364,13 @@ function reduceMaybeTemporalAccessor(f) {

export const reduceIdentity = {
reduceIndex(I, X) {
let K; // lazy
return new Proxy(I, {get: (I, prop) => (K ??= take(X, I))[prop]});
return take(X, I);
}
};

const reduceIdentityLazy = {
reduceIndex(I, X) {
return () => take(X, I);
}
};

Expand Down

0 comments on commit 23a29fe

Please sign in to comment.