Skip to content

Commit 8cfbdac

Browse files
committed
Added hook updateBeforeUpdate
1 parent a16967a commit 8cfbdac

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-element",
3-
"version": "0.0.225",
3+
"version": "0.0.228",
44
"description": "",
55
"license": "LGPL-3.0",
66
"main": "./index.js",

src/main/js-element-hooks.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,31 @@ export const useAfterMount = hook(
309309

310310
// === useBeforeUpdate ===================================================
311311

312-
export const useBeforeMount = hook(
312+
export const useBeforeUpdate = hook(
313313
'useBeforeUpdate',
314314
function (action: () => void | undefined | null | (() => void)) {
315315
let cleanup: Task | null | undefined | void
316316
const c = currentCtrl!
317317

318+
c.beforeUpdate(() => {
319+
cleanup && cleanup()
320+
cleanup = action()
321+
})
322+
323+
c.beforeUnmount(() => {
324+
cleanup && cleanup()
325+
})
326+
}
327+
)
328+
329+
// === useBeforeMount ====================================================
330+
331+
export const useBeforeMount = hook(
332+
'useBeforeMount',
333+
function (action: () => void | undefined | null | (() => void)) {
334+
let cleanup: Task | null | undefined | void
335+
const c = currentCtrl!
336+
318337
c.beforeMount(() => {
319338
cleanup = action()
320339
})

src/main/js-element.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class BaseElement extends HTMLElement {
280280
try {
281281
onceBeforeUpdateNotifier.notify()
282282
} finally {
283-
onceBeforeMountNotifier.clear()
283+
onceBeforeUpdateNotifier.clear()
284284
}
285285

286286
beforeUpdateNotifier.notify()
@@ -324,6 +324,7 @@ class BaseElement extends HTMLElement {
324324
if (!rendered) {
325325
addPropHandling(this)
326326
onceBeforeMountNotifier.notify()
327+
onceBeforeMountNotifier.close()
327328
}
328329

329330
beforeMountNotifier.notify()
@@ -457,13 +458,24 @@ function propNameToAttrName(propName: string) {
457458
}
458459

459460
function createNotifier() {
460-
const subscribers: (() => void)[] = []
461+
let subscribers: (() => void)[] | null = []
461462

462463
return {
463-
subscribe: (subscriber: () => void) => void subscribers.push(subscriber),
464-
notify: () =>
465-
void (subscribers.length && subscribers.forEach((it) => it())),
466-
clear: () => (subscribers.length = 0)
464+
subscribe(subscriber: () => void) {
465+
subscribers && subscribers.push(subscriber)
466+
},
467+
468+
notify() {
469+
subscribers && subscribers.length && subscribers.forEach((it) => it())
470+
},
471+
472+
clear() {
473+
subscribers && (subscribers.length = 0)
474+
},
475+
476+
close() {
477+
subscribers = null
478+
}
467479
}
468480
}
469481

0 commit comments

Comments
 (0)