Skip to content

Commit

Permalink
bugfix: Add ability to reset mini
Browse files Browse the repository at this point in the history
  • Loading branch information
notthatjen committed Jun 8, 2024
1 parent e4d8595 commit 41c5756
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
15 changes: 15 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,17 @@ export class Entity {
)
}

setEachItemTemplate() {
if (this.element.dataset.childTemplate) return
this.element.dataset.childTemplate = this.element.innerHTML
}

getEachItemTemplate() {
const template = document.createElement('template')
template.innerHTML = this.element.dataset.childTemplate
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
9 changes: 9 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()
}

async _domReady() {
return new Promise((resolve) => {
if (document.readyState == 'loading') {
Expand Down Expand Up @@ -108,6 +114,9 @@ const MiniJS = (() => {
extendEvents: (events) => {
mini.extensions.events.extend(events)
},
reset: async () => {
await mini.reset()
},
}
})()

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

dispose() {
Array.from(this.entities.values()).forEach((entity) => entity.dispose())
this.entities.clear()
this.variables.clear()
this.entityVariables.clear()
this.shouldUpdate = false
}
}

0 comments on commit 41c5756

Please sign in to comment.