Permalink
Browse files

Other: Unified behaviour of and docs on Class constructor / Class.create

  • Loading branch information...
dcodeIO committed Jan 21, 2017
1 parent ef7be35 commit 641625fd64aca55b1163845e6787b58054ac36ec

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 +0 Bytes (100%) dist/light/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

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

Oops, something went wrong.

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

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

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.
@@ -1,24 +1,28 @@
export as namespace protobuf;
/**
* Constructs a class instance, which is also a {@link Message} prototype.
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @classdesc Runtime class providing the tools to create your own custom classes.
* @constructor
* @param {Type} type Reflected type
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
*/
export class Class {
/**
* Constructs a class instance, which is also a {@link Message} prototype.
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @classdesc Runtime class providing the tools to create your own custom classes.
* @constructor
* @param {Type} type Reflected type
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
*/
constructor(type: Type);
constructor(type: Type, ctor?: any);
/**
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @memberof Class
* @function
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
@@ -7,23 +7,14 @@ var Message = require("./message"),
var Type; // cyclic
/**
* Constructs a class instance, which is also a {@link Message} prototype.
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @classdesc Runtime class providing the tools to create your own custom classes.
* @constructor
* @param {Type} type Reflected type
*/
function Class(type) {
return create(type);
}
/**
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @memberof Class
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
*/
function create(type, ctor) {
function Class(type, ctor) {
if (!Type)
Type = require("./type");
@@ -79,7 +70,14 @@ function create(type, ctor) {
return prototype;
}
Class.create = create;
/**
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @function
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
*/
Class.create = Class;
// Static methods on Message are instance methods on Class and vice versa
Class.prototype = Message;
@@ -203,7 +203,7 @@ Object.defineProperties(TypePrototype, {
*/
ctor: {
get: function() {
return this._ctor || (this._ctor = Class.create(this).constructor);
return this._ctor || (this._ctor = Class(this).constructor);
},
set: function(ctor) {
if (ctor && !(ctor.prototype instanceof Message))
@@ -9,9 +9,7 @@ tape.test("reflected classes", function(test) {
var root = protobuf.parse(proto).root,
Something = root.lookup("Something");
test.throws(function() {
new protobuf.Class("a");
}, TypeError, "new Class should throw if first argument is not a Type");
test.equal(protobuf.Class.create, protobuf.Class, "Class.create should be an alias of Class (constructor)");
test.throws(function() {
protobuf.Class.create("a");

0 comments on commit 641625f

Please sign in to comment.