Skip to content

Commit

Permalink
Merge 3588dc5 into 5cea8dd
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed May 26, 2024
2 parents 5cea8dd + 3588dc5 commit 0a7b348
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export class Cell<
it means that [v] will be invalidated by an ongoing computation */
protected _valueRank = 0;
protected _currentComputationRank = 0;
private _version = 0;

protected _pending_: PendingMaybe<V, MaybeError> | undefined;
private _pendingRank: number | null = null;
Expand Down Expand Up @@ -214,6 +215,10 @@ export class Cell<
// console.log(`Cell ${id}: `, "constructed")
}

get version(): number {
return this._version;
}

get isPointer(): boolean {
return this._isPointer;
}
Expand Down Expand Up @@ -550,6 +555,7 @@ export class Cell<
newValueRank: computationRank
});
this.v = newValue;
this._version++;
this._valueRank = computationRank;
// Update localStorage if set.
if (needUpdate && this._storageKey) {
Expand Down Expand Up @@ -744,6 +750,9 @@ export class ValueCell<V> extends Cell<V, false, false> {

if (value !== undefined) {
this.v = value;
// @ts-expect-error: _version is private, this is the single instance where
// we update it when creating a new ValueCell with a direct value.
this._version++;
if (value instanceof Cell) {
this.setPointed(value.id);
} else {
Expand Down
44 changes: 44 additions & 0 deletions src/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect, test } from "vitest";

import { isEqual } from "./isEqual.test";
import { delayed, sleep } from "./promise";
import { Sheet } from "./sheet";

test("version for ValueCell", async () => {
const proxy = new Sheet(isEqual).newProxy();
const a = proxy.new(1);
expect(a.version).toBe(1);
a.set(2);
expect(a.version).toBe(2);
a.update((v) => v + 1);
expect(a.version).toBe(3);
a.update((v) => delayed(v + 1, 10));
expect(a.version).toBe(3);
await sleep(20);
expect(a.version).toBe(4);
});

test("version for ValueCell promise", async () => {
const proxy = new Sheet(isEqual).newProxy();
const a = proxy.new(delayed(1, 10));
expect(a.version).toBe(0);
await sleep(20);
expect(a.version).toBe(1);
});

test("version for MapCell", async () => {
const proxy = new Sheet(isEqual).newProxy();
const a = proxy.new(1);
const b = a.map((v) => v + 1);
expect(b.version).toBe(1);
a.set(2);
expect(b.version).toBe(2);
const c = a.map((v) => delayed(v * 2, 10));
expect(c.version).toBe(0);
await sleep(20);
expect(c.version).toBe(1);
a.set(3);
expect(c.version).toBe(1);
await sleep(20);
expect(c.version).toBe(2);
});

0 comments on commit 0a7b348

Please sign in to comment.