Skip to content

Commit

Permalink
Safer signaling for synchronous load, see #529
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 8, 2016
1 parent 3aa984e commit 954577c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"no-process-env": 1,
"no-process-exit": 1,
"no-restricted-modules": 1,
"no-sync": 1,
"no-sync": 0, // for loadSync

// Stylistic Issues
"semi": 1, // maybe next time
Expand Down
25 changes: 12 additions & 13 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Root.fromJSON = function fromJSON(json, root) {
*/
RootPrototype.resolvePath = util.resolvePath;

// A symbol-like function to safely signal synchronous loading
function SYNC() {}

/**
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
* @param {string|string[]} filename Names of one or multiple files to load
Expand All @@ -74,6 +77,8 @@ RootPrototype.load = function load(filename, callback) {
cb(err, root);
}

var sync = callback === SYNC; // undocumented

// Processes a single file
function process(filename, source) {
try {
Expand All @@ -96,12 +101,10 @@ RootPrototype.load = function load(filename, callback) {
finish(err);
return;
}
if (!queued)
if (!sync && !queued)
finish(null, self);
}

var sync = arguments[2] === true; // undocumented

// Fetches a single file
function fetch(filename, weak) {

Expand All @@ -120,9 +123,9 @@ RootPrototype.load = function load(filename, callback) {

// Shortcut bundled definitions
if (filename in common) {
if (sync) {
if (sync)
process(filename, common[filename]);
} else {
else {
++queued;
setTimeout(function() {
--queued;
Expand Down Expand Up @@ -168,6 +171,8 @@ RootPrototype.load = function load(filename, callback) {
fetch(self.resolvePath("", filename));
});

if (sync)
return self;
if (!queued)
finish(null, self);
return undefined;
Expand All @@ -191,13 +196,7 @@ RootPrototype.load = function load(filename, callback) {
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
*/
RootPrototype.loadSync = function loadSync(filename) {
var ret;
this.load(filename, function(err, root) {
if (err)
throw err;
ret = root;
}, /* undocumented */ true);
return ret;
return this.load(filename, SYNC);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* protobuf.js v6.1.0 TypeScript definitions
* Generated Thu, 08 Dec 2016 18:49:15 UTC
* Generated Thu, 08 Dec 2016 19:14:58 UTC
*/
declare module "protobufjs" {

Expand Down

0 comments on commit 954577c

Please sign in to comment.