Skip to content

Commit a817397

Browse files
committed
fix for polls not starting on initial page load
1 parent 3eb9ebd commit a817397

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

packages/core/src/page.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fireNavigateEvent } from './events'
22
import { History } from './history'
33
import { Scroll } from './scroll'
4-
import { Component, Page, PageHandler, PageResolver, PreserveStateOption, RouterInitParams } from './types'
4+
import { Component, Page, PageEvent, PageHandler, PageResolver, PreserveStateOption, RouterInitParams } from './types'
55
import { hrefToUrl, isSameUrlWithoutHash } from './url'
66

77
class CurrentPage {
@@ -10,6 +10,11 @@ class CurrentPage {
1010
protected resolveComponent!: PageResolver
1111
protected componentId = {}
1212
protected onNewComponentCallbacks: VoidFunction[] = []
13+
protected onFirstLoadCallbacks: VoidFunction[] = []
14+
protected listeners: {
15+
event: PageEvent
16+
callback: VoidFunction
17+
}[] = []
1318
protected firstPageLoad = true
1419

1520
public init({ initialPage, swapComponent, resolveComponent }: RouterInitParams) {
@@ -47,12 +52,18 @@ class CurrentPage {
4752
replace = replace || isSameUrlWithoutHash(hrefToUrl(page.url), window.location)
4853
replace ? History.replaceState(page) : History.pushState(page)
4954

50-
const isNewComponent = !this.isTheSame(page) || this.firstPageLoad
55+
const isNewComponent = !this.isTheSame(page)
5156

5257
this.page = page
5358

5459
if (isNewComponent) {
55-
this.onNewComponentCallbacks.forEach((cb) => cb())
60+
this.listeners
61+
.filter((listener) => listener.event === 'newComponent')
62+
.forEach((listener) => listener.callback())
63+
}
64+
65+
if (this.firstPageLoad) {
66+
this.listeners.filter((listener) => listener.event === 'firstLoad').forEach((listener) => listener.callback())
5667
}
5768

5869
this.firstPageLoad = false
@@ -105,11 +116,11 @@ class CurrentPage {
105116
return this.page.component === page.component
106117
}
107118

108-
public onNewComponent(cb: VoidFunction): VoidFunction {
109-
this.onNewComponentCallbacks.push(cb)
119+
public on(event: PageEvent, callback: VoidFunction): VoidFunction {
120+
this.listeners.push({ event, callback })
110121

111122
return () => {
112-
this.onNewComponentCallbacks = this.onNewComponentCallbacks.filter((callback) => callback !== cb)
123+
this.listeners = this.listeners.filter((listener) => listener.event !== event && listener.callback !== callback)
113124
}
114125
}
115126
}

packages/core/src/router.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ export class Router {
3838
})
3939

4040
public init({ initialPage, resolveComponent, swapComponent }: RouterInitParams): void {
41-
currentPage
42-
.init({
43-
initialPage,
44-
resolveComponent,
45-
swapComponent,
46-
})
47-
.onNewComponent(() => {
48-
poll.clear()
49-
this.loadDeferredProps()
50-
})
41+
currentPage.init({
42+
initialPage,
43+
resolveComponent,
44+
swapComponent,
45+
})
46+
47+
currentPage.on('firstLoad', () => {
48+
this.loadDeferredProps()
49+
})
50+
51+
currentPage.on('newComponent', () => {
52+
poll.clear()
53+
this.loadDeferredProps()
54+
})
5155

5256
this.clearRememberedStateOnReload()
5357
this.initializeVisit()

packages/core/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export type GlobalEventsMap = {
153153
}
154154
}
155155

156+
export type PageEvent = 'newComponent' | 'firstLoad'
157+
156158
export type GlobalEventNames = keyof GlobalEventsMap
157159

158160
export type GlobalEvent<TEventName extends GlobalEventNames> = CustomEvent<GlobalEventDetails<TEventName>>

0 commit comments

Comments
 (0)