Mobx: Call-stack size exceeded when deleting then getting an observable prop (on a class)

Created on 14 Jun 2020  路  4Comments  路  Source: mobxjs/mobx

Intended outcome:

Deleting then getting/setting an observable class-prop would succeed, without errors.

Actual outcome:

Deleting then getting/setting an observable class-prop infinitely calls the mobx get function, causing an error:

Uncaught RangeError: Maximum call stack size exceeded
    at initializeInstance (app.js:244522)
    at Class1.get [as prop1] (app.js:244511)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)
    at Class1.get [as prop1] (app.js:244512)

When I step through the code, pressing "step next", it just repeats the two lines marked below (deepening the stack each time):

function createPropertyInitializerDescriptor(prop, enumerable) {
    var cache = enumerable ? enumerableDescriptorCache : nonEnumerableDescriptorCache;
    return (cache[prop] ||
        (cache[prop] = {
            configurable: true,
            enumerable: enumerable,
            get: function () {
                initializeInstance(this);   <<
                return this[prop];      << function calls itself here!
            },
            set: function (value) {
                initializeInstance(this);
                this[prop] = value;
            }
        }));
}

How to reproduce the issue:

Just run this code:

class Class1 {
    @observable prop1 = false;
}
const obj = new Class1();
delete obj.prop1;
obj.prop1; // or: obj.prop1 = true;

Versions
[email protected]

馃悰 bug 馃毝 stale

Most helpful comment

Don't delete prop, set them to undefined or null. Deleting is always problematic.

All 4 comments

Don't delete prop, set them to undefined or null. Deleting is always problematic.

Is this listed in the readme somewhere?

Also, it seems like an error message should get added to MobX to make this more clear. (perhaps just checking if the infinite loop above gets hit, then logging a warning)

It's kinda common sense that deleting property doesn't give any useful benefits, most often it causes problems. It's not necessarily related to MobX.

But I guess it could be tackled more gracefully in upcoming MobX V6, what do you think @mweststrate?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geohuz picture geohuz  路  3Comments

rodryquintero picture rodryquintero  路  3Comments

etinif picture etinif  路  3Comments

cafreeman picture cafreeman  路  4Comments

bb picture bb  路  3Comments