Skip to content

Commit 12ef15b

Browse files
Add remove step
1 parent 6ca1909 commit 12ef15b

File tree

9 files changed

+142
-13
lines changed

9 files changed

+142
-13
lines changed

resources/tests.mjs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,45 @@ Suites.push({
294294
}
295295
await indexedDBCompletedPromise;
296296
}),
297-
// new BenchmarkTestStep("DeletingAllItems", (page) => {
298-
// const items = page.querySelectorAll("todo-item", ["todo-app", "todo-list"]);
299-
// for (let i = numberOfItemsToAdd - 1; i >= 0; i--) {
300-
// const item = items[i].querySelectorInShadowRoot(".remove-todo-button");
301-
// item.click();
302-
// }
303-
// }),
297+
new BenchmarkTestStep("DeletingAllItems", async (page) => {
298+
const numberOfItemsPerIteration = 10;
299+
const numberOfIterations = 10;
300+
page.setGlobalVariable("numberOfItemsToAdd", numberOfItemsToAdd);
301+
const indexedDBCompletedPromise = new Promise((resolve) => {
302+
page.addEventListener("indexeddb-remove-completed", () => {
303+
resolve();
304+
});
305+
});
306+
function iterationFinishedListener () {
307+
console.log("Listener listened");
308+
iterationFinishedListener.promiseResolve();
309+
}
310+
page.addEventListener('previous-page-loaded', iterationFinishedListener);
311+
for (let j=0; j < numberOfIterations; j++) {
312+
const iterationFinishedPromise = new Promise((resolve) => {
313+
iterationFinishedListener.promiseResolve = resolve;
314+
});
315+
const items = page.querySelectorAll("todo-item", ["todo-app", "todo-list"]);
316+
for (let i = numberOfItemsPerIteration - 1; i >= 0; i--) {
317+
const item = items[i].querySelectorInShadowRoot(".remove-todo-button");
318+
item.click();
319+
}
320+
// Let the layout update???
321+
// Give a change to the pending indexedDB operations to complete in main thread.
322+
await new Promise((resolve) => {
323+
setTimeout(() => {resolve();}, 0);
324+
});
325+
if (j<9) {
326+
const previousPageButton = page.querySelector(".previous-page-button", ["todo-app", "todo-bottombar"]);
327+
previousPageButton.click();
328+
console.log("Clicked previous page button");
329+
await iterationFinishedPromise;
330+
console.log("Promise resolved for previous page button");
331+
}
332+
}
333+
await indexedDBCompletedPromise;
334+
console.log("Finished deleting all items");
335+
}),
304336
],
305337
});
306338

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/dist/components/todo-app/todo-app.component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TodoApp extends HTMLElement {
6565
}
6666
this.#numberOfItems--;
6767
this.update("remove-item", event.detail.id);
68+
this.list.removeItem(event.detail.itemNumber);
6869
}
6970

