Skip to content

Commit 97bf7ee

Browse files
committed
fix(source): triggering onmessage event.
1 parent c66f8f1 commit 97bf7ee

File tree

9 files changed

+89
-14
lines changed

9 files changed

+89
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sveltekit-sse",
3-
"version": "0.14.2",
3+
"version": "0.14.3",
44
"license": "MIT",
55
"scripts": {
66
"dev": "vite dev",

src/lib/consume.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export function consume({
4848
const id = uuid({ short: true })
4949
const isLocal = localAbort
5050
localAbort = false
51+
52+
if (options.onclose) {
53+
options.onclose()
54+
}
55+
5156
for (const onClose of events.onClose) {
5257
onClose({
5358
id,
@@ -117,17 +122,22 @@ export function consume({
117122
openWhenHidden,
118123
method: 'POST',
119124
...rest,
120-
async onopen({
121-
headers: headersLocal,
122-
status: statusLocal,
123-
statusText: statusTextLocal,
124-
ok,
125-
}) {
125+
async onopen(response) {
126+
const {
127+
headers: headersLocal,
128+
status: statusLocal,
129+
statusText: statusTextLocal,
130+
ok,
131+
} = response
126132
status = statusLocal
127133
statusText = statusTextLocal
128134
headers = headersLocal
129135

130136
if (ok && headers.get('content-type') === 'text/event-stream') {
137+
if (options.onopen) {
138+
options.onopen(response)
139+
}
140+
131141
for (const onOpen of events.onOpen) {
132142
onOpen({
133143
id: '',
@@ -148,6 +158,10 @@ export function consume({
148158
}
149159
},
150160
onmessage({ id, event, data }) {
161+
if (options.onmessage) {
162+
options.onmessage({ id, event, data })
163+
}
164+
151165
for (const onMessage of events.onMessage) {
152166
onMessage({
153167
id,
@@ -163,6 +177,10 @@ export function consume({
163177
}
164178
},
165179
onclose() {
180+
if (options.onclose) {
181+
options.onclose()
182+
}
183+
166184
for (const onClose of events.onClose) {
167185
onClose({
168186
id: '',
@@ -178,6 +196,9 @@ export function consume({
178196
}
179197
},
180198
onerror(error) {
199+
if (options.onerror) {
200+
options.onerror(error)
201+
}
181202
for (const onError of events.onError) {
182203
onError({
183204
id: '',

src/lib/fetchEventSource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export function fetchEventSource(input, init = {}) {
205205

206206
try {
207207
const response = await fetch(input, init)
208-
await onopen(response)
208+
onopen(response)
209209
if (!response.body) {
210210
reject(new Error(`empty response from source ${input}.`))
211211
return

src/lib/playwright/playwright.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ import { IS_BROWSER } from '../constants'
3434
* @property {string} message2
3535
*/
3636

37+
/**
38+
* @typedef Issue70
39+
* @property {number} status
40+
* @property {import('$lib/types.external').FetchEventSourceMessage[]} onmessage
41+
*/
42+
3743
/**
3844
* @typedef PlaywrightState
3945
* @property {number} counter
@@ -43,6 +49,7 @@ import { IS_BROWSER } from '../constants'
4349
* @property {Issue65} issue65
4450
* @property {Issue68} issue68
4551
* @property {PullRequest69} pullRequest69
52+
* @property {Issue70} issue70
4653
*/
4754

4855
/** @type {PlaywrightState} */
@@ -70,6 +77,10 @@ let state = {
7077
message1: '',
7178
message2: '',
7279
},
80+
issue70: {
81+
onmessage: [],
82+
status: -1,
83+
},
7384
}
7485

7586
if (IS_BROWSER) {

src/lib/source.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ function connectable({ resource, options, onClose, onError, onOpen }) {
2424
const consumedStream = consume({
2525
resource,
2626
options,
27-
onClose(e) {
28-
if (onClose) {
29-
onClose(e)
30-
}
31-
},
27+
onClose,
3228
onOpen,
3329
onError,
3430
onMessage(e) {

src/lib/types.external.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export {}
203203
* @property {string} [method]
204204
* @property {any} [body]
205205
* @property {Record<string,string>} [headers]
206-
* @property {function(Response):Promise<void>} [onopen]
206+
* @property {function(Response):void} [onopen]
207207
* @property {function(FetchEventSourceMessage):void} [onmessage]
208208
* @property {function():void} [onclose]
209209
* @property {function(Error):void} [onerror]

src/routes/issue-70/+page.svelte

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- +page.svelte -->
2+
<script>
3+
import { source } from '$lib'
4+
import { playwright } from '$lib/playwright/playwright'
5+
6+
const value = source(`/issue-70/events`, {
7+
options: {
8+
onmessage(e) {
9+
console.log({ onmessage: e })
10+
playwright.state.issue70.onmessage.push(e)
11+
},
12+
onopen(e) {
13+
console.log({ onopen: e })
14+
playwright.state.issue70.status = e.status
15+
},
16+
},
17+
}).select('message')
18+
</script>
19+
20+
<span>Value:{$value}</span>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { produce } from '$lib'
2+
import { delay } from '$lib/delay.js'
3+
4+
export async function POST() {
5+
return produce(async function start({ emit }) {
6+
while (true) {
7+
const { error } = emit('message', `hello ${Date.now()}`)
8+
if (error) {
9+
return
10+
}
11+
await delay(1000)
12+
}
13+
})
14+
}

tests/main.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,16 @@ test('Making sure connection gets cached using non Latin1 characters.', async fu
7474
expect(pullRequest69.message1).toBe(pullRequest69.message2)
7575
expect(pullRequest69.message1).toContain('hello 猫')
7676
})
77+
78+
test('Making sure onmessage event is triggered on the source object.', async function run({
79+
page,
80+
}) {
81+
// Testing changes made in response to issue 70 https://github.com/razshare/sveltekit-sse/issues/70
82+
await page.goto('/issue-70')
83+
await delay(1000)
84+
const { issue70 } = await getPlaywrightState({ page })
85+
expect(issue70.status).toBe(200)
86+
expect(issue70.onmessage.length).toBeGreaterThan(0)
87+
expect(issue70.onmessage[0].event).toBe('message')
88+
expect(issue70.onmessage[0].id).toBe('1')
89+
})

0 commit comments

Comments
 (0)