-
-
Notifications
You must be signed in to change notification settings - Fork 613
fix: implement proper ServiceWorkerSource singleton pattern
#2715
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
Merged
+432
−49
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1d304e3
test: add failing start-after-stop test
kettanaito 7f2c988
fix(service-worker-source): open and close `WorkerChannel` properly
kettanaito 2bb6b78
feat: implement proper `ServiceWorkerSource` singleton pattern
kettanaito 0d208fb
chore: add unit tests for `shouldInvalidateWorker`
kettanaito 088239b
fix(service-worker-source): abort `beforeunload` listener in `.termin…
kettanaito 3f3ebe5
fix(service-worker-source): clear listeners on `.disable()` too
kettanaito ae33207
fix: abort `frame` listeners on sources after disable
kettanaito 2bf6c99
fix: use `NetworkSource.prototype.disable` to prevent `frame` listene…
kettanaito 0041c12
Merge branch 'main' into fix/worker-enable-after-disable
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { type ServiceWorkerSourceOptions } from '../sources/service-worker-source' | ||
| import { shouldInvalidateWorker } from './should-invalidate-worker' | ||
|
|
||
| function createOptions( | ||
| overrides: Partial<ServiceWorkerSourceOptions> = {}, | ||
| ): ServiceWorkerSourceOptions { | ||
| return { | ||
| serviceWorker: { | ||
| url: '/mockServiceWorker.js', | ||
| options: { scope: '/' }, | ||
| }, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| it('returns true when the worker url differs', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ | ||
| serviceWorker: { url: '/a.js', options: { scope: '/' } }, | ||
| }), | ||
| createOptions({ | ||
| serviceWorker: { url: '/b.js', options: { scope: '/' } }, | ||
| }), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true when the registration options differ', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ | ||
| serviceWorker: { url: '/sw.js', options: { scope: '/' } }, | ||
| }), | ||
| createOptions({ | ||
| serviceWorker: { url: '/sw.js', options: { scope: '/app' } }, | ||
| }), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true when only one side has registration options', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ serviceWorker: { url: '/sw.js' } }), | ||
| createOptions({ | ||
| serviceWorker: { url: '/sw.js', options: { scope: '/' } }, | ||
| }), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true when findWorker differs by reference', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ findWorker: () => true }), | ||
| createOptions({ findWorker: () => true }), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true when findWorker is added on one side', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions(), | ||
| createOptions({ findWorker: () => true }), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false for the same options reference', () => { | ||
| const options = createOptions() | ||
| expect(shouldInvalidateWorker(options, options)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for deeply equal options', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ | ||
| serviceWorker: { url: '/sw.js', options: { scope: '/' } }, | ||
| }), | ||
| createOptions({ | ||
| serviceWorker: { url: '/sw.js', options: { scope: '/' } }, | ||
| }), | ||
| ), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for the same worker url without options', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ serviceWorker: { url: '/sw.js' } }), | ||
| createOptions({ serviceWorker: { url: '/sw.js' } }), | ||
| ), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false when findWorker is the same reference', () => { | ||
| const findWorker = () => true | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ findWorker }), | ||
| createOptions({ findWorker }), | ||
| ), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false regardless of the "quiet" option', () => { | ||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ quiet: true }), | ||
| createOptions({ quiet: true }), | ||
| ), | ||
| ).toBe(false) | ||
|
|
||
| expect( | ||
| shouldInvalidateWorker( | ||
| createOptions({ quiet: false }), | ||
| createOptions({ quiet: true }), | ||
| ), | ||
| ).toBe(false) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.