-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the cache API specified in [RFC 615](https://github.com/emberjs/rfcs/blob/master/text/0615-autotracking-memoization.md). There were a few minor refactors in addition to this: - Moved `trackedData` into a separate module, since the `tracking` module was getting pretty big. - Renamed `isConst` to `isConstTagged`, since there is a naming conflict with the new `isConst` function. Given `isConstTagged` will be removed soon and is not generally much used outside of the VM, I thought it was better to prioritize the new API getting this name. - Rewrote the `memoizeTracked` implementation on top of the cache APIs. One thing worth noting is that in order to support the API `memoizeTracked` had before, `getValue` also needed to be able to accept and pass on args. This is something we'll wrap to hide when we expose this publicly in Ember/Glimmer for the time being, since that was a capability that was not added in the RFC.
- Loading branch information
Chris Garrett
committed
May 12, 2020
1 parent
140af2a
commit 01acfbe
Showing
11 changed files
with
379 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DEBUG } from '@glimmer/env'; | ||
import { tagFor, dirtyTagFor } from './meta'; | ||
import { assertTagNotConsumed } from './debug'; | ||
import { consumeTag } from './tracking'; | ||
|
||
export type Getter<T, K extends keyof T> = (self: T) => T[K] | undefined; | ||
export type Setter<T, K extends keyof T> = (self: T, value: T[K]) => void; | ||
|
||
export function trackedData<T extends object, K extends keyof T>( | ||
key: K, | ||
initializer?: (this: T) => T[K] | ||
): { getter: Getter<T, K>; setter: Setter<T, K> } { | ||
let values = new WeakMap<T, T[K]>(); | ||
let hasInitializer = typeof initializer === 'function'; | ||
|
||
function getter(self: T) { | ||
consumeTag(tagFor(self, key)); | ||
|
||
let value; | ||
|
||
// If the field has never been initialized, we should initialize it | ||
if (hasInitializer && !values.has(self)) { | ||
value = initializer!.call(self); | ||
values.set(self, value); | ||
} else { | ||
value = values.get(self); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
function setter(self: T, value: T[K]): void { | ||
if (DEBUG) { | ||
assertTagNotConsumed!(tagFor(self, key), self, key, true); | ||
} | ||
|
||
dirtyTagFor(self, key); | ||
values.set(self, value); | ||
} | ||
|
||
return { getter, setter }; | ||
} |
Oops, something went wrong.