Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell: update directly from a pointer #20

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,11 @@ export class ValueCell<V> extends Cell<V, false, false> {
update = (
fn: (v: V) => V | Promise<V> | AnyCell<V> | Promise<AnyCell<V>>
) => {
if (this.isPointer) throw this.newError("Cell is a pointer");
if (this.isPointer) {
const cell = this._sheet.get(this.pointed);
if (cell instanceof ValueCell) return cell.update(fn);
throw this.newError("Cell is a pointer to a MapCell");
}
// this line is here to help the typechecker
//@ts-expect-error isPointer rules out AnyCell
const v: V | Error | undefined = this.v;
Expand Down
9 changes: 9 additions & 0 deletions src/pointers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,12 @@ test("get pointer chain", async () => {
const m2 = m.map((v) => delayed(m, 20));
await expect(m2.get()).resolves.toBe(2);
});

test("update from pointer", async () => {
const proxy = new Sheet().newProxy();
const v = proxy.new(1);
const p = proxy.new(v);
p.update((x) => x + 1);
await expect(v.get()).resolves.toBe(2);
await expect(p.get()).resolves.toBe(2);
});
Loading