Skip to content

Commit 8f3d17a

Browse files
committed
chore: refactor block property getter for styles to share a common implementation.
1 parent 9337e77 commit 8f3d17a

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
2525
};
2626

2727
export type Style = BlockClass | State | SubState;
28-
export type StyleContainer = Block | BlockClass | State | null;
28+
export type StyleContainer = Block | BlockClass | State;
2929

3030
/**
3131
* Abstract class that serves as the base for all Styles. Contains basic
@@ -40,6 +40,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
4040

4141
/** cache of resolveStyles() */
4242
private _resolvedStyles: Set<Style> | undefined;
43+
/** cache for the block getter. */
44+
private _block: Block | undefined;
4345

4446
/**
4547
* Save name, parent container, and create the PropertyContainer for this data object.
@@ -95,10 +97,20 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
9597
public abstract cssClass(opts: OptionsReader): string;
9698

9799
/**
98-
* Crawl up the container tree and return the base block object.
100+
* traverse parents and return the base block object.
99101
* @returns The base block in this container tree.
100102
*/
101-
public abstract get block(): Block;
103+
public get block(): Block {
104+
if (this._block !== undefined) {
105+
return this._block;
106+
}
107+
let p: StyleContainer = this.parent;
108+
if (isBlock(p)) {
109+
return this._block = p;
110+
} else {
111+
return this._block = p.block;
112+
}
113+
}
102114

103115
/**
104116
* Returns all the classes needed to represent this block object
@@ -178,6 +190,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
178190
return `${this.asSource()} => ${this.cssClasses(opts).map(n => `.${n}`).join(" ")}`;
179191
}
180192

193+
// TypeScript can't figure out that `this` is the `StyleType` so this private
194+
// method casts it in a few places where it's needed.
181195
private asStyle(): StyleType {
182196
return <StyleType><any>this;
183197
}
@@ -778,10 +792,6 @@ export class BlockClass extends BlockObject<BlockClass, Block> {
778792
super(name, parent);
779793
}
780794

781-
get block(): Block {
782-
return this.parent;
783-
}
784-
785795
get parent(): Block {
786796
return <Block>super.parent;
787797
}
@@ -1142,10 +1152,6 @@ export class State extends BlockObject<State, BlockClass> {
11421152
return this._subStates && this._subStates[name] || null;
11431153
}
11441154

1145-
get block(): Block {
1146-
return this.blockClass.block;
1147-
}
1148-
11491155
/**
11501156
* Retrieve the BlockClass that this state belongs to, if applicable.
11511157
* @returns The parent block class, or null.
@@ -1273,10 +1279,6 @@ export class SubState extends BlockObject<SubState, State> {
12731279
return undefined;
12741280
}
12751281

1276-
get block(): Block {
1277-
return this._container.block;
1278-
}
1279-
12801282
localName(): string {
12811283
return `${this._container.localName()}-${this.name}`;
12821284
}

0 commit comments

Comments
 (0)