Skip to content

Commit

Permalink
Cleanup: findCell, debug messages (#33)
Browse files Browse the repository at this point in the history
* array: remove findCell

* sheet: improve debug messages

* cellify: improve cell names

* cell: improve debug messages

* package: update, update biome

* json: jsonStringify preserves number if bigint < 1M

* package: update lock
  • Loading branch information
hbbio committed May 10, 2024
1 parent 06a3097 commit 1403428
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 99 deletions.
8 changes: 5 additions & 3 deletions biome.json
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"$schema": "https://biomejs.dev/schemas/1.7.1/schema.json",
"organizeImports": {
"enabled": true
},
Expand All @@ -8,12 +8,14 @@
"indentWidth": 2
},
"javascript": {
"formatter": { "trailingComma": "none" }
"formatter": {
"trailingComma": "none"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
}
76 changes: 38 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@okcontract/cells",
"version": "0.3.0",
"version": "0.3.1",
"description": "Simplified reactive functional programming for the web",
"private": false,
"main": "dist/cells.umd.cjs",
Expand All @@ -14,12 +14,17 @@
"types": "./dist/index.d.ts"
}
},
"files": ["dist/", "assets/", "README.md", "LICENSE"],
"files": [
"dist/",
"assets/",
"README.md",
"LICENSE"
],
"dependencies": {
"@okcontract/graph": "^0.1.4"
},
"devDependencies": {
"@biomejs/biome": "^1.7.0",
"@biomejs/biome": "^1.7.1",
"@types/node": "^20.12.7",
"@vitest/coverage-v8": "^1.5.0",
"happy-dom": "^14.7.1",
Expand All @@ -45,4 +50,4 @@
},
"author": "Henri Binsztok",
"license": "MIT"
}
}
43 changes: 0 additions & 43 deletions src/array.ts
Expand Up @@ -219,49 +219,6 @@ export const find = <T, NF extends boolean = false>(
);
};

// @todo generalize
export const findCell = <T, NF extends boolean = false>(
proxy: SheetProxy,
arr: CellArray<T>,
predicate: AnyCell<(v: AnyCell<T>) => AnyCell<boolean>>,
name = "find",
nf?: NF
) => {
const coll = collector<MapCell<T, NF>>(proxy);
// Since predicate is a reactive function, we have to instantiate
// the computation for each cell.
// const keep = mapArrayCell(proxy, arr, predicate, "keep", nf);
let prevFn: (elt: AnyCell<T>) => AnyCell<boolean>;
const keep = proxy.map(
[predicate, arr],
(fn, cells, _prev: AnyCell<boolean>[]) => {
// console.log({ keep: cells.length });
// @todo if the predicate function has changed, collect previous mapped cells
const keep = cells.map((cell) => {
// We can reuse a cell only if the predicate hasn't changed.
// @todo collect previously mapped cells for deleted cells in arr
const reuse =
prevFn === fn &&
_prev?.find((_c) => _c.dependencies?.[0] === cell.id);
return reuse || fn(cell);
});
prevFn = fn;
return keep;
},
"keep",
nf
);
return proxy.map(
[arr, keep],
(cells, _keep) =>
coll(
proxy.mapNoPrevious(_keep, (..._flat) => cells.find((_, i) => _flat[i]))
),
name,
nf
);
};

export const findIndex = <T, NF extends boolean = false>(
proxy: SheetProxy,
arr: CellArray<T>,
Expand Down
7 changes: 4 additions & 3 deletions src/cell.ts
Expand Up @@ -964,10 +964,11 @@ export class MapCell<V, NF extends boolean> extends Cell<V, true, NF> {
false,
false
);
// this.sheet._debug &&
console.warn(
this.sheet.debug(
[this.id],
"cancelled",
simplifier({ paramsResults, firstCanceled, cell: this.id })
{ paramsResults, firstCanceled },
console.warn
);
return cancelComputation;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cellify.ts
Expand Up @@ -41,7 +41,7 @@ const errIsCell = new Error("value is cell");
export const cellify = <T>(
proxy: SheetProxy,
v: T,
name = "cellify",
name = "ç",
failOnCell = false
): Cellified<T> => {
if (v instanceof Cell) {
Expand All @@ -50,11 +50,11 @@ export const cellify = <T>(
}
return proxy.new(
Array.isArray(v)
? v.map((vv) => cellify(proxy, vv, name, failOnCell), "cellify.[]")
? v.map((vv, i) => cellify(proxy, vv, `${name}[${i}]`, failOnCell), "ç[]")
: isObject(v)
? Object.fromEntries(
Object.entries(v).map(
([k, vv]) => [k, cellify(proxy, vv, name, failOnCell)],
([k, vv]) => [k, cellify(proxy, vv, `${name}[${k}]`, failOnCell)],
"ç{}"
)
)
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Expand Up @@ -31,7 +31,6 @@ export {
filter,
filterPredicateCell,
find,
findCell,
findIndex,
first,
last,
Expand All @@ -43,9 +42,9 @@ export {
} from "./array";
export {
cellify,
uncellify,
follow,
isObject,
uncellify,
type Cellified,
type Key,
type Path,
Expand Down
2 changes: 1 addition & 1 deletion src/json.ts
Expand Up @@ -57,7 +57,7 @@ export const jsonStringify = <T>(
case "symbol":
break;
case "bigint":
out += `"${v.toString()}"`;
out += v < 1_000_000n ? Number(v) : `"${v.toString()}"`;
break;
default:
out += JSON.stringify(v);
Expand Down
4 changes: 2 additions & 2 deletions src/sheet.ts
Expand Up @@ -937,7 +937,7 @@ export class Sheet {
.filter((id) => this._cells[id]);
const refs = this.references(ids);
if (refs.size > 0) {
console.warn("deletion failed", { refs });
console.warn("deletion failed", { input, refs });
throw ReferencesLeft;
}
for (const id of ids) {
Expand All @@ -954,7 +954,7 @@ export class Sheet {
*/
collect(...input: (number | AnyCell<unknown>)[]) {
const ids = input.map((v) => (typeof v === "number" ? v : v.id));
if (this._debug) console.log(simplifier({ collecting: ids, cells: input }));
this.debug(ids, "collect", { collecting: ids, cells: input });
for (const id of ids) {
const deps = this.g.partialTopologicalSort(id);
this.debug(undefined, "collect", { deps });
Expand Down

0 comments on commit 1403428

Please sign in to comment.