7071
updateItem(event) {
@@ -86,7 +87,9 @@ class TodoApp extends HTMLElement {
8687
moveToPreviousPage() {
8788
// Skeleton implementation of previous page navigation
8889
this.list.moveToPreviousPage().then(() => {
90+
console.log("Moving to previous page");
8991
this.bottombar.reenablePreviousPageButton();
92+
window.dispatchEvent(new CustomEvent("previous-page-loaded", {}));
9093
});
9194
}
9295

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/dist/components/todo-bottombar/todo-bottombar.component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class TodoBottombar extends HTMLElement {
8888
}
8989

9090
MoveToPreviousPage() {
91+
console.log("Moving to previous page button clicked");
9192
this.element.querySelector(".previous-page-button").disabled = true;
9293
this.dispatchEvent(new CustomEvent("previous-page"));
9394
}
@@ -115,6 +116,7 @@ class TodoBottombar extends HTMLElement {
115116

116117
reenablePreviousPageButton() {
117118
this.element.querySelector(".previous-page-button").disabled = false;
119+
window.dispatchEvent(new CustomEvent("previous-page-button-enabled", {}));
118120
}
119121

120122
connectedCallback() {

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/dist/components/todo-item/todo-item.component.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ class TodoItem extends HTMLElement {
100100
}
101101

102102
removeItem() {
103-
// The todo-list keeps a reference to all elements and updates the list on removal.
104-
// (therefore the removal has to happen after the list is updated)
105103
this.dispatchEvent(
106104
new CustomEvent("remove-item", {
107-
detail: { completed: this.togglegetAtInput.checked},
105+
detail: { completed: this.toggleInput.checked, itemNumber: this.itemIndex },
108106
bubbles: true,
109107
})
110108
);

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/dist/components/todo-list/todo-list.component.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class IndexedDBManager {
1212
this.db = null;
1313
this.pendingAdditions = 0;
1414
this.totalItemsToggled = 0;
15+
this.totalItemsDeleted = 0;
1516
this.initDB().then(() => {
1617
const newDiv = document.createElement("div");
1718
newDiv.classList.add("indexeddb-ready");
@@ -221,6 +222,47 @@ class IndexedDBManager {
221222
};
222223
});
223224
}
225+
226+
removeTodo(itemNumber) {
227+
return new Promise((resolve, reject) => {
228+
// Ensure the database connection is established
229+
if (!this.db) {
230+
this.initDB()
231+
.then(() => this.removeTodo(itemNumber))
232+
.then(resolve)
233+
.catch(reject);
234+
return;
235+
}
236+
237+
// Access the todo item directly by its itemNumber (keyPath)
238+
const transaction = this.db.transaction(this.storeName, 'readwrite');
239+
const store = transaction.objectStore(this.storeName);
240+
241+
// Delete the todo item directly using its primary key (itemNumber)
242+
const deleteRequest = store.delete(itemNumber);
243+
244+
deleteRequest.onsuccess = () => {
245+
if (window.numberOfItemsToAdd && ++this.totalItemsDeleted === window.numberOfItemsToAdd) {
246+
window.dispatchEvent(new CustomEvent("indexeddb-remove-completed", {}));
247+
}
248+
console.log(`Total items deleted: ${this.totalItemsDeleted}`);
249+
console.log(`Todo item with itemNumber '${itemNumber}' deleted successfully`);
250+
resolve(itemNumber);
251+
};
252+
253+
deleteRequest.onerror = (event) => {
254+
console.error('Error deleting todo item:', event.target.error);
255+
reject(event.target.error);
256+
};
257+
258+
// Handle transaction errors
259+
transaction.onerror = (event) => {
260+
console.error('Transaction error while deleting todo:', event.target.error);
261+
reject(event.target.error);
262+
};
263+
264+
});
265+
}
224266
}
225267

226268
const MAX_ON_SCREEN_ITEMS = 10;
@@ -290,6 +332,10 @@ class TodoList extends HTMLElement {
290332
this.#addItemToStorage(itemIndex, id, title, priority, completed);
291333
}
292334

335+
removeItem(itemIndex) {
336+
this.storageManager.removeTodo(itemIndex);
337+
}
338+
293339
addItems(items) {
294340
items.forEach((entry) => this.addItem(entry));
295341
}

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/src/components/todo-app/todo-app.component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TodoApp extends HTMLElement {
6565
}
6666
this.#numberOfItems--;
6767
this.update("remove-item", event.detail.id);
68+
this.list.removeItem(event.detail.itemNumber);
6869
}
6970

7071
updateItem(event) {
@@ -86,7 +87,9 @@ class TodoApp extends HTMLElement {
8687
moveToPreviousPage() {
8788
// Skeleton implementation of previous page navigation
8889
this.list.moveToPreviousPage().then(() => {
90+
console.log("Moving to previous page");
8991
this.bottombar.reenablePreviousPageButton();
92+
window.dispatchEvent(new CustomEvent("previous-page-loaded", {}));
9093
});
9194
}
9295

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/src/components/todo-bottombar/todo-bottombar.component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class TodoBottombar extends HTMLElement {
8888
}
8989

9090
MoveToPreviousPage() {
91+
console.log("Moving to previous page button clicked");
9192
this.element.querySelector(".previous-page-button").disabled = true;
9293
this.dispatchEvent(new CustomEvent("previous-page"));
9394
}

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/src/components/todo-item/todo-item.component.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ class TodoItem extends HTMLElement {
100100
}
101101

102102
removeItem() {
103-
// The todo-list keeps a reference to all elements and updates the list on removal.
104-
// (therefore the removal has to happen after the list is updated)
105103
this.dispatchEvent(
106104
new CustomEvent("remove-item", {
107-
detail: { completed: this.togglegetAtInput.checked},
105+
detail: { completed: this.toggleInput.checked, itemNumber: this.itemIndex },
108106
bubbles: true,
109107
})
110108
);

resources/todomvc/vanilla-examples/javascript-wc-indexeddb/src/components/todo-list/todo-list.component.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class IndexedDBManager {
1212
this.db = null;
1313
this.pendingAdditions = 0;
1414
this.totalItemsToggled = 0;
15+
this.totalItemsDeleted = 0;
1516
this.initDB().then(() => {
1617
const newDiv = document.createElement("div");
1718
newDiv.classList.add("indexeddb-ready");
@@ -221,6 +222,47 @@ class IndexedDBManager {
221222
};
222223
});
223224
}
225+
226+
removeTodo(itemNumber) {
227+
return new Promise((resolve, reject) => {
228+
// Ensure the database connection is established
229+
if (!this.db) {
230+
this.initDB()
231+
.then(() => this.removeTodo(itemNumber))
232+
.then(resolve)
233+
.catch(reject);
234+
return;
235+
}
236+
237+
// Access the todo item directly by its itemNumber (keyPath)
238+
const transaction = this.db.transaction(this.storeName, 'readwrite');
239+
const store = transaction.objectStore(this.storeName);
240+
241+
// Delete the todo item directly using its primary key (itemNumber)
242+
const deleteRequest = store.delete(itemNumber);
243+
244+
deleteRequest.onsuccess = () => {
245+
if (window.numberOfItemsToAdd && ++this.totalItemsDeleted === window.numberOfItemsToAdd) {
246+
window.dispatchEvent(new CustomEvent("indexeddb-remove-completed", {}));
247+
}
248+
console.log(`Total items deleted: ${this.totalItemsDeleted}`);
249+
console.log(`Todo item with itemNumber '${itemNumber}' deleted successfully`);
250+
resolve(itemNumber);
251+
};
252+
253+
deleteRequest.onerror = (event) => {
254+
console.error('Error deleting todo item:', event.target.error);
255+
reject(event.target.error);
256+
};
257+
258+
// Handle transaction errors
259+
transaction.onerror = (event) => {
260+
console.error('Transaction error while deleting todo:', event.target.error);
261+
reject(event.target.error);
262+
};
263+
264+
});
265+
}
224266
}
225267

226268
const MAX_ON_SCREEN_ITEMS = 10;
@@ -290,6 +332,10 @@ class TodoList extends HTMLElement {
290332
this.#addItemToStorage(itemIndex, id, title, priority, completed);
291333
}
292334

335+
removeItem(itemIndex) {
336+
this.storageManager.removeTodo(itemIndex);
337+
}
338+
293339
addItems(items) {
294340
items.forEach((entry) => this.addItem(entry));
295341
}

0 commit comments

Comments
 (0)