Repository navigation
feat(plot): Convert Mosaic Plot to TypeScript - #1398
dangotbanned wants to merge 34 commits into
Conversation
| import type { SVG } from '../../types.js'; | ||
|
|
||
| function wrap(brush) { | ||
| function wrap(brush: BrushBehavior<unknown>) { |
There was a problem hiding this comment.
wrap is a very misleading name for what's happening here
It would make sense if the returned object was like:
result = {brush: BrushBehavior, ..extras}But patching methods onto the same object? 🤔
| @@ -2,14 +2,14 @@ | |||
| * Return the bound datum value on a Plot-generated SVG element. | |||
| * Following D3, bound data is assigned to the `__data__` property. | |||
There was a problem hiding this comment.
If D3 exposes the type being described - that should be used instead of Element
| // @ts-ignore | ||
| const node = this; | ||
| export function patchScreenCTM(): void { | ||
| const node: SVGGraphicsElement = this; |
There was a problem hiding this comment.
this is the singular hit for noImplicitThis
I fixed some of those in vgplot but this one looks different
| @@ -0,0 +1,9 @@ | |||
| import { toKebabCase } from './to-kebab-case.js'; | |||
|
|
|||
| export function sanitizeStyles(styles: Record<string, string | number>): Record<string, string | number> { | |||
There was a problem hiding this comment.
This function is called on things unrelated to styles
| export type EncodingName = ChannelName | 'tip' | ConstantOption | 'orderby'; // (uwdata/mosaic#266) | ||
| export type Encodings = { [name in EncodingName]?: any }; | ||
| export type Reqs = { [name in EncodingName]?: mc.Stat[] }; | ||
|
|
||
| export type Field = { stats?: { stats?: mc.Stat[], column?: mc.FieldRef; }; basis?: unknown; } | msql.ExprValue | msql.ColumnRefNode | msql.ColumnParamNode | msql.SQLNode; | ||
|
|
||
| /** `channels: Entry[]` is being used to describe every possible input that is not `data` or `type: MarkType`. | ||
| * | ||
| * For example, {@link markQuery} anticipates `channel: 'orderby'` - which is a property of a transform (`WindowOptions.orderby`). | ||
| */ | ||
| interface Entry { | ||
| channel: EncodingName; | ||
| value?: any; | ||
| field?: Field; | ||
| as?: string; | ||
| column?: mc.FieldInfo['column']; | ||
| type?: mc.FieldInfo['type']; | ||
| count?: mc.FieldInfo['count']; | ||
| min?: mc.FieldInfo['min']; | ||
| max?: mc.FieldInfo['max']; | ||
| sqlType?: mc.FieldInfo['sqlType']; | ||
| } |
There was a problem hiding this comment.
I'm just describing the current state of things - but I don't understand why they are.
- What is a channel?
- What is an encoding?
- What is a
field? - What is
reqs? - Why are
statsnested instats? - Why is a
columnavailable in multiple levels? - Even though
Entryonly requireschannel- why is that not required here?
There was a problem hiding this comment.
Had more time to think and here are some problems I've found.
Note
TL;DR: I think Mark.channels is wearing too many hats.
If they need to all to go on 1 head, it should not be Record<string, unknown>[]
Mark.prepare
This includes Mark._fieldInfo and Mark.channels, but the body of the method is where things get weird:
_fieldInfo represents a state change that happens when a subset Mark.channels[*] (an Entry with a field) is assigned FieldInfo results:
I'd been struggling to understand the motivation for that.
It isn't something you can represent in types and it isn't documented either.
Mark.constructor
However I now believe it is because Mark's constructor can set event listeners on an Entry.value.
So in this case, assigning to an existing object would prevent breaking the references that may have dependencies
The rest of the constructor is (mainly) about populating Mark.channels, where each Entry should actually be:
type EntryRevised = { channel: string, value: unknown; } | { channel: string, field: object; as: string; };EntryRevised is not a very useful type, but we can't narrow it further since there aren't any errors raised to define what invalid input looks like.
Mark.channels
Now we have a rough how?, what? (v1), what? (v2) for channels.
The stuff with Param.value is a guess at why?
The answer to where is channels used? is, kind of, everywhere.
Direct references are mostly scoped to methods I've mentioned + the (including subclasses):
Mark.query: for field-related things (mosaic-core,mosaic-sql)Mark.plotSpecs,Marks.markPlotSpec: for value and (resolved) field-related things (@observablehq/plotand DOM)
Beyond that though, a huge amount of usage comes from all of the utilities that arise from dealing with such a complex object.
We start with some seemingly harmless searching of an array:
Show 2 "search for something" functions
What's wrong here?
mosaic/packages/vgplot/plot/src/marks/Mark.ts
Lines 210 to 212 in ea79760
mosaic/packages/vgplot/plot/src/marks/Mark.ts
Lines 214 to 219 in ea79760
Well, channels: Entry[] started it's life as options: Record<string, any>.
That was keyed on Entry.channel, meaning we could represent this as a Map and avoid all the linear time searching
Okay, so perhaps sub-optimal, but there are bigger fish to fry right?
Well, the vg.plot thickens when more utilities start stacking on top - and we gets loops in loops in ...
Show 4 more "search for something" functions
mosaic/packages/vgplot/plot/src/interactors/util/get-field.ts
Lines 3 to 17 in ea79760
mosaic/packages/vgplot/plot/src/interactors/util/get-field.ts
Lines 19 to 21 in ea79760
mosaic/packages/vgplot/plot/src/interactors/util/get-field.ts
Lines 23 to 42 in ea79760
mosaic/packages/vgplot/plot/src/legend.ts
Lines 176 to 189 in ea79760
And then by the time we're multiple steps removed from a Mark, things begin looking quadratic:
Show more ...
mosaic/packages/vgplot/plot/src/marks/util/bin-expr.ts
Lines 12 to 18 in ea79760
mosaic/packages/vgplot/plot/src/marks/util/channel-scale.ts
Lines 6 to 11 in ea79760
mosaic/packages/vgplot/plot/src/plot-renderer.ts
Lines 106 to 134 in ea79760
mosaic/packages/vgplot/plot/src/interactors/Toggle.ts
Lines 31 to 42 in ea79760
All of this is screaming Map to me
| const rx = Array.from(this.xscale.range).slice().sort(asc); | ||
| const ry = Array.from(this.yscale.range).slice().sort(asc); |
There was a problem hiding this comment.
Here and a few other places I needed to add Array.from due to upstream typing:
- https://github.com/observablehq/plot/blob/2245188895e95ef2043f0ad12c2f900d6e48ef1a/src/scales.d.ts#L402
- https://github.com/observablehq/plot/blob/2245188895e95ef2043f0ad12c2f900d6e48ef1a/src/scales.d.ts#L386
Internally, it wouldn't make sense for Observable to be storing an Iterable - as they can't guarantee multiple passes are valid.
My guess is they've reused the permissive typing that describes what the use can pass but these accesses are on some normalized data structure (probably Array)
| // generate a faux mark to pass to an interactor | ||
| /** | ||
| * @import { InteractorMark } from './marks/Mark.js' | ||
| * @returns {InteractorMark | undefined} | ||
| */ | ||
| function interactorMark(legend) { | ||
| function interactorMark(legend: Legend): InteractorMark | undefined { | ||
| const { channel, plot } = legend; | ||
| const field = legend.field ?? findField(plot.marks, channel) ?? 'value'; | ||
| if (field) { | ||
| const f = { field }; | ||
| const f = { field, channel }; | ||
| return { | ||
| plot, | ||
| channelField: c => channel === c ? f : undefined, | ||
| isUnnested: () => false | ||
| channelField: (c) => channel === c ? f : undefined, | ||
| isUnnested: () => false, | ||
| channels: [], | ||
| query: () => null, | ||
| filterBy: undefined, | ||
| coordinator: undefined | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
InteractorMark causes a lot of yelling from the type checker.
On main, we have:
but that definition didn't reflect usage, and even what I've updated it to isn't enough:
mosaic/packages/vgplot/plot/src/marks/Mark.ts
Lines 82 to 96 in 9d15831
InteractorMark must be a MosaicClient for what is going on in Highlight:
I'm not sure why we are where we are - but Legend appears to be the source of all these errors:
ts-expect-error Argument of type 'InteractorMark' is not assignable to parameter of type 'MosaicClient'.
| import type * as mc from '@uwdata/mosaic-core'; | ||
|
|
||
| export interface LegendOptions { | ||
| as?: mc.Selection; | ||
| field?: string; | ||
| label?: string; | ||
| tickSize?: number; | ||
| marginTop?: number; | ||
| marginRight?: number; |
There was a problem hiding this comment.
I was planning to fill this with all of the (Mark and Interactor) *Options interfaces.
I still think it would be a good thing to aim for, but:
- Defining all of them wasn't required to fix every (non-strict) error
- I have an idea for reusing the definitions in Mosaic Spec without creating circular dependencies
Either way, LegendOptions was the only one I've used in vgplot - so that's all that I've exposed in this mini namespace 🙂
| @@ -1,6 +1,9 @@ | |||
| import * as Plot from '@observablehq/plot'; | |||
| import * as ObPlot from '@observablehq/plot'; | |||
There was a problem hiding this comment.
I'm not precious about the name ObPlot - I just thought Plot should be reserved for the class defined in this package 😅
| /**Observable Plot spec. */ | ||
| export type Spec = { [x: string]: any; marks?: ObPlot.Mark[]; }; | ||
|
|
||
| /**Observable plot element */ | ||
| export type SVG = (SVGSVGElement | HTMLElement) & ObPlot.Plot; |
There was a problem hiding this comment.
These two types are kinda limited by how plot-renderer is written.
Functions that mutate and return void can't be observed in a type system:
mosaic/packages/vgplot/plot/src/plot-renderer.ts
Lines 43 to 44 in 9d15831
These set a lot of things:
So by the time we get here something presumably has changed - but you would need to read the body of every function to know what that was:
However, if these functions could return an intersection - we might be able to model what changed in each signature?
The problems with `(Mark.)channels` are leaking everywhere Related #1360, #1398 (comment)
The problems with `(Mark.)channels` are leaking everywhere Related #1360, #1398 (comment)
9d15831 to
61d0a95
Compare
61d0a95 to
461c1dc
Compare
The problems with `(Mark.)channels` are leaking everywhere Related #1360, #1398 (comment)
461c1dc to
f89032d
Compare
The problems with `(Mark.)channels` are leaking everywhere Related #1360, #1398 (comment)
f490c24 to
ed2a136
Compare
Still need to export `types` too
Resolved around 100 errors!
(#1397) Found 479 errors in 45 files. Not planning on dealing with those now
> 479 errors in 45 files. Now reduced to these, where all are in `vgplot/plot` > 248 errors in 31 files.
a161635 to
ea79760
Compare
| let field: Field | undefined; | ||
| if ((field = f?.field)) { | ||
| fields.push(typeof field === 'object' && 'basis' in field ? field.basis : field); |
There was a problem hiding this comment.
I finally have an answer to what Field.basis is was
Per #91
Add
basisexpression property to aggregates/windows to identify an untransformed column for selection.
Which refers to two getters that no longer exist (1, 2).
Why do I care?
That is one of the causes for the following 5 errors:
Show errors
Which is displaying the type I needed to create to represent Mark.channels[*].field:
But this is just dead code 😓
(#1398 (comment)) is related - and would be helped by removing basis - but there are multiple other issues left
| queryResult(data: Table | Record<string, unknown>[]): this { | ||
| this.data = toDataColumns(data); |
There was a problem hiding this comment.
data is correct in terms of what it could support, but might be wider than we need since #1172
| //@ts-expect-error Property 'stats' does not exist on type 'string' | ||
| Object.assign(entry, lookup[field.stats?.column ?? field]); |
There was a problem hiding this comment.
In (#1398 (comment)) I focused on the assignment and state change as problems.
I thought lookup looked fishy too, but didn't understand why it was working until now.
What's going on?
The 1st pass in Mark.prepare builds a Map<unknown, Set<Stat>>.
That unknown is probably intended to be SQLNode or something narrower that describes one column.
(skipping some steps)
Those keys then end up over here (as column: string | ExprNode).
And then a FieldInfo.column is a string interpolation of that ...
mosaic/packages/mosaic/core/src/util/field-info.ts
Lines 87 to 89 in f0a2cfb
Which is the source for lookup's "keys":
mosaic/packages/vgplot/plot/src/marks/Mark.ts
Line 243 in ea79760
Finally, the index-access implicitly performs that ExprNode.toString() again for every field
Object.assign(entry, lookup[field.stats?.column ?? field]);🤨 🤨 🤨
IIUC, that means for this method - every field channel for a Mark will run through DuckDBCodeGenerator twice?
I can't wrap my head around why codegen is required here. If it is unavoidable; would that not be considered an expensive thing to want to avoid doing twice?
Closes #1388
Description
This PR adds enough typing to
vgplot/plotto get it to compile.As mentioned in (#1388 (comment)) there was a lot to fix 🫠
I have not aimed to fix every error, I'm not sure that's possible without it being a rewrite.
But this is a start and the issues which were already there are now more visible