-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loader-utils: Move Worker utils from core (#842)
- Loading branch information
Showing
35 changed files
with
328 additions
and
141 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
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
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 was deleted.
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
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
1 change: 1 addition & 0 deletions
1
...re/src/javascript-utils/binary-utils.d.ts → ...ls/src/lib/binary-utils/binary-utils.d.ts
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,2 +1,3 @@ | ||
// TODO better data type | ||
export function toArrayBuffer(data: any): ArrayBuffer; | ||
export function toBuffer(data: any): Buffer; |
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
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...les/core/src/node/utils/to-buffer.node.js → ...ader-utils/src/lib/node/to-buffer.node.js
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
6 changes: 6 additions & 0 deletions
6
modules/loader-utils/src/lib/worker-loader-utils/create-worker.d.ts
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,6 @@ | ||
|
||
/** | ||
* Set up a WebWorkerGlobalScope to talk with the main thread | ||
* @param loader | ||
*/ | ||
export default function createWorker(loader: any); |
130 changes: 130 additions & 0 deletions
130
modules/loader-utils/src/lib/worker-loader-utils/create-worker.js
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,130 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
/* global TextDecoder, self */ | ||
|
||
import {getTransferList} from '../worker-utils/get-transfer-list'; | ||
import {validateLoaderVersion} from './validate-loader-version'; | ||
|
||
export default function createWorker(loader) { | ||
// TODO - explain when this happens? Just a sanity check? Throw an error or log a warning? | ||
if (typeof self === 'undefined') { | ||
return; | ||
} | ||
|
||
let requestId = 0; | ||
const parse = (arraybuffer, options = {}, url) => | ||
new Promise((resolve, reject) => { | ||
const id = requestId++; | ||
|
||
const onMessage = ({data}) => { | ||
if (!data || data.id !== id) { | ||
// not ours | ||
return; | ||
} | ||
switch (data.type) { | ||
case 'parse-done': | ||
self.removeEventListener('message', onMessage); | ||
resolve(data.result); | ||
break; | ||
|
||
case 'parse-error': | ||
self.removeEventListener('message', onMessage); | ||
reject(data.message); | ||
break; | ||
|
||
default: | ||
// ignore | ||
} | ||
}; | ||
self.addEventListener('message', onMessage); | ||
// Ask the main thread to decode data | ||
// @ts-ignore self is WorkerGlobalScope | ||
self.postMessage({type: 'parse', id, arraybuffer, options, url}, [arraybuffer]); | ||
}); | ||
|
||
self.onmessage = async evt => { | ||
const {data} = evt; | ||
|
||
try { | ||
if (!isKnownMessage(data, loader.name)) { | ||
return; | ||
} | ||
|
||
validateLoaderVersion(loader, data.source.split('@')[1]); | ||
|
||
const {arraybuffer, byteOffset = 0, byteLength = 0, options = {}} = data; | ||
|
||
const result = await parseData({ | ||
loader, | ||
arraybuffer, | ||
byteOffset, | ||
byteLength, | ||
options, | ||
context: {parse} | ||
}); | ||
const transferList = getTransferList(result); | ||
// @ts-ignore self is WorkerGlobalScope | ||
self.postMessage({type: 'done', result}, transferList); | ||
} catch (error) { | ||
// @ts-ignore self is WorkerGlobalScope | ||
self.postMessage({type: 'error', message: error.message}); | ||
} | ||
}; | ||
} | ||
|
||
// TODO - Support byteOffset and byteLength (enabling parsing of embedded binaries without copies) | ||
// TODO - Why not support async loader.parse* funcs here? | ||
// TODO - Why not reuse a common function instead of reimplementing loader.parse* selection logic? Keeping loader small? | ||
// TODO - Lack of appropriate parser functions can be detected when we create worker, no need to wait until parse | ||
async function parseData({loader, arraybuffer, byteOffset, byteLength, options, context}) { | ||
let data; | ||
let parser; | ||
if (loader.parseSync || loader.parse) { | ||
data = arraybuffer; | ||
parser = loader.parseSync || loader.parse; | ||
} else if (loader.parseTextSync) { | ||
const textDecoder = new TextDecoder(); | ||
data = textDecoder.decode(arraybuffer); | ||
parser = loader.parseTextSync; | ||
} else { | ||
throw new Error(`Could not load data with ${loader.name} loader`); | ||
} | ||
|
||
// TODO - proper merge in of loader options... | ||
options = { | ||
...options, | ||
modules: (loader && loader.options && loader.options.modules) || {}, | ||
worker: false | ||
}; | ||
|
||
return await parser(data, {...options}, context, loader); | ||
} | ||
|
||
// Filter out noise messages sent to workers | ||
function isKnownMessage(data, name) { | ||
return data && data.type === 'parse' && data.source && data.source.startsWith('loaders.gl'); | ||
} | ||
|
||
/* | ||
function checkMessage(evt, name) { | ||
switch (evt.data && evt.data.source) { | ||
// Ignore known noise event from react-dev-tools bridge, webpack build progress etc... | ||
case 'react-devtools-bridge': | ||
case 'react-devtools-content-script': | ||
case 'react-devtools-detector': | ||
return false; | ||
default: | ||
// fall through | ||
} | ||
switch (evt.data && evt.data.type) { | ||
case 'webpackProgress': | ||
case 'webpackOk': | ||
return false; | ||
default: | ||
// Enable to debug messages | ||
// const message = `${name.toLowerCase()}-worker: ignoring unknown message`; | ||
// console.log(message, evt.data, evt); // eslint-disable-line | ||
return false; | ||
} | ||
} | ||
*/ |
7 changes: 7 additions & 0 deletions
7
modules/loader-utils/src/lib/worker-loader-utils/validate-loader-version.d.ts
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,7 @@ | ||
/** | ||
* | ||
* @param loader | ||
* @param coreVersion | ||
* @returns `true` if the two versions are compatible | ||
*/ | ||
export function validateLoaderVersion(loader, coreVersion?: string): boolean; |
Oops, something went wrong.