MCPcopy Index your code
hub / github.com/atomicdata-dev/atomic-data-browser / useResources

Function useResources

react/src/hooks.ts:79–117  ·  view source on GitHub ↗
(
  subjects: string[],
  opts: {
    /**
     * If this is true, incomplete resources will not be automatically fetched.
     * This limits the amount of requests. Use this for things like menu items.
     */
    allowIncomplete?: boolean;
  } = {},
)

Source from the content-addressed store, hash-verified

77 * a long time.
78 */
79export function useResources(
80 subjects: string[],
81 opts: {
82 /**
83 * If this is true, incomplete resources will not be automatically fetched.
84 * This limits the amount of requests. Use this for things like menu items.
85 */
86 allowIncomplete?: boolean;
87 } = {},
88): Map<string, Resource> {
89 const [resources, setResources] = useState(new Map());
90 const store = useStore();
91
92 useEffect(() => {
93 function handleNotify(updated: Resource) {
94 // When a change happens, set the new Resource.
95 resources.set(updated.getSubject(), updated);
96 // We need to create new Maps for react hooks to update - React only checks references, not content
97 setResources(new Map(resources));
98 }
99
100 // Iterate over all resources asynchronously
101 subjects.map(subject => {
102 const resource = store.getResourceLoading(subject, opts);
103 resources.set(subject, resource);
104 setResources(new Map(resources));
105 // Let the store know to call handleNotify when a resource is updated.
106 store.subscribe(subject, handleNotify);
107 });
108
109 return () => {
110 // When the component is unmounted, unsubscribe from the store.
111 subjects.map(subject => store.unsubscribe(subject, handleNotify));
112 };
113 // maybe add resources here
114 }, [subjects, store]);
115
116 return resources;
117}
118
119/**
120 * Hook for using a Property. Will return null if the Property is not yet

Callers 1

useLocalSearchFunction · 0.90

Calls 5

useStoreFunction · 0.85
getResourceLoadingMethod · 0.80
setMethod · 0.80
subscribeMethod · 0.80
unsubscribeMethod · 0.80

Tested by

no test coverage detected