Skip to content

Commit 000cab1

Browse files
committed
Move flatten functions into formula.ExpressionValue
1 parent 41e92eb commit 000cab1

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

src/classes.svelte.js

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -410,48 +410,6 @@ functions.crypto = async (ticker) => {
410410
}
411411
}
412412

413-
function flattenArgs(computed) {
414-
if (computed?.refs == null) {
415-
return computed.value;
416-
}
417-
return (
418-
computed.refs
419-
.map((r) => flattenArgs(r))
420-
.flat()
421-
// Hack for detecting stores instead of primitive values
422-
.filter((r) => r?.subscribe != null)
423-
);
424-
}
425-
426-
function flattenComputedToFunction(computed) {
427-
if (computed?.refs == null) {
428-
return (...args) => args;
429-
}
430-
let thunk = computed.thunk ?? ((...args) => args);
431-
let refArgsCounts = computed.refs.map((r) => r?.numRefArgs ?? 1);
432-
return async (...args) => {
433-
let offset = 0;
434-
// Call the thunk with the correct args from the flattened list. Use `.call`
435-
// and `.apply` to pass the correct `this` value.
436-
return await thunk.apply(
437-
this,
438-
(
439-
await Promise.all(
440-
computed.refs.map((r, i) => {
441-
const oldOffset = offset;
442-
offset += refArgsCounts[i];
443-
// Recurse with the relevant portion of the flattened arguments list
444-
return flattenComputedToFunction.call(
445-
this,
446-
r,
447-
)(...args.slice(oldOffset, offset));
448-
}),
449-
)
450-
).flat(),
451-
);
452-
};
453-
}
454-
455413
export class Sheet {
456414
name = $state();
457415
cells = $state();
@@ -515,7 +473,7 @@ export class Sheet {
515473
cell.col,
516474
);
517475
cell.value.rederive(
518-
flattenArgs(computed),
476+
computed.flattenArgs(),
519477
(dependencyValues, _set, _update) => {
520478
const set = (...args) => {
521479
try {
@@ -545,11 +503,8 @@ export class Sheet {
545503
element: undefined,
546504
globals: this.globals,
547505
};
548-
flattenComputedToFunction
549-
.call(
550-
_this,
551-
computed,
552-
)(...dependencyValues)
506+
computed
507+
.flattenComputedToFunction(_this)(...dependencyValues)
553508
.then(([result]) => {
554509
update((old) => {
555510
// TODO: Find a better trigger for resets than just waiting

src/formula.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,55 @@ class ExpressionValue {
4242
this.numRefArgs = arraySum(refs.map(({ numRefArgs: n }) => n));
4343
}
4444
}
45+
46+
flattenArgs() {
47+
if (this?.refs == null) {
48+
return this.value;
49+
}
50+
return (
51+
this.refs
52+
// Using `this` here is a hack for calling this method on arguments that
53+
// may not be instances of ExpressionValue. `r.flattenArgs()` would be
54+
// more correct, but does not always apply.
55+
.map((r) => this.flattenArgs.call(r))
56+
.flat()
57+
// Hack for detecting stores instead of primitive values
58+
.filter((r) => r?.subscribe != null)
59+
);
60+
}
61+
62+
flattenComputedToFunction(formulaFunctionThis) {
63+
const computed = this;
64+
if (computed?.refs == null) {
65+
return (...args) => args;
66+
}
67+
let thunk = computed.thunk ?? ((...args) => args);
68+
let refArgsCounts = computed.refs.map((r) => r?.numRefArgs ?? 1);
69+
return async (...args) => {
70+
let offset = 0;
71+
// Call the thunk with the correct args from the flattened list. Use
72+
// `.call` and `.apply` to pass the correct `this` value.
73+
return await thunk.apply(
74+
formulaFunctionThis,
75+
(
76+
await Promise.all(
77+
computed.refs.map((r, i) => {
78+
const oldOffset = offset;
79+
offset += refArgsCounts[i];
80+
// Recurse with the relevant portion of the flattened arguments
81+
// list. Using `this` here is a hack for calling this method on
82+
// arguments that may not be instances of ExpressionValue. See
83+
// flattenArgs above as well.
84+
return this.flattenComputedToFunction.call(
85+
r,
86+
formulaFunctionThis,
87+
)(...args.slice(oldOffset, offset));
88+
}),
89+
)
90+
).flat(),
91+
);
92+
};
93+
}
4594
}
4695

4796
function singleton(f) {

0 commit comments

Comments
 (0)