Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
hide experiment hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-chancetop committed Oct 25, 2023
1 parent 2823302 commit 21336e1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 114 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wonder/core-fe",
"version": "1.6.0",
"version": "1.6.1",
"main": "lib/index.js",
"module": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -17,7 +17,7 @@
"build": "node script/build.js",
"test": "jest --config config/jest.config.js",
"format": "prettier --write \"{src,test,script,config}/**/*.{js,ts,tsx,json}\"",
"prepublishOnly": "node ../../script/build.js --mode fast",
"prepublishOnly": "node script/build.js --mode fast",
"deploy": "node script/publish.js"
},
"devDependencies": {
Expand Down
9 changes: 1 addition & 8 deletions script/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ function spawn(command, args, errorMessage, options) {

function publish() {
console.info("distribute ...");
fs.mkdirsSync("build/dist/lib");
fs.mkdirsSync("build/dist/lib");
fs.copySync("lib", "build/dist/lib/", {dereference: true});
fs.copySync("package.json", "build/dist/package.json", {dereference: true});
fs.copySync("README.md", "build/dist/README.md", {dereference: true});
fs.copySync("src", "build/dist/src", {dereference: true});
fs.copySync("./.npmrc", "build/dist/.npmrc", {dereference: true});
return spawn("npm", ["publish", "--registry=https://pkgs.dev.azure.com/foodtruckinc/Wonder/_packaging/npm-local/npm/registry/"], "publish failed, please fix", {cwd: "build/dist"});
return spawn("npm", ["publish", "--registry=https://pkgs.dev.azure.com/foodtruckinc/Wonder/_packaging/npm-local/npm/registry/"], "publish failed, please fix");
}

publish();
208 changes: 104 additions & 104 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,107 +68,107 @@ export function usePromise() {
return dispatch;
}

type Keys<S extends State, T extends keyof S["app"]> = keyof S["app"][T] & string;

const getModuleState = <S extends State, T extends keyof S["app"] & string, P extends keyof S["app"][T]>(state: S, moduleStateName: T, keys: Array<P>): Pick<S["app"][T], P> => {
if (!state.app[moduleStateName]) {
return {} as S["app"][T];
}
return keys.reduce(
(moduleState, key) => {
moduleState[key] = state.app[moduleStateName][key];
return moduleState;
},
{} as S["app"][T]
);
};

const compareState = <T extends keyof State["app"], P extends keyof State["app"][T], S extends Pick<State["app"][T], P>>(left: S, right: S, keys: Array<P>): boolean => {
if (!left || !right) {
return true;
}
for (const key of keys) {
if (left[key] !== right[key]) {
return false;
}
}
return true;
};

export function useModuleState<
RS extends State,
M extends Partial<{
[key in keyof RS["app"]]: Array<Keys<RS, key>>;
}>,
T extends keyof M,
>(
moduleKeys: M
): {
[key in T]: key extends keyof RS["app"] ? Pick<RS["app"][key], M[key] extends Array<Keys<RS, key>> ? M[key][number] : never> : never;
};
export function useModuleState<RS extends State, M extends keyof RS["app"], T extends keyof RS["app"][M]>(moduleStateName: M, keys: Array<T>): Pick<RS["app"][M], T>;

export function useModuleState<
RS extends State,
M extends
| {
[key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
}
| (keyof RS["app"] & string),
T extends M extends keyof RS["app"] ? keyof RS["app"][M] : keyof M,
ModuleKeys extends Array<keyof RS["app"] & string>,
S extends Pick<RS["app"], keyof RS["app"]>,
>(moduleNameOrKeys: M, keys?: Array<T>) {
const state = useSelector(
(state: RS) => {
if (typeof moduleNameOrKeys === "string") {
return getModuleState(state, moduleNameOrKeys, keys as any) as M extends keyof RS["app"] ? (T extends keyof RS["app"][M] ? Pick<RS["app"][M], T> : never) : never;
}
return (
Object.keys(
moduleNameOrKeys as {
[key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
}
) as ModuleKeys
).reduce(
(result, moduleStateName) => {
result[moduleStateName] = getModuleState(
state,
moduleStateName,
(
moduleNameOrKeys as {
[key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
}
)[moduleStateName]
) as any;
return result;
},
{} as {
[key in keyof RS["app"]]: Pick<RS["app"][key], keyof RS["app"][key]>;
}
);
},
(left, right) => {
if (typeof moduleNameOrKeys === "string") {
moduleNameOrKeys;
return compareState(left, right, keys as any);
}

// return compareRootState(left as S, right as S, moduleNameOrKeys[moduleStateName] as Array<Keys<typeof moduleStateName>>);
return !(Object.keys(moduleNameOrKeys) as ModuleKeys).some(
moduleStateName =>
!compareState(
(left as S)[moduleStateName],
(right as S)[moduleStateName],
(
moduleNameOrKeys as {
[key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
}
)[moduleStateName]
)
);
}
);

return useMemo(() => state ?? {}, [state]);
}
// type Keys<S extends State, T extends keyof S["app"]> = keyof S["app"][T] & string;

// const getModuleState = <S extends State, T extends keyof S["app"] & string, P extends keyof S["app"][T]>(state: S, moduleStateName: T, keys: Array<P>): Pick<S["app"][T], P> => {
// if (!state.app[moduleStateName]) {
// return {} as S["app"][T];
// }
// return keys.reduce(
// (moduleState, key) => {
// moduleState[key] = state.app[moduleStateName][key];
// return moduleState;
// },
// {} as S["app"][T]
// );
// };

// const compareState = <T extends keyof State["app"], P extends keyof State["app"][T], S extends Pick<State["app"][T], P>>(left: S, right: S, keys: Array<P>): boolean => {
// if (!left || !right) {
// return true;
// }
// for (const key of keys) {
// if (left[key] !== right[key]) {
// return false;
// }
// }
// return true;
// };

// export function useModuleState<
// RS extends State,
// M extends Partial<{
// [key in keyof RS["app"]]: Array<Keys<RS, key>>;
// }>,
// T extends keyof M,
// >(
// moduleKeys: M
// ): {
// [key in T]: key extends keyof RS["app"] ? Pick<RS["app"][key], M[key] extends Array<Keys<RS, key>> ? M[key][number] : never> : never;
// };
// export function useModuleState<RS extends State, M extends keyof RS["app"], T extends keyof RS["app"][M]>(moduleStateName: M, keys: Array<T>): Pick<RS["app"][M], T>;

// export function useModuleState<
// RS extends State,
// M extends
// | {
// [key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
// }
// | (keyof RS["app"] & string),
// T extends M extends keyof RS["app"] ? keyof RS["app"][M] : keyof M,
// ModuleKeys extends Array<keyof RS["app"] & string>,
// S extends Pick<RS["app"], keyof RS["app"]>,
// >(moduleNameOrKeys: M, keys?: Array<T>) {
// const state = useSelector(
// (state: RS) => {
// if (typeof moduleNameOrKeys === "string") {
// return getModuleState(state, moduleNameOrKeys, keys as any) as M extends keyof RS["app"] ? (T extends keyof RS["app"][M] ? Pick<RS["app"][M], T> : never) : never;
// }
// return (
// Object.keys(
// moduleNameOrKeys as {
// [key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
// }
// ) as ModuleKeys
// ).reduce(
// (result, moduleStateName) => {
// result[moduleStateName] = getModuleState(
// state,
// moduleStateName,
// (
// moduleNameOrKeys as {
// [key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
// }
// )[moduleStateName]
// ) as any;
// return result;
// },
// {} as {
// [key in keyof RS["app"]]: Pick<RS["app"][key], keyof RS["app"][key]>;
// }
// );
// },
// (left, right) => {
// if (typeof moduleNameOrKeys === "string") {
// moduleNameOrKeys;
// return compareState(left, right, keys as any);
// }

// // return compareRootState(left as S, right as S, moduleNameOrKeys[moduleStateName] as Array<Keys<typeof moduleStateName>>);
// return !(Object.keys(moduleNameOrKeys) as ModuleKeys).some(
// moduleStateName =>
// !compareState(
// (left as S)[moduleStateName],
// (right as S)[moduleStateName],
// (
// moduleNameOrKeys as {
// [key in keyof RS["app"] as string]: Array<Keys<RS, key>>;
// }
// )[moduleStateName]
// )
// );
// }
// );

// return useMemo(() => state ?? {}, [state]);
// }

0 comments on commit 21336e1

Please sign in to comment.