-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64221fe
commit 78cfa53
Showing
4 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function defineParamTypes<T>(): Partial<T> { | ||
export function defineParamTypes<T>(): T { | ||
return undefined as any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import type { CoreStore } from "@stackflow/core"; | ||
import type { ActivityDefinition, BaseParams } from "@stackflow/core/future"; | ||
import { makeActivityId } from "__internal__/activity"; | ||
|
||
function parseActionOptions(options?: { animate?: boolean }) { | ||
if (!options) { | ||
return { skipActiveState: false }; | ||
} | ||
|
||
const isNullableAnimateOption = options.animate == null; | ||
|
||
if (isNullableAnimateOption) { | ||
return { skipActiveState: false }; | ||
} | ||
|
||
return { skipActiveState: !options.animate }; | ||
} | ||
|
||
export type Actions<T extends ActivityDefinition<string, BaseParams>> = { | ||
/** | ||
* Push new activity | ||
*/ | ||
push<K extends T["name"]>( | ||
activityName: K, | ||
params: NonNullable<Extract<T, { name: K }>["paramTypes"]>, | ||
options?: { | ||
animate?: boolean; | ||
}, | ||
): { | ||
activityId: string; | ||
}; | ||
|
||
/** | ||
* Push new activity in the top and remove current top activity when new activity is activated | ||
*/ | ||
replace<K extends T["name"]>( | ||
activityName: K, | ||
params: NonNullable<Extract<T, { name: K }>["paramTypes"]>, | ||
options?: { | ||
animate?: boolean; | ||
activityId?: string; | ||
}, | ||
): { | ||
activityId: string; | ||
}; | ||
|
||
/** | ||
* Remove top activity | ||
*/ | ||
pop(): void; | ||
pop(options: { animate?: boolean }): void; | ||
pop(count: number, options?: { animate?: boolean }): void; | ||
}; | ||
|
||
export function makeActions<T extends ActivityDefinition<string, BaseParams>>( | ||
getCoreActions: () => CoreStore["actions"] | undefined, | ||
): Actions<T> { | ||
return { | ||
push(activityName, activityParams, options) { | ||
const activityId = makeActivityId(); | ||
|
||
getCoreActions()?.push({ | ||
activityId, | ||
activityName, | ||
activityParams: activityParams as any, | ||
skipEnterActiveState: parseActionOptions(options).skipActiveState, | ||
}); | ||
|
||
return { | ||
activityId, | ||
}; | ||
}, | ||
replace(activityName, activityParams, options) { | ||
const activityId = makeActivityId(); | ||
|
||
getCoreActions()?.replace({ | ||
activityId: options?.activityId ?? makeActivityId(), | ||
activityName, | ||
activityParams: activityParams as any, | ||
skipEnterActiveState: parseActionOptions(options).skipActiveState, | ||
}); | ||
|
||
return { | ||
activityId, | ||
}; | ||
}, | ||
pop( | ||
count?: number | { animate?: boolean } | undefined, | ||
options?: { animate?: boolean } | undefined, | ||
) { | ||
let _count = 1; | ||
let _options: { animate?: boolean } = {}; | ||
|
||
if (typeof count === "object") { | ||
_options = { | ||
...count, | ||
}; | ||
} | ||
if (typeof count === "number") { | ||
_count = count; | ||
} | ||
if (options) { | ||
_options = { | ||
...options, | ||
}; | ||
} | ||
|
||
for (let i = 0; i < _count; i += 1) { | ||
getCoreActions()?.pop({ | ||
skipExitActiveState: | ||
i === 0 ? parseActionOptions(_options).skipActiveState : true, | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters