Skip to content

Commit 954577c

Browse files
committed
Safer signaling for synchronous load, see #529
1 parent 3aa984e commit 954577c

8 files changed

+29
-31
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"no-process-env": 1,
110110
"no-process-exit": 1,
111111
"no-restricted-modules": 1,
112-
"no-sync": 1,
112+
"no-sync": 0, // for loadSync
113113

114114
// Stylistic Issues
115115
"semi": 1, // maybe next time

dist/protobuf.js

+12-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-4 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/root.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Root.fromJSON = function fromJSON(json, root) {
5454
*/
5555
RootPrototype.resolvePath = util.resolvePath;
5656

57+
// A symbol-like function to safely signal synchronous loading
58+
function SYNC() {}
59+
5760
/**
5861
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
5962
* @param {string|string[]} filename Names of one or multiple files to load
@@ -74,6 +77,8 @@ RootPrototype.load = function load(filename, callback) {
7477
cb(err, root);
7578
}
7679

80+
var sync = callback === SYNC; // undocumented
81+
7782
// Processes a single file
7883
function process(filename, source) {
7984
try {
@@ -96,12 +101,10 @@ RootPrototype.load = function load(filename, callback) {
96101
finish(err);
97102
return;
98103
}
99-
if (!queued)
104+
if (!sync && !queued)
100105
finish(null, self);
101106
}
102107

103-
var sync = arguments[2] === true; // undocumented
104-
105108
// Fetches a single file
106109
function fetch(filename, weak) {
107110

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

121124
// Shortcut bundled definitions
122125
if (filename in common) {
123-
if (sync) {
126+
if (sync)
124127
process(filename, common[filename]);
125-
} else {
128+
else {
126129
++queued;
127130
setTimeout(function() {
128131
--queued;
@@ -168,6 +171,8 @@ RootPrototype.load = function load(filename, callback) {
168171
fetch(self.resolvePath("", filename));
169172
});
170173

174+
if (sync)
175+
return self;
171176
if (!queued)
172177
finish(null, self);
173178
return undefined;
@@ -191,13 +196,7 @@ RootPrototype.load = function load(filename, callback) {
191196
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
192197
*/
193198
RootPrototype.loadSync = function loadSync(filename) {
194-
var ret;
195-
this.load(filename, function(err, root) {
196-
if (err)
197-
throw err;
198-
ret = root;
199-
}, /* undocumented */ true);
200-
return ret;
199+
return this.load(filename, SYNC);
201200
};
202201

203202
/**

types/protobuf.js.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* protobuf.js v6.1.0 TypeScript definitions
3-
* Generated Thu, 08 Dec 2016 18:49:15 UTC
3+
* Generated Thu, 08 Dec 2016 19:14:58 UTC
44
*/
55
declare module "protobufjs" {
66

0 commit comments

Comments
 (0)