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
Summary
Add a
beforelifecycle 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
beforehook would allow:Possible API
Open Questions
this.before(fn)) vs other approaches?schedule,after,cleanup