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

mobxjs / mobx / 6989803934

25 Nov 2023 03:01PM CUT coverage: 90.949% (-0.4%) from 91.391%
6989803934

Pull #3803

github

mweststrate
Removed old v4 compatibility tests
Pull Request #3803: Some cleanups of old stuff

1775 of 2214 branches covered (0.0%)

3296 of 3624 relevant lines covered (90.95%)

2879.95 hits per line

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

68.63
/packages/mobx/src/types/legacyobservablearray.ts
1
import {
34✔
2
    getNextId,
3
    addHiddenFinalProp,
4
    makeIterable,
5
    addHiddenProp,
6
    ObservableArrayAdministration,
7
    $mobx,
8
    arrayExtensions,
9
    IEnhancer,
10
    isObservableArray,
11
    IObservableArray,
12
    defineProperty,
13
    initObservable
14
} from "../internal"
15

16
// Bug in safari 9.* (or iOS 9 safari mobile). See #364
17
const ENTRY_0 = createArrayEntryDescriptor(0)
34✔
18

19
const safariPrototypeSetterInheritanceBug = (() => {
34✔
20
    let v = false
34✔
21
    const p = {}
34✔
22
    Object.defineProperty(p, "0", {
34✔
23
        set: () => {
24
            v = true
34✔
25
        }
26
    })
27
    Object.create(p)["0"] = 1
34✔
28
    return v === false
34✔
29
})()
30

31
/**
32
 * This array buffer contains two lists of properties, so that all arrays
33
 * can recycle their property definitions, which significantly improves performance of creating
34
 * properties on the fly.
35
 */
36
let OBSERVABLE_ARRAY_BUFFER_SIZE = 0
34✔
37

38
// Typescript workaround to make sure ObservableArray extends Array
39
class StubArray {}
40
function inherit(ctor, proto) {
41
    if (Object.setPrototypeOf) {
34!
42
        Object.setPrototypeOf(ctor.prototype, proto)
34✔
43
    } else if (ctor.prototype.__proto__ !== undefined) {
×
44
        ctor.prototype.__proto__ = proto
×
45
    } else {
46
        ctor.prototype = proto
×
47
    }
48
}
49
inherit(StubArray, Array.prototype)
34✔
50

51
// Weex proto freeze protection was here,
52
// but it is unclear why the hack is need as MobX never changed the prototype
53
// anyway, so removed it in V6
54

55
export class LegacyObservableArray<T> extends StubArray {
34✔
56
    constructor(
57
        initialValues: T[] | undefined,
58
        enhancer: IEnhancer<T>,
59
        name = __DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray",
6!
60
        owned = false
4✔
61
    ) {
62
        super()
4✔
63
        initObservable(() => {
4✔
64
            const adm = new ObservableArrayAdministration(name, enhancer, owned, true)
4✔
65
            adm.proxy_ = this as any
4✔
66
            addHiddenFinalProp(this, $mobx, adm)
4✔
67

68
            if (initialValues && initialValues.length) {
4✔
69
                // @ts-ignore
70
                this.spliceWithArray(0, 0, initialValues)
2✔
71
            }
72

73
            if (safariPrototypeSetterInheritanceBug) {
4!
74
                // Seems that Safari won't use numeric prototype setter untill any * numeric property is
75
                // defined on the instance. After that it works fine, even if this property is deleted.
76
                Object.defineProperty(this, "0", ENTRY_0)
×
77
            }
78
        })
79
    }
80

81
    concat(...arrays: T[][]): T[] {
82
        ;(this[$mobx] as ObservableArrayAdministration).atom_.reportObserved()
×
83
        return Array.prototype.concat.apply(
×
84
            (this as any).slice(),
85
            //@ts-ignore
86
            arrays.map(a => (isObservableArray(a) ? a.slice() : a))
×
87
        )
88
    }
89

90
    get length(): number {
91
        return (this[$mobx] as ObservableArrayAdministration).getArrayLength_()
×
92
    }
93

94
    set length(newLength: number) {
95
        ;(this[$mobx] as ObservableArrayAdministration).setArrayLength_(newLength)
×
96
    }
97

98
    get [Symbol.toStringTag]() {
99
        return "Array"
×
100
    }
101

102
    [Symbol.iterator]() {
103
        const self = this
×
104
        let nextIndex = 0
×
105
        return makeIterable({
×
106
            next() {
107
                return nextIndex < self.length
×
108
                    ? { value: self[nextIndex++], done: false }
109
                    : { done: true, value: undefined }
110
            }
111
        })
112
    }
113
}
114

115
Object.entries(arrayExtensions).forEach(([prop, fn]) => {
34✔
116
    if (prop !== "concat") {
1,054✔
117
        addHiddenProp(LegacyObservableArray.prototype, prop, fn)
1,020✔
118
    }
119
})
120

121
function createArrayEntryDescriptor(index: number) {
122
    return {
37,434✔
123
        enumerable: false,
124
        configurable: true,
125
        get: function () {
126
            return this[$mobx].get_(index)
×
127
        },
128
        set: function (value) {
129
            this[$mobx].set_(index, value)
×
130
        }
131
    }
132
}
133

134
function createArrayBufferItem(index: number) {
135
    defineProperty(LegacyObservableArray.prototype, "" + index, createArrayEntryDescriptor(index))
37,400✔
136
}
137

138
export function reserveArrayBuffer(max: number) {
34✔
139
    if (max > OBSERVABLE_ARRAY_BUFFER_SIZE) {
36✔
140
        for (let index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max + 100; index++) {
34✔
141
            createArrayBufferItem(index)
37,400✔
142
        }
143
        OBSERVABLE_ARRAY_BUFFER_SIZE = max
34✔
144
    }
145
}
146

147
reserveArrayBuffer(1000)
34✔
148

149
export function createLegacyArray<T>(
34✔
150
    initialValues: T[] | undefined,
151
    enhancer: IEnhancer<T>,
152
    name?: string
153
): IObservableArray<T> {
154
    return new LegacyObservableArray(initialValues, enhancer, name) as any
4✔
155
}
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