Skip to content

Commit

Permalink
Other: Unified behaviour of and docs on Class constructor / Class.create
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 21, 2017
1 parent ef7be35 commit 641625f
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 62 deletions.
26 changes: 12 additions & 14 deletions dist/light/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/light/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

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

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
26 changes: 12 additions & 14 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.

16 changes: 10 additions & 6 deletions index.d.ts
@@ -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
Expand Down
22 changes: 10 additions & 12 deletions src/class.js
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/type.js
Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions tests/api_Class.js
Expand Up @@ -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");
Expand Down

0 comments on commit 641625f

Please sign in to comment.