Skip to content

Commit 0547398

Browse files
committed
Add flow types, update package structure.
Adds index.js.flow and index.d.ts to package. Fixes #45
1 parent f9dbc11 commit 0547398

File tree

6 files changed

+136
-111
lines changed

6 files changed

+136
-111
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.*/dist/.*
44
.*/coverage/.*
55
.*/resources/.*
6+
.*/node_modules/y18n/test/.*
67

78
[include]
89

dataloader.d.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"type": "git",
1717
"url": "http://github.com/facebook/dataloader.git"
1818
},
19-
"main": "dist/index.js",
19+
"main": "index.js",
2020
"options": {
2121
"mocha": "--require resources/mocha-bootload src/**/__tests__/**/*.js"
2222
},
@@ -25,16 +25,17 @@
2525
"testonly": "mocha $npm_package_options_mocha",
2626
"lint": "eslint src",
2727
"check": "flow check",
28-
"build": "babel src --ignore __tests__ --out-dir dist/",
28+
"build": "babel src --ignore __tests__ --out-dir dist/ ; cp src/index.js dist/index.js.flow ; cp src/index.d.ts dist/",
2929
"watch": "babel --optional runtime resources/watch.js | node",
3030
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
3131
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
3232
"preversion": ". ./resources/checkgit.sh && npm test",
3333
"prepublish": ". ./resources/prepublish.sh"
3434
},
3535
"files": [
36-
"dist",
37-
"dataloader.d.ts",
36+
"index.js",
37+
"index.js.flow",
38+
"index.d.ts",
3839
"README.md",
3940
"LICENSE",
4041
"PATENTS"
@@ -48,10 +49,10 @@
4849
"coveralls": "2.11.6",
4950
"eslint": "1.10.3",
5051
"eslint-plugin-babel": "2.2.0",
51-
"flow-bin": "0.20.1",
52+
"flow-bin": "0.32.0",
5253
"isparta": "3.0.3",
5354
"mocha": "2.3.4",
5455
"sane": "1.3.0"
5556
},
56-
"typings": "dataloader.d.ts"
57+
"typings": "index.d.ts"
5758
}

resources/prepublish.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ fi;
1818

1919
# Build before publishing
2020
npm run build;
21+
22+
# When Travis CI publishes to NPM, the published files are available in the root
23+
# directory, which produces a cleaner distribution.
24+
#
25+
cp dist/* .
26+
27+
# Ensure a vanilla package.json before deploying so other tools do not interpret
28+
# The built output as requiring any further transformation.
29+
node -e "var package = require('./package.json'); \
30+
delete package.scripts; \
31+
delete package.options; \
32+
delete package.devDependencies; \
33+
require('fs').writeFileSync('package.json', JSON.stringify(package));"

src/index.d.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
11+
type CacheMap<K, V> = {
12+
get(key: K): V | void;
13+
set(key: K, value: V): any;
14+
delete(key: K): any;
15+
clear(): any;
16+
}
17+
18+
// A Function, which when given an Array of keys, returns a Promise of an Array
19+
// of values or Errors.
20+
type BatchLoadFn<K, V> = (keys: K[]) => Promise<Array<V | Error>>;
21+
22+
// Optionally turn off batching or caching or provide a cache key function or a
23+
// custom cache instance.
24+
type Options<K, V> = {
25+
26+
/**
27+
* Default `true`. Set to `false` to disable batching,
28+
* instead immediately invoking `batchLoadFn` with a
29+
* single load key.
30+
*/
31+
batch?: boolean,
32+
33+
/**
34+
* Default `true`. Set to `false` to disable caching,
35+
* instead creating a new Promise and new key in
36+
* the `batchLoadFn` for every load.
37+
*/
38+
cache?: boolean,
39+
40+
/**
41+
* A function to produce a cache key for a given load key.
42+
* Defaults to `key => key`. Useful to provide when JavaScript
43+
* objects are keys and two similarly shaped objects should
44+
* be considered equivalent.
45+
*/
46+
cacheKeyFn?: (key: any) => any,
47+
48+
/**
49+
* An instance of Map (or an object with a similar API) to
50+
* be used as the underlying cache for this loader.
51+
* Default `new Map()`.
52+
*/
53+
cacheMap?: CacheMap<K, Promise<V>>;
54+
}
55+
56+
/**
57+
* DataLoader creates a public API for loading data from a particular
58+
* data back-end with unique keys such as the id column of a SQL table
59+
* or document name in a MongoDB database, given a batch loading function.
60+
*
61+
* Each DataLoader instance contains a unique memoized cache. Use caution
62+
* when used in long-lived applications or those which serve many users
63+
* with different access permissions and consider creating a new instance
64+
* per web request.
65+
*/
66+
export class DataLoader<K, V> {
67+
68+
constructor(batchLoadFn: BatchLoadFn<K, V>, options?: Options<K, V>);
69+
70+
/**
71+
* Loads a key, returning a `Promise` for the value represented by that key.
72+
*/
73+
load(key: K): Promise<V>;
74+
75+
/**
76+
* Loads multiple keys, promising an array of values:
77+
*
78+
* var [ a, b ] = await myLoader.loadMany([ 'a', 'b' ]);
79+
*
80+
* This is equivalent to the more verbose:
81+
*
82+
* var [ a, b ] = await Promise.all([
83+
* myLoader.load('a'),
84+
* myLoader.load('b')
85+
* ]);
86+
*
87+
*/
88+
loadMany(keys: K[]): Promise<V[]>;
89+
90+
/**
91+
* Clears the value at `key` from the cache, if it exists. Returns itself for
92+
* method chaining.
93+
*/
94+
clear(key: K): DataLoader<K, V>;
95+
96+
/**
97+
* Clears the entire cache. To be used when some event results in unknown
98+
* invalidations across this particular `DataLoader`. Returns itself for
99+
* method chaining.
100+
*/
101+
clearAll(): DataLoader<K, V>;
102+
103+
/**
104+
* Adds the provied key and value to the cache. If the key already exists, no
105+
* change is made. Returns itself for method chaining.
106+
*/
107+
prime(key: K, value: V): DataLoader<K, V>;
108+
}

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export default class DataLoader<K, V> {
5252
}
5353
this._batchLoadFn = batchLoadFn;
5454
this._options = options;
55-
this._promiseCache = options && options.cacheMap || new Map();
55+
this._promiseCache =
56+
options && options.cacheMap || (new Map(): Map<K,Promise<V>>);
5657
this._queue = [];
5758
}
5859

@@ -69,7 +70,7 @@ export default class DataLoader<K, V> {
6970
if (key === null || key === undefined) {
7071
throw new TypeError(
7172
'The loader.load() function must be called with a value,' +
72-
`but got: ${key}.`
73+
`but got: ${String(key)}.`
7374
);
7475
}
7576

@@ -232,7 +233,7 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
232233
return failedDispatch(loader, queue, new TypeError(
233234
'DataLoader must be constructed with a function which accepts ' +
234235
'Array<key> and returns Promise<Array<value>>, but the function did ' +
235-
`not return a Promise: ${batchPromise}.`
236+
`not return a Promise: ${String(batchPromise)}.`
236237
));
237238
}
238239

@@ -244,7 +245,7 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
244245
throw new Error(
245246
'DataLoader must be constructed with a function which accepts ' +
246247
'Array<key> and returns Promise<Array<value>>, but the function did ' +
247-
`not return a Promise of an Array: ${values}.`
248+
`not return a Promise of an Array: ${String(values)}.`
248249
);
249250
}
250251
if (values.length !== keys.length) {
@@ -253,8 +254,8 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
253254
'Array<key> and returns Promise<Array<value>>, but the function did ' +
254255
'not return a Promise of an Array of the same length as the Array ' +
255256
'of keys.' +
256-
`\n\nKeys:\n${keys}` +
257-
`\n\nValues:\n${values}`
257+
`\n\nKeys:\n${String(keys)}` +
258+
`\n\nValues:\n${String(values)}`
258259
);
259260
}
260261

0 commit comments

Comments
 (0)