Skip to content

Commit

Permalink
fix: be able to reference other objects in own class or layer
Browse files Browse the repository at this point in the history
This is intende to be a fix for #50
  • Loading branch information
nytamin committed Sep 27, 2019
1 parent 51998a0 commit 3996a80
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export interface ResolvedTimelineObject extends TimelineObject {
parentId?: string
/** True if object is a keyframe */
isKeyframe?: boolean
/** True if object is referencing itself (only directly, not indirectly via another object) */
isSelfReferencing?: boolean
}
}
export interface TimelineObjectInstance {
Expand Down
24 changes: 19 additions & 5 deletions src/resolver/__tests__/resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lookupExpression, Resolver } from '../resolver'
import { ResolvedTimeline, TimelineObject } from '../../api/api'
import { ResolvedTimeline, TimelineObject, ResolvedTimelineObject } from '../../api/api'
import { interpretExpression } from '../expression'
import { EventType } from '../../api/enums'
import { resetId } from '../../lib'
Expand All @@ -25,11 +25,16 @@ describe('resolver', () => {
resolvedKeyframeCount: 0
}
}
const stdObj: TimelineObject = {
const stdObj: ResolvedTimelineObject = {
id: 'obj0',
layer: '10',
enable: {},
content: {}
content: {},
resolved: {
resolved: false,
resolving: false,
instances: []
}
}
test('expression: basic math', () => {
expect(lookupExpression(rtl, stdObj, interpretExpression('1+2'), 'start')) .toEqual({ value: 1 + 2, references: [] })
Expand Down Expand Up @@ -238,11 +243,16 @@ describe('resolver', () => {
classes: {},
layers: {}
}
const obj: TimelineObject = {
const obj: ResolvedTimelineObject = {
id: 'obj0',
layer: '10',
enable: {},
content: {}
content: {},
resolved: {
resolved: false,
resolving: false,
instances: []
}
}

expect(lookupExpression(rtl, obj, interpretExpression('#unknown'), 'start')).toEqual(null)
Expand Down Expand Up @@ -1480,12 +1490,14 @@ describe('resolver', () => {
end: 8
}])
expect(resolved.objects['video1']).toBeTruthy()
expect(resolved.objects['video1'].resolved.isSelfReferencing).toEqual(true)
expect(resolved.objects['video1'].resolved.instances).toMatchObject([{
start: 8,
end: 9, // becuse it's overridden by video2
originalEnd: 10
}])
expect(resolved.objects['video2']).toBeTruthy()
expect(resolved.objects['video2'].resolved.isSelfReferencing).toEqual(true)
expect(resolved.objects['video2'].resolved.instances).toMatchObject([{
start: 9,
end: 11
Expand Down Expand Up @@ -1543,12 +1555,14 @@ describe('resolver', () => {
end: 8
}])
expect(resolved.objects['video1']).toBeTruthy()
expect(resolved.objects['video1'].resolved.isSelfReferencing).toEqual(true)
expect(resolved.objects['video1'].resolved.instances).toMatchObject([{
start: 8,
end: 9, // becuse it's overridden by video2
originalEnd: 10
}])
expect(resolved.objects['video2']).toBeTruthy()
expect(resolved.objects['video2'].resolved.isSelfReferencing).toEqual(true)
expect(resolved.objects['video2'].resolved.instances).toMatchObject([{
start: 9,
end: 11
Expand Down
72 changes: 51 additions & 21 deletions src/resolver/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export class Resolver {
resolved: false,
resolving: false,
instances: [],
levelDeep: levelDeep
levelDeep: levelDeep,
isSelfReferencing: false
}
})
if (parentId) o.resolved.parentId = parentId
Expand Down Expand Up @@ -359,7 +360,7 @@ export function resolveTimelineObj (resolvedTimeline: ResolvedTimeline, obj: Res
type ObjectRefType = 'start' | 'end' | 'duration'
export function lookupExpression (
resolvedTimeline: ResolvedTimeline,
obj: TimelineObject,
obj: ResolvedTimelineObject,
expr: Expression | null,
context: ObjectRefType
): Array<TimelineObjectInstance> | ValueWithReference | null {
Expand Down Expand Up @@ -390,10 +391,11 @@ export function lookupExpression (
return []
}
}

// Look up string
let invert: boolean = false
let ignoreFirstIfZero: boolean = false
const referencedObjs: ResolvedTimelineObject[] = []
let referencedObjs: ResolvedTimelineObject[] = []
let ref: ObjectRefType = context
let rest: string = ''

Expand Down Expand Up @@ -425,13 +427,25 @@ export function lookupExpression (
}
}
}
_.each(objIdsToReference, (objId: string) => {
const obj = resolvedTimeline.objects[objId]
if (obj) {
referencedObjs.push(obj)
_.each(objIdsToReference, (refObjId: string) => {
if (refObjId !== obj.id) {
const refObj = resolvedTimeline.objects[refObjId]
if (refObj) {
referencedObjs.push(refObj)
}
} else {
// Looks like the object is referencing itself!
if (obj.resolved.resolving) {
obj.resolved.isSelfReferencing = true
}
}
})

if (obj.resolved.isSelfReferencing) {
// Exclude any self-referencing objects:
referencedObjs = _.filter(referencedObjs, refObj => {
return !refObj.resolved.isSelfReferencing
})
}
if (referencedObjs.length) {
if (rest.match(/start/)) ref = 'start'
if (rest.match(/end/)) ref = 'end'
Expand All @@ -443,18 +457,26 @@ export function lookupExpression (
_.each(referencedObjs, (referencedObj: ResolvedTimelineObject) => {
resolveTimelineObj(resolvedTimeline, referencedObj)
if (referencedObj.resolved.resolved) {
const firstInstance = _.first(referencedObj.resolved.instances)
if (firstInstance) {
const duration: number | null = (
firstInstance.end !== null ?
firstInstance.end - firstInstance.start :
null
)
if (duration !== null) {
instanceDurations.push({
value: duration,
references: joinReferences(referencedObj.id, firstInstance.references)
})
if (
obj.resolved.isSelfReferencing &&
referencedObj.resolved.isSelfReferencing
) {
// If the querying object is self-referencing, exclude any other self-referencing objects,
// ignore the object
} else {
const firstInstance = _.first(referencedObj.resolved.instances)
if (firstInstance) {
const duration: number | null = (
firstInstance.end !== null ?
firstInstance.end - firstInstance.start :
null
)
if (duration !== null) {
instanceDurations.push({
value: duration,
references: joinReferences(referencedObj.id, firstInstance.references)
})
}
}
}
}
Expand All @@ -477,7 +499,15 @@ export function lookupExpression (
_.each(referencedObjs, (referencedObj: ResolvedTimelineObject) => {
resolveTimelineObj(resolvedTimeline, referencedObj)
if (referencedObj.resolved.resolved) {
returnInstances = returnInstances.concat(referencedObj.resolved.instances)
if (
obj.resolved.isSelfReferencing &&
referencedObj.resolved.isSelfReferencing
) {
// If the querying object is self-referencing, exclude any other self-referencing objects,
// ignore the object
} else {
returnInstances = returnInstances.concat(referencedObj.resolved.instances)
}
}
})
if (returnInstances.length) {
Expand Down

0 comments on commit 3996a80

Please sign in to comment.