Skip to content

Commit

Permalink
refactor(addon/components/paper-grid-list): refactors to use notifier…
Browse files Browse the repository at this point in the history
… pattern when children need to update.
  • Loading branch information
matthewhartstonge committed Nov 14, 2024
1 parent f6187dd commit 2fb155c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
11 changes: 7 additions & 4 deletions addon/components/paper-grid-list.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<md-grid-list class={{@class}} {{did-insert this.didInsertNode}} {{did-update this.didUpdateNode @cols @gutter @rowHeight}} ...attributes>
{{yield (hash
tile=(component "paper-grid-tile" parent=this)
)}}
<md-grid-list
class={{@class}}
{{did-insert this.didInsertNode}}
{{did-update this.didUpdateNode @cols @gutter @rowHeight}}
...attributes
>
{{yield (hash tile=(component 'paper-grid-tile' parent=this))}}
</md-grid-list>
16 changes: 13 additions & 3 deletions addon/components/paper-grid-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default class PaperGridList extends Component {
* @type {Set<PaperGridTile>}
*/
@tracked children;
/**
* Set of callbacks to notify children when they need to update their position.
* @type {Set<callback>}
*/
@tracked childrenNotifyUpdate;
/**
* Reference to the component's DOM element
* @type {HTMLElement}
Expand Down Expand Up @@ -90,6 +95,7 @@ export default class PaperGridList extends Component {
super(...arguments);

this.children = new Set();
this.childrenNotifyUpdate = new Set();
}

@action didInsertNode(element) {
Expand All @@ -109,18 +115,22 @@ export default class PaperGridList extends Component {
/**
* Registers a child tile component
* @param {PaperGridTile} tile - The tile component to register
* @param {callback} notifyUpdate - A callback to notify children on when they should update.
*/
@action registerChild(tile) {
@action registerChild(tile, notifyUpdate) {
this.children.add(tile);
this.childrenNotifyUpdate.add(notifyUpdate);
this.updateGrid();
}

/**
* Unregisters a child tile component
* @param {PaperGridTile} tile - The tile component to unregister
* @param {callback} notifyUpdate - The notify callback to remove.
*/
@action unregisterChild(tile) {
@action unregisterChild(tile, notifyUpdate) {
this.children.delete(tile);
this.childrenNotifyUpdate.delete(notifyUpdate);
this.updateGrid();
}

Expand Down Expand Up @@ -167,7 +177,7 @@ export default class PaperGridList extends Component {
// Debounce until the next frame
const updateGrid = () => {
applyStyles(this.element, this._gridStyle());
this.children.forEach((tile) => tile.updateTile());
this.childrenNotifyUpdate.forEach((notify) => notify());
if (this.args.onUpdate) {
this.args.onUpdate();
}
Expand Down
11 changes: 7 additions & 4 deletions addon/components/paper-grid-tile.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<md-grid-tile class={{@class}} {{did-insert this.didInsertNode}} {{did-update this.didUpdateNode @colspan @rowspan}} ...attributes>
<md-grid-tile
class={{@class}}
{{did-insert this.didInsertNode}}
{{did-update this.didUpdateNode @colspan @rowspan}}
...attributes
>
<figure>
{{yield (hash
footer=(component "paper-grid-tile-footer")
)}}
{{yield (hash footer=(component 'paper-grid-tile-footer'))}}
</figure>
</md-grid-tile>
4 changes: 2 additions & 2 deletions addon/components/paper-grid-tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class PaperGridTile extends Component {
@action didInsertNode(element) {
this.element = element;

this.args.parent.registerChild(this);
this.args.parent.registerChild(this, this.updateTile);
}

@action didUpdateNode() {
Expand All @@ -94,7 +94,7 @@ export default class PaperGridTile extends Component {
willDestroy() {
super.willDestroy(...arguments);

this.args.parent.unregisterChild(this);
this.args.parent.unregisterChild(this, this.updateTile);
}

/**
Expand Down

0 comments on commit 2fb155c

Please sign in to comment.