Skip to content

Feature request: more lifecycle callbacks. #318

@yyny

Description

@yyny

Proposing:

  • this.afterElement(callback?) - like this.after(...), but only executes when the passed value (the element) changes (loose inequality), passing the new element. Can be called without arguments to return a Promise that awaits an element change (and returns the new element).

  • this.cleanupElement(callback?) - like this.after(...), but only executes when the passed value (the element) changes (loose inequality), passing the old element. The callback can also be called on this.cleanup(...) (but the callback only ever gets executed once). Can be called without arguments to return a Promise that awaits an element change (and returns the old element).

  • this.scheduleEach(callback) - like this.schedule(...), but executes the callback before each render.

  • this.afterEach(callback) - like this.after(...), but executes the callback after each render.

  • this.afterEachElement(callback) - like this.afterEach(...), but only executes when the passed value (the element) changes (loose inequality), passing the new element.

  • this.cleanupEachElement(callback) - like this.afterEach(...), but only executes when the the passed value (the element) changes (loose inequality), passing the old element. The callback also gets called on this.cleanup(...).

Note that the last four can just be implemented à la requestAnimationFrame, but they still save a few line of code and are a bit easier to read (plus it's nice to have symmetry in the API).

These proposed functions can be implemented in userspace with an injectHelpers(this) or something similar, but they can be hard to get right, and I think they are useful enough to be included in Crank itself.

afterEachElement and cleanupEachElement in particular are useful for managing element-based state like ResizeObserver more easily.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions