-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* glimmer-validator-compat * +
- Loading branch information
Showing
3 changed files
with
116 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * as caching from './caching-primitives'; | ||
export * as destroyable from './destroyable'; | ||
export * as storage from './storage-primitives'; | ||
export * as validator from './glimmer-validator'; |
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,92 @@ | ||
import { | ||
type Cell, | ||
cellFor, | ||
cellsMap, | ||
getTracker, | ||
isRendering, | ||
setIsRendering, | ||
} from './reactive'; | ||
|
||
export { cellFor as tagFor } from '@lifeart/gxt'; | ||
|
||
export function dirtyTagFor(obj: object, key: string | number | symbol): void { | ||
// @ts-expect-error | ||
const cell = cellFor(obj, key); | ||
cell.update(cell.value); | ||
} | ||
export function tagMetaFor(obj: object): any { | ||
return cellsMap.get(obj); | ||
} | ||
|
||
export function isTracking(): boolean { | ||
return getTracker() !== null; | ||
} | ||
|
||
export function consumeTag(tag: Cell): void { | ||
const TRACKER = getTracker(); | ||
if (TRACKER !== null) { | ||
TRACKER.add(tag); | ||
} | ||
} | ||
|
||
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(cellFor(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 { | ||
dirtyTagFor(self, key); | ||
values.set(self, value); | ||
} | ||
|
||
return { getter, setter }; | ||
} | ||
|
||
let renderingStateBeforeBegin = isRendering(); | ||
|
||
export function beginTrackFrame() { | ||
renderingStateBeforeBegin = isRendering(); | ||
if (!isRendering()) { | ||
setIsRendering(true); | ||
} | ||
} | ||
|
||
export function endTrackFrame() { | ||
if (isRendering() !== renderingStateBeforeBegin) { | ||
setIsRendering(renderingStateBeforeBegin); | ||
} | ||
} | ||
|
||
export function beginUntrackFrame() { | ||
renderingStateBeforeBegin = isRendering(); | ||
if (renderingStateBeforeBegin) { | ||
setIsRendering(false); | ||
} | ||
} | ||
|
||
export function endUntrackFrame() { | ||
if (isRendering() !== renderingStateBeforeBegin) { | ||
setIsRendering(renderingStateBeforeBegin); | ||
} | ||
} |
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