Skip to content

Commit 5b28b71

Browse files
committed
chore: DRY up the base getter for Style types by implementing it on BlockObject.
1 parent 8f3d17a commit 5b28b71

File tree

1 file changed

+41
-61
lines changed

1 file changed

+41
-61
lines changed

packages/css-blocks/src/Block/Block.ts

+41-61
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
4242
private _resolvedStyles: Set<Style> | undefined;
4343
/** cache for the block getter. */
4444
private _block: Block | undefined;
45+
/** cache for the block getter. */
46+
private _base: StyleType | null;
4547

4648
/**
4749
* Save name, parent container, and create the PropertyContainer for this data object.
@@ -67,10 +69,36 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
6769
}
6870

6971
/**
70-
* Get the inherited block object for this block object of the same name and type.
71-
* @returns The base inherited block object.
72+
* Given a parent that is a base class of this style, retrieve this style's
73+
* base style from it, if it exists. This method does not traverse into base styles.
7274
*/
73-
public abstract get base(): StyleType | undefined;
75+
protected abstract getBaseFromBaseParent(baseParent: ContainerType): StyleType | undefined;
76+
77+
/**
78+
* Get the style that this style inherits from, if any.
79+
*
80+
* This walks down the declared styles of the parent's inheritance chain,
81+
* and attempts to find a matching directly declared style on each.
82+
*
83+
* The result is cached because it never changes and is decidable as soon
84+
* as the style is instantiated.
85+
*/
86+
public get base(): StyleType | undefined {
87+
if (this._base !== undefined) {
88+
return this._base || undefined;
89+
}
90+
let baseParent: ContainerType | undefined = <ContainerType | undefined>this.parent.base;
91+
while (baseParent) {
92+
let cls = this.getBaseFromBaseParent(baseParent);
93+
if (cls) {
94+
this._base = cls;
95+
return cls;
96+
}
97+
baseParent = <ContainerType | undefined>baseParent.base;
98+
}
99+
this._base = null;
100+
return undefined;
101+
}
74102

75103
/**
76104
* Return the local identifier for this `Style`.
@@ -309,6 +337,9 @@ export class Block
309337
return new Array(...this._dependencies);
310338
}
311339

340+
/**
341+
* Get the block that this block inherits from.
342+
*/
312343
get base(): Block | undefined {
313344
return this._base;
314345
}
@@ -339,7 +370,7 @@ export class Block
339370
}
340371

341372
/**
342-
* Validate that this block implements all foreign selectors from blocks it impelemnts.
373+
* Validate that this block implements all foreign selectors from blocks it implements.
343374
* @param b The block to check implementation against.
344375
* @returns The Styles from b that are missing in the block.
345376
*/
@@ -779,7 +810,6 @@ export type StyleParent = Block | BlockClass | State | undefined;
779810
* Represents a Class present in the Block.
780811
*/
781812
export class BlockClass extends BlockObject<BlockClass, Block> {
782-
private _base: BlockClass | null | undefined;
783813
private _sourceAttribute: Attribute;
784814
private _states: StateMap = {};
785815

@@ -792,29 +822,12 @@ export class BlockClass extends BlockObject<BlockClass, Block> {
792822
super(name, parent);
793823
}
794824

795-
get parent(): Block {
796-
return <Block>super.parent;
797-
}
798-
799825
get isRoot(): boolean {
800826
return this.name === "root";
801827
}
802828

803-
get base(): BlockClass | undefined {
804-
if (this._base !== undefined) {
805-
return this._base || undefined;
806-
}
807-
let base = this.block.base;
808-
while (base) {
809-
let cls = base.getClass(this.name);
810-
if (cls) {
811-
this._base = cls;
812-
return cls;
813-
}
814-
base = base.base;
815-
}
816-
this._base = null;
817-
return undefined;
829+
protected getBaseFromBaseParent(baseParent: Block): BlockClass | undefined {
830+
return baseParent.getClass(this.name);
818831
}
819832

820833
localName(): string {
@@ -1073,7 +1086,6 @@ export class State extends BlockObject<State, BlockClass> {
10731086
isGlobal = false;
10741087

10751088
private _hasSubStates: boolean;
1076-
private _base: State | null | undefined;
10771089
private _subStates: undefined | ObjectDictionary<SubState>;
10781090
private _sourceAttributes: Attr[];
10791091

@@ -1226,24 +1238,8 @@ export class State extends BlockObject<State, BlockClass> {
12261238
}
12271239
}
12281240

1229-
/**
1230-
* Get the state that this state inherits from.
1231-
*/
1232-
get base(): State | undefined {
1233-
if (this._base !== undefined) {
1234-
return this._base || undefined;
1235-
}
1236-
let baseClass: BlockClass | undefined = this._container.base;
1237-
while (baseClass) {
1238-
let state = baseClass.getState(this._name);
1239-
if (state) {
1240-
this._base = state;
1241-
return state;
1242-
}
1243-
baseClass = baseClass.base;
1244-
}
1245-
this._base = null;
1246-
return undefined;
1241+
getBaseFromBaseParent(baseParent: BlockClass): State | undefined {
1242+
return baseParent.getState(this._name) || undefined;
12471243
}
12481244

12491245
/**
@@ -1253,30 +1249,14 @@ export class State extends BlockObject<State, BlockClass> {
12531249
all(): Style[] {
12541250
return [this];
12551251
}
1256-
12571252
}
12581253

12591254
export class SubState extends BlockObject<SubState, State> {
1260-
private _base: SubState | null | undefined;
1261-
12621255
_sourceAttributes: Array<AttributeNS>;
12631256
isGlobal = false;
12641257

1265-
get base(): SubState | undefined {
1266-
if (this._base !== undefined) {
1267-
return this._base || undefined;
1268-
}
1269-
let baseState: State | undefined = this._container.base;
1270-
while (baseState) {
1271-
let subState = baseState.getSubState(this._name);
1272-
if (subState) {
1273-
this._base = subState;
1274-
return subState;
1275-
}
1276-
baseState = baseState.base;
1277-
}
1278-
this._base = null;
1279-
return undefined;
1258+
getBaseFromBaseParent(baseParent: State): SubState | undefined {
1259+
return baseParent.getSubState(this._name) || undefined;
12801260
}
12811261

12821262
localName(): string {

0 commit comments

Comments
 (0)