feat(data): optional preload scheduler for AsyncDataService.createLazy - #171
Open
kunalkindra wants to merge 3 commits into
Open
feat(data): optional preload scheduler for AsyncDataService.createLazy#171kunalkindra wants to merge 3 commits into
kunalkindra wants to merge 3 commits into
Conversation
A lazy service loads only when its first property is touched. That keeps heavy imports out of the initial bundle, but the first send/read/call pays for the load — and when that first touch happens right before the page tears down (e.g. an analytics event fired immediately before navigation), the load can lose the race and the call is dropped. Add an optional third `options` arg with a `preload` scheduler. It is invoked once per instance with a `warm` callback that eagerly triggers the real load. The caller owns the policy (when to warm — idle, eager, after first input); createLazy owns the mechanism (invoking `load`, memoizing). `warm` is idempotent and dedupes with the first real property access. Fully backward compatible: omitting `options` keeps the existing load-on-first-touch behavior. The framework stays environment-agnostic — browser concerns like requestIdleCallback live in the caller's scheduler. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Documents contributing from a personal GitHub identity when a corporate/EMU account can't fork or push to the public repo, using ~/.ssh/config host aliases with IdentitiesOnly. Requested during review of the createLazy preload PR. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Ships a ready-made preload scheduler so callers of createLazy's preload option
don't have to hand-roll (and get subtly wrong) the requestIdleCallback glue.
AsyncDataService.preloadWhenIdle warms a lazy service at browser idle via
requestIdleCallback (setTimeout fallback for older Safari), runs once, never
throws, and is a no-op outside a browser.
Exported under the AsyncDataService namespace; createLazy's core stays
environment-agnostic since the browser API lives only in this opt-in helper.
Usage: createLazy(load, props, { preload: AsyncDataService.preloadWhenIdle }).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an optional third
optionsargument toAsyncDataService.createLazywith apreloadscheduler:It is invoked once when the factory creates a lazy service instance, receiving a
warmcallback that eagerly triggers the real load — constructing the service before its first property is touched. Fully backward compatible: omitoptions(oroptions.preload) and the existing load-on-first-touch behavior is unchanged.Why
A lazy service loads only when its first property is accessed. That keeps heavy imports out of the initial bundle, but the very first
send/ read / call pays for the load — and when that first touch happens right before the page tears down (e.g. an analytics event fired immediately before a navigation), the load can lose the race and the call is dropped.preloadcloses that gap without giving up laziness. The caller owns the policy (when to warm — at browser idle, eagerly, after first input), whilecreateLazyowns the mechanism (invokingloadand memoizing). Because loading is idempotent,warmis safe to call any number of times and dedupes with the first real property access. The framework stays environment-agnostic — browser concerns likerequestIdleCallbacklive in the caller's scheduler:Tests
Five new cases in
create-lazy.test.ts: no-load without the option, scheduler invoked withwarmon instance creation,warmloads before any property touch,warmdedupes with the first real touch, queued calls still drain after a preload.create-lazy.mddocuments the option. Localpnpm typecheck,pnpm lint,check:workspace, and the full data-package test suite (44/44) pass.