-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(robot): fallback in case shared worker not supported (#963)
* fix(backend): fallback in case shared worker not supproted * refact(workers); tiny refactoring for callback * refact(workers); reuse expose method for fallback BL * refact(workers): simplify fallback
- Loading branch information
Showing
11 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { wrap } from 'comlink'; | ||
import { WorkerUrl } from 'worker-url'; | ||
import { BackendWorkerApi } from './worker'; | ||
import { createWorker } from '../workerUtils'; | ||
|
||
const worker = new SharedWorker(new URL('./worker.ts', import.meta.url), { | ||
name: 'cyb~backend', | ||
}); | ||
const workerUrl = new WorkerUrl(new URL('./worker.ts', import.meta.url)); | ||
|
||
export const backendApi = wrap<BackendWorkerApi>(worker.port); | ||
export const { apiProxy: backendApi } = createWorker<BackendWorkerApi>( | ||
workerUrl, | ||
'cyb~backend' | ||
); | ||
|
||
// export const backendApi; |
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
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,28 @@ | ||
import { wrap, Remote, expose } from 'comlink'; | ||
|
||
type WorkerType = SharedWorker | Worker; | ||
|
||
// Create Shared Worker with fallback to usual Worker(in case of DEV too) | ||
// eslint-disable-next-line import/prefer-default-export | ||
export function createWorker<T>( | ||
workerUrl: URL, | ||
workerName: string | ||
): { worker: WorkerType; apiProxy: Remote<T> } { | ||
const isSharedWorkersSupported = typeof SharedWorker !== 'undefined'; | ||
|
||
if (isSharedWorkersSupported && !process.env.IS_DEV) { | ||
const worker = new SharedWorker(workerUrl, { name: workerName }); | ||
return { worker, apiProxy: wrap<T>(worker.port) }; | ||
} | ||
|
||
const worker = new Worker(workerUrl); | ||
return { worker, apiProxy: wrap<T>(worker) }; | ||
} | ||
|
||
export function exposeWorker<T>(worker: WorkerType, api: T) { | ||
if (typeof worker.onconnect !== 'undefined') { | ||
worker.onconnect = (e) => expose(api, e.ports[0]); | ||
} else { | ||
expose(api); | ||
} | ||
} |
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
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,38 @@ | ||
class BundleInfoPlugin { | ||
constructor(options) { | ||
// you can define options if needed | ||
this.options = options || {}; | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.emit.tapAsync( | ||
'BundleInfoPlugin', | ||
(compilation, callback) => { | ||
let result = {}; | ||
|
||
for (let filename in compilation.assets) { | ||
let size = compilation.assets[filename].size(); | ||
result[filename] = size; | ||
} | ||
|
||
// Convert the result object to JSON format | ||
let jsonResult = JSON.stringify(result, null, 2); | ||
|
||
// Add the JSON to the compilation's assets for output | ||
compilation.assets['bundle_info.json'] = { | ||
source: function () { | ||
return jsonResult; | ||
}, | ||
size: function () { | ||
return jsonResult.length; | ||
}, | ||
}; | ||
|
||
// Continue with the build process | ||
callback(); | ||
} | ||
); | ||
} | ||
} | ||
|
||
module.exports = BundleInfoPlugin; |
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