Skip to content

Commit

Permalink
Introduce Callback template
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 28, 2017
1 parent b828503 commit 2fa14be
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
17 changes: 17 additions & 0 deletions flow-interfaces/callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// Flow can't perfectly type Node-style callbacks yet; it does not have a way to
// express that if the first parameter is null, the second is not, so for the time
// being, both parameters must be optional. Use the following convention when defining
// a callback:
//
// asyncFunction((error, result) => {
// if (error) {
// // handle error
// } else if (result) {
// // handle success
// }
// });
//
// See https://github.com/facebook/flow/issues/2123 for more.

declare type Callback<T> = (error: ?Error, result: ?T) => void;
7 changes: 3 additions & 4 deletions src/source/geojson_worker_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export type LoadGeoJSONParameters = {
geojsonVtOptions?: Object
};

export type LoadGeoJSONCallback = (error: ?Error, result: ?GeoJSON) => void;
export type LoadGeoJSON = (params: LoadGeoJSONParameters, callback: LoadGeoJSONCallback) => void;
export type LoadGeoJSON = (params: LoadGeoJSONParameters, callback: Callback<GeoJSON>) => void;

export interface GeoJSONIndex {
}
Expand Down Expand Up @@ -102,7 +101,7 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
* @param params.source The id of the source.
* @param callback
*/
loadData(params: LoadGeoJSONParameters, callback: (error: ?Error) => void) {
loadData(params: LoadGeoJSONParameters, callback: Callback<void>) {
this.loadGeoJSON(params, (err, data) => {
if (err || !data) {
return callback(err);
Expand Down Expand Up @@ -157,7 +156,7 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
* @param [params.url] A URL to the remote GeoJSON data.
* @param [params.data] Literal GeoJSON data. Must be provided if `params.url` is not.
*/
loadGeoJSON(params: LoadGeoJSONParameters, callback: LoadGeoJSONCallback) {
loadGeoJSON(params: LoadGeoJSONParameters, callback: Callback<GeoJSON>) {
// Because of same origin issues, urls must either include an explicit
// origin or absolute path.
// ie: /foo/bar.json or http://example.com/bar.json
Expand Down
2 changes: 1 addition & 1 deletion src/source/vector_tile_worker_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {StyleLayerIndex} from '../style/style_layer_index';
* @param vectorTile
* @private
*/
export type LoadVectorDataCallback = (error: ?Error, result: ?AugmentedVectorTile) => void;
export type LoadVectorDataCallback = Callback<?AugmentedVectorTile>;

export type AbortVectorData = () => void;
export type LoadVectorData = (params: WorkerTileParameters, callback: LoadVectorDataCallback) => ?AbortVectorData;
Expand Down
4 changes: 2 additions & 2 deletions src/source/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Worker {
* function taking `(name, workerSourceObject)`.
* @private
*/
loadWorkerSource(map: string, params: {url: string}, callback: Function) {
loadWorkerSource(map: string, params: {url: string}, callback: Callback<void>) {
try {
this.self.importScripts(params.url);
callback();
Expand All @@ -117,7 +117,7 @@ class Worker {
}
}

loadRTLTextPlugin(map: string, pluginURL: string, callback: Function) {
loadRTLTextPlugin(map: string, pluginURL: string, callback: Callback<void>) {
try {
if (!globalRTLTextPlugin.applyArabicShaping && !globalRTLTextPlugin.processBidirectionalText) {
this.self.importScripts(pluginURL);
Expand Down

0 comments on commit 2fa14be

Please sign in to comment.