Permalink
Browse files

Safer signaling for synchronous load, see #529

  • Loading branch information...
dcodeIO committed Dec 8, 2016
1 parent 3aa984e commit 954577c6b421f7d7f4905bcc32f57e4ebaf548da
Showing with 29 additions and 31 deletions.
  1. +1 −1 .eslintrc.json
  2. +12 −13 dist/protobuf.js
  3. +1 −1 dist/protobuf.js.map
  4. +2 −2 dist/protobuf.min.js
  5. BIN dist/protobuf.min.js.gz
  6. +1 −1 dist/protobuf.min.js.map
  7. +11 −12 src/root.js
  8. +1 −1 types/protobuf.js.d.ts
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -4 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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
@@ -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 {
@@ -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) {
@@ -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;
@@ -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;
@@ -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);
};
/**
@@ -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" {

0 comments on commit 954577c

Please sign in to comment.