From 3c8e8e4b6d05f7e3ba9ac9d8617bfa0178cf216c Mon Sep 17 00:00:00 2001 From: Henri Binsztok <808274+hbbio@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:35:56 +0800 Subject: [PATCH] sheet, cell: Computations are Record --- src/cell.ts | 22 ++++++++++++++-------- src/sheet.ts | 18 +++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/cell.ts b/src/cell.ts index 75d084e..3039b75 100644 --- a/src/cell.ts +++ b/src/cell.ts @@ -47,10 +47,10 @@ export type PendingArray = Promise< >; /** Array of (maybe pending) computation results */ -export type MaybePendingDict = ( - | PendingMaybe - | MaybeResultOrPointer -)[]; +export type MaybePendingDict = Record< + number, + PendingMaybe | MaybeResultOrPointer +>; export type Not = T extends true ? false : true; @@ -97,7 +97,10 @@ abstract class SubscribeBench { 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) { @@ -895,9 +898,12 @@ export class MapCell extends Cell { * @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 | MaybeResultOrPointer; - }): PendingArray | MaybeResultOrPointer[] { + private _gatherDependencies( + provided: Record< + number, + PendingMaybe | MaybeResultOrPointer + > + ): PendingArray | MaybeResultOrPointer[] { const deps: (PendingMaybe | V)[] = []; this.sheet.debug([this.id], "gathering deps values", { id: this.id, diff --git a/src/sheet.ts b/src/sheet.ts index c87b6e7..1b60443 100644 --- a/src/sheet.ts +++ b/src/sheet.ts @@ -17,10 +17,10 @@ import { dispatch, dispatchPromiseOrValueArray } from "./promise"; import { SheetProxy } from "./proxy"; import type { AnyCellArray } from "./types"; -type Computations = ( - | Pending - | CellResult -)[]; +type Computations = Record< + number, + Pending | CellResult +>; type IterationResult = { computations: Computations; updated: Set; @@ -545,7 +545,7 @@ export class Sheet { ); }; - const computations: Computations = []; + const computations: Computations = {}; const release = this.working.startNewComputation(); for (const id of roots) { computations[id] = dispatch( @@ -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 @@ -700,7 +700,7 @@ export class Sheet { borderComputation, ({ computationsOfBorder, nextIteration }) => { return dispatchPromiseOrValueArray( - computationsOfBorder, + Object.values(computationsOfBorder), (computationsOfBorder) => { //checking for canceled computations this.registerCancelAndDone( @@ -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)]) ); @@ -879,7 +879,7 @@ export class Sheet { ): Computations { 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) {