glideapps / glide-data-grid

🚀 Glide Data Grid is a no compromise, outrageously react fast data grid with rich rendering, first class accessibility, and full TypeScript support.
https://grid.glideapps.com
MIT License
3.98k stars 288 forks source link

[Improvement] useAsyncDataSource cache invalidation #994

Open dellamonica opened 3 weeks ago

dellamonica commented 3 weeks ago

It would be nice to be able to invalidate rows cached by the useAsyncDataSource hook. My API suggestion would be to return an additional object in this hook, say a "cache" object, which encapsulates the internal cache and allows some control from the outside.

Perhaps this could be the type def for this cache:

interface AsyncDataSourceCache<TData> {
    /**
     * Gets the cached object (if there is any) for the given row.
     */
    get: (row: number) => TData | undefined
    /**
     * Invalidate the given cached rows, when no rows are specified, the entire cache is invalidated.
     */
    invalidate(rows?: number[]) => void
}

This allows easy access to cached row objects, which can come in handy when processing the grid events. It also allows invalidating any rows (say, by controlling their timestamp or whatever logic the client needs).

Currently, I have a component that needs this type of functionality and I had to resort to changing the component's key just to "clear" the internal state of the hook.