Skip to content

Commit

Permalink
Generate lookupTYPE functions programmatically; Ensure working reflec…
Browse files Browse the repository at this point in the history
…tion class names with minified builds
  • Loading branch information
dcodeIO committed Dec 14, 2016
1 parent 6f9ffb6 commit 19c269f
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 56 deletions.
59 changes: 37 additions & 22 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.

6 changes: 3 additions & 3 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.

2 changes: 1 addition & 1 deletion dist/runtime/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/runtime/protobuf.min.js

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

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ function bundle(compress, runtime) {
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(
gulpif(compress, uglify({
mangleProperties: { regex: /^_/ }
mangleProperties: {
regex: /^_/
}
}))
)
.pipe(header(license, {
Expand Down
2 changes: 2 additions & 0 deletions src/enum.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Enum;

Enum.className = "Enum";

var ReflectionObject = require("./object");
/** @alias Enum.prototype */
var EnumPrototype = ReflectionObject.extend(Enum);
Expand Down
2 changes: 2 additions & 0 deletions src/field.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Field;

Field.className = "Field";

var ReflectionObject = require("./object");
/** @alias Field.prototype */
var FieldPrototype = ReflectionObject.extend(Field);
Expand Down
2 changes: 2 additions & 0 deletions src/mapfield.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = MapField;

MapField.className = "MapField";

var Field = require("./field");
/** @alias Field.prototype */
var FieldPrototype = Field.prototype;
Expand Down
2 changes: 2 additions & 0 deletions src/method.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Method;

Method.className = "Method";

var ReflectionObject = require("./object");
/** @alias Method.prototype */
var MethodPrototype = ReflectionObject.extend(Method);
Expand Down
25 changes: 13 additions & 12 deletions src/namespace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Namespace;

Namespace.className = "Namespace";

var ReflectionObject = require("./object");
/** @alias Namespace.prototype */
var NamespacePrototype = ReflectionObject.extend(Namespace);
Expand Down Expand Up @@ -268,30 +270,29 @@ NamespacePrototype.lookup = function lookup(path, parentAlreadyChecked) {
return this.parent.lookup(path);
};

[ Type, Service ].forEach(function(T) {
NamespacePrototype['lookup' + T.className] = function lookupT() {
var found = this.lookup(path);
if (!(found instanceof T))
throw Error("no such " + T.className);
return found;
};
});

/**
* Looks up the {@link Type|type} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
* @name Namespace#lookupType
* @param {string|string[]} path Path to look up
* @returns {Type} Looked up type
* @throws {Error} If `path` does not point to a type
*/
NamespacePrototype.lookupType = function lookupType(path) {
var found = this.lookup(path);
if (!(found instanceof Type))
throw Error("no such type");
return found;
};

/**
* Looks up the {@link Service|service} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
* @name Namespace#lookupService
* @param {string|string[]} path Path to look up
* @returns {Service} Looked up service
* @throws {Error} If `path` does not point to a service
*/
NamespacePrototype.lookupService = function lookupService(path) {
var found = this.lookup(path);
if (!(found instanceof Service))
throw Error("no such service");
return found;
};
9 changes: 7 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
module.exports = ReflectionObject;

ReflectionObject.className = "ReflectionObject";
ReflectionObject.extend = extend;

var Root = require("./root"),
Expand Down Expand Up @@ -190,8 +191,12 @@ ReflectionObjectPrototype.setOptions = function setOptions(options, ifNotSet) {

/**
* Converts this instance to its string representation.
* @returns {string} Constructor name, space, full name
* @returns {string} Class name[, space, full name]
*/
ReflectionObjectPrototype.toString = function toString() {
return this.constructor.name + " " + this.getFullName();
var className = this.constructor.className;
var fullName = this.getFullName();
if (fullName.length)
return className + " " + fullName;
return className;
};
2 changes: 2 additions & 0 deletions src/oneof.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = OneOf;

OneOf.className = "OneOf";

var ReflectionObject = require("./object");
/** @alias OneOf.prototype */
var OneOfPrototype = ReflectionObject.extend(OneOf);
Expand Down
9 changes: 2 additions & 7 deletions src/root.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Root;

Root.className = "Root";

var Namespace = require("./namespace");
/** @alias Root.prototype */
var RootPrototype = Namespace.extend(Root);
Expand Down Expand Up @@ -270,10 +272,3 @@ RootPrototype._handleRemove = function handleRemove(object) {
this._handleRemove(nested[i]);
}
};

/**
* @override
*/
RootPrototype.toString = function toString() {
return this.constructor.name;
};
2 changes: 2 additions & 0 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Service;

Service.className = "Service";

var Namespace = require("./namespace");
/** @alias Namespace.prototype */
var NamespacePrototype = Namespace.prototype;
Expand Down
2 changes: 2 additions & 0 deletions src/type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Type;

Type.className = "Type";

var Namespace = require("./namespace");
/** @alias Namespace.prototype */
var NamespacePrototype = Namespace.prototype;
Expand Down
8 changes: 7 additions & 1 deletion tests/browser.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html>
<head><script src="../dist/protobuf.js"></script></head>
<head><script src="../dist/protobuf.min.js"></script></head>
<body>
<script>
protobuf.util.fetch("browser.html")
Expand All @@ -13,6 +13,12 @@
.catch(function(err) {
console.log("error", err);
});

var root = new protobuf.Root();
console.log(root.toString());
var message = new protobuf.Type("Message");
root.add(message);
console.log(message.toString());
</script>
</body>
</html>
Loading

0 comments on commit 19c269f

Please sign in to comment.