@colyseus/schema 2.0.7 → 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.
@@ -2310,16 +2310,25 @@ class Schema {
2310
2310
  setDirty(property, operation) {
2311
2311
  this.$changes.change(property, operation);
2312
2312
  }
2313
- listen(attr, callback) {
2313
+ /**
2314
+ * Client-side: listen for changes on property.
2315
+ * @param prop the property name
2316
+ * @param callback callback to be triggered on property change
2317
+ * @param immediate trigger immediatelly if property has been already set.
2318
+ */
2319
+ listen(prop, callback, immediate = true) {
2314
2320
  if (!this.$callbacks) {
2315
2321
  this.$callbacks = {};
2316
2322
  }
2317
- if (!this.$callbacks[attr]) {
2318
- this.$callbacks[attr] = [];
2323
+ if (!this.$callbacks[prop]) {
2324
+ this.$callbacks[prop] = [];
2325
+ }
2326
+ this.$callbacks[prop].push(callback);
2327
+ if (immediate && this[prop] !== undefined) {
2328
+ callback(this[prop], undefined);
2319
2329
  }
2320
- this.$callbacks[attr].push(callback);
2321
2330
  // return un-register callback.
2322
- return () => spliceOne(this.$callbacks[attr], this.$callbacks[attr].indexOf(callback));
2331
+ return () => spliceOne(this.$callbacks[prop], this.$callbacks[prop].indexOf(callback));
2323
2332
  }
2324
2333
  decode(bytes, it = { offset: 0 }, ref = this) {
2325
2334
  const allChanges = [];