{"version":3,"file":"resolvers-cache-middleware.mjs","names":[],"sources":["../src/resolvers-cache-middleware.ts"],"sourcesContent":["import type { Middleware } from 'redux';\nimport type { DataRegistry } from './types';\n\n/**\n * Creates a middleware handling resolvers cache invalidation.\n *\n * @param registry  Registry for which to create the middleware.\n * @param storeName Name of the store for which to create the middleware.\n *\n * @return Middleware function.\n */\nconst createResolversCacheMiddleware =\n\t( registry: DataRegistry, storeName: string ): Middleware =>\n\t() =>\n\t( next ) =>\n\t( action ) => {\n\t\tconst resolvers = registry.select( storeName ).getCachedResolvers();\n\t\tconst resolverEntries =\n\t\t\tObject.entries< Map< string, { status: 'finished' | 'error' } > >(\n\t\t\t\tresolvers\n\t\t\t);\n\t\tresolverEntries.forEach( ( [ selectorName, resolversByArgs ] ) => {\n\t\t\tconst resolver =\n\t\t\t\tregistry.stores[ storeName ]?.resolvers?.[ selectorName ];\n\t\t\tif ( ! resolver || ! resolver.shouldInvalidate ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst { shouldInvalidate } = resolver;\n\t\t\tresolversByArgs.forEach( ( value, args ) => {\n\t\t\t\t// Works around a bug in `EquivalentKeyMap` where `map.delete` merely sets an entry value\n\t\t\t\t// to `undefined` and `map.forEach` then iterates also over these orphaned entries.\n\t\t\t\tif ( value === undefined ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// resolversByArgs is the map Map([ args ] => boolean) storing the cache resolution status for a given selector.\n\t\t\t\t// If the value is \"finished\" or \"error\" it means this resolver has finished its resolution which means we need\n\t\t\t\t// to invalidate it, if it's true it means it's inflight and the invalidation is not necessary.\n\t\t\t\tif ( value.status !== 'finished' && value.status !== 'error' ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( ! shouldInvalidate( action, ...args ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Trigger cache invalidation\n\t\t\t\tregistry\n\t\t\t\t\t.dispatch( storeName )\n\t\t\t\t\t.invalidateResolution( selectorName, args );\n\t\t\t} );\n\t\t} );\n\t\treturn next( action );\n\t};\n\nexport default createResolversCacheMiddleware;\n"],"mappings":";;;;;;;;;AAWA,MAAM,kCACH,UAAwB,qBAExB,UACA,WAAY;CACb,MAAM,YAAY,SAAS,OAAQ,SAAU,CAAC,CAAC,mBAAmB;CAKlE,OAHQ,QACN,SAEY,CAAC,CAAC,SAAW,CAAE,cAAc,qBAAuB;EACjE,MAAM,WACL,SAAS,OAAQ,UAAW,EAAE,YAAa;EAC5C,IAAK,CAAE,YAAY,CAAE,SAAS,kBAC7B;EAED,MAAM,EAAE,qBAAqB;EAC7B,gBAAgB,SAAW,OAAO,SAAU;GAG3C,IAAK,UAAU,KAAA,GACd;GAMD,IAAK,MAAM,WAAW,cAAc,MAAM,WAAW,SACpD;GAGD,IAAK,CAAE,iBAAkB,QAAQ,GAAG,IAAK,GACxC;GAID,SACE,SAAU,SAAU,CAAC,CACrB,qBAAsB,cAAc,IAAK;EAC5C,CAAE;CACH,CAAE;CACF,OAAO,KAAM,MAAO;AACrB"}