Skip to content

Commit

Permalink
wait for markers
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Aug 2, 2024
1 parent f7f7b78 commit e6ea3a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
48 changes: 45 additions & 3 deletions catalog/app/components/Assistant/Model/GlobalContext/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { Schema as S } from '@effect/schema'

import search from 'containers/Search/Route'
import * as ROUTES from 'constants/routes'
import { runtime } from 'utils/Effect'
import * as Log from 'utils/Logging'
import * as Nav from 'utils/Navigation'
import useConst from 'utils/useConstant'

import * as Content from '../Content'
import * as Context from '../Context'
import * as Tool from '../Tool'

const MODULE = 'GlobalContext/navigation'
Expand Down Expand Up @@ -112,7 +115,13 @@ type NavigableRoute = typeof NavigableRouteSchema.Type

type History = ReturnType<typeof RR.useHistory>

const navigate = (route: NavigableRoute, history: History) =>
const WAIT_TIMEOUT = Eff.Duration.seconds(30)

const navigate = (
route: NavigableRoute,
history: History,
markersChanges: Eff.Stream.Stream<Record<string, boolean>>,

Check warning on line 123 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L123

Added line #L123 was not covered by tests
) =>
Log.scoped({

Check warning on line 125 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L125

Added line #L125 was not covered by tests
name: `${MODULE}.navigate`,
enter: [`to: ${route.name}`, Log.br, 'params:', route.params],
Expand All @@ -123,6 +132,28 @@ const navigate = (route: NavigableRoute, history: History) =>
S.encode(routes[route.name].paramsSchema),
Eff.Effect.tap((loc) => Eff.Effect.log(`Navigating to location:`, Log.br, loc)),
Eff.Effect.andThen((loc) => Eff.Effect.sync(() => history.push(loc))),
Eff.Effect.andThen(() =>
Eff.Effect.gen(function* () {
const { waitForMarkers } = routes[route.name]

Check warning on line 137 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L133-L137

Added lines #L133 - L137 were not covered by tests
if (!waitForMarkers.length) return
yield* Eff.Effect.log(`Waiting for markers: ${waitForMarkers.join(', ')}`)
yield* Eff.pipe(

Check warning on line 140 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L139-L140

Added lines #L139 - L140 were not covered by tests
markersChanges,
Eff.Stream.timeoutFail(() => ({ _tag: 'timeout' as const }), WAIT_TIMEOUT),
Eff.Stream.runForEachWhile((markers) =>
Eff.Effect.succeed(!waitForMarkers.every((k) => markers[k])),

Check warning on line 144 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L142-L144

Added lines #L142 - L144 were not covered by tests
),
Eff.Effect.andThen(() => Eff.Effect.log('Markers found')),
Eff.Effect.catchTag('timeout', () =>
Eff.Effect.log(

Check warning on line 148 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L146-L148

Added lines #L146 - L148 were not covered by tests
`Timed out after ${Eff.Duration.format(
WAIT_TIMEOUT,
)} while waiting for markers`,
),
),
)
}),
),
),
)

Expand Down Expand Up @@ -203,13 +234,24 @@ export const NavigateSchema = S.Struct({
description: 'navigate to a provided route',
})

function useMarkersRef() {
const { markers } = Context.useAggregatedContext()
const ref = useConst(() => runtime.runSync(Eff.SubscriptionRef.make(markers)))
React.useEffect(() => {
runtime.runFork(Eff.SubscriptionRef.set(ref, markers))

Check warning on line 241 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L237-L241

Added lines #L237 - L241 were not covered by tests
}, [markers, ref])
return ref

Check warning on line 243 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L243

Added line #L243 was not covered by tests
}

export function useNavigate() {
const history = RR.useHistory()
const markers = useMarkersRef()

Check warning on line 248 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L247-L248

Added lines #L247 - L248 were not covered by tests

return Tool.useMakeTool(

Check warning on line 250 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L250

Added line #L250 was not covered by tests
NavigateSchema,
({ route }) =>
Eff.pipe(

Check warning on line 253 in catalog/app/components/Assistant/Model/GlobalContext/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/navigation.ts#L252-L253

Added lines #L252 - L253 were not covered by tests
navigate(route, history),
navigate(route, history, markers.changes),
Eff.Effect.match({
onSuccess: () =>
Tool.succeed(Content.text(`Navigating to the '${route.name}' route.`)),
Expand All @@ -220,6 +262,6 @@ export function useNavigate() {
}),
Eff.Effect.map(Eff.Option.some),
),
[history],
[history, markers],
)
}
1 change: 1 addition & 0 deletions catalog/app/containers/Search/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,5 @@ export default Nav.makeRoute({
path: routes.search.path,
description: 'Search page',
searchParams: RouteSearchParams,
waitForMarkers: ['searchResultsReady'],
})
3 changes: 3 additions & 0 deletions catalog/app/utils/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ interface RouteDescriptor<
name: Name
params: RouteSearchParams & RoutePathParams
}>
waitForMarkers: string[]
}

export function makeRoute<
Expand All @@ -178,6 +179,7 @@ export function makeRoute<
description: string
searchParams?: S.Schema<RouteSearchParams, typeof SearchParams.Type>
pathParams?: S.Schema<RoutePathParams, typeof PathParams.Type>
waitForMarkers?: string[]
}): RouteDescriptor<Name, Path, RouteSearchParams, RoutePathParams> {
// Location <-> ParsedLocation
const locationSchema = makeParsedLocation(input.path)
Expand Down Expand Up @@ -213,5 +215,6 @@ export function makeRoute<
paramsSchema,
navigableRouteSchema,
description: input.description,
waitForMarkers: input.waitForMarkers ?? [],
}
}

0 comments on commit e6ea3a2

Please sign in to comment.