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

[ Pending RFC ] create Cell #1682

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/@glimmer/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (Reflect.has(globalThis, GLIMMER_VALIDATOR_REGISTRATION)) {

Reflect.set(globalThis, GLIMMER_VALIDATOR_REGISTRATION, true);

export { Cell } from './lib/cell';
export { debug } from './lib/debug';
export { dirtyTagFor, tagFor, type TagMeta, tagMetaFor } from './lib/meta';
export { trackedData } from './lib/tracked-data';
Expand Down
54 changes: 54 additions & 0 deletions packages/@glimmer/validator/lib/cell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { UpdatableTag } from '@glimmer/interfaces';

import { consumeTag } from './tracking';
import { createUpdatableTag, DIRTY_TAG } from './validators';

type Equality<T> = (a: T, b: T) => boolean;

export class Cell<Value> {
static create<T>(value: T, equals?: Equality<T>): Cell<T> {
return new Cell(value, equals);
}

#value: Value;
readonly #equals?: Equality<Value> | undefined;
readonly #tag: UpdatableTag;

private constructor(value: Value, equals?: Equality<Value>) {
this.#value = value;
this.#equals = equals;
this.#tag = createUpdatableTag();
}

get current() {
consumeTag(this.#tag);

return this.#value;
}

read(): Value {
consumeTag(this.#tag);

return this.#value;
}

set(value: Value): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth making the set a bounded func? For example:

  set = (value: Value) =>  boolean {

This way we could pass this into templates and not need to create a func just to set the value of a cell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you mean?

it's a bug in the vm that this doesn't work:

<button {{on 'click' (fn cell.set 'hi')}}></button>

if (this.#equals?.(this.#value, value)) {
return false;
}

this.#value = value;

DIRTY_TAG(this.#tag);

return true;
}

update(updater: (value: Value) => Value): void {
this.set(updater(this.#value));
}

freeze(): void {
throw new Error(`Not Implemented`);
}
}
1 change: 1 addition & 0 deletions packages/@glimmer/validator/test/-utils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const module = QUnit.module;
export const test = QUnit.test;
export const skip = QUnit.skip;
28 changes: 28 additions & 0 deletions packages/@glimmer/validator/test/cell-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Cell } from '@glimmer/validator';

import { module, skip, test } from './-utils';

module('Cell', () => {
test('creates reactive storage', (assert) => {
const cell = Cell.create('hello');
assert.strictEqual(cell.read(), 'hello');
});

test('updates when set', (assert) => {
const cell = Cell.create('hello');
cell.set('world');
assert.strictEqual(cell.read(), 'world');
});

test('updates when update() is called', (assert) => {
const cell = Cell.create('hello');
cell.update((value) => value + ' world');
assert.strictEqual(cell.read(), 'hello world');
});

skip('is frozen when freeze() is called', (assert) => {
const cell = Cell.create('hello');
cell.freeze();
assert.throws(() => cell.set('world'));
});
});
Loading