Skip to content

Commit

Permalink
refactor(model): model.name is a normal property now
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Dec 5, 2022
1 parent f1c6c30 commit 527d561
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/model/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {
EffectCtx,
DefineModelOptions,
GetInitialState,
GetName,
GetState,
Model,
ComputedCtx,
Expand Down Expand Up @@ -94,10 +93,6 @@ export const defineModel = <
}
}

const getName = <T extends object>(obj: T): T & GetName<Name> => {
return defineGetter(obj, 'name', () => uniqueName);
};

const getState = <T extends object>(obj: T): T & GetState<State> => {
return defineGetter(obj, 'state', () => {
const state = modelStore.getState()[uniqueName];
Expand All @@ -114,8 +109,9 @@ export const defineModel = <
};

const actionCtx: ActionCtx<State> = composeGetter(
{},
getName,
{
name: uniqueName,
},
getInitialState,
);

Expand All @@ -137,7 +133,11 @@ export const defineModel = <
},
),
};
return composeGetter(obj, getName, getState, getInitialState);
return composeGetter(
Object.assign(obj, { name: uniqueName }),
getState,
getInitialState,
);
};

const enhancedMethods: {
Expand All @@ -163,7 +163,7 @@ export const defineModel = <
if (computed) {
const computedCtx: ComputedCtx<State> & {
[K in string]?: ComputedValue;
} = composeGetter({}, getName, getState);
} = composeGetter({ name: uniqueName }, getState);

Object.keys(computed).forEach((key) => {
computedCtx[key] = enhancedMethods[getMethodCategory(key)][key] =
Expand Down Expand Up @@ -206,7 +206,7 @@ export const defineModel = <
if (events) {
const { onInit, onChange, onDestroy } = events;
const eventCtx: EventCtx<State> = Object.assign(
composeGetter({}, getName, getState),
composeGetter({ name: uniqueName }, getState),
enhancedMethods.external,
enhancedMethods.internal,
);
Expand Down Expand Up @@ -260,9 +260,9 @@ export const defineModel = <
Object.assign(
composeGetter(
{
name: uniqueName,
_$opts: options,
},
getName,
getState,
),
enhancedMethods.external,
Expand Down

0 comments on commit 527d561

Please sign in to comment.