feat(core): Add Deck#getView() method#9948
Conversation
| const viewState = getViewState(map); | ||
| const {views} = deck.props; | ||
| const view = | ||
| (views && flatten(views).find((v: {id: string}) => v.id === MAPBOX_VIEW_ID)) || |
There was a problem hiding this comment.
I thought the use of flatten was odd since I can't think of a case where you'd have nested arrays of views.. the type is
// view-manager.ts:18
export type ViewOrViews = View | View[] | null;
But, looks like ViewManager is likely just being defensive, or its an old pattern?
// view-manager.ts:268-269
private _setViews(views: View[]): void {
views = flatten(views, Boolean); // why are views flattened?
...
}
Either way, calling deck.getView() maintains the same behavior since view manager already normalized the list.
aecfd6a to
345dc2a
Compare
modules/mapbox/src/deck-utils.ts
Outdated
| const view = | ||
| (views && flatten(views).find((v: {id: string}) => v.id === MAPBOX_VIEW_ID)) || | ||
| getDefaultView(map); | ||
| const view = (deck.isInitialized && deck.getView(MAPBOX_VIEW_ID)) || getDefaultView(map); |
There was a problem hiding this comment.
Is adding isInitialized a change in behavior?
There was a problem hiding this comment.
It shouldn't be. That guards against assert(this.viewManager) in deck.getView(), and the fallback returns the same view that would have been found anyway unless a user is supplying their own custom view with the id mapbox.
There was a problem hiding this comment.
Hmm, actually digging in more to the custom view case, claude laid out a subtle behavior change:
Let me trace through the custom view scenario:
Original code:
const view = (views && flatten(views).find(...)) || getDefaultView(map);Before initialization, this searches
deck.props.viewsand WOULD find the user's custom view.New code:
const view = ((deck.isInitialized && deck.getView(MAPBOX_VIEW_ID)) || getDefaultView(map))Before initialization, this falls back to
getDefaultView(map)- NOT the user's custom view.So yes, there's a subtle difference: if a user provides a custom view with id
'mapbox', and the map renders before deck initializes, the cached viewport would be created from the default view instead of their custom view.However:
- This window is very brief (one frame at most)
afterRenderclears the cache once deck initializes- Subsequent frames use the correct custom view
We could preserve the original behavior by adding a fallback search of
deck.props.views:const view = (deck.isInitialized && deck.getView(MAPBOX_VIEW_ID)) || deck.props.views?.find?.(v => v.id === MAPBOX_VIEW_ID) || getDefaultView(map);
We could move the deck.isInitialized guard in drawLayer to the top, and then remove the guard within getViewport altogether. I pushed a commit with this change - It seems cleaner to do nothing until deck is initialized
345dc2a to
177393f
Compare
Add a public `getView(viewId)` method to the Deck class that returns a View by its id. This extracts the view lookup logic that was previously duplicated in places like MapboxOverlay's deck-utils. - Add `Deck#getView(viewId: string): View | undefined` - Update `getViewport` in deck-utils to use the new API - Add tests for single view and multiple views cases Closes #9709 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This ensures getViewport is only called when deck is initialized, which allows us to simplify getViewport by removing the redundant isInitialized check. The fallback to getDefaultView remains for safety but deck.getView() will now always find the configured view. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
73cb4e9 to
12a9e04
Compare
|
I've tested on basemap-browser and website examples. All looks good! |
* feat(core): Add Deck#getView() method Add a public `getView(viewId)` method to the Deck class that returns a View by its id. This extracts the view lookup logic that was previously duplicated in places like MapboxOverlay's deck-utils. - Add `Deck#getView(viewId: string): View | undefined` - Update `getViewport` in deck-utils to use the new API - Add tests for single view and multiple views cases Closes #9709 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor(mapbox): Move isInitialized check to top of draw functions This ensures getViewport is only called when deck is initialized, which allows us to simplify getViewport by removing the redundant isInitialized check. The fallback to getDefaultView remains for safety but deck.getView() will now always find the configured view. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Closes #9709
Summary
Deck#getView(viewId)method that returns a View by its idgetViewportin@deck.gl/mapboxdeck-utils to use the new API instead of manually searching throughdeck.props.viewsThis extracts the view lookup logic that was previously duplicated, making it a proper part of the Deck public API.
Test plan
modules/coreandmodules/mapboxDeck#getView with single view,Deck#getView with multiple views)🤖 Generated with Claude Code
Note
Low Risk
Small, additive API change with limited refactor in mapbox viewport selection; risk is mainly incorrect view resolution/regressions in mapbox overlays.
Overview
Adds a public view lookup API.
Decknow exposesgetView(viewId)(in addition togetViews) and documents it in the API reference.@deck.gl/mapboxswitches its viewport creation logic to usedeck.getView('mapbox')instead of manually searchingdeck.props.views, and reordersisInitializedguards to exit earlier indrawLayer/drawLayerGroup. New unit tests covergetViewfor single and multiple view configurations.Written by Cursor Bugbot for commit ba3a9b9. This will update automatically on new commits. Configure here.