Skip to content

Commit

Permalink
refactor: Implement debounce to improve performance (#46)
Browse files Browse the repository at this point in the history
* chore: implement debounce for input event listener

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>

* test: fix tests by awaiting the onFilterInput fn

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>

* chore: update README

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>

* chore: remove unused reject fn in promise

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>

* chore: making 500 the default delay for debounce

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>

---------

Signed-off-by: Juan Muñoz <juan.munoz@alliander.com>
  • Loading branch information
juancho0202 authored Oct 30, 2023
1 parent 287f1f2 commit d00f143
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ npm start
To run a local development server that serves the basic demo located in `demo/index.html`


## `src/OscdFilteredList.ts`:

### Exports

| Kind | Name | Declaration | Module | Package |
| --------------------------- | -------------------- | ---------------- | -------------------------- | ------- |
| `custom-element-definition` | `oscd-filtered-list` | OscdFilteredList | /src/oscd-filtered-list.js | |
| `js` | `OscdFilteredList` | OscdFilteredList | src/OscdFilteredList.ts | |

## `src/oscd-filtered-list.ts`:

### class: `OscdFilteredList`, `oscd-filtered-list`
Expand Down Expand Up @@ -115,9 +124,9 @@ To run a local development server that serves the basic demo located in `demo/in

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| --------------- | ------- | ----------- | ---------- | ------ | -------------- |
| `onFilterInput` | | | | `void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| --------------- | ------- | ----------- | ---------- | --------------- | -------------- |
| `onFilterInput` | public | | | `Promise<void>` | |

#### Events

Expand All @@ -143,14 +152,5 @@ To run a local development server that serves the basic demo located in `demo/in
| `js` | `redispatchEvent` | redispatchEvent | src/oscd-filtered-list.ts | |
| `js` | `OscdFilteredList` | OscdFilteredList | src/oscd-filtered-list.ts | |

## `src/OscdFilteredList.ts`:

### Exports

| Kind | Name | Declaration | Module | Package |
| --------------------------- | -------------------- | ---------------- | -------------------------- | ------- |
| `custom-element-definition` | `oscd-filtered-list` | OscdFilteredList | /src/oscd-filtered-list.js | |
| `js` | `OscdFilteredList` | OscdFilteredList | src/OscdFilteredList.ts | |


&copy; 2023 Alliander N.V.
25 changes: 19 additions & 6 deletions src/oscd-filtered-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ import { TextField } from '@material/mwc-textfield';
import '@material/mwc-list/mwc-list-item-base';
import { ListItem } from '@material/mwc-list/mwc-list-item';

function debounce(callback: () => void, delay = 500): Promise<void> {
let timeout: any;
return new Promise(resolve => {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback();
resolve();
}, delay);
});
}

function slotItem(item: Element): Element {
if (!item.closest('oscd-filtered-list') || !item.parentElement) return item;
if (item.parentElement instanceof OscdFilteredList) return item;
Expand Down Expand Up @@ -145,12 +156,14 @@ export class OscdFilteredList extends LitElement {
.forEach(item => (item.selected = select));
}

onFilterInput(): void {
Array.from(
this.querySelectorAll(
'mwc-list-item, mwc-check-list-item, mwc-radio-list-item'
)
).forEach(item => hideFiltered(item as ListItem, this.searchField.value));
public async onFilterInput(): Promise<void> {
return debounce(() => {
Array.from(
this.querySelectorAll(
'mwc-list-item, mwc-check-list-item, mwc-radio-list-item'
)
).forEach(item => hideFiltered(item as ListItem, this.searchField.value));
});
}

firstUpdated(): void {
Expand Down
20 changes: 10 additions & 10 deletions test/oscd-filtered-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('oscd-filtered-list', () => {
describe('allows to filter on', () => {
it('directly slotted mwc-check-list-item', async () => {
element.searchField.value = 'item1';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;

Expand All @@ -155,7 +155,7 @@ describe('oscd-filtered-list', () => {

it('directly slotted twoline mwc-check-list-item', async () => {
element.searchField.value = 'item2sec';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;

Expand All @@ -168,7 +168,7 @@ describe('oscd-filtered-list', () => {

it('uses space as logic AND ', async () => {
element.searchField.value = 'item item3sec';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand All @@ -180,7 +180,7 @@ describe('oscd-filtered-list', () => {

it('nested mwc-list-item elements', async () => {
element.searchField.value = 'nesteditem5';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand All @@ -192,7 +192,7 @@ describe('oscd-filtered-list', () => {

it('nested mwc-radio-list-item elements', async () => {
element.searchField.value = 'nesteditem6';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand All @@ -204,7 +204,7 @@ describe('oscd-filtered-list', () => {

it('items value attribute', async () => {
element.searchField.value = 'item7';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand All @@ -216,7 +216,7 @@ describe('oscd-filtered-list', () => {

it('allows filtering with a ? wildcard', async () => {
element.searchField.value = 'item?';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const hiddenItems = element.querySelectorAll('.hidden').length;
Expand All @@ -225,7 +225,7 @@ describe('oscd-filtered-list', () => {

it('allows filtering with a * wildcard', async () => {
element.searchField.value = 'te*sec';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const hiddenItems = element.querySelectorAll('.hidden').length;
Expand All @@ -239,7 +239,7 @@ describe('oscd-filtered-list', () => {

it('allows filtering with two ? wildcards', async () => {
element.searchField.value = 'nest??item';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand All @@ -253,7 +253,7 @@ describe('oscd-filtered-list', () => {

it('allows filtering with a * and ? wildcard', async () => {
element.searchField.value = 'n*tem?';
element.onFilterInput();
await element.onFilterInput();
element.requestUpdate();
await element.updateComplete;
const visibleItems =
Expand Down

0 comments on commit d00f143

Please sign in to comment.