Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Add ability to reset mini #25

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class Entity {
return document.documentElement.contains(this.element)
}

isEachEl() {
return this.element.hasAttribute(':each')
}

isInsideEachEl() {
if (this.element.hasAttribute(':each')) return false
if (this.element.hasAttribute(':each.item')) return true
Expand Down Expand Up @@ -80,6 +84,20 @@ export class Entity {
)
}

setEachItemTemplate() {
if (this.getEachItemTemplate()) return
const template = document.createElement('template')
template.innerHTML = this.element.innerHTML
template.id = "ChildTemplate:" + this.id
document.body.appendChild(template)
this.element.dataset.templateId = template.id
}

getEachItemTemplate() {
const template = document.getElementById(this.element.dataset.templateId)
return template?.content
}

getClosestEl(attr) {
attr = attr.replaceAll(':', '\\:').replaceAll('.', '\\.')

Expand Down
3 changes: 2 additions & 1 deletion lib/entity/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ export class Attributes {

try {
const items = await this._interpret(iterable)
this.childClone ||= this.entity.element.cloneNode(true).children
this.entity.setEachItemTemplate()
this.childClone ||= this.entity.getEachItemTemplate().children

this.entity.element.innerHTML = ''

Expand Down
16 changes: 16 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export class Mini {
console.log(`MiniJS took ${executionTime}ms to run.`)
}

async reset() {
this.observer.disconnect()
this.state.dispose()
await this.init()
}
Comment on lines +45 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure error handling for the init method in the reset function.

- await this.init()
+ try {
+   await this.init()
+ } catch (error) {
+   console.error('Error during re-initialization:', error);
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async reset() {
this.observer.disconnect()
this.state.dispose()
await this.init()
}
async reset() {
this.observer.disconnect()
this.state.dispose()
try {
await this.init()
} catch (error) {
console.error('Error during re-initialization:', error);
}
}


async _domReady() {
return new Promise((resolve) => {
if (document.readyState == 'loading') {
Expand Down Expand Up @@ -86,6 +92,13 @@ const MiniJS = (() => {
console.error('Error initializing MiniJS:', error)
})

// Reset MiniJS when the user navigates back
window.addEventListener('popstate', () => {
mini.reset().catch((error) => {
console.error('Error resetting MiniJS after navigation:', error);
});
});

return {
get debug() {
return Mini.debug
Expand All @@ -108,6 +121,9 @@ const MiniJS = (() => {
extendEvents: (events) => {
mini.extensions.events.extend(events)
},
reset: async () => {
await mini.reset()
}
}
})()

Expand Down
7 changes: 7 additions & 0 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,11 @@ export class State {
const key = `${entityID}.${variable}`
this.entityVariables.delete(key)
}

dispose() {
this.shouldUpdate = false
this.entities.clear()
this.entityVariables.clear()
this.variables.clear()
}
}
Loading