-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
images: worker-loader (for extracting image data in parallel)
- Loading branch information
Showing
18 changed files
with
320 additions
and
57 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 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
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,112 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
/* global TextDecoder, self */ | ||
|
||
import {getTransferList, validateLoaderVersion} from '@loaders.gl/loader-utils/'; | ||
|
||
export default function createWorker(workerDescriptor) { | ||
// 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 process = (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 'process-done': | ||
self.removeEventListener('message', onMessage); | ||
resolve(data.result); | ||
break; | ||
|
||
case 'process-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: 'process', id, arraybuffer, options, url}, [arraybuffer]); | ||
}); | ||
|
||
self.onmessage = async evt => { | ||
const {data} = evt; | ||
|
||
try { | ||
if (!isKnownMessage(data, workerDescriptor.name)) { | ||
return; | ||
} | ||
|
||
validateLoaderVersion(workerDescriptor, data.source.split('@')[1]); | ||
|
||
const {arraybuffer, byteOffset = 0, byteLength = 0, options = {}} = data; | ||
|
||
const result = await parseData({ | ||
workerDescriptor, | ||
arraybuffer, | ||
byteOffset, | ||
byteLength, | ||
options, | ||
context: {process} | ||
}); | ||
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 workerDescriptor.process* funcs here? | ||
// TODO - Why not reuse a common function instead of reimplementing workerDescriptor.process* selection logic? Keeping workerDescriptor small? | ||
// TODO - Lack of appropriate parser functions can be detected when we create worker, no need to wait until process | ||
async function parseData({ | ||
workerDescriptor, | ||
arraybuffer, | ||
byteOffset, | ||
byteLength, | ||
options, | ||
context | ||
}) { | ||
let data; | ||
let parser; | ||
if (workerDescriptor.parseSync || workerDescriptor.process) { | ||
data = arraybuffer; | ||
parser = workerDescriptor.parseSync || workerDescriptor.process; | ||
} else if (workerDescriptor.parseTextSync) { | ||
const textDecoder = new TextDecoder(); | ||
data = textDecoder.decode(arraybuffer); | ||
parser = workerDescriptor.parseTextSync; | ||
} else { | ||
throw new Error(`Could not load data with ${workerDescriptor.name} workerDescriptor`); | ||
} | ||
|
||
// TODO - proper merge in of workerDescriptor options... | ||
options = { | ||
...options, | ||
modules: | ||
(workerDescriptor && workerDescriptor.options && workerDescriptor.options.modules) || {}, | ||
worker: false | ||
}; | ||
|
||
return await parser(data, {...options}, context, workerDescriptor); | ||
} | ||
|
||
// Filter out noise messages sent to workers | ||
function isKnownMessage(data, name) { | ||
return data && data.type === 'process' && data.source && data.source.startsWith('loaders.gl'); | ||
} |
Oops, something went wrong.