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

RFC: 669, @glimmer/tracking/primitives/storage #20814

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
77 changes: 77 additions & 0 deletions packages/@ember/-internals/metal/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { UpdatableTag } from '@glimmer/interfaces';

import { consumeTag } from './tracking';

Check failure on line 3 in packages/@ember/-internals/metal/lib/storage.ts

View workflow job for this annotation

GitHub Actions / Type Checking (current version)

Cannot find module './tracking' or its corresponding type declarations.

Check failure on line 3 in packages/@ember/-internals/metal/lib/storage.ts

View workflow job for this annotation

GitHub Actions / Linting

Unable to resolve path to module './tracking'
import { createUpdatableTag, DIRTY_TAG } from './validators';

Check failure on line 4 in packages/@ember/-internals/metal/lib/storage.ts

View workflow job for this annotation

GitHub Actions / Type Checking (current version)

Cannot find module './validators' or its corresponding type declarations.

Check failure on line 4 in packages/@ember/-internals/metal/lib/storage.ts

View workflow job for this annotation

GitHub Actions / Linting

Unable to resolve path to module './validators'
import { assert } from '@ember/debug';

const SET = Symbol.for('TrackedStorage.set');
const READ = Symbol.for('TrackedStorage.read');

function tripleEq<Value>(a: Value, b: Value): boolean {
return a === b;
}

class Storage<Value> {
#tag: UpdatableTag;
#value: Value;
#lastValue: Value;
#isEqual: (a: Value, b: Value) => boolean;

get #current() {
consumeTag(this.#tag);
return this.#value;
}
set #current(value) {
if (this.#isEqual(this.#value, this.#lastValue)) {
return;
}

this.#value = this.#lastValue = value;

DIRTY_TAG(this.#tag);
}

constructor(initialValue: Value, isEqual?: (a: Value, b: Value) => boolean) {
this.#tag = createUpdatableTag();
this.#value = this.#lastValue = initialValue;
this.#isEqual = isEqual ?? tripleEq;
}

[READ]() {
return this.#current;
}

[SET](value: Value) {
this.#current = value;
}
}

export function createStorage<Value>(
initialValue: Value,
isEqual?: (oldValue: Value, newValue: Value) => boolean
): Storage<Value> {
assert(
'the second parameter to `createStorage` must be an equality function or undefined',
isEqual === undefined || typeof isEqual === 'function'
);

return new Storage(initialValue, isEqual);
}

export function getValue<Value>(storage: Storage<Value>): Value {
assert(
'getValue must be passed a tracked store created with `createStorage`.',
storage instanceof Storage
);

return storage[READ]();
}

export function setValue<Value>(storage: Storage<Value>, value: Value): void {
assert(
'setValue must be passed a tracked store created with `createStorage`.',
storage instanceof Storage
);

storage[SET](value);
}
85 changes: 85 additions & 0 deletions packages/@ember/-internals/metal/tests/tracked/storage_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { createCache, getValue as getCacheValue } from '../../lib/cache';

import { createStorage, getValue, setValue } from '../../lib/storage';

import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

moduleFor(
'@ember/-internals/metal/storage',
class extends AbstractTestCase {
['@test it works'](assert) {
let count = 0;

let storage = createStorage();

let cache = createCache(() => {
getValue(storage);
return ++count;
});

assert.equal(getValue(storage), undefined, 'does not have a value initially');
assert.equal(getCacheValue(cache), 1, 'cache runs the first time');
assert.equal(getCacheValue(cache), 1, 'cache does not the second time');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value is set correctly');
assert.equal(getCacheValue(cache), 2, 'cache ran after storage was set');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value remains the same');
assert.equal(getCacheValue(cache), 2, 'cache not ran after storage was set to same value');
}

['@test it can set an initial value'](assert) {
let count = 0;

let storage = createStorage(123);

let cache = createCache(() => {
getValue(storage);
return ++count;
});

assert.equal(getValue(storage), 123, 'has a initial value');
assert.equal(getCacheValue(cache), 1, 'cache runs the first time');
assert.equal(getCacheValue(cache), 1, 'cache does not the second time');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value is not updated');
assert.equal(getCacheValue(cache), 1, 'cache not ran after storage was set to same value');

setValue(storage, 456);

assert.equal(getValue(storage), 456, 'value updated');
assert.equal(getCacheValue(cache), 2, 'cache ran after storage was set to different value');
}

['@test it can set an equality function'](assert) {
let count = 0;

let storage = createStorage(123, () => false);

let cache = createCache(() => {
getValue(storage);
return ++count;
});

assert.equal(getValue(storage), 123, 'has a initial value');
assert.equal(getCacheValue(cache), 1, 'cache runs the first time');
assert.equal(getCacheValue(cache), 1, 'cache does not the second time');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value is not updated');
assert.equal(getCacheValue(cache), 2, 'cache runs again');

setValue(storage, 456);

assert.equal(getValue(storage), 456, 'value updated');
assert.equal(getCacheValue(cache), 3, 'cache ran after storage was set to different value');
}
}
);
10 changes: 10 additions & 0 deletions packages/@glimmer/tracking/primitives/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { createStorage, getValue, setValue } from '@ember/-internals/metal/lib/storage';

/**
* NOTE: '@ember/-internals/metal' already exports a getValue function
* from @glimmer/tracking/primitives/cache.ts,
* so we can't use the pattern of re-export everything from
* @ember/-internals/metal
*
* At somepoint we need to untangle all that actually move things to their appropriate packages.
*/
34 changes: 34 additions & 0 deletions packages/@glimmer/tracking/tests/storage_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

import { createStorage, getValue, setValue } from '@glimmer/tracking/primitives/storage';
import { createCache, getValue as getCacheValue } from '@glimmer/tracking/primitives/cache';

moduleFor(
'@glimmer/tracking/primitives/storage',
class extends AbstractTestCase {
['@test it works'](assert: QUnit['assert']) {
let count = 0;

let storage = createStorage();

Check failure on line 12 in packages/@glimmer/tracking/tests/storage_test.ts

View workflow job for this annotation

GitHub Actions / Type Checking (current version)

Expected 1-2 arguments, but got 0.

let cache = createCache(() => {
getValue(storage);
return ++count;
});

assert.equal(getValue(storage), undefined, 'does not have a value initially');
assert.equal(getCacheValue(cache), 1, 'cache runs the first time');
assert.equal(getCacheValue(cache), 1, 'cache does not the second time');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value is set correctly');
assert.equal(getCacheValue(cache), 2, 'cache ran after storage was set');

setValue(storage, 123);

assert.equal(getValue(storage), 123, 'value remains the same');
assert.equal(getCacheValue(cache), 2, 'cache not ran after storage was set to same value');
}
}
);
Loading