Skip to content

Commit

Permalink
fix needsMakeObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Mar 27, 2024
1 parent 2e59e7f commit 0f7ba53
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions packages/lib/src/modelShared/modelDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const model =
const afterClassInitializationData = new WeakMap<
ModelClass<AnyModel | AnyDataModel>,
{
makeObservableFailed: boolean
needsMakeObservable: boolean | undefined
type: "class" | "data"
}
>()
Expand All @@ -52,25 +52,34 @@ const runAfterClassInitialization = (
) => {
runLateInitializationFunctions(instance, runAfterNewSymbol)

// compatibility with mobx 6
const tag = afterClassInitializationData.get(target)!
if (!tag.makeObservableFailed && getMobxVersion() >= 6) {
try {
mobx6.makeObservable(instance)
} catch (e) {
// sadly we need to use this hack since the PR to do this the proper way
// was rejected on the mobx side
tag.makeObservableFailed = true

const err = e as Error
if (
err.message !==
"[MobX] No annotations were passed to makeObservable, but no decorator members have been found either" &&
err.message !==
"[MobX] No annotations were passed to makeObservable, but no decorated members have been found either"
) {
throw err

// compatibility with mobx 6
if (tag.needsMakeObservable) {
// we know it can be done and shouldn't fail
mobx6.makeObservable(instance)
} else if (tag.needsMakeObservable === undefined) {
if (getMobxVersion() >= 6) {
try {
mobx6.makeObservable(instance)
tag.needsMakeObservable = true
} catch (e) {
const err = e as Error
if (
err.message !==
"[MobX] No annotations were passed to makeObservable, but no decorator members have been found either" &&
err.message !==
"[MobX] No annotations were passed to makeObservable, but no decorated members have been found either"
) {
throw err
}

// sadly we need to use this hack since the PR to do this the proper way
// was rejected on the mobx side
tag.needsMakeObservable = false
}
} else {
tag.needsMakeObservable = false
}
}

Expand Down Expand Up @@ -132,7 +141,7 @@ const internalModel = <MC extends ModelClass<AnyModel | AnyDataModel>>(
}

// track if we fail so we only try it once per class
afterClassInitializationData.set(clazz, { makeObservableFailed: false, type })
afterClassInitializationData.set(clazz, { needsMakeObservable: undefined, type })

if (addInitializer) {
// standard decorator API, avoid proxies
Expand Down

0 comments on commit 0f7ba53

Please sign in to comment.