Last active
October 10, 2023 17:00
-
-
Save NazariiShvets/4daa17991c0a1c878270810ef2ba1478 to your computer and use it in GitHub Desktop.
chainRoute with feature flags?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Kind, RouteInstance, RouteParams } from 'atomic-router'; | |
import { attach, combine, merge, sample, Store } from 'effector'; | |
import { not } from 'patronum'; | |
function mergeChained<TRouteParams extends RouteParams = {}>( | |
$condition: Store<boolean>, | |
route1: RouteInstance<TRouteParams>, | |
route2: RouteInstance<TRouteParams> | |
) { | |
const resultRoute: RouteInstance<TRouteParams> = { | |
open: attach({ | |
source: $condition, | |
effect: (condition, params) => | |
condition ? route1.open(params) : route2.open(params) | |
}), | |
navigate: attach({ | |
source: $condition, | |
effect: (condition, params) => | |
condition ? route1.navigate(params) : route2.navigate(params) | |
}), | |
$isOpened: combine( | |
$condition, | |
route1.$isOpened, | |
route2.$isOpened, | |
(condition, left, right) => (condition ? left : right) | |
), | |
$params: combine( | |
$condition, | |
route1.$params, | |
route2.$params, | |
(condition, left, right) => (condition ? left : right) | |
), | |
$query: combine( | |
$condition, | |
route1.$query, | |
route2.$query, | |
(condition, left, right) => (condition ? left : right) | |
), | |
opened: merge([ | |
sample({ clock: route1.opened, filter: $condition }), | |
sample({ clock: route2.opened, filter: not($condition) }) | |
]), | |
updated: merge([ | |
sample({ clock: route1.updated, filter: $condition }), | |
sample({ clock: route2.updated, filter: not($condition) }) | |
]), | |
closed: merge([ | |
sample({ clock: route1.closed, filter: $condition }), | |
sample({ clock: route2.closed, filter: not($condition) }) | |
]), | |
kind: Kind.ROUTE, | |
//@ts-expect-error https://github.com/atomic-router/atomic-router/blob/a0c4b2a254297b889ba1e16fada0a3bd5de2b4d3/src/methods/create-route.ts#L94 | |
settings: { | |
derived: true | |
} | |
}; | |
return resultRoute; | |
} | |
export { mergeChained }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Kind, RouteInstance, RouteParams } from 'atomic-router'; | |
import { attach, combine, sample, Store } from 'effector'; | |
function prepareConditionalRouteForChain<TRouteParams extends RouteParams = {}>( | |
$condition: Store<boolean>, | |
route: RouteInstance<TRouteParams> | |
) { | |
const stableEmptyObjectReference = {} as TRouteParams; | |
const conditionedRoute: RouteInstance<TRouteParams> = { | |
open: attach({ | |
source: $condition, | |
effect: (condition, params) => { | |
if (!condition) throw new Error('Condition for this route is disabled'); | |
return route.open(params); | |
} | |
}), | |
navigate: attach({ | |
source: $condition, | |
effect: (condition, params) => { | |
if (!condition) throw new Error('Condition for this route is disabled'); | |
return route.navigate(params); | |
} | |
}), | |
$isOpened: combine($condition, route.$isOpened, (condition, isOpened) => | |
condition ? isOpened : false | |
), | |
$params: combine($condition, route.$params, (condition, params) => | |
condition ? params : stableEmptyObjectReference | |
), | |
$query: combine($condition, route.$params, (condition, params) => | |
condition ? params : stableEmptyObjectReference | |
), | |
opened: sample({ clock: route.opened, filter: $condition }), | |
updated: sample({ clock: route.updated, filter: $condition }), | |
closed: sample({ clock: route.closed, filter: $condition }), | |
kind: Kind.ROUTE, | |
//@ts-expect-error https://github.com/atomic-router/atomic-router/blob/a0c4b2a254297b889ba1e16fada0a3bd5de2b4d3/src/methods/create-route.ts#L94 | |
settings: { | |
derived: true | |
} | |
}; | |
return conditionedRoute; | |
} | |
export { prepareConditionalRouteForChain }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment