Skip to content

Commit f8fcf97

Browse files
authored
feat: rename hatch to route (#15)
2 parents 86fa314 + a3b6862 commit f8fcf97

11 files changed

Lines changed: 73 additions & 70 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ jobs:
2222

2323
- name: Build
2424
run: yarn build
25+
26+
- name: Test
27+
run: yarn test

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,31 @@ forward({
4646

4747
```ts
4848
// some-page/contract.ts
49-
import { createHatch } from 'bypath';
49+
import { createRoute } from 'bypath';
5050

5151
import { navigationDomain } from 'entities/navigation';
5252

53-
export const hatch = createHatch(navigationDomain);
53+
export const route = createRoute(navigationDomain);
5454
// domain is optional
5555
// for debug/logging
5656
```
5757

5858
```ts
5959
// some-page/index.tsx
60-
import { guard, createDomain } from 'effector';
61-
import { withHatch } from 'bypath';
60+
import { sample, createDomain } from 'effector';
61+
import { withRoute } from 'bypath';
6262

6363
import { historyPush } from 'entities/navigation';
6464
import { $isAuthenticated } from 'entities/session';
6565

66-
import { hatch } from './contract';
66+
import { route } from './contract';
6767

68-
export const Page = withHatch(hatch, () => {
68+
export const Page = withRoute(route, () => {
6969
//...
7070
});
7171

72-
guard({
73-
source: hatch.enter,
72+
sample({
73+
source: route.enter,
7474
filter: $isAuthenticated.map((is) => !is),
7575
target: historyPush.prepend(() => '/login')
7676
});

src/browser-application.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Domain, Event, combine, forward, guard } from 'effector';
22
import { RouteConfig, matchRoutes } from 'react-router-config';
33
import { splitMap } from 'patronum/split-map';
44

5-
import { HatchParams, getHatch } from './hatch';
5+
import { RouteParams, getRoute } from './routing';
66
import { createNavigation } from './navigation';
77
import { defaultDomain } from './default-domain';
88

@@ -54,15 +54,15 @@ export function createBrowserApplication(config: {
5454
},
5555
});
5656

57-
const hatchEnter = domain.createEvent<HatchParams>({ name: `hatchEnter:${path}` });
58-
const hatchUpdate = domain.createEvent<HatchParams>({ name: `hatchUpdate:${path}` });
59-
const hatchExit = domain.createEvent<void>({ name: `hatchExit:${path}` });
57+
const pageRouteEnter = domain.createEvent<RouteParams>({ name: `routeEnter:${path}` });
58+
const pageRouteUpdate = domain.createEvent<RouteParams>({ name: `routeUpdate:${path}` });
59+
const pageRouteExit = domain.createEvent<void>({ name: `routeExit:${path}` });
6060

61-
const componentHatch = getHatch(component);
62-
if (componentHatch) {
63-
forward({ from: hatchEnter, to: componentHatch.enter });
64-
forward({ from: hatchUpdate, to: componentHatch.update });
65-
forward({ from: hatchExit, to: componentHatch.exit });
61+
const pageRoute = getRoute(component);
62+
if (pageRoute) {
63+
forward({ from: pageRouteEnter, to: pageRoute.enter });
64+
forward({ from: pageRouteUpdate, to: pageRoute.update });
65+
forward({ from: pageRouteExit, to: pageRoute.exit });
6666
}
6767

6868
// Shows that user is on the route
@@ -78,16 +78,16 @@ export function createBrowserApplication(config: {
7878
guard({
7979
clock: routeMatched,
8080
filter: $onPage,
81-
target: hatchUpdate,
81+
target: pageRouteUpdate,
8282
});
8383

8484
guard({
8585
clock: routeMatched,
8686
filter: combine($onPage, $onRoute, (page, route) => !page && route),
87-
target: hatchEnter,
87+
target: pageRouteEnter,
8888
});
8989

90-
$onPage.on(hatchEnter, () => true);
90+
$onPage.on(pageRouteEnter, () => true);
9191
//#endregion route matched
9292

9393
//#region NOT matched
@@ -96,10 +96,10 @@ export function createBrowserApplication(config: {
9696
guard({
9797
clock: notMatched,
9898
filter: $onPage,
99-
target: hatchExit,
99+
target: pageRouteExit,
100100
});
101101

102-
$onPage.on(hatchExit, () => false);
102+
$onPage.on(pageRouteExit, () => false);
103103
//#endregion NOT matched
104104
}
105105

src/default-domain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { createDomain } from 'effector';
22

3-
export const defaultDomain = createDomain('framework/default');
3+
export const defaultDomain = createDomain('bypath/default');

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import loadable from '@loadable/component';
22
import { RouteConfig, renderRoutes } from 'react-router-config';
33

44
export { createBrowserApplication } from './browser-application';
5-
export { createHatch, getHatch, withHatch, lookupHatch } from './hatch';
6-
export type { Hatch, HatchParams } from './hatch';
5+
export { createRoute, getRoute, withRoute, lookupRoute } from './routing';
6+
export type { Route, RouteParams } from './routing';
77

88
export { contract } from './contract';
99

src/hatch.ts renamed to src/routing.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import { MatchedRoute } from 'react-router-config';
44

55
import { defaultDomain } from './default-domain';
66

7-
const HATCH = 'framework/page-hatch';
7+
const ROUTE_PROPERTY = 'bypath/page-route';
88

9-
export interface HatchParams {
9+
export interface RouteParams {
1010
params: Record<string, string>;
1111
query: Record<string, string>;
1212
}
1313

1414
/**
15-
* Hatch is like a Gate, but just for models
15+
* Route is like a Gate, but just for models
1616
*/
17-
export interface Hatch {
17+
export interface Route {
1818
// Called by history from withHatch
19-
enter: Event<HatchParams>;
20-
update: Event<HatchParams>;
19+
enter: Event<RouteParams>;
20+
update: Event<RouteParams>;
2121
exit: Event<void>;
2222

2323
$opened: Store<boolean>;
2424
$params: Store<Record<string, string>>;
2525
$query: Store<Record<string, string>>;
2626

27-
$props: Store<HatchParams>;
27+
$props: Store<RouteParams>;
2828
}
2929

3030
interface Config {
31-
enter: Event<HatchParams>;
32-
update: Event<HatchParams>;
31+
enter: Event<RouteParams>;
32+
update: Event<RouteParams>;
3333
exit: Event<void>;
3434
domain?: Domain;
3535
}
@@ -39,7 +39,7 @@ interface Config {
3939
* Stores is derived from this events and holds specific parameters
4040
* `$opened` holds current state of page, if user visited page but not left, it is `true`
4141
*/
42-
export function createHatch(config_: Config | Domain = defaultDomain): Hatch {
42+
export function createRoute(config_: Config | Domain = defaultDomain): Route {
4343
let domain;
4444
let config: Partial<Config>;
4545
if (is.domain(config_)) {
@@ -57,41 +57,41 @@ export function createHatch(config_: Config | Domain = defaultDomain): Hatch {
5757
const $params = domain.createStore<Record<string, string>>({});
5858
const $query = domain.createStore<Record<string, string>>({});
5959

60-
const hatch = {
61-
enter: config.enter ?? domain.createEvent<HatchParams>(),
62-
update: config.update ?? domain.createEvent<HatchParams>(),
60+
const route = {
61+
enter: config.enter ?? domain.createEvent<RouteParams>(),
62+
update: config.update ?? domain.createEvent<RouteParams>(),
6363
exit: config.exit ?? domain.createEvent<void>(),
6464
$opened,
6565
$params,
6666
$query,
6767
$props: combine({ params: $params, query: $query }),
6868
};
6969

70-
$params.on([hatch.enter, hatch.update], (_, { params }) => params);
71-
$query.on([hatch.enter, hatch.update], (_, { query }) => query);
70+
$params.on([route.enter, route.update], (_, { params }) => params);
71+
$query.on([route.enter, route.update], (_, { query }) => query);
7272

73-
hatch.$opened.on(hatch.enter, () => Boolean(true)).reset(hatch.exit);
73+
route.$opened.on(route.enter, () => Boolean(true)).reset(route.exit);
7474
// Developer may want to read props when user leaves the page
75-
// if $opened store will reset on hatch.exit, data will be deleted
75+
// if $opened store will reset on route.exit, data will be deleted
7676

77-
return hatch;
77+
return route;
7878
}
7979

80-
export function withHatch<C extends React.ComponentType>(hatch: Hatch, component: C): C {
80+
export function withRoute<C extends React.ComponentType>(hatch: Route, component: C): C {
8181
// eslint-disable-next-line @typescript-eslint/no-explicit-any
82-
(component as any)[HATCH] = hatch;
82+
(component as any)[ROUTE_PROPERTY] = hatch;
8383
return component;
8484
}
8585

8686
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
export function getHatch<T extends React.ComponentType<any>>(component: T): Hatch | undefined {
87+
export function getRoute<T extends React.ComponentType<any>>(component: T): Route | undefined {
8888
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89-
return (component as any)[HATCH];
89+
return (component as any)[ROUTE_PROPERTY];
9090
}
9191

92-
export function lookupHatch<P>(match: MatchedRoute<P>): Hatch | undefined {
92+
export function lookupRoute<P>(match: MatchedRoute<P>): Route | undefined {
9393
if (match.route.component) {
9494
// eslint-disable-next-line @typescript-eslint/no-explicit-any
95-
return getHatch(match.route.component as any);
95+
return getRoute(match.route.component as any);
9696
}
9797
}

src/server-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createNavigation } from './navigation';
77
import { defaultDomain } from './default-domain';
88

99
export function createServerApplication(config: { routes: RouteConfig[]; domain?: Domain }) {
10-
const debug = createDebug('framework-server');
10+
const debug = createDebug('bypath/server');
1111
const domain = config.domain || defaultDomain;
1212
const navigation = createNavigation(domain, { emitHistory: false, trackRedirects: true });
1313

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React from 'react';
22
import { sample } from 'effector';
33

4-
import { createHatch, withHatch } from '../../../src';
4+
import { createRoute, withRoute } from '../../../src';
55
import { pageNameSet, root } from './internal';
66

7-
const hatch = createHatch(root);
7+
const route = createRoute(root);
88

9-
const PageAnother = withHatch(hatch, () => {
9+
const PageAnother = withRoute(route, () => {
1010
return <div>For example just another page</div>;
1111
});
1212

1313
export default PageAnother;
1414

1515
sample({
16-
source: hatch.enter,
16+
source: route.enter,
1717
fn: () => 'page-another',
1818
target: pageNameSet,
1919
});

tests/browser/fixtures/page1.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import React from 'react';
22
import { sample } from 'effector';
33

4-
import { createHatch, withHatch } from '../../../src';
4+
import { createRoute, withRoute } from '../../../src';
55
import { pageNameSet, root } from './internal';
66

7-
const hatch = createHatch(root);
7+
const route = createRoute(root);
88

9-
hatch.enter.watch((e) => console.info('PAGE1 Hatch Enter', e));
9+
route.enter.watch((e) => console.info('PAGE1 Hatch Enter', e));
1010

11-
const Page1 = withHatch(hatch, () => {
11+
const Page1 = withRoute(route, () => {
1212
return <div>For example first page</div>;
1313
});
1414

1515
export default Page1;
1616

1717
sample({
18-
source: hatch.enter,
18+
source: route.enter,
1919
fn: () => 'page1',
2020
target: pageNameSet,
2121
});

tests/browser/routing-with-chunks.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { allSettled, fork } from 'effector';
44
import { $pageName, root } from './fixtures/internal';
55
import { createBrowserApplication, loadable } from '../../src';
66

7-
test.skip('triggering hatch loaded from chunk', async () => {
7+
test.skip('triggering route loaded from chunk', async () => {
88
const routes = [
99
{
1010
path: '/page1',

0 commit comments

Comments
 (0)