Skip to content

Commit

Permalink
Merge pull request #288 from PrefectHQ/update-viewport-date-range-whe…
Browse files Browse the repository at this point in the history
…n-scale-changes

Sync global scales when scale changes
  • Loading branch information
pleek91 authored Oct 30, 2023
2 parents 4cdb75e + 144b017 commit 0833123
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/factories/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export async function nodesContainerFactory(runId: string) {
if (!data.end_time) {
interval = setTimeout(() => fetch(), DEFAULT_POLL_INTERVAL)
}

container.emit('fetched', data)
}

async function renderNodes(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion src/objects/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EffectScope } from 'vue'
import { eventsFactory } from '@/factories/events'
import { HorizontalScale } from '@/factories/position'
import { LayoutSettings } from '@/models/layout'
import { RequiredGraphConfig } from '@/models/RunGraph'
import { RequiredGraphConfig, RunGraphData } from '@/models/RunGraph'
import { ViewportDateRange } from '@/models/viewport'
import { Fonts } from '@/objects/fonts'
import { VisibilityCull } from '@/services/visibilityCull'
Expand All @@ -28,6 +28,8 @@ type Events = {
cullCreated: Cull,
labelCullCreated: VisibilityCull,
edgeCullCreated: VisibilityCull,
runDataCreated: RunGraphData,
runDataUpdated: RunGraphData,
}

export type EventKey = keyof Events
Expand Down
21 changes: 21 additions & 0 deletions src/objects/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Ticker } from 'pixi.js'
import { NodesContainer, nodesContainerFactory } from '@/factories/nodes'
import { RunGraphData } from '@/models/RunGraph'
import { waitForConfig } from '@/objects/config'
import { EventKey, emitter, waitForEvent } from '@/objects/events'
import { centerViewport, waitForViewport } from '@/objects/viewport'

let nodes: NodesContainer | null = null
let data: RunGraphData | null = null

export async function startNodes(): Promise<void> {
const viewport = await waitForViewport()
Expand All @@ -18,11 +21,29 @@ export async function startNodes(): Promise<void> {
nodes.render()

nodes.element.once('rendered', center)
nodes.element.on('fetched', onFetched)
}

export function stopNodes(): void {
nodes?.stop()
nodes = null
data = null
}

export async function waitForRunData(): Promise<RunGraphData> {
if (data) {
return data
}

return await waitForEvent('runDataCreated')
}

function onFetched(value: RunGraphData): void {
const event: EventKey = data ? 'runDataUpdated' : 'runDataCreated'

data = value

emitter.emit(event, data)
}

function center(): void {
Expand Down
21 changes: 14 additions & 7 deletions src/objects/scale.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { HorizontalScale, horizontalScaleFactory } from '@/factories/position'
import { horizontalSettingsFactory } from '@/factories/settings'
import { waitForConfig } from '@/objects/config'
import { emitter, waitForEvent } from '@/objects/events'
import { EventKey, emitter, waitForEvent } from '@/objects/events'
import { waitForRunData } from '@/objects/nodes'

let scale: HorizontalScale | null = null

export async function startScale(): Promise<void> {
const config = await waitForConfig()
const data = await config.fetch(config.runId)
const settings = await horizontalSettingsFactory(data.start_time)
const data = await waitForRunData()

scale = await horizontalScaleFactory(settings)
setHorizontalScale(data.start_time)

emitter.emit('scaleCreated', scale)
emitter.on('layoutUpdated', () => setHorizontalScale(data.start_time))
}

export function stopScale(): void {
Expand All @@ -26,4 +24,13 @@ export async function waitForScale(): Promise<HorizontalScale> {
}

return await waitForEvent('scaleCreated')
}

function setHorizontalScale(startTime: Date): void {
const event: EventKey = scale ? 'scaleUpdated' : 'scaleCreated'
const settings = horizontalSettingsFactory(startTime)

scale = horizontalScaleFactory(settings)

emitter.emit(event, scale)
}
1 change: 1 addition & 0 deletions src/objects/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function startViewport(props: RunGraphProps): Promise<void> {

emitter.emit('viewportCreated', viewport)
emitter.on('applicationResized', resizeViewport)
emitter.on('scaleUpdated', () => updateViewportDateRange())

watchVisibleDateRange(props)
startViewportDateRange()
Expand Down
3 changes: 3 additions & 0 deletions src/pixi.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { RunGraphData } from '@/models'

declare namespace GlobalMixins {
interface DisplayObjectEvents {
resized: [{ height: number, width: number }],
rendered: [],
fetched: [RunGraphData],
}
}

0 comments on commit 0833123

Please sign in to comment.