The selectLoader()
and selectLoaderSync()
functions will automatically select
an appropriate loader for a specific resource. selectLoader()
is called by the
parse()
and load()
functions, but can also be called directly from applications.
Loader selection heuristics are based on:
Response
content-type
headers or Blob.type
/File.type
fields)loader registry - selectLoader()
and selectLoaderSync()
are also aware of the
loader registry.
The list of pre-registered loaders will be included in the search for a compatible loader,
unless options.ignoreRegisteredLoaders
is true
.
Select a loader from a list of provided loaders:
import {selectLoaderSync} from '@loaders.gl/core';
import {ArrowLoader} from '@loaders.gl/arrow';
import {CSVLoader} from '@loaders.gl/csv';
selectLoaderSync('filename.csv', [ArrowLoader, CSVLoader]); // => CSVLoader
Select a loader from pre-registered loaders in the loader registry:
import {registerLoaders, selectLoader} from '@loaders.gl/core';
import {ArrowLoader} from '@loaders.gl/arrow';
import {CSVLoader} from '@loaders.gl/csv';
registerLoaders(ArrowLoader, CSVLoader);
await selectLoader('filename.csv'); // => CSVLoader
Select a loader by specifying MIME type (using unregistered MIME types, see below)
const data = new Blob([string], {type: 'application/x.csv'});
await selectLoader(blob); // => CSVLoader
The async selectLoader
function can identify loaders without extension and mimeType
by content sniffing Blob
and File
objects (useful when user drags and drops files into your application).
const data = new Blob(['DRACO...'] /* Binary Draco files start with these characters */]);
await selectLoader(blob, DracoLoader); // => DracoLoader
selectLoader(data: Response | ArrayBuffer | String | Blob, ..., loaders?: LoaderWithParser[], options?: object, context?: object): Promise<boolean>
Selects an appropriate loader for a file from a list of candidate loaders by examining the data
parameter, looking at URL extension, mimeType ('Content-Type') and/or an initial data chunk.
Parameters:
data
- data to perform autodetection againstloaders
- can be a single loader or an array of loaders, or null.options
- See LoaderOptions
.options.nothrow
=false
- Return null instead of throwing exception if no loader can be foundReturns:
null
if options.nothrow
was set and no matching loader was found).Throws:
options.nothrow
was not set.Regarding the loaders
parameter:
null
loader list will use the pre-registered list of loaders.selectLoaderSync(data: Response | ArrayBuffer | String | Blob, ..., loaders?: LoaderWithParser[], options?: object, context?: object): boolean
Response
objects: url
and headers.get('Content-Type')
fields will be used.File
and Blob
objects:Peeking into batched input sources is not supported directly by selectLoader
:
Response
: Avoids requesting initial data to make sure the response body is not marked as used.Stream
: It is not possible to non-destructively peek into a stream.Iterator/AsyncIterator
: it is not possible to peek into an iterator.Instead use helpers to get access to initialContents and pass it in separately.
If the standard MIME types for each format are not precise enough, loaders.gl also supports unregistered MIME types. Each loader will match the application/x.<id>
where the <id>
is the documented id
of the loader, e.g. application/x.ply
/application/x.draco
/etc ...