Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Map Schema Removing value creates issue #690

Open
akroshteotia opened this issue Jan 31, 2024 · 2 comments
Open

[Bug]: Map Schema Removing value creates issue #690

akroshteotia opened this issue Jan 31, 2024 · 2 comments

Comments

@akroshteotia
Copy link

akroshteotia commented Jan 31, 2024

Context

Front end JS SDK

Bug description

Using map schema to track last 5 games in state. For first 5 games it works fine as last 5 game map is empty but after sixth game when i remove a value and add new value in state it gives error

index.js:2313 Uncaught TypeError: Cannot read properties of undefined (reading '$changes')
at eval (index.js:2313:41)
at Set.forEach ()
at ReferenceTracker.garbageCollectDeletedRefs (index.js:2292:30)
at Schema.decode (index.js:2679:19)
at SchemaSerializer.patch (SchemaSerializer.js:13:27)
at Room.patch (Room.js:220:25)
at Room.onMessageCallback (Room.js:195:18)

Steps to reproduce

Login at http://3.28.126.178/
U-MA0001
P-1234

go to Manage games and view games
wait for sometime it will start showing this

Environment & Versions

"colyseus": "^0.15.9",
Node - v20.10.0
TypeScript - 4.9.5
OS -Ubuntu- v20

@akroshteotia
Copy link
Author

akroshteotia commented Feb 8, 2024

So i tried some changes to file ./node_module/@colyseus/schema/build/umd/index.js

add if(ref != undefined) solved the issue in else condition

// for decoding
        ReferenceTracker.prototype.garbageCollectDeletedRefs = function () {
            var _this = this;
            this.deletedRefs.forEach(function (refId) {
                //
                // Skip active references.
                //
                if (_this.refCounts[refId] > 0) {
                    return;
                }
                var ref = _this.refs.get(refId);
                //
                // Ensure child schema instances have their references removed as well.
                //
                if (ref instanceof Schema) {
                    for (var fieldName in ref['_definition'].schema) {
                        if (typeof (ref['_definition'].schema[fieldName]) !== "string" &&
                            ref[fieldName] &&
                            ref[fieldName]['$changes']) {
                            _this.removeRef(ref[fieldName]['$changes'].refId);
                        }
                    }
                }
                else if(ref != undefined) {
                    var definition = ref['$changes'].parent._definition;
                    var type = definition.schema[definition.fieldsByIndex[ref['$changes'].parentIndex]];
                    if (typeof (Object.values(type)[0]) === "function") {
                        Array.from(ref.values())
                            .forEach(function (child) { return _this.removeRef(child['$changes'].refId); });
                    }
                }
                _this.refs.delete(refId);
                delete _this.refCounts[refId];
            });
            // clear deleted refs.
            this.deletedRefs.clear();
        };
        return ReferenceTracker;

@akroshteotia
Copy link
Author

akroshteotia commented Feb 10, 2024

Found same thing in Unity SDK in ReferenceTracker function

@github-actions github-actions bot added the Stale label Apr 11, 2024
@endel endel removed the Stale label Apr 11, 2024
@colyseus colyseus deleted a comment from github-actions bot Apr 11, 2024
@endel endel added this to the 0.16 milestone Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants