RFC: ids and matching for views and widgets, without extending `Component`

Open
#11,154 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

This RFC centers on modules/core/src/lifecycle/component.ts, ViewManager, and the widget manager; it also cites #11114, #11135, #11115, and #11064 as the implementation PRs. Read the RFC recommendation and those PRs to understand the proposed shared id and matching contract. Done means the project has agreed which proposal to follow; the issue itself describes design choices rather than a bounded code change.

Written by the indexing model from the issue text.

Description

Should views and widgets extend the lifecycle Component, as #11136 proposes for widgets? This recaps what Component is, sets out the use case driving the question, weighs four proposals, and recommends a lighter shared model for both.

What Component is

modules/core/src/lifecycle/component.ts is the base class of Layer. It was split out of Layer in 2018 (#1815) to "generalize some prop creation code" for the v5.3 async-props RFC, with ComponentState following in #1779. In 2022, #6933 moved the remaining layer fields out, "to extract the props resolution logic out of the Layer class".

  • It was built for layers. Before #11136, no RFC or PR proposed using it elsewhere. Layer is still its only subclass, and it's exported only as the internal _Component (index.ts:157-160).
  • #11136 proposes it for widgets. The direction is that ids should work the same for layers and widgets, instead of two systems with their own corner cases, possibly with widgets inheriting from Component in v10.0. #11136 is a POC to see how that would look.
Features of Component, and which ones only layers use
Piece What it does Generic or layer-only
Async props and ComponentState URL, promise and stream loading per prop, stale-load dropping, onAsyncPropUpdated. The getters call internalState.hasAsyncProp. Layer-only in practice
data, extensions, SQL and Arrow accessors Special cases inside createProps and ComponentState. Layer-only
diffProps Data, update-trigger, extension and transition change flags. Layer-only
clone, count Copies async originals and SQL queries back out; a debug counter core never reads. Mostly layer-only
Defaults on a prototype Supplied props become own keys over a cached per-class prototype of defaults. The default id sits on that prototype, so hasOwn(props, 'id') tells an explicit id from the default. Generic
Frozen props Props are frozen after construction; mutating them throws. Generic
Prop types Every object default is parsed as a prop type, and compareProps diffs by each type's equal. Generic, with a trap: an object default with a type or value key is read as a prop-type definition (prop-types.ts:308-316)
Deprecated props Setters that log and forward to the new name. Generic

The matching and state transfer people associate with layers isn't in Component at all. LayerManager matches by id, and Layer._transferState moves internalState and state onto the new instance.

The use case

The React Fiber renderer (planned as the v10 @deck.gl/react) writes views, widgets and effects as JSX, the same way as layers. Two things follow for app authors:

  • They shouldn't need to write ids. The renderer gives an id-less element one from its React key path, so a keyed list just works (visgl/deck.gl-community#1077).
  • An explicit id must always win. Code refers to ids: initialViewState.main, a widget's viewId, a layerFilter. So the renderer has to tell an id the app passed from the class default, even when they're equal.

Layers already support both; views and widgets don't. Each manager also matches a little differently:

Layers Views Widgets
Default id Layer name displayName Per class (unique since #11116)
Explicit id detectable Yes No; resolved id is written into props No; defaults are merged into props
Matching By id By equals(): class and props By id only
On a match New instance takes over the old state Old instance kept Old instance takes the new props
Duplicate ids Warn Leaks controllers (#11067) Mounted twice (#11066, fix in #11064)

Views and widgets already keep their state across updates, because their managers keep the old instance and hand it the new props. Views keep initialize() state and controllers. What's missing for this use case is explicit-id detection and consistent matching. None of that requires a lifecycle.

Effects: what matters is their children

An effect doesn't need an id from the app. An app uses one LightingEffect that holds all its lights, which apply globally. PostProcessEffect stacks its shader module's passes and takes its id from the module. So effect ids aren't part of this discussion.

The open question is the elements inside an effect. A future fiber renderer could support more element types nested under an effect: each light as a child of the lighting effect, each pass as a child of a post-processing effect. Those children would form keyed lists, which is where ids and matching come back, and they could follow the same contract as views and widgets.

Proposals

A. Views and widgets extend Component (not recommended)

Widgets as in #11136, extended to views. #11136 also adds a widget lifecycle on top: the new instance with the same id becomes active and inherits state and the DOM root, through _transferState, setState and getCurrentWidget().

For

  • One model across layers, views and widgets.
  • Explicit-id detection comes for free from the props prototype.
  • For widgets, the instance the app holds becomes the live one.

Against

  • The layer-only pieces would need to be refactored out of Component first: async props, the data and SQL paths, and diffProps. Otherwise views and widgets carry them along. Either way, every object default would also be parsed as a prop type.
  • Views would break: OrbitView writes this.props after construction (orbit-view.ts:48), makeViewport spreads this.props and would lose the defaults on the prototype (view.ts:149), and x/width take numbers or strings.
  • Views gain nothing from a lifecycle; ViewManager already keeps the old instance and its controllers.

What #11136 would change for authors of custom widgets:

  1. Instance fields other than state are lost on each update.
  2. Handlers bound to this point at a stale instance; long-lived callbacks need getCurrentWidget().
  3. state, internalState, clone and count become reserved names.
  4. deck, rootElement and widgetManager become accessors over internalState. A subclass that declares one of them as a class field shadows the accessor under standard class-field semantics (plain JS, or TypeScript targeting ES2022 or later), so its value never moves with the transferred state.
  5. Moving a widget no longer calls onRemove and then onAdd.
  6. Object defaults are parsed as prop types.

It also departs from the widget RFC, which matches by "same constructor and the same id" and keeps the old instance (v8.0/ui-module-rfc.md:134-136); #11136 matches by id across classes and re-renders every replacement, even when props are equal.

B. A lighter shared base class (possible, later)

A new base, for example DescriptorComponent, that takes only the generic parts: the default id on a prototype, typed default resolution, and an optional transferState hook. No async props, prop-type parsing or layer cases. Layers keep the full Component.

For

  • Consistency without the layer-only pieces.
  • A natural home for a widget lifecycle, if we want one.

Against

  • A new public base class to document and keep stable.
  • Its transfer hook would only be used by widgets.
  • Subclassing is a heavier contract than the id goal needs.
C. The id PRs alone (in review)

Two stacked PRs, without touching the managers:

  • #11114 keeps the default view and widget id on a prototype through createPropsWithDefaultId, so Object.hasOwn(props, 'id') works as it does for layers. No other behavior changes, so it fits a 9.4 patch.
  • #11135, stacked on #11114, makes a widget treat a falsy id as the class default, as views already do. After it, view and widget ids resolve the same way. It changes behavior, so it targets 10.0.

For

  • Solves explicit-id detection for views and widgets with no new API.
  • View and widget ids resolve by the same rule.
  • No change for existing apps or custom classes.

Against

  • Matching still differs: views match with equals(), widgets by id alone, and duplicate widget ids still mount twice.
D. The id PRs plus one shared contract (recommended)

Keep #11114 and #11135, and give views and widgets the same small contract instead of a base class:

  • Ids: every class accepts an id in its constructor, keeps its default on a prototype through createPropsWithDefaultId, and exposes the resolved id.

  • One matcher shared by the view and widget managers:

    • The id is the identity, as for layers: one instance per id, with a warning on duplicates.
    • The type decides reuse or replace. Same type: the old instance takes the new props, as today. Different type: the old instance is finalized and the new one added. This matches React, which remounts when the element type at a key changes.
    • The type is the class default id, not the constructor: displayName for views, defaultProps.id for widgets (unique per class since #11116). It's a string literal, so it survives hot module reload and minification, and it's the same value #11114 keeps on the props prototype. A custom widget that doesn't set its own default id is treated as the same type as its parent class, which is today's behavior.

    For widgets, this is already open as #11115 (type by class default id) and #11064 (one instance per id); ViewManager would follow.

  • Lifecycle hooks called the same way everywhere: set up once when first matched, update on new props, tear down once on removal.

  • Optionally, frozen props: any of these proposals could freeze props, as Component does. Nothing depends on mutating them. Only the base Widget.setProps writes widget props (Object.assign(this.props, props)), and no widget in deck.gl or deck.gl-community writes this.props itself.

For

  • Meets the goal: no generated ids needed, and explicit ids always win.
  • Effect children, if they come, can use the same contract.
  • Uniform behavior across managers, which is the consistency #11136 is after.
  • None of Component's layer-only pieces, and no new base class.
  • Small, separate PRs; most of it is already open (#11114, #11135, #11115, #11064).

Against

  • Doesn't give widgets the layer-style lifecycle by itself.
  • Apps that swap widget types under one id now get a fresh instance instead of the old one with new props.
  • Unlike layers, a type change doesn't carry state over. Layers pass state to whatever new layer has the id (layer-manager.ts:413-429).

Recommendation

Don't extend Component for views or widgets. Its layer-only pieces (async props, data and SQL handling, diffProps) would first need to be refactored out of it, prop-type parsing of every default would still come along, and it would break existing views and custom widgets. Ids that work the same for layers and widgets, instead of two systems with their own corner cases, is the right goal. Proposal D reaches it without inheriting Component: the same default-id prototype and "an own id wins" rule as layers, plus one matcher shared by views and widgets that future effect children could use too.

  1. Now: land #11114 (default ids on a prototype, safe for 9.4), then #11135 on top for 10.0 (view and widget ids resolve the same way).
  2. Next: one matcher for the widget and view managers, built from #11115 (type by class default id) and #11064 (one instance per id).
  3. Separately: decide whether widgets should get a layer-style lifecycle at all. If yes, give widgets a small base for it (Proposal B, widgets only), rather than Component.
  4. Later, worth exploring: element types nested under effects, such as lights and passes, following the same contract.

Open questions

  • If the goal is ids that work the same as layers, does D's shared contract cover it? Or is a layer-style lifecycle for widgets (the new instance takes over the state) a goal in its own right?
  • Is the id as identity, with the class default id deciding reuse or replace, acceptable for widgets? #11136 matches by id alone.
  • For widget ids, the view rule (any falsy id uses the default, #11135) or the layer rule (whatever own value is passed is kept)?

References

  • #11136: POC, widgets as lifecycle Components, with dev-docs/RFCs/widget-component-lifecycle.md
  • #11114: default view and widget ids on a prototype
  • #11135: a falsy widget id uses the class default
  • #11115 and #11064: WidgetManager matching and duplicate ids
  • #11113: tracking issue for view and widget ids
  • #9670: view system improvements tracker

File and line references are at master 57a739942.

Dominant language
TypeScript
Stars
14.8k
Forks
2.3k
Avg merge
1d 9h
Merged PRs (30d)
230

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from visgl/deck.gl

All issues in visgl/deck.gl

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.