From d5d24f4d387b9fc52c95f1025f306a9180e883fc Mon Sep 17 00:00:00 2001 From: unadlib Date: Fri, 20 Dec 2024 20:52:33 +0800 Subject: [PATCH] refactor(coaction): refactor act() --- packages/coaction-mobx/src/index.ts | 2 +- packages/core/src/getRawState.ts | 4 ++-- packages/core/src/handleState.ts | 4 ++-- packages/core/src/interface.ts | 6 +----- packages/core/src/internal.ts | 12 ++++++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/coaction-mobx/src/index.ts b/packages/coaction-mobx/src/index.ts index a5c2dd8..6bf5d87 100644 --- a/packages/coaction-mobx/src/index.ts +++ b/packages/coaction-mobx/src/index.ts @@ -15,7 +15,7 @@ const handleStore = ( Object.assign(store, { subscribe: autorun }); - store.act = runInAction; + internal.actMutable = runInAction; store.apply = (state = store.getState(), patches) => { if (!patches) { if (store.isSliceStore) { diff --git a/packages/core/src/getRawState.ts b/packages/core/src/getRawState.ts index b5dd47a..569a6d4 100644 --- a/packages/core/src/getRawState.ts +++ b/packages/core/src/getRawState.ts @@ -192,8 +192,8 @@ export const getRawState = ( done?.(result); return result; } - if (internal.mutableInstance && store.act) { - const result = store.act(() => { + if (internal.mutableInstance && internal.actMutable) { + const result = internal.actMutable(() => { return fn.apply( sliceKey ? store.getState()[sliceKey] : store.getState(), args diff --git a/packages/core/src/handleState.ts b/packages/core/src/handleState.ts index 10d0448..96cf9b0 100644 --- a/packages/core/src/handleState.ts +++ b/packages/core/src/handleState.ts @@ -47,8 +47,8 @@ export const handleState = ( store.transport ?? (options as StoreOptions).enablePatches; if (!enablePatches) { if (internal.mutableInstance) { - if (store.act) { - store.act(() => { + if (internal.actMutable) { + internal.actMutable(() => { fn.apply(null); }); return []; diff --git a/packages/core/src/interface.ts b/packages/core/src/interface.ts index b816542..4d08d19 100644 --- a/packages/core/src/interface.ts +++ b/packages/core/src/interface.ts @@ -34,7 +34,7 @@ export interface Store { */ getState: () => T; /** - * Subscribe to the state changes. + * Subscribe to the state changes, and return the unsubscribe function. */ subscribe: (listener: Listener) => () => void; /** @@ -72,10 +72,6 @@ export interface Store { patches: Patches; inversePatches: Patches; }; - /** - * The act is used to run the function in the action. - */ - act?: any>(fn: T) => ReturnType; /** * The trace is used to trace the action */ diff --git a/packages/core/src/internal.ts b/packages/core/src/internal.ts index 413ceb9..1095a62 100644 --- a/packages/core/src/internal.ts +++ b/packages/core/src/internal.ts @@ -2,10 +2,6 @@ import type { Draft, Patches } from 'mutative'; import type { CreateState, Listener } from './interface'; export interface Internal { - /** - * Get the mutable raw instance via the initial state. - */ - toMutableRaw?: (key: any) => any; /** * The store module. */ @@ -38,4 +34,12 @@ export interface Internal { * The listeners. */ listeners: Set; + /** + * The act is used to run the function in the action for mutable state. + */ + actMutable?: any>(fn: T) => ReturnType; + /** + * Get the mutable raw instance via the initial state. + */ + toMutableRaw?: (key: any) => any; }