-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor LitElement logic out into a superclass
- Loading branch information
Showing
4 changed files
with
94 additions
and
95 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { LitElement, PropertyValues } from "lit"; | ||
|
||
import { reverseMap } from "./utils"; | ||
|
||
export abstract class LitWidget< | ||
ModelType, | ||
SubclassType extends LitWidget<any, any> | ||
> extends LitElement { | ||
private _model: any | undefined = undefined; // AnyModel<ModelType> | ||
|
||
abstract modelNameToViewName(): Map< | ||
keyof ModelType, | ||
keyof SubclassType | null | ||
>; | ||
|
||
viewNameToModelName(): Map<keyof SubclassType | null, keyof ModelType> { | ||
return reverseMap(this.modelNameToViewName()); | ||
} | ||
|
||
set model(model: any) { | ||
// TODO(naschmitz): model should be of type AnyModel<ModelType>. AnyModel | ||
// requires a type that conforms to a non-exported member of anywidget. | ||
this._model = model; | ||
for (const [modelKey, widgetKey] of this.modelNameToViewName()) { | ||
if (widgetKey) { | ||
// Get initial values from the Python model. | ||
(this as any)[widgetKey] = model.get(modelKey); | ||
// Listen for updates to the model. | ||
model.on(`change:${String(modelKey)}`, () => { | ||
(this as any)[widgetKey] = model.get(modelKey); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
get model(): any { | ||
// TODO(naschmitz): model should be of type AnyModel<ModelType>. AnyModel | ||
// requires a type that conforms to a non-exported member of anywidget. | ||
return this._model; | ||
} | ||
|
||
updated(changedProperties: PropertyValues<SubclassType>): void { | ||
// Update the model properties so they're reflected in Python. | ||
const viewToModelMap = this.viewNameToModelName(); | ||
for (const [viewProp, _] of changedProperties) { | ||
const castViewProp = viewProp as keyof SubclassType; | ||
if (viewToModelMap.has(castViewProp)) { | ||
const modelProp = viewToModelMap.get(castViewProp); | ||
this._model?.set( | ||
modelProp as any, | ||
this[castViewProp as keyof this] as any | ||
); | ||
} | ||
} | ||
this._model?.save_changes(); | ||
} | ||
} |
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