• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ecamp / hal-json-vuex / 14422895817

12 Apr 2025 07:59PM UTC coverage: 93.432%. Remained the same
14422895817

push

github

usu
3.0.0-alpha.10

160 of 183 branches covered (87.43%)

Branch coverage included in aggregate %.

281 of 289 relevant lines covered (97.23%)

930.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/LoadingCollection.ts
1
import type ResourceInterface from './interfaces/ResourceInterface'
2

3
import LoadingResource from './LoadingResource'
4

5
class LoadingCollection {
6
  /**
7
   * Returns a placeholder for an array that has not yet finished loading from the API. The array placeholder
8
   * will respond to functional calls (like .find(), .map(), etc.) with further LoadingStoreCollections or
9
   * LoadingStoreValues. If passed the existingContent argument, random access and .length will also work.
10
   * @param loadArray       Promise that resolves once the array has finished loading
11
   * @param existingContent optionally set the elements that are already known, for random access
12
   */
13
  static create<ItemType extends ResourceInterface> (loadArray: Promise<Array<ItemType> | undefined>, existingContent: Array<ItemType> = []): Array<ItemType> {
45✔
14
    // if the Promise resolves to undefined, provide empty array
15
    // this could happen if items is accessed from a LoadingResource, which resolves to a normal entity without 'items'
16
    const loadArraySafely = loadArray.then(array => array ?? [])
48!
17

18
    // proxy array function 'find' to a LoadingResource (Resource)
19
    const singleResultFunctions = ['find']
48✔
20
    singleResultFunctions.forEach(func => {
48✔
21
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
      existingContent[func] = (...args: any[]) => {
48✔
23
        const resultLoaded = loadArraySafely.then(array => array[func](...args) as ItemType)
6✔
24
        return new LoadingResource(resultLoaded)
6✔
25
      }
26
    })
27

28
    // proxy array functions with multiple results to a LoadingCollection (Array<ResourceInterface>)
29
    const arrayResultFunctions = ['map', 'flatMap', 'filter']
48✔
30
    arrayResultFunctions.forEach(func => {
48✔
31
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
      existingContent[func] = (...args: any[]) => {
144✔
33
        const resultLoaded = loadArraySafely.then(array => array[func](...args) as Array<ItemType>) // TODO: return type for .map() is not necessarily an Array<ResourceInterface>
18✔
34
        return LoadingCollection.create(resultLoaded)
18✔
35
      }
36
    })
37
    return existingContent
48✔
38
  }
39
}
40

41
export default LoadingCollection
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc