Skip to content

Commit 7c2a8b9

Browse files
committed
An alternative to a Proxy: return an array with a custom property transform, 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.
1 parent 9448278 commit 7c2a8b9

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function keyword(input, name, allowed) {
132132

133133
// Promotes the specified data to an array as needed.
134134
export function arrayify(data) {
135+
if (typeof data?.transform === "function") data = data.transform();
135136
return data == null || data instanceof Array || data instanceof TypedArray ? data : Array.from(data);
136137
}
137138

src/transforms/group.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function groupn(
6666
x, // optionally group on x
6767
y, // optionally group on y
6868
{
69-
data: reduceData = reduceIdentity,
69+
data: reduceData = reduceIdentityLazy,
7070
filter,
7171
sort,
7272
reverse,
@@ -153,6 +153,7 @@ function groupn(
153153
groupFacets.push(groupFacet);
154154
}
155155
maybeSort(groupFacets, sort, reverse);
156+
if (reduceData === reduceIdentityLazy) groupData.transform = () => groupData.map((d) => d());
156157
return {data: groupData, facets: groupFacets};
157158
}),
158159
...(!hasOutput(outputs, "x") && (GX ? {x: GX} : {x1, x2})),
@@ -236,6 +237,7 @@ export function maybeGroup(I, X) {
236237
export function maybeReduce(reduce, value, fallback = invalidReduce) {
237238
if (reduce == null) return fallback(reduce);
238239
if (typeof reduce.reduceIndex === "function") return reduce;
240+
if (typeof reduce.reduceIndexLazy === "function") return reduce;
239241
if (typeof reduce.reduce === "function" && isObject(reduce)) return reduceReduce(reduce); // N.B. array.reduce
240242
if (typeof reduce === "function") return reduceFunction(reduce);
241243
if (/^p\d{2}$/i.test(reduce)) return reduceAccessor(percentile(reduce));
@@ -362,8 +364,13 @@ function reduceMaybeTemporalAccessor(f) {
362364

363365
export const reduceIdentity = {
364366
reduceIndex(I, X) {
365-
let K; // lazy
366-
return new Proxy(I, {get: (I, prop) => (K ??= take(X, I))[prop]});
367+
return take(X, I);
368+
}
369+
};
370+
371+
const reduceIdentityLazy = {
372+
reduceIndex(I, X) {
373+
return () => take(X, I);
367374
}
368375
};
369376

0 commit comments

Comments
 (0)