-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { Tag } from '@glimmer/interfaces'; | ||
|
||
import type { MonomorphicTagImpl } from './validators'; | ||
|
||
import { infoForTag, tagFor } from './meta'; | ||
import { track } from './tracking'; | ||
import { validateTag, valueForTag } from './validators'; | ||
|
||
type Info = { | ||
tag: MonomorphicTagImpl; | ||
prevValue: number; | ||
dependencies: { | ||
object: object; | ||
propertyKey: string; | ||
changed: boolean; | ||
}[]; | ||
}; | ||
|
||
export function getTrackedDependencies(obj: Record<string, any>, property: string, info?: Info) { | ||
info = info || ({} as Info); | ||
const tag = info?.tag || track(() => obj[property]); | ||
const dependencies = []; | ||
// do not include tracked properties from dependencies | ||
|
||
const subtags = (Array.isArray(tag.subtag) ? [tag, ...tag.subtag] : [tag, tag.subtag]).filter( | ||
(t) => !!t | ||
) as Tag[]; | ||
for (const subtag of subtags) { | ||
if (subtag === tag) continue; | ||
dependencies.push({ ...infoForTag(subtag), tag: subtag }); | ||
if (subtag.subtag && !Array.isArray(subtag.subtag)) { | ||
dependencies.push({ ...infoForTag(subtag.subtag) }); | ||
} | ||
} | ||
|
||
let maxRevision = valueForTag(tag); | ||
|
||
const hasChange = (info.prevValue && maxRevision !== info.prevValue) || false; | ||
let latestValue = info.prevValue || 0; | ||
|
||
info.dependencies = dependencies.map((t) => { | ||
if (t.tag.lastValue > latestValue) { | ||
latestValue = t.tag.lastValue; | ||
} | ||
const changed = hasChange && t.tag.lastValue > info!.prevValue; | ||
return { object: t.object, propertyKey: t.propertyKey, changed }; | ||
}); | ||
|
||
info.prevValue = maxRevision; | ||
|
||
return info; | ||
} | ||
|
||
type TrackedInfo = { | ||
changed: string[]; | ||
propertyInfo: Record<string, any>; | ||
}; | ||
|
||
export function getChangedProperties(obj: object, trackedInfo?: TrackedInfo) { | ||
trackedInfo = trackedInfo || ({} as TrackedInfo); | ||
trackedInfo['changed'] = []; | ||
trackedInfo.propertyInfo = trackedInfo.propertyInfo || {}; | ||
for (const name in obj) { | ||
const tag = tagFor(obj, name); | ||
const revision = valueForTag(tag); | ||
let tagInfo = trackedInfo.propertyInfo?.[name] || { | ||
tag: tag, | ||
revision, | ||
}; | ||
if (!tagInfo.tag) return; | ||
trackedInfo.propertyInfo[name] = tagInfo; | ||
|
||
const changed = !validateTag(tagInfo.tag, tagInfo.revision); | ||
tagInfo.revision = revision; | ||
if (changed) { | ||
trackedInfo['changed'].push(name); | ||
} | ||
} | ||
return trackedInfo; | ||
} |
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,99 @@ | ||
import { getChangedProperties, getTrackedDependencies, trackedData } from '@glimmer/validator'; | ||
|
||
import { module, test } from './-utils'; | ||
|
||
module('@glimmer/validator: tracked-utils', () => { | ||
class TestObject { | ||
declare item1: string; | ||
declare item2: string; | ||
item3 = ''; | ||
constructor() {} | ||
|
||
get getterWithTracked() { | ||
return this.item1 + ' world' + this.item2; | ||
} | ||
} | ||
|
||
{ | ||
const { getter, setter } = trackedData<TestObject, 'item1'>('item1', () => ''); | ||
Object.defineProperty(TestObject.prototype, 'item1', { | ||
enumerable: true, | ||
get(this) { | ||
return getter(this); | ||
}, | ||
set(this, v) { | ||
return setter(this, v); | ||
}, | ||
}); | ||
} | ||
{ | ||
const { getter, setter } = trackedData<TestObject, 'item2'>('item2', () => ''); | ||
Object.defineProperty(TestObject.prototype, 'item2', { | ||
enumerable: true, | ||
get(this) { | ||
return getter(this); | ||
}, | ||
set(this, v) { | ||
return setter(this, v); | ||
}, | ||
}); | ||
} | ||
|
||
test('it can detect changed properties', (assert) => { | ||
const obj = new TestObject(); | ||
let trackedInfo = getChangedProperties(obj); | ||
assert.deepEqual(trackedInfo?.changed, []); | ||
|
||
obj.item1 = 'hello'; | ||
|
||
assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, ['item1']); | ||
assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, []); | ||
|
||
obj.item1 = 'hi'; | ||
obj.item2 = 'hi'; | ||
assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, ['item1', 'item2']); | ||
}); | ||
|
||
test('it can detect tracked dependencies', (assert) => { | ||
const obj = new TestObject(); | ||
let info = getTrackedDependencies(obj, 'getterWithTracked'); | ||
assert.deepEqual(info.dependencies, [ | ||
{ | ||
changed: false, | ||
object: obj, | ||
propertyKey: 'item1', | ||
}, | ||
{ | ||
changed: false, | ||
object: obj, | ||
propertyKey: 'item2', | ||
}, | ||
]); | ||
|
||
obj.item1 = 'hi'; | ||
assert.deepEqual(getTrackedDependencies(obj, 'getterWithTracked', info).dependencies, [ | ||
{ | ||
changed: true, | ||
object: obj, | ||
propertyKey: 'item1', | ||
}, | ||
{ | ||
changed: false, | ||
object: obj, | ||
propertyKey: 'item2', | ||
}, | ||
]); | ||
assert.deepEqual(getTrackedDependencies(obj, 'getterWithTracked', info).dependencies, [ | ||
{ | ||
changed: false, | ||
object: obj, | ||
propertyKey: 'item1', | ||
}, | ||
{ | ||
changed: false, | ||
object: obj, | ||
propertyKey: 'item2', | ||
}, | ||
]); | ||
}); | ||
}); |