pull/17124/head
Elliott Johnson 1 month ago
parent 9c7da6c9eb
commit 5de63834dc

@ -14,13 +14,13 @@ const cache = new Map();
* @template {typeof Resource} [TResource=typeof Resource] * @template {typeof Resource} [TResource=typeof Resource]
* @param {string} name * @param {string} name
* @param {(...args: TArgs) => TReturn} fn * @param {(...args: TArgs) => TReturn} fn
* @param {{ Resource?: TResource, transport?: Transport }} [options] * @param {{ Resource?: TResource, transport?: Transport, hash?: (args: TArgs) => string }} [options]
* @returns {(...args: TArgs) => Resource<TReturn>} * @returns {(...args: TArgs) => Resource<TReturn>}
*/ */
export function define_resource(name, fn, options = {}) { export function define_resource(name, fn, options = {}) {
const ResolvedResource = options?.Resource ?? Resource; const ResolvedResource = options?.Resource ?? Resource;
return (...args) => { return (...args) => {
const stringified_args = (options.transport?.stringify ?? JSON.stringify)(args); const stringified_args = (options.hash ?? JSON.stringify)(args);
const cache_key = `${name}:${stringified_args}`; const cache_key = `${name}:${stringified_args}`;
let entry = cache.get(cache_key); let entry = cache.get(cache_key);
const maybe_remove = create_remover(cache_key); const maybe_remove = create_remover(cache_key);

@ -120,14 +120,14 @@ export class Resource {
* @template {typeof Resource} [TResource=typeof Resource] * @template {typeof Resource} [TResource=typeof Resource]
* @param {string} name * @param {string} name
* @param {(...args: TArgs) => TReturn} fn * @param {(...args: TArgs) => TReturn} fn
* @param {{ Resource?: TResource, transport?: Transport }} [options] * @param {{ Resource?: TResource, transport?: Transport, hash?: (args: TArgs) => string }} [options]
* @returns {(...args: TArgs) => Resource<TReturn>} * @returns {(...args: TArgs) => Resource<TReturn>}
*/ */
export function defineResource(name, fn, options = {}) { export function defineResource(name, fn, options = {}) {
const ResolvedResource = options?.Resource ?? Resource; const ResolvedResource = options?.Resource ?? Resource;
return (...args) => { return (...args) => {
const cache = get_render_store().resources; const cache = get_render_store().resources;
const stringified_args = (options.transport?.stringify ?? JSON.stringify)(args); const stringified_args = (options.hash ?? JSON.stringify)(args);
const cache_key = `${name}:${stringified_args}`; const cache_key = `${name}:${stringified_args}`;
const entry = cache.get(cache_key); const entry = cache.get(cache_key);
if (entry) { if (entry) {

Loading…
Cancel
Save