Skip to content

Commit 641625f

Browse files
committed
Other: Unified behaviour of and docs on Class constructor / Class.create
1 parent ef7be35 commit 641625f

17 files changed

+58
-62
lines changed

dist/light/protobuf.js

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

dist/light/protobuf.js.map

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

dist/light/protobuf.min.js

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

dist/light/protobuf.min.js.gz

0 Bytes
Binary file not shown.

dist/light/protobuf.min.js.map

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

dist/minimal/protobuf.js

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

dist/minimal/protobuf.min.js

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

dist/minimal/protobuf.min.js.gz

0 Bytes
Binary file not shown.

dist/protobuf.js

+12-14
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

+3-3
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.

index.d.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
export as namespace protobuf;
22

33
/**
4-
* Constructs a class instance, which is also a {@link Message} prototype.
4+
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
55
* @classdesc Runtime class providing the tools to create your own custom classes.
66
* @constructor
7-
* @param {Type} type Reflected type
7+
* @param {Type} type Reflected message type
8+
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
9+
* @returns {Message} Message prototype
810
*/
911
export class Class {
1012

1113
/**
12-
* Constructs a class instance, which is also a {@link Message} prototype.
14+
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
1315
* @classdesc Runtime class providing the tools to create your own custom classes.
1416
* @constructor
15-
* @param {Type} type Reflected type
17+
* @param {Type} type Reflected message type
18+
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
19+
* @returns {Message} Message prototype
1620
*/
17-
constructor(type: Type);
21+
constructor(type: Type, ctor?: any);
1822

1923
/**
2024
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
21-
* @memberof Class
25+
* @function
2226
* @param {Type} type Reflected message type
2327
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
2428
* @returns {Message} Message prototype

src/class.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,14 @@ var Message = require("./message"),
77
var Type; // cyclic
88

99
/**
10-
* Constructs a class instance, which is also a {@link Message} prototype.
10+
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
1111
* @classdesc Runtime class providing the tools to create your own custom classes.
1212
* @constructor
13-
* @param {Type} type Reflected type
14-
*/
15-
function Class(type) {
16-
return create(type);
17-
}
18-
19-
/**
20-
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
21-
* @memberof Class
2213
* @param {Type} type Reflected message type
2314
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
2415
* @returns {Message} Message prototype
2516
*/
26-
function create(type, ctor) {
17+
function Class(type, ctor) {
2718
if (!Type)
2819
Type = require("./type");
2920

@@ -79,7 +70,14 @@ function create(type, ctor) {
7970
return prototype;
8071
}
8172

82-
Class.create = create;
73+
/**
74+
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
75+
* @function
76+
* @param {Type} type Reflected message type
77+
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
78+
* @returns {Message} Message prototype
79+
*/
80+
Class.create = Class;
8381

8482
// Static methods on Message are instance methods on Class and vice versa
8583
Class.prototype = Message;

src/type.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Object.defineProperties(TypePrototype, {
203203
*/
204204
ctor: {
205205
get: function() {
206-
return this._ctor || (this._ctor = Class.create(this).constructor);
206+
return this._ctor || (this._ctor = Class(this).constructor);
207207
},
208208
set: function(ctor) {
209209
if (ctor && !(ctor.prototype instanceof Message))

tests/api_Class.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ tape.test("reflected classes", function(test) {
99
var root = protobuf.parse(proto).root,
1010
Something = root.lookup("Something");
1111

12-
test.throws(function() {
13-
new protobuf.Class("a");
14-
}, TypeError, "new Class should throw if first argument is not a Type");
12+
test.equal(protobuf.Class.create, protobuf.Class, "Class.create should be an alias of Class (constructor)");
1513

1614
test.throws(function() {
1715
protobuf.Class.create("a");

0 commit comments

Comments
 (0)