mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.93k stars 641 forks source link

RangeError: Maximum call stack size exceeded #1311

Closed IgorEfrem closed 5 years ago

IgorEfrem commented 5 years ago

I'm not quite sure but i think this is a bug. please help! Bug report

Sandbox link or minimal reproduction code

export function Document () {
    return(
        types
            .model("Document", {
                id: types.identifier,
                name: "",
                path: "",
                upload_patient_id: types.maybeNull(types.string),
                created_at: types.Date,
                patient: types.late(() => types.reference(Patient)),
                for_signing: types.enumeration("Sign", ["0", "1"]),
                pades_path: "",
                signicat_status: "",
                sms: types.optional(types.array(types.reference(SMS())),[]),
                source: types.enumeration("Source", ["proxy", "adopus"]),
                sort_date: types.Date
            })

            )
    )
} 
export function SMS (){

    return(

        types.model("SMS", {
            id: types.identifier,
            soknadId: types.maybe(types.number),
            documentId: types.optional(types.array(types.safeReference(Document())),[]),
            type: types.number,
            sendMbl: types.string,
            createDate: types.Date,
            createUser: types.string,
            sentDate: types.Date,
            content: types.string 
        })
    )
} 

Describe the expected behavior

Is sexpected that reference to SMS from document and form SMS to Document to be initiated!

Describe the observed behavior

I ran in infinite loop. and get following error: image error stak-trace :

 5256 | __extends(Literal$$1, _super);
  5257 | 
  5258 | function Literal$$1(value) {
> 5259 |   var _this = _super.call(this, JSON.stringify(value)) || this;
       | ^  5260 | 
  5261 |   _this.shouldAttachNode = false;
  5262 |   _this.flags = TypeFlags$$1.Literal;
View compiled
literal$$1
C:/Sites/hs-designer/node_modules/mobx-state-tree/dist/mobx-state-tree.module.js:5309
  5306 |     if (!isPrimitive(value)) fail("Literal types can be built only on top of primitives");
  5307 |   }
  5308 | 
> 5309 |   return new Literal$$1(value);
  5310 | }
  5311 | /**
  5312 |  * Returns if a given value represents a literal type.
View compiled
(anonymous function)
C:/Sites/hs-designer/node_modules/mobx-state-tree/dist/mobx-state-tree.module.js:5456
  5453 | }
  5454 | 
  5455 | var type = union$$1.apply(void 0, realOptions.map(function (option) {
> 5456 |   return literal$$1("" + option);
       | ^  5457 | }));
  5458 | if (typeof name === "string") type.name = name;
  5459 | return type;
View compiled
enumeration$$1 [as enumeration]
C:/Sites/hs-designer/node_modules/mobx-state-tree/dist/mobx-state-tree.module.js:5455
  5452 |   });
  5453 | }
  5454 | 
> 5455 | var type = union$$1.apply(void 0, realOptions.map(function (option) {
       | ^  5456 |   return literal$$1("" + option);
  5457 | }));
  5458 | if (typeof name === "string") type.name = name;
View compiled
Document
C:/Sites/hs-designer/src/mst/models/document.js:19
  16 | upload_patient_id: types.maybeNull(types.string),
  17 | created_at: types.Date,
  18 | patient: types.late(() => types.reference(Patient)),
> 19 | for_signing: types.enumeration("Sign", ["0", "1"]),
     | ^  20 | pades_path: "",
  21 | signicat_status: "",
  22 | sms: types.optional(types.array(types.reference(SMS())),[]),
View compiled
SMS
C:/Sites/hs-designer/src/mst/models/sms.js:14
  11 | types.model("SMS", {
  12 |     id: types.identifier,
  13 |     soknadId: types.maybe(types.number),
> 14 |     documentId: types.optional(types.array(types.safeReference(Document())),[]),
     | ^  15 |     type: types.number,
  16 |     sendMbl: types.string,
  17 |     createDate: types.Date,
View compiled
Document
C:/Sites/hs-designer/src/mst/models/document.js:22
  19 |     for_signing: types.enumeration("Sign", ["0", "1"]),
  20 |     pades_path: "",
  21 |     signicat_status: "",
> 22 |     sms: types.optional(types.array(types.reference(SMS())),[]),
     | ^  23 |     source: types.enumeration("Source", ["proxy", "adopus"]),
  24 |     sort_date: types.Date
  25 | })
View compiled
SMS
C:/Sites/hs-designer/src/mst/models/sms.js:14
  11 | types.model("SMS", {
  12 |     id: types.identifier,
  13 |     soknadId: types.maybe(types.number),
> 14 |     documentId: types.optional(types.array(types.safeReference(Document())),[]),
     | ^  15 |     type: types.number,
  16 |     sendMbl: types.string,
  17 |     createDate: types.Date,
View compiled
Document
C:/Sites/hs-designer/src/mst/models/document.js:22
  19 |     for_signing: types.enumeration("Sign", ["0", "1"]),
  20 |     pades_path: "",
  21 |     signicat_status: "",
> 22 |     sms: types.optional(types.array(types.reference(SMS())),[]),
     | ^  23 |     source: types.enumeration("Source", ["proxy", "adopus"]),
  24 |     sort_date: types.Date
  25 | })
View compiled
SMS
C:/Sites/hs-designer/src/mst/models/sms.js:14
  11 | types.model("SMS", {
  12 |     id: types.identifier,
  13 |     soknadId: types.maybe(types.number),
> 14 |     documentId: types.optional(types.array(types.safeReference(Document())),[]),
     | ^  15 |     type: types.number,
  16 |     sendMbl: types.string,
  17 |     createDate: types.Date,
Maaartinus commented 5 years ago

I'm afraid that what you did is basically equivalent to

export function Document () {
    return(
      [
        SMS(),
        .... whatever,
      ]})

export function SMS () {
    return(
      [
        Document(),
        .... whatever,
      ]})

i.e., an endless (mutual) recursion - unrelated to mst.

You create a new model with every call - that sounds wrong. Create it once and return the same model from your function (model is like a data type, not the data itself and each data type exists just once).

IgorEfrem commented 5 years ago

Oh. Thankyou. Tomorrow I will make a try).

IgorEfrem commented 5 years ago

Oh. Thankyou. Tomorrow I will make a try).

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.