Skip to content

Commit

Permalink
WIP: 짜잔
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfromundefined committed Jul 19, 2024
1 parent 64221fe commit 78cfa53
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/src/future/ActivityDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export type ActivityDefinition<
name: Name;
path: string;
paramTypes?: Params;
loader?: (args: { params: Params; context: any }) => any | Promise<any>;
loader?: (args: { params: Partial<Params>; context: any }) =>
| any
| Promise<any>;
};
2 changes: 1 addition & 1 deletion core/src/future/defineParamTypes.ts
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;
}
116 changes: 116 additions & 0 deletions integrations/react/src/future/makeActions.ts
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,
});
}
},
};
}
10 changes: 5 additions & 5 deletions integrations/react/src/future/stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
StackComponentType,
StackflowReactPlugin,
} from "../stable";
import { type Actions, makeActions } from "./makeActions";

export type StackflowPluginsEntry =
| StackflowReactPlugin<never>
Expand All @@ -33,16 +34,14 @@ export type StackInput<T extends ActivityDefinition<string, BaseParams>> = {
plugins?: Array<StackflowPluginsEntry>;
};

export type StackOutput = {
/**
* Created `<Stack />` component
*/
export type StackOutput<T extends ActivityDefinition<string, BaseParams>> = {
Stack: StackComponentType;
actions: Actions<T>;
};

export function stack<T extends ActivityDefinition<string, BaseParams>>(
input: StackInput<T>,
): StackOutput {
): StackOutput<T> {
const plugins = (input.plugins ?? [])
.flat(Number.POSITIVE_INFINITY as 0)
.map((p) => p as StackflowReactPlugin);
Expand Down Expand Up @@ -141,5 +140,6 @@ export function stack<T extends ActivityDefinition<string, BaseParams>>(

return {
Stack,
actions: makeActions(() => getCoreStore()?.actions),
};
}

0 comments on commit 78cfa53

Please sign in to comment.