Skip to content

Commit

Permalink
refactor(coaction): refactor act()
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Dec 20, 2024
1 parent ba6f4b8 commit d5d24f4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/coaction-mobx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/getRawState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export const getRawState = <T extends CreateState>(
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
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/handleState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export const handleState = <T extends CreateState>(
store.transport ?? (options as StoreOptions<T>).enablePatches;
if (!enablePatches) {
if (internal.mutableInstance) {
if (store.act) {
store.act(() => {
if (internal.actMutable) {
internal.actMutable(() => {
fn.apply(null);
});
return [];
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Store<T extends ISlices = ISlices> {
*/
getState: () => T;
/**
* Subscribe to the state changes.
* Subscribe to the state changes, and return the unsubscribe function.
*/
subscribe: (listener: Listener) => () => void;
/**
Expand Down Expand Up @@ -72,10 +72,6 @@ export interface Store<T extends ISlices = ISlices> {
patches: Patches;
inversePatches: Patches;
};
/**
* The act is used to run the function in the action.
*/
act?: <T extends () => any>(fn: T) => ReturnType<T>;
/**
* The trace is used to trace the action
*/
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import type { Draft, Patches } from 'mutative';
import type { CreateState, Listener } from './interface';

export interface Internal<T extends CreateState = CreateState> {
/**
* Get the mutable raw instance via the initial state.
*/
toMutableRaw?: (key: any) => any;
/**
* The store module.
*/
Expand Down Expand Up @@ -38,4 +34,12 @@ export interface Internal<T extends CreateState = CreateState> {
* The listeners.
*/
listeners: Set<Listener>;
/**
* The act is used to run the function in the action for mutable state.
*/
actMutable?: <T extends () => any>(fn: T) => ReturnType<T>;
/**
* Get the mutable raw instance via the initial state.
*/
toMutableRaw?: (key: any) => any;
}

0 comments on commit d5d24f4

Please sign in to comment.