@wonderlandlabs-pixi-ux/utils
v1.2.5
Published
Shared utilities for wonderlandlabs-pixi-ux packages
Readme
@wonderlandlabs-pixi-ux/utils
Shared utility helpers for the @wonderlandlabs-pixi-ux/* packages.
getSharedRenderHelper(app, options?)
Returns an app-scoped shared render helper from an internal WeakMap, creating it on first access.
- Key:
appobject - Value: one shared render helper for that app
- Cache rule: first config wins per app key
- If
getSharedRenderHelper(app, { throttleMs: 30 })is called first, later calls with different values for the sameappkeep using30.
- If
This is intended for boot-time setup where many package consumers (drag/zoom/etc.) should share one throttle stream and one render policy.
import { getSharedRenderHelper } from '@wonderlandlabs-pixi-ux/utils';
// App boot: establish the shared policy once.
const sharedRender = getSharedRenderHelper(app, { throttleMs: 30 });
// Later, other modules can fetch the same shared helper.
const sameSharedRender = getSharedRenderHelper(app);
sameSharedRender.request();Singleton and lifetime rules:
- The first call to
getSharedRenderHelper(app, options?)for a givenappdetermines the helper configuration. - The shared helper lives for the lifetime of that
appand auto-cleans up whenapp.destroy(...)is called. - Any later call for that same
appreturns the original helper instance with that original timing/config profile. Create or retrieve your shared helper during app boot with your desired config.
Notes:
destroy()on a shared helper is intentionally a no-op so one consumer cannot tear down rendering for others.- Shared helper internals are torn down automatically when
app.destroy(...)is called. - The cache uses
WeakMap, so entries are eligible for GC when the app object is no longer referenced.
createRenderHelper(app, options?)
Creates a throttled render helper for apps that expose render().
Options:
throttleMs(default30): throttle window in milliseconds forrequest().leading(defaulttrue): if true, first request in a window renders immediately.trailing(defaultfalse): if true, emits one final render at the end of the window when additional requests arrive.
import { createRenderHelper } from '@wonderlandlabs-pixi-ux/utils';
const helper = createRenderHelper(app, {
throttleMs: 30,
trailing: true,
});
helper.request(); // throttled render
helper.now(); // immediate render
helper.destroy(); // cleanup queued trailing renderTiming behavior:
request()sends a render pulse into a throttled stream (Subject+throttleTime).- With defaults (
leading: true,trailing: false), rapid calls render at most once perthrottleMswindow, and intermediate requests are dropped. now()always callsapp.render()immediately and does not wait for throttle timing.destroy()unsubscribes and completes the stream so no further throttled renders can fire.- If
throttleMs <= 0,request()behaves likenow()(immediate render every call).
Example timeline (throttleMs: 30, leading: true, trailing: false):
t=0msrequest()-> render nowt=8msrequest()-> skippedt=20msrequest()-> skippedt=31msrequest()-> render now
