* Gets a resource by URL. Fetches and parses it if it's not available in the * store. Instantly returns an empty loading resource, while the fetching is * done in the background . If the subject is undefined, an empty non-saved * resource will be returned.
(
subject?: string,
opts: {
/** Won't fetch the resource if it's new */
newResource?: boolean;
/**
* 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;
} = {},
)
| 137 | * resource will be returned. |
| 138 | */ |
| 139 | getResourceLoading( |
| 140 | subject?: string, |
| 141 | opts: { |
| 142 | /** Won't fetch the resource if it's new */ |
| 143 | newResource?: boolean; |
| 144 | /** |
| 145 | * If this is true, incomplete resources will not be automatically |
| 146 | * fetched. This limits the amount of requests. Use this for things like |
| 147 | * menu items. |
| 148 | */ |
| 149 | allowIncomplete?: boolean; |
| 150 | } = {}, |
| 151 | ): Resource | null { |
| 152 | // This is needed because it can happen that the useResource react hook is called while there is no subject passed. |
| 153 | if (subject == undefined) { |
| 154 | const newR = new Resource(unknownSubject, opts.newResource); |
| 155 | return newR; |
| 156 | } |
| 157 | const found = this.resources.get(subject); |
| 158 | if (found == undefined) { |
| 159 | const newR = new Resource(subject, opts.newResource); |
| 160 | newR.loading = true; |
| 161 | this.addResource(newR); |
| 162 | if (!opts.newResource) { |
| 163 | this.fetchResource(subject); |
| 164 | } |
| 165 | return newR; |
| 166 | } else if (!opts.allowIncomplete && found.loading == false) { |
| 167 | // In many cases, a user will always need a complete resource. |
| 168 | // This checks if the resource is incomplete and fetches it if it is. |
| 169 | if (found.get(urls.properties.incomplete)) { |
| 170 | found.loading = true; |
| 171 | this.addResource(found); |
| 172 | this.fetchResource(subject); |
| 173 | } |
| 174 | return found; |
| 175 | } |
| 176 | return found; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Gets a resource by URL. Fetches and parses it if it's not available in the |
no test coverage detected