{"version":3,"file":"index.mjs","sources":["../src/makeObservable.ts"],"sourcesContent":["import { Computed, IGettable, Observable, tx } from \"onek\";\n\nconst enum GetterContext {\n    NONE,\n    INSTANCE,\n    NOTIFY,\n}\n\nlet getterContext = GetterContext.NONE;\nlet getterResult: IGettable<any> | undefined = undefined;\n\nexport function instance<T>(fn: () => T): IGettable<T> | undefined {\n    getterContext = GetterContext.INSTANCE;\n    try {\n        fn();\n        return getterResult;\n    } finally {\n        getterContext = GetterContext.NONE;\n        getterResult = undefined;\n    }\n}\n\nexport function notify(thunk: () => any): void {\n    getterContext = GetterContext.NOTIFY;\n    tx(() => {\n        try {\n            thunk();\n        } finally {\n            getterContext = GetterContext.NONE;\n        }\n    });\n}\n\nexport function makeObservable<T extends {}>(obj: T): T {\n    const descriptors: (PropertyDescriptor & { key: string })[] = [];\n\n    Object.keys(obj).forEach((key) => {\n        const currentDescriptor = Object.getOwnPropertyDescriptor(obj, key);\n\n        if (\n            currentDescriptor &&\n            currentDescriptor.configurable &&\n            currentDescriptor.value !== undefined\n        ) {\n            const prop = currentDescriptor.value;\n\n            if (prop instanceof Observable) {\n                descriptors.push({\n                    key: key,\n                    enumerable: true,\n                    configurable: true,\n                    get() {\n                        if (getterContext === GetterContext.NONE) {\n                            return prop.get();\n                        } else if (getterContext === GetterContext.INSTANCE) {\n                            getterResult = prop;\n                        } else if (getterContext === GetterContext.NOTIFY) {\n                            prop.notify();\n                        }\n                    },\n                    set(value: any) {\n                        prop.set(value);\n                    },\n                });\n            } else if (prop instanceof Computed) {\n                descriptors.push({\n                    key: key,\n                    enumerable: true,\n                    configurable: true,\n                    get() {\n                        if (getterContext === GetterContext.NONE) {\n                            return prop.get();\n                        } else if (getterContext === GetterContext.INSTANCE) {\n                            getterResult = prop;\n                        }\n                    },\n                });\n            }\n        }\n    });\n\n    descriptors.forEach((descriptor) => {\n        Object.defineProperty(obj, descriptor.key, descriptor);\n    });\n\n    return obj;\n}\n"],"names":["getterResult","getterContext","instance","fn","undefined","notify","thunk","tx","makeObservable","obj","descriptors","Object","keys","forEach","key","currentDescriptor","getOwnPropertyDescriptor","configurable","value","prop","Observable","push","enumerable","get","set","Computed","descriptor","defineProperty"],"mappings":"wDAQA,IACIA,EADAC,IAGE,SAAUC,EAAYC,GACxBF,IACA,IAEI,OADAE,IACOH,CACX,CAAC,QACGC,IACAD,OAAeI,CACnB,CACJ,UAEgBC,EAAOC,GACnBL,IACAM,EAAG,KACC,IACID,GACJ,CAAC,QACGL,GACJ,GAER,CAEgB,SAAAO,EAA6BC,GACzC,MAAMC,EAAwD,GAmD9D,OAjDAC,OAAOC,KAAKH,GAAKI,QAASC,IACtB,MAAMC,EAAoBJ,OAAOK,yBAAyBP,EAAKK,GAE/D,GACIC,GACAA,EAAkBE,mBACUb,IAA5BW,EAAkBG,MACpB,CACE,MAAMC,EAAOJ,EAAkBG,MAE3BC,aAAgBC,EAChBV,EAAYW,KAAK,CACbP,IAAKA,EACLQ,YAAY,EACZL,cAAc,EACdM,GAAAA,GACI,GAAwC,IAApCtB,EACA,OAAOkB,EAAKI,MACmC,IAAxCtB,EACPD,EAAemB,EAC8B,IAAtClB,GACPkB,EAAKd,QAEb,EACAmB,GAAAA,CAAIN,GACAC,EAAKK,IAAIN,EACb,IAEGC,aAAgBM,GACvBf,EAAYW,KAAK,CACbP,IAAKA,EACLQ,YAAY,EACZL,cAAc,EACdM,GAAAA,GACI,GAAwC,IAApCtB,EACA,OAAOkB,EAAKI,MACmC,IAAxCtB,IACPD,EAAemB,EAEvB,GAGZ,IAGJT,EAAYG,QAASa,IACjBf,OAAOgB,eAAelB,EAAKiB,EAAWZ,IAAKY,EAC/C,GAEOjB,CACX"}