Skip to content

Commit

Permalink
move stories to lib (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliusKoronciCH authored Apr 8, 2024
1 parent ca81962 commit 2a2db56
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 242 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
plugins: ['react'],
rules: {
'@typescript-eslint/semi': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/strict-boolean-expressions': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
},
Expand Down
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StorybookConfig } from '@storybook/react-vite';

const config: StorybookConfig = {
stories: ['../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
stories: ['../lib/stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
Expand Down
Binary file modified .yarn/install-state.gz
Binary file not shown.
4 changes: 1 addition & 3 deletions lib/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function createEventStore<T extends object>(
if (event.type === '@@INIT' || event.type === '@@HYDRATED') {
return event.payload as T;
}

return set(event.type, event.payload, state);
}, initialState),
tap((state) => {
Expand Down Expand Up @@ -113,10 +114,8 @@ export function createEventStore<T extends object>(

const defaultValue: GetValueType<T, K> = get(type, state$.getValue());


const [value, setValue] = useState<GetValueType<T, K>>(defaultValue);


const handleUpdate = useCallback((payload: GetValueType<T, K>) => {
if (!disableCache) setValue(payload);
if (!disableCache) setValue(payload);
Expand Down Expand Up @@ -152,7 +151,6 @@ export function createEventStore<T extends object>(
return [value, handleUpdate];
};


const useHydrateStore = () => {
return useCallback((payload: T) => {
globalEventStore$.next({ type: '@@HYDRATED', payload });
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { Flex, Text, Button } from '@radix-ui/themes';
import useStore, { useCounterSelector } from './counterStore';

Expand All @@ -9,9 +8,10 @@ const CountText = () => {
};

export const Counter = () => {
const [state, updateCount] = useStore();
const increment = () =>
const [, updateCount] = useStore();
const increment = () => {
updateCount((prevState) => ({ ...prevState, count: prevState.count + 1 }));
};

return (
<Flex direction="column" gap="2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { Flex, Text, Button } from '@radix-ui/themes';

import useStore, { useCounter2Selector } from './counterStore2';
Expand All @@ -10,9 +9,10 @@ const CountText = () => {
};

export const Counter2 = () => {
const [state, updateCount] = useStore();
const increment = () =>
const [, updateCount] = useStore();
const increment = () => {
updateCount((prevState) => ({ ...prevState, count: prevState.count + 1 }));
};

return (
<Flex direction="column" gap="2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { Flex, Text, Button } from '@radix-ui/themes';
import useStore from './counterStore2';

export const Counter3 = () => {
const [state, updateCount] = useStore();
const increment = () =>
const increment = () => {
updateCount((prevState) => ({ ...prevState, count: prevState.count + 1 }));
};

return (
<Flex direction="column" gap="2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Button, Flex, Text } from '@radix-ui/themes';
import React from 'react';
import useStore, { useCounterPromiseSelector } from './counterStorePromise';

export const DefaultPromise = () => {
Expand Down
1 change: 0 additions & 1 deletion stories/counter/Root.tsx → lib/stories/counter/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Flex } from '@radix-ui/themes';
import { Counter } from './Counter';
import React from 'react';
import { Counter2 } from './Counter2';
import { Counter3 } from './Counter3';
import { DefaultPromise } from './DefaultPromise';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildClassicStore } from '../../lib';
import { buildClassicStore } from '../..';

export interface CounterState {
count: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildClassicStore } from '../../lib';
import { buildClassicStore } from '../..';

export interface Counter2State {
count: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { buildClassicStore } from '../../lib';
import { buildClassicStore } from '../..';

export interface CounterState {
count: number;
}

const counterStore3Builder = await buildClassicStore<CounterState>({
hydrator: () => Promise.resolve({ count: 88 }),
hydrator: async () => await Promise.resolve({ count: 88 }),
beforeLoadState: { count: 0 },
});
const { useStore, useSelector: useCounterPromiseSelector } =
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Flex } from '@radix-ui/themes';

import React from 'react';
import { state$, useStoreValue } from './store';

export const Root = () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createEventStore } from '../../lib';
import { State } from './state';
import { createEventStore } from '../..';
import { type State } from './state';

export const {
state$,
Expand All @@ -15,19 +15,19 @@ export const {
user: { address: { city: 'n/a', street: 'n/a' }, name: 'n/a' },
},
{
hydrator: () => {
return new Promise((resolve) => {
hydrator: async () => {
return await new Promise((resolve) => {
setTimeout(
() =>
resolve({
{ resolve({
user: {
address: {
city: 'Aubonne',
street: 'Chemin du Mont-Blanc 16',
},
name: 'Julius',
},
}),
}); },
3000,
);
});
Expand Down
20 changes: 13 additions & 7 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
export type IsObject<T> = T extends object ? true : false;

export type PropertyPath<T> = {
[K in keyof T]: IsObject<T[K]> extends true ? PropertyPath<T[K]> : string;
[K in keyof T]: K extends string
? T[K] extends object
? `${K}.${PropertyPath<T[K]>}` | K
: K
: never;
}[keyof T];

export type GetValueType<
T,
P extends PropertyPath<T>,
> = P extends `${infer K}.${infer Rest}`
? K extends keyof T
? GetValueType<T[K], Rest>
Path extends PropertyPath<T>,
> = Path extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends PropertyPath<T[Key]>
? GetValueType<T[Key], Rest>
: never
: never
: P extends keyof T
? T[P]
: Path extends keyof T
? T[Path]
: never;

export interface NestedEvent<T> {
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
"@storybook/testing-library": "^0.2.2",
"@types/lodash.set": "^4",
"@types/node": "^20.11.20",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^7.0.2",
"@typescript-eslint/eslint-plugin": "7.6.0",
"@typescript-eslint/parser": "7.6.0",
"@vitest/ui": "^1.3.1",
"eslint": "^8.0.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-storybook": "^0.8.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-config-standard-with-typescript": "43.0.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-n": "17.0.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-storybook": "0.8.0",
"husky": "^9.0.11",
"jsdom": "^24.0.0",
"lint-staged": "^15.2.2",
Expand All @@ -71,7 +71,7 @@
"rxjs": "^7.8.1",
"standard-version": "^9.5.0",
"storybook": "^7.6.17",
"typescript": "*",
"typescript": "5.4.4",
"vite": "^5.1.4",
"vite-plugin-dts": "^3.7.3",
"vitest": "^1.3.1"
Expand Down
Loading

0 comments on commit 2a2db56

Please sign in to comment.