Skip to content

Add before lifecycle hook #324

Description

@brainkim

Summary

Add a before lifecycle hook that fires before a component re-renders. This enables early cancellation of in-flight async work that's about to become stale.

Motivation

When a component re-renders, any in-flight async operations from the previous render become stale. While Crank already discards stale render results (newer renders win), the async work itself continues running until completion - consuming bandwidth, CPU, and other resources unnecessarily.

A before hook would allow:

  • Early cancellation of fetch requests via AbortController
  • Cleanup of expensive computations before they complete
  • Integration with AbortSignal-based APIs

Possible API

async function *UserProfile({userId}) {
  let controller = new AbortController();

  for await (const {userId} of this) {
    this.before(() => {
      controller.abort();
      controller = new AbortController();
    });

    yield <div>Loading...</div>;

    const user = await fetchUser(userId, {signal: controller.signal});
    yield <div>{user.name}</div>;
  }
}

Open Questions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions