|
| 1 | +import type { Tag } from '@glimmer/interfaces'; |
| 2 | + |
| 3 | +import type { MonomorphicTagImpl } from './validators'; |
| 4 | + |
| 5 | +import { infoForTag, tagFor } from './meta'; |
| 6 | +import { track } from './tracking'; |
| 7 | +import { validateTag, valueForTag } from './validators'; |
| 8 | + |
| 9 | +type Info = { |
| 10 | + tag: MonomorphicTagImpl; |
| 11 | + prevValue: number; |
| 12 | + dependencies: { |
| 13 | + object: object; |
| 14 | + propertyKey: string; |
| 15 | + changed: boolean; |
| 16 | + }[]; |
| 17 | +}; |
| 18 | + |
| 19 | +export function getTrackedDependencies(obj: Record<string, any>, property: string, info?: Info) { |
| 20 | + info = info || ({} as Info); |
| 21 | + const tag = info?.tag || track(() => obj[property]); |
| 22 | + const dependencies = []; |
| 23 | + // do not include tracked properties from dependencies |
| 24 | + |
| 25 | + const subtags = (Array.isArray(tag.subtag) ? [tag, ...tag.subtag] : [tag, tag.subtag]).filter( |
| 26 | + (t) => !!t |
| 27 | + ) as Tag[]; |
| 28 | + for (const subtag of subtags) { |
| 29 | + if (subtag === tag) continue; |
| 30 | + dependencies.push({ ...infoForTag(subtag), tag: subtag }); |
| 31 | + if (subtag.subtag && !Array.isArray(subtag.subtag)) { |
| 32 | + dependencies.push({ ...infoForTag(subtag.subtag) }); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + let maxRevision = info?.prevValue || valueForTag(tag); |
| 37 | + dependencies.forEach((t) => { |
| 38 | + const value = valueForTag(t.tag); |
| 39 | + maxRevision = Math.max(maxRevision, value); |
| 40 | + }); |
| 41 | + |
| 42 | + const hasChange = (info.prevValue && maxRevision !== info.prevValue) || false; |
| 43 | + let latestValue = valueForTag(tag); |
| 44 | + |
| 45 | + info.prevValue = latestValue; |
| 46 | + info.dependencies = dependencies.map((t) => { |
| 47 | + if (t.tag.lastValue > latestValue) { |
| 48 | + latestValue = t.tag.lastValue; |
| 49 | + } |
| 50 | + const changed = hasChange && t.tag.lastValue >= maxRevision; |
| 51 | + return { object: t.object, propertyKey: t.propertyKey, changed }; |
| 52 | + }); |
| 53 | + |
| 54 | + return info; |
| 55 | +} |
| 56 | + |
| 57 | +type TrackedInfo = { |
| 58 | + changed: string[]; |
| 59 | + propertyInfo: Record<string, any>; |
| 60 | +}; |
| 61 | + |
| 62 | +export function getChangedProperties(obj: object, trackedInfo?: TrackedInfo) { |
| 63 | + trackedInfo = trackedInfo || ({} as TrackedInfo); |
| 64 | + trackedInfo['changed'] = []; |
| 65 | + trackedInfo.propertyInfo = trackedInfo.propertyInfo || {}; |
| 66 | + for (const name in obj) { |
| 67 | + let tagInfo = trackedInfo.propertyInfo?.[name] || { |
| 68 | + tag: tagFor(obj, name), |
| 69 | + revision: (tagFor(obj, name) as MonomorphicTagImpl)['revision'], |
| 70 | + }; |
| 71 | + if (!tagInfo.tag) return; |
| 72 | + trackedInfo.propertyInfo[name] = tagInfo; |
| 73 | + |
| 74 | + const changed = !validateTag(tagInfo.tag, tagInfo.revision); |
| 75 | + tagInfo.revision = (tagInfo.tag as MonomorphicTagImpl)['revision']; |
| 76 | + if (changed) { |
| 77 | + trackedInfo['changed'].push(name); |
| 78 | + } |
| 79 | + } |
| 80 | + return trackedInfo; |
| 81 | +} |
0 commit comments