Skip to content

Commit 185e332

Browse files
committed
add 'immediate' mode for 'On{Field}Change' callbacks for Unity/C#
1 parent 9acfb2a commit 185e332

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen"

src/codegen/languages/csharp.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,22 @@ function generateAllFieldCallbacks(klass: Class, indent: string) {
155155
return `${klass.properties
156156
.filter(prop => !prop.deprecated) // generate only for properties that haven't been deprecated.
157157
.map(prop => {
158-
const eventName = `_${prop.name}Change`;
158+
const eventName = `__${prop.name}Change`;
159159
eventNames.push(eventName);
160+
161+
const defaultNull = (prop.childType)
162+
? "null"
163+
: `default(${getType(prop)})`;
164+
160165
return `\t${indent}protected event PropertyChangeHandler<${getType(prop)}> ${eventName};
161-
\t${indent}public Action On${capitalize(prop.name)}Change(PropertyChangeHandler<${getType(prop)}> handler) {
166+
\t${indent}public Action On${capitalize(prop.name)}Change(PropertyChangeHandler<${getType(prop)}> __handler, bool __immediate = true) {
162167
\t${indent}\tif (__callbacks == null) { __callbacks = new SchemaCallbacks(); }
163-
\t${indent}\t__callbacks.AddPropertyCallback(nameof(${prop.name}));
164-
\t${indent}\t${eventName} += handler;
168+
\t${indent}\t__callbacks.AddPropertyCallback(nameof(this.${prop.name}));
169+
\t${indent}\t${eventName} += __handler;
170+
\t${indent}\tif (__immediate && this.${prop.name} != ${defaultNull}) { __handler(this.${prop.name}, ${defaultNull}); }
165171
\t${indent}\treturn () => {
166172
\t${indent}\t\t__callbacks.RemovePropertyCallback(nameof(${prop.name}));
167-
\t${indent}\t\t${eventName} -= handler;
173+
\t${indent}\t\t${eventName} -= __handler;
168174
\t${indent}\t};
169175
\t${indent}}`;
170176
}).join("\n\n")}

0 commit comments

Comments
 (0)