Skip to content

Commit 2bb779e

Browse files
committed
fix: remove code of the chunk loading and fix the bug
1 parent b897d59 commit 2bb779e

3 files changed

Lines changed: 40 additions & 100 deletions

File tree

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const presets = ['@babel/preset-react', '@babel/preset-typescript'];
33
const common = {
44
presets,
55
plugins: [
6-
['effector/babel-plugin', { factories: ['src/contract'] }],
6+
['effector/babel-plugin', { factories: ['src/contract', 'src/logical'] }],
77
['@babel/plugin-proposal-class-properties', { loose: true }],
88
'@babel/plugin-proposal-object-rest-spread',
99
'@babel/plugin-proposal-optional-chaining',

src/browser-application.ts

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -58,122 +58,49 @@ export function createBrowserApplication(config: {
5858
const hatchUpdate = domain.createEvent<HatchParams>({ name: `hatchUpdate:${path}` });
5959
const hatchExit = domain.createEvent<void>({ name: `hatchExit:${path}` });
6060

61-
// Triggered when hatch is used from the main bundle
62-
const dontNeedLoadChunk = domain.createEvent({ name: `dontNeedLoadChunk:${path}` });
63-
64-
const $chunkLoaded = domain.createStore(false, { name: `$chunkLoaded:${path}` });
65-
const $hasHatch = domain.createStore(getHatch(component) !== undefined, {
66-
name: `$hasHatch:${path}`,
67-
});
68-
69-
const loadPageFx = domain.createEffect({
70-
name: `loadPageFx:${path}`,
71-
handler: async () => {
72-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
const loader = (component as any).load;
74-
if (typeof loader === 'function') {
75-
const module = await loader();
76-
if (!module.default) {
77-
console.info(`Not found default export for "${path}" route`);
78-
return null;
79-
}
80-
// eslint-disable-next-line @typescript-eslint/ban-types
81-
return module.default as {};
82-
}
83-
return component;
84-
},
85-
});
86-
87-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88-
const setupHatchLinksFx = domain.createEffect({
89-
name: `setupHatchLinksFx:${path}`,
90-
handler: (page: any) => {
91-
const hatch = getHatch(page);
92-
if (hatch) {
93-
forward({ from: hatchEnter, to: hatch.enter });
94-
forward({ from: hatchUpdate, to: hatch.update });
95-
forward({ from: hatchExit, to: hatch.exit });
96-
// Refork here?
97-
// Because computations still in the old scope, we can't switch to a new scope where page's events already forked
98-
// We need effector v22
99-
// https://share.effector.dev/HEXzaOU0
100-
return true;
101-
}
102-
return false;
103-
},
104-
});
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 });
66+
}
10567

10668
// Shows that user is on the route
107-
const $onRoute = domain
108-
.createStore(false, { name: `$onRoute:${path}` })
109-
.on(routeMatched, () => true)
110-
.on(notMatched, () => false);
111-
112-
// Shows that user visited route and wait for page
113-
// If true, page.hatch.enter is triggered and logic is ran
114-
const $onPage = domain
115-
.createStore(false, { name: `$onPage:${path}` })
116-
.on(hatchEnter, () => true)
117-
.on(hatchExit, () => false);
118-
119-
$chunkLoaded.on(loadPageFx.done, () => true).on(dontNeedLoadChunk, () => true);
120-
$hasHatch.on(setupHatchLinksFx.doneData, (_, has) => has);
121-
122-
// When hatch not found on component from route and chunk don't load before
123-
guard({
124-
source: routeMatched,
125-
filter: combine(
126-
$hasHatch,
127-
$chunkLoaded,
128-
(hasHatch, chunkLoaded) => !hasHatch && !chunkLoaded,
129-
),
130-
target: loadPageFx,
131-
});
69+
const $onRoute = domain.createStore(false, { name: `$onRoute:${path}` });
13270

133-
// After loading page chunk check that it has Page and try to connect with local events
134-
guard({
135-
source: loadPageFx.doneData,
136-
filter: (value) => value !== null,
137-
target: setupHatchLinksFx,
138-
});
139-
140-
loadPageFx.failData.watch((error) => {
141-
console.error(`Failed to load page for ${path}`, error);
142-
});
71+
// Shows that user visited route and waited for page
72+
// If true, page.hatch.enter is triggered and logic was run
73+
const $onPage = domain.createStore(false, { name: `$onPage:${path}` });
14374

144-
setupHatchLinksFx.failData.watch((error) => {
145-
console.error(`Failed to setup hatch links for ${path}`, error);
146-
});
75+
//#region route matched
76+
$onRoute.on(routeMatched, () => true);
14777

148-
// Hatch found on component from route, but chunk never loaded
149-
// We need to setup connections between hatch from component and local triggers
15078
guard({
151-
source: routeMatched,
152-
filter: combine($hasHatch, $chunkLoaded, (hasHatch, chunkLoaded) => hasHatch && !chunkLoaded),
153-
target: [setupHatchLinksFx.prepend(() => component), dontNeedLoadChunk],
79+
clock: routeMatched,
80+
filter: $onPage,
81+
target: hatchUpdate,
15482
});
15583

156-
// Trigger local unit only after loading chunk and setup connections
157-
// Set onPage = true
15884
guard({
159-
source: routeMatched,
160-
clock: setupHatchLinksFx.doneData,
161-
filter: $onRoute,
85+
clock: routeMatched,
86+
filter: combine($onPage, $onRoute, (page, route) => !page && route),
16287
target: hatchEnter,
16388
});
16489

165-
guard({
166-
source: routeMatched,
167-
filter: $onPage,
168-
target: hatchUpdate,
169-
});
90+
$onPage.on(hatchEnter, () => true);
91+
//#endregion route matched
92+
93+
//#region NOT matched
94+
$onRoute.on(notMatched, () => false);
17095

171-
// onPage = false should set only after exit logic is run
17296
guard({
173-
source: notMatched,
97+
clock: notMatched,
17498
filter: $onPage,
17599
target: hatchExit,
176100
});
101+
102+
$onPage.on(hatchExit, () => false);
103+
//#endregion NOT matched
177104
}
178105

179106
return { navigation };

src/logical.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Store, combine } from 'effector';
2+
3+
export function or(...stores: Array<Store<boolean>>): Store<boolean> {
4+
return combine(stores, (list) => list.reduce((all, current) => all || current));
5+
}
6+
7+
export function and(...stores: Array<Store<boolean>>): Store<boolean> {
8+
return combine(stores, (list) => list.reduce((all, current) => all && current));
9+
}
10+
11+
export function not(store: Store<boolean>): Store<boolean> {
12+
return store.map((is) => !is);
13+
}

0 commit comments

Comments
 (0)