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

ecamp / hal-json-vuex / 13321877237

13 Feb 2025 10:43PM UTC coverage: 84.708%. Remained the same
13321877237

push

github

web-flow
Update babel monorepo to v7.26.8

159 of 206 branches covered (77.18%)

Branch coverage included in aggregate %.

262 of 291 relevant lines covered (90.03%)

740.07 hits per line

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

95.45
/src/LoadingStoreValue.js
1
import LoadingStoreCollection from './LoadingStoreCollection'
2

3
/**
4
 * Creates a placeholder for an entity which has not yet finished loading from the API.
5
 * Such a LoadingStoreValue can safely be used in Vue components, since it will render as an empty
6
 * string and Vue's reactivity system will replace it with the real data once that is available.
7
 *
8
 * Accessing nested functions in a LoadingStoreValue yields another LoadingStoreValue:
9
 * new LoadingStoreValue(...).author().organization() // gives another LoadingStoreValue
10
 *
11
 * Using a LoadingStoreValue or a property of a LoadingStoreValue in a view renders to empty strings:
12
 * let user = new LoadingStoreValue(...)
13
 * 'The "' + user + '" is called "' + user.name + '"' // gives 'The "" is called ""'
14
 *
15
 * @param entityLoaded a Promise that resolves to a StoreValue when the entity has finished
16
 *                     loading from the API
17
 * @param absoluteSelf optional fully qualified URI of the entity being loaded, if available. If passed, the
18
 *                     returned LoadingStoreValue will return it in calls to .self and ._meta.self
19
 */
20
class LoadingStoreValue {
21
  constructor (entityLoaded, absoluteSelf = null) {
60✔
22
    const handler = {
720✔
23
      get: function (target, prop, _) {
24
        if (prop === Symbol.toPrimitive) {
627✔
25
          return () => ''
6✔
26
        }
27
        if (['then', 'toJSON', Symbol.toStringTag, 'state', 'getters', '$options', '_isVue', '__file', 'render', 'constructor'].includes(prop)) {
621!
28
          // This is necessary so that Vue's reactivity system understands to treat this LoadingStoreValue
29
          // like a normal object.
30
          return undefined
×
31
        }
32
        if (prop === 'loading') {
621✔
33
          return true
24✔
34
        }
35
        if (prop === 'load') {
597✔
36
          return entityLoaded
99✔
37
        }
38
        if (prop === 'self') {
498✔
39
          return absoluteSelf
54✔
40
        }
41
        if (prop === '_meta') {
444✔
42
          // When _meta is requested on a LoadingStoreValue, we keep on using the unmodified promise, because
43
          // ._meta.load is supposed to resolve to the whole object, not just the ._meta part of it
44
          return new LoadingStoreValue(entityLoaded, absoluteSelf)
276✔
45
        }
46
        if (['$reload'].includes(prop)) {
168✔
47
          // Skip reloading entities that are already loading
48
          return () => entityLoaded
3✔
49
        }
50
        if (['$loadItems', '$post', '$patch', '$del'].includes(prop)) {
165✔
51
          // It is important to call entity[prop] without first saving it into a variable, because saving to a
52
          // variable would change the value of `this` inside the function
53
          return (...args) => entityLoaded.then(entity => entity[prop](...args))
15✔
54
        }
55
        const propertyLoaded = entityLoaded.then(entity => entity[prop])
150✔
56
        if (['items', 'allItems'].includes(prop)) {
150✔
57
          return new LoadingStoreCollection(propertyLoaded)
21✔
58
        }
59
        // Normal property access: return a function that yields another LoadingStoreValue and renders as empty string
60
        const result = templateParams => new LoadingStoreValue(propertyLoaded.then(property => property(templateParams)._meta.load))
129✔
61
        result.loading = true
129✔
62
        result.toString = () => ''
129✔
63
        return result
129✔
64
      }
65
    }
66
    return new Proxy(this, handler)
720✔
67
  }
68
}
69

70
export default LoadingStoreValue
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

© 2025 Coveralls, Inc