Skip to content

Commit b9574ad

Browse files
committed
New: Simplified reusing / replacing internal constructors
1 parent 270cc94 commit b9574ad

19 files changed

+66
-58
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,28 +289,27 @@ Detailed information on the reflection structure is available within the [docume
289289

290290
### Using custom classes
291291

292-
You can also extend runtime message classes with your own custom functionality by registering your own class with a reflected message type:
292+
You can also extend runtime message classes with your own custom functionality and even register your own constructor with a reflected message type:
293293

294294
```js
295295
...
296296

297-
// Define your own prototypal class
297+
// Define your own constructor
298298
function AwesomeMessage(properties) {
299-
protobuf.Message.call(this, properties); // call the super constructor
299+
// custom initialization code
300+
...
300301
}
301302

302-
// Register your custom class with its reflected type (*)
303-
protobuf.Class.create(root.lookup("awesomepackage.AwesomeMessage") /* or use reflection */, AwesomeMessage);
303+
// Register your constructor with its reflected type (*)
304+
root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
304305

305306
// Define your custom functionality
306307
AwesomeMessage.customStaticMethod = function() { ... };
307308
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
308309

309-
// Continue at "Create a message"
310+
// Continue at "Create a new message" above
310311
```
311312

312-
Afterwards, decoded messages of this type are `instanceof AwesomeMessage`.
313-
314313
(*) Besides referencing its reflected type through `AwesomeMessage.$type` and `AwesomeMesage#$type`, the respective custom class is automatically populated with:
315314

316315
* `AwesomeMessage.create`
@@ -319,6 +318,23 @@ Afterwards, decoded messages of this type are `instanceof AwesomeMessage`.
319318
* `AwesomeMessage.verify`
320319
* `AwesomeMessage.fromObject`, `AwesomeMessage.toObject`, `AwesomeMessage#toObject` and `AwesomeMessage#toJSON`
321320

321+
Afterwards, decoded messages of this type are `instanceof AwesomeMessage`.
322+
323+
Alternatively, you can also just reuse and extend the internal constructor if custom initialization code is not required:
324+
325+
```js
326+
...
327+
328+
// Reuse the internal constructor
329+
var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
330+
331+
// Define your custom functionality
332+
AwesomeMessage.customStaticMethod = function() { ... };
333+
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
334+
335+
// Continue at "Create a new message" above
336+
```
337+
322338
### Using services
323339

324340
The library also supports services but it doesn't make any assumptions about the actual transport channel. Instead, a user must provide a suitable RPC implementation, which is an asynchronous function that takes the reflected service method, the binary request and a node-style callback as its parameters:

dist/light/protobuf.js

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js.gz

-22 Bytes
Binary file not shown.

dist/light/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js.gz

-1 Bytes
Binary file not shown.

dist/protobuf.js

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)