UNPKG

4.99 kBPlain TextView Raw
1import {bindAll} from '../util/util';
2
3import type Dispatcher from '../util/dispatcher';
4import type {Event, Evented} from '../util/evented';
5import type Map from '../ui/map';
6import type Tile from './tile';
7import type {OverscaledTileID} from './tile_id';
8import type {Callback} from '../types/callback';
9import {CanonicalTileID} from './tile_id';
10
11/**
12 * The `Source` interface must be implemented by each source type, including "core" types (`vector`, `raster`,
13 * `video`, etc.) and all custom, third-party types.
14 *
15 * @private
16 *
17 * @param {string} id The id for the source. Must not be used by any existing source.
18 * @param {Object} options Source options, specific to the source type (except for `options.type`, which is always
19 * required).
20 * @param {string} options.type The source type, matching the value of `name` used in {@link Style#addSourceType}.
21 * @param {Dispatcher} dispatcher A {@link Dispatcher} instance, which can be used to send messages to the workers.
22 *
23 * @fires data with `{dataType: 'source', sourceDataType: 'metadata'}` to indicate that any necessary metadata
24 * has been loaded so that it's okay to call `loadTile`; and with `{dataType: 'source', sourceDataType: 'content'}`
25 * to indicate that the source data has changed, so that any current caches should be flushed.
26 * @property {string} id The id for the source. Must match the id passed to the constructor.
27 * @property {number} minzoom
28 * @property {number} maxzoom
29 * @property {boolean} isTileClipped `false` if tiles can be drawn outside their boundaries, `true` if they cannot.
30 * @property {boolean} reparseOverscaled `true` if tiles should be sent back to the worker for each overzoomed zoom
31 * level, `false` if not.
32 * @property {boolean} roundZoom `true` if zoom levels are rounded to the nearest integer in the source data, `false`
33 * if they are floor-ed to the nearest integer.
34 */
35export interface Source {
36 readonly type: string;
37 id: string;
38 minzoom: number;
39 maxzoom: number;
40 tileSize: number;
41 attribution?: string;
42 roundZoom?: boolean;
43 isTileClipped?: boolean;
44 tileID?: CanonicalTileID;
45 reparseOverscaled?: boolean;
46 vectorLayerIds?: Array<string>;
47 hasTransition(): boolean;
48 loaded(): boolean;
49 fire(event: Event): unknown;
50 readonly onAdd?: (map: Map) => void;
51 readonly onRemove?: (map: Map) => void;
52 loadTile(tile: Tile, callback: Callback<void>): void;
53 readonly hasTile?: (tileID: OverscaledTileID) => boolean;
54 readonly abortTile?: (tile: Tile, callback: Callback<void>) => void;
55 readonly unloadTile?: (tile: Tile, callback: Callback<void>) => void;
56 /**
57 * @returns A plain (stringifiable) JS object representing the current state of the source.
58 * Creating a source using the returned object as the `options` should result in a Source that is
59 * equivalent to this one.
60 * @private
61 */
62 serialize(): any;
63 readonly prepare?: () => void;
64}
65
66type SourceStatics = {
67 /*
68 * An optional URL to a script which, when run by a Worker, registers a {@link WorkerSource}
69 * implementation for this Source type by calling `self.registerWorkerSource(workerSource: WorkerSource)`.
70 */
71 workerSourceURL?: URL;
72};
73
74export type SourceClass = {
75 new (...args: any): Source;
76} & SourceStatics;
77
78import vector from '../source/vector_tile_source';
79import raster from '../source/raster_tile_source';
80import rasterDem from '../source/raster_dem_tile_source';
81import geojson from '../source/geojson_source';
82import video from '../source/video_source';
83import image from '../source/image_source';
84import canvas from '../source/canvas_source';
85
86import type {SourceSpecification} from '../style-spec/types.g';
87
88const sourceTypes = {
89 vector,
90 raster,
91 'raster-dem': rasterDem,
92 geojson,
93 video,
94 image,
95 canvas
96};
97
98/*
99 * Creates a tiled data source instance given an options object.
100 *
101 * @param id
102 * @param {Object} source A source definition object compliant with
103 * [`maplibre-gl-style-spec`](https://maplibre.org/maplibre-gl-js-docs/style-spec/#sources) or, for a third-party source type,
104 * with that type's requirements.
105 * @param {Dispatcher} dispatcher
106 * @returns {Source}
107 */
108export const create = function(id: string, specification: SourceSpecification, dispatcher: Dispatcher, eventedParent: Evented) {
109 const source = new sourceTypes[specification.type](id, (specification as any), dispatcher, eventedParent);
110
111 if (source.id !== id) {
112 throw new Error(`Expected Source id to be ${id} instead of ${source.id}`);
113 }
114
115 bindAll(['load', 'abort', 'unload', 'serialize', 'prepare'], source);
116 return source;
117};
118
119export const getSourceType = function (name: string) {
120 return sourceTypes[name];
121};
122
123export const setSourceType = function (name: string, type: {
124 new (...args: any): Source;
125}) {
126 sourceTypes[name] = type;
127};
128
129export interface Actor {
130 send(type: string, data: any, callback: Callback<any>): void;
131}