Skip to content

Commit 70080e2

Browse files
committed
moved on start to request class and fixed event ordering
1 parent b9454aa commit 70080e2

File tree

5 files changed

+20
-34
lines changed

5 files changed

+20
-34
lines changed

packages/core/src/request.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { default as axios, AxiosProgressEvent, AxiosRequestConfig, AxiosResponse } from 'axios'
2-
import { fireExceptionEvent, fireFinishEvent, fireProgressEvent } from './events'
2+
import { fireExceptionEvent, fireFinishEvent, fireProgressEvent, fireStartEvent } from './events'
33
import { page as currentPage } from './page'
44
import { RequestParams } from './requestParams'
55
import { Response } from './response'
@@ -17,14 +17,18 @@ export class Request {
1717
) {
1818
this.requestParams = RequestParams.create(params)
1919
this.cancelToken = new AbortController()
20-
this.requestParams.onCancelToken(() => this.cancel({ cancelled: true }))
2120
}
2221

2322
public static create(params: ActiveVisit, page: Page): Request {
2423
return new Request(params, page)
2524
}
2625

2726
public async send() {
27+
this.requestParams.onCancelToken(() => this.cancel({ cancelled: true }))
28+
29+
fireStartEvent(this.requestParams.all())
30+
this.requestParams.onStart()
31+
2832
return axios({
2933
method: this.requestParams.params.method,
3034
url: urlWithoutHash(this.requestParams.params.url).href,

packages/core/src/requestParams.ts

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class RequestParams {
5151
this.params.onFinish(this.params)
5252
}
5353

54+
public onStart() {
55+
this.params.onStart(this.params)
56+
}
57+
5458
public all() {
5559
return this.params
5660
}

packages/core/src/router.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import debounce from './debounce'
2-
import { fireBeforeEvent, fireNavigateEvent, fireStartEvent } from './events'
2+
import { fireBeforeEvent, fireNavigateEvent } from './events'
33
import { hasFiles } from './files'
44
import { isFormData, objectToFormData } from './formData'
55
import { History } from './history'
@@ -169,9 +169,6 @@ export class Router {
169169
// Save scroll regions for the current page
170170
Scroll.save(currentPage.get())
171171

172-
fireStartEvent(visit)
173-
onStart(visit)
174-
175172
const request = Request.create(
176173
{
177174
...visit,

packages/core/tests/request.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,17 @@ test.each([
152152

153153
const abortSpy = vi.spyOn(AbortController.prototype, 'abort')
154154
const fireFinishEventsSpy = vi.spyOn(events, 'fireFinishEvent').mockReturnValue()
155+
const fireStartEventsSpy = vi.spyOn(events, 'fireStartEvent').mockReturnValue()
155156
const onCancel = vi.fn()
156157
const onFinish = vi.fn()
158+
const onStart = vi.fn()
157159

158160
axiosMock.mockResolvedValue(axiosResponse())
159161

160162
const requestParams = getRequestParams({
161163
onCancel,
162164
onFinish,
165+
onStart,
163166
})
164167

165168
const request = Request.create(requestParams, page.get())
@@ -180,6 +183,12 @@ test.each([
180183
expect(fireFinishEventsSpy).toHaveBeenCalledOnce()
181184
expect(fireFinishEventsSpy).toHaveBeenCalledWith(finalParams)
182185

186+
expect(fireStartEventsSpy).toHaveBeenCalledOnce()
187+
expect(fireStartEventsSpy).toHaveBeenCalledWith(finalParams)
188+
189+
expect(onStart).toHaveBeenCalledOnce()
190+
expect(onStart).toHaveBeenCalledWith(finalParams)
191+
183192
expect(onFinish).toHaveBeenCalledOnce()
184193
expect(onFinish).toHaveBeenCalledWith(finalParams)
185194

packages/core/tests/router.test.ts

-28
Original file line numberDiff line numberDiff line change
@@ -415,34 +415,6 @@ suite('visit', () => {
415415
expect(requestSpies.send).toHaveBeenCalledOnce()
416416
})
417417

418-
test('start event callbacks are fired', async () => {
419-
const requestSpies = {
420-
create: vi.spyOn(Request, 'create'),
421-
send: vi.spyOn(Request.prototype, 'send').mockResolvedValue(),
422-
}
423-
424-
const globalOnStart = vi.fn()
425-
const localOnStart = vi.fn()
426-
427-
const router = getRouter()
428-
429-
const stopListening = router.on('start', globalOnStart)
430-
431-
router.visit('/home', {
432-
onStart: localOnStart,
433-
})
434-
435-
await vi.runAllTimersAsync()
436-
437-
stopListening()
438-
439-
expect(requestSpies.create).toHaveBeenCalledOnce()
440-
expect(requestSpies.send).toHaveBeenCalledOnce()
441-
442-
expect(globalOnStart).toHaveBeenCalledOnce()
443-
expect(localOnStart).toHaveBeenCalledOnce()
444-
})
445-
446418
test('verify that request params are passed to the request', { todo: true }, async () => {
447419
const requestSpies = {
448420
create: vi.spyOn(Request, 'create'),

0 commit comments

Comments
 (0)