@colyseus/schema 2.0.8 → 2.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2422,17 +2422,27 @@ var Schema = /** @class */ (function () {
2422
2422
  Schema.prototype.setDirty = function (property, operation) {
2423
2423
  this.$changes.change(property, operation);
2424
2424
  };
2425
- Schema.prototype.listen = function (attr, callback) {
2425
+ /**
2426
+ * Client-side: listen for changes on property.
2427
+ * @param prop the property name
2428
+ * @param callback callback to be triggered on property change
2429
+ * @param immediate trigger immediatelly if property has been already set.
2430
+ */
2431
+ Schema.prototype.listen = function (prop, callback, immediate) {
2426
2432
  var _this = this;
2433
+ if (immediate === void 0) { immediate = true; }
2427
2434
  if (!this.$callbacks) {
2428
2435
  this.$callbacks = {};
2429
2436
  }
2430
- if (!this.$callbacks[attr]) {
2431
- this.$callbacks[attr] = [];
2437
+ if (!this.$callbacks[prop]) {
2438
+ this.$callbacks[prop] = [];
2439
+ }
2440
+ this.$callbacks[prop].push(callback);
2441
+ if (immediate && this[prop] !== undefined) {
2442
+ callback(this[prop], undefined);
2432
2443
  }
2433
- this.$callbacks[attr].push(callback);
2434
2444
  // return un-register callback.
2435
- return function () { return spliceOne(_this.$callbacks[attr], _this.$callbacks[attr].indexOf(callback)); };
2445
+ return function () { return spliceOne(_this.$callbacks[prop], _this.$callbacks[prop].indexOf(callback)); };
2436
2446
  };
2437
2447
  Schema.prototype.decode = function (bytes, it, ref) {
2438
2448
  var _a;