Skip to content

Commit

Permalink
sheet, cell: Computations are Record<number, ...>
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed Apr 29, 2024
1 parent 63d2c1e commit 3c8e8e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
22 changes: 14 additions & 8 deletions src/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export type PendingArray<V, CanBeError> = Promise<
>;

/** Array of (maybe pending) computation results */
export type MaybePendingDict<V, CanBeError> = (
| PendingMaybe<V, CanBeError>
| MaybeResultOrPointer<V, CanBeError>
)[];
export type MaybePendingDict<V, CanBeError> = Record<
number,
PendingMaybe<V, CanBeError> | MaybeResultOrPointer<V, CanBeError>
>;

export type Not<T extends boolean> = T extends true ? false : true;

Expand Down Expand Up @@ -97,7 +97,10 @@ abstract class SubscribeBench<V> {
dispatch(
value,
(value) => {
if (value instanceof Canceled) return;
if (value instanceof Canceled) {
console.log("canceled", value);
return;
}
// if their is no value, wait for the
// next update for the first notification
if (value !== undefined) {
Expand Down Expand Up @@ -895,9 +898,12 @@ export class MapCell<V, NF extends boolean> extends Cell<V, true, NF> {
* @param provided dependencies provided by the sheet as their computation have been triggered before.
* @returns The list of promise of values for all dependencies (in the order of the dependencies)
*/
private _gatherDependencies(provided: {
[key: number]: PendingMaybe<V, boolean> | MaybeResultOrPointer<V, boolean>;
}): PendingArray<V, boolean> | MaybeResultOrPointer<V, boolean>[] {
private _gatherDependencies(
provided: Record<
number,
PendingMaybe<V, boolean> | MaybeResultOrPointer<V, boolean>
>
): PendingArray<V, boolean> | MaybeResultOrPointer<V, boolean>[] {
const deps: (PendingMaybe<V, boolean> | V)[] = [];
this.sheet.debug([this.id], "gathering deps values", {
id: this.id,
Expand Down
18 changes: 9 additions & 9 deletions src/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { dispatch, dispatchPromiseOrValueArray } from "./promise";
import { SheetProxy } from "./proxy";
import type { AnyCellArray } from "./types";

type Computations<V> = (
| Pending<V | Canceled, true>
| CellResult<V | Canceled, true>
)[];
type Computations<V> = Record<
number,
Pending<V | Canceled, true> | CellResult<V | Canceled, true>
>;
type IterationResult<V> = {
computations: Computations<V>;
updated: Set<number>;
Expand Down Expand Up @@ -545,7 +545,7 @@ export class Sheet {
);
};

const computations: Computations<V> = [];
const computations: Computations<V> = {};
const release = this.working.startNewComputation();
for (const id of roots) {
computations[id] = dispatch(
Expand Down Expand Up @@ -668,7 +668,7 @@ export class Sheet {
// Form now on, we need updatable pointers to be up-to-date to continue
const newComputations = this.computeUpdatable(toBeRecomputed, computations);
const borderComputation = dispatchPromiseOrValueArray(
newComputations,
Object.values(newComputations),
// wait for all new computations to be over before proceeding to the next step
(newComputations) => {
// finding all canceled computations
Expand Down Expand Up @@ -700,7 +700,7 @@ export class Sheet {
borderComputation,
({ computationsOfBorder, nextIteration }) => {
return dispatchPromiseOrValueArray(
computationsOfBorder,
Object.values(computationsOfBorder),
(computationsOfBorder) => {
//checking for canceled computations
this.registerCancelAndDone(
Expand Down Expand Up @@ -797,7 +797,7 @@ export class Sheet {
return res;
}

private dependentCells(id) {
private dependentCells(id: number) {
return Array.from(
new Set([...(this.g.get(id) || []), ...this._pointers.get(id)])
);
Expand Down Expand Up @@ -879,7 +879,7 @@ export class Sheet {
): Computations<V> {
const order = toBeRecomputed.slice(); // slice copies the array
let currentCellId: number | undefined;
const newComputations = [];
const newComputations = {};

// biome-ignore lint/suspicious/noAssignInExpressions: shorter, still explicit
while ((currentCellId = order.pop()) !== undefined) {
Expand Down

0 comments on commit 3c8e8e4

Please sign in to comment.