Skip to content

Makes FetchEventSource compatible with webworker #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ name: Node.js CI

on:
push:
branches: [ main ]
branches: [ main, web_worker_compat ]
pull_request:
branches: [ main ]
branches: [ main, web_worker_compat ]

jobs:
build:

runs-on: ubuntu-latest
runs-on: self-hosted

strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [12.x, 14.x, 15.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
21 changes: 21 additions & 0 deletions helpers/abortcontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

class AbortController {
constructor() {
Object.defineProperty(this, 'signal', { value: {aborted: false}, writable: true, configurable: true });
}
}

Object.defineProperty(global, 'AbortController', {
writable: true,
enumerable: false,
configurable: true,
value: AbortController,
});

Object.defineProperty(global, 'self', {
writable: true,
enumerable: false,
configurable: true,
value: global,
});
3 changes: 3 additions & 0 deletions jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"spec_dir": "lib",
"spec_files": [
"**/*.spec.js"
],
"helpers": [
"../helpers/**/*.js"
]
}
29 changes: 29 additions & 0 deletions src/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as fetch from "./fetch";

describe("fetch", () => {
describe("fetchEventSource", () => {
it("cannot create event source since there is no document", () => {
fetch.fetchEventSource("http://localhost:3000", {}).catch((error) => {
expect(error).toEqual(new ReferenceError("document is not defined"));
});
});

it("cannot create event source since there is no window", () => {
fetch
.fetchEventSource("http://localhost:3000", { openWhenHidden: true })
.catch((error) => {
expect(error).toEqual(new ReferenceError("window is not defined"));
});
});

it("can create event source", () => {
const fetchStub = jasmine.createSpy();

const promise = fetch.fetchEventSource("http://localhost:3000", {
openWhenHidden: true,
fetch: fetchStub,
});
expect(promise).toBeDefined();
});
});
});
10 changes: 6 additions & 4 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ export function fetchEventSource(input: RequestInfo, {
let retryInterval = DefaultRetryInterval;
let retryTimer = 0;
function dispose() {
document.removeEventListener('visibilitychange', onVisibilityChange);
window.clearTimeout(retryTimer);
if (!openWhenHidden) {
document.removeEventListener('visibilitychange', onVisibilityChange);
}
self.clearTimeout(retryTimer);
curRequestController.abort();
}

Expand Down Expand Up @@ -131,8 +133,8 @@ export function fetchEventSource(input: RequestInfo, {
try {
// check if we need to retry:
const interval: any = onerror?.(err) ?? retryInterval;
window.clearTimeout(retryTimer);
retryTimer = window.setTimeout(create, interval);
self.clearTimeout(retryTimer);
retryTimer = self.setTimeout(create, interval);
} catch (innerErr) {
// we should not retry anymore:
dispose();
Expand Down