generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,45 @@ | ||
// eslint-disable-next-line camelcase | ||
declare let __webpack_public_path__: string; | ||
|
||
// @ts-expect-error ts does not find types in @okikio/sharedworker/@types/index.d.ts | ||
import {SharedWorkerPolyfill} from '@okikio/sharedworker'; | ||
|
||
class WebWorker extends Worker { | ||
constructor(url: string | URL, options?: WorkerOptions) { | ||
const objectURL = generateWorkerLoader(url); | ||
super(objectURL, options); | ||
URL.revokeObjectURL(objectURL); | ||
} | ||
} | ||
|
||
class SharedWebWorker extends SharedWorkerPolyfill { | ||
constructor(url: string | URL, options?: string | WorkerOptions) { | ||
const objectURL = generateWorkerLoader(url); | ||
super(objectURL, options); | ||
URL.revokeObjectURL(objectURL); | ||
} | ||
} | ||
|
||
export {WebWorker as Worker, SharedWebWorker as SharedWorker}; | ||
|
||
function generateWorkerLoader(url: string | URL) { | ||
// eslint-disable-next-line camelcase | ||
const publicPath = __webpack_public_path__; | ||
const workerPublicPath = publicPath.match(/^https?:\/\//) | ||
? publicPath | ||
: new URL(publicPath, window.location.href).toString(); | ||
const objectURL = URL.createObjectURL( | ||
new Blob( | ||
[ | ||
[ | ||
`self.__PUBLIC_PATH__ = ${JSON.stringify(workerPublicPath)}`, | ||
`importScripts(${JSON.stringify(url.toString())});`, | ||
].join('\n'), | ||
], | ||
{ | ||
type: 'application/javascript', | ||
}, | ||
), | ||
); | ||
return objectURL; | ||
} |
This file contains 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