Skip to content

Commit d05e045

Browse files
Fixed a pesky async timing bug
1 parent 888107e commit d05e045

8 files changed

Lines changed: 47 additions & 33 deletions

File tree

dist/ref-parser.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ $RefParser.prototype.parse = function(schema, options, callback) {
298298

299299
// Read the schema file/url
300300
return read(args.schema, this.$refs, args.options)
301-
.then(function($ref) {
301+
.then(function(cached$Ref) {
302+
var $ref = cached$Ref.$ref;
302303
if (!$ref.value || typeof($ref.value) !== 'object' || $ref.value instanceof Buffer) {
303304
throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath);
304305
}
@@ -309,7 +310,7 @@ $RefParser.prototype.parse = function(schema, options, callback) {
309310
}
310311
})
311312
.catch(function(err) {
312-
util.doCallback(args.callback, err);
313+
util.doCallback(args.callback, err, me.schema);
313314
return Promise.reject(err);
314315
});
315316
};
@@ -898,8 +899,8 @@ module.exports = read;
898899
* @param {$RefParserOptions} options
899900
*
900901
* @returns {Promise}
901-
* The promise resolves with a {@link $Ref} object.
902-
* {@link $Ref#cached} will be true if the file was already cached.
902+
* The promise resolves with an object that contains a {@link $Ref}
903+
* and a flag indicating whether the {@link $Ref} came from cache or not.
903904
*/
904905
function read(path, $refs, options) {
905906
try {
@@ -911,8 +912,10 @@ function read(path, $refs, options) {
911912
var $ref = $refs._get$Ref(path);
912913
if ($ref && !$ref.isExpired()) {
913914
util.debug(' cached from %s', $ref.type);
914-
$ref.cached = true;
915-
return Promise.resolve($ref);
915+
return Promise.resolve({
916+
$ref: $ref,
917+
cached: true
918+
});
916919
}
917920

918921
// Add a placeholder $ref to the cache, so we don't read this URL multiple times
@@ -945,9 +948,11 @@ function read$Ref($ref, options) {
945948
// Update the $ref with the parsed file contents
946949
var value = parse(data, $ref.path, options);
947950
$ref.setValue(value, options);
948-
$ref.cached = false;
949951

950-
return $ref;
952+
return {
953+
$ref: $ref,
954+
cached: false
955+
};
951956
});
952957
}
953958
else {
@@ -1576,9 +1581,11 @@ function crawl(obj, path, pathFromRoot, $refs, options) {
15761581
*/
15771582
function crawl$Ref(path, pathFromRoot, $refs, options) {
15781583
return read(path, $refs, options)
1579-
.then(function($ref) {
1584+
.then(function(cached$Ref) {
15801585
// If a cached $ref is returned, then we DON'T need to crawl it
1581-
if (!$ref.cached) {
1586+
if (!cached$Ref.cached) {
1587+
var $ref = cached$Ref.$ref;
1588+
15821589
// This is a new $ref, so store the path from the root of the JSON schema to this $ref
15831590
$ref.pathFromRoot = pathFromRoot;
15841591

dist/ref-parser.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ $RefParser.prototype.parse = function(schema, options, callback) {
100100

101101
// Read the schema file/url
102102
return read(args.schema, this.$refs, args.options)
103-
.then(function($ref) {
103+
.then(function(cached$Ref) {
104+
var $ref = cached$Ref.$ref;
104105
if (!$ref.value || typeof($ref.value) !== 'object' || $ref.value instanceof Buffer) {
105106
throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath);
106107
}
@@ -111,7 +112,7 @@ $RefParser.prototype.parse = function(schema, options, callback) {
111112
}
112113
})
113114
.catch(function(err) {
114-
util.doCallback(args.callback, err);
115+
util.doCallback(args.callback, err, me.schema);
115116
return Promise.reject(err);
116117
});
117118
};

lib/read.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module.exports = read;
2020
* @param {$RefParserOptions} options
2121
*
2222
* @returns {Promise}
23-
* The promise resolves with a {@link $Ref} object.
24-
* {@link $Ref#cached} will be true if the file was already cached.
23+
* The promise resolves with an object that contains a {@link $Ref}
24+
* and a flag indicating whether the {@link $Ref} came from cache or not.
2525
*/
2626
function read(path, $refs, options) {
2727
try {
@@ -33,8 +33,10 @@ function read(path, $refs, options) {
3333
var $ref = $refs._get$Ref(path);
3434
if ($ref && !$ref.isExpired()) {
3535
util.debug(' cached from %s', $ref.type);
36-
$ref.cached = true;
37-
return Promise.resolve($ref);
36+
return Promise.resolve({
37+
$ref: $ref,
38+
cached: true
39+
});
3840
}
3941

4042
// Add a placeholder $ref to the cache, so we don't read this URL multiple times
@@ -67,9 +69,11 @@ function read$Ref($ref, options) {
6769
// Update the $ref with the parsed file contents
6870
var value = parse(data, $ref.path, options);
6971
$ref.setValue(value, options);
70-
$ref.cached = false;
7172

72-
return $ref;
73+
return {
74+
$ref: $ref,
75+
cached: false
76+
};
7377
});
7478
}
7579
else {

lib/resolve.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ function crawl(obj, path, pathFromRoot, $refs, options) {
107107
*/
108108
function crawl$Ref(path, pathFromRoot, $refs, options) {
109109
return read(path, $refs, options)
110-
.then(function($ref) {
110+
.then(function(cached$Ref) {
111111
// If a cached $ref is returned, then we DON'T need to crawl it
112-
if (!$ref.cached) {
112+
if (!cached$Ref.cached) {
113+
var $ref = cached$Ref.$ref;
114+
113115
// This is a new $ref, so store the path from the root of the JSON schema to this $ref
114116
$ref.pathFromRoot = pathFromRoot;
115117

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"karma": "karma start --single-run",
3030
"test": "npm run browserify -- --test && npm run istanbul && npm run karma",
3131
"upgrade": "ncu --upgradeAll && npm update && bower update",
32-
"bump": "bump --prerelease --grep lib/bundle.js dist/* --tag --push --all",
32+
"bump": "bump --prompt --grep lib/bundle.js dist/* --tag --push --all",
3333
"release": "npm run upgrade && npm test && npm run bump && npm publish"
3434
},
3535
"repository": {
@@ -66,4 +66,4 @@
6666
"js-yaml": "^3.4.2",
6767
"ono": "^1.0.22"
6868
}
69-
}
69+
}

0 commit comments

Comments
 (0)