MCPcopy
hub / github.com/mobxjs/mobx / set_

Method set_

packages/mobx/src/types/observablearray.ts:359–398  ·  view source on GitHub ↗
(index: number, newValue: any)

Source from the content-addressed store, hash-verified

357 }
358
359 set_(index: number, newValue: any) {
360 const values = this.values_
361 if (this.legacyMode_ && index > values.length) {
362 // out of bounds
363 die(17, index, values.length)
364 }
365 if (index < values.length) {
366 // update at index in range
367 checkIfStateModificationsAreAllowed(this.atom_)
368 const oldValue = values[index]
369 if (hasInterceptors(this)) {
370 const change = interceptChange<IArrayWillChange<any>>(this as any, {
371 type: UPDATE,
372 object: this.proxy_ as any, // since "this" is the real array we need to pass its proxy
373 index,
374 newValue
375 })
376 if (!change) {
377 return
378 }
379 newValue = change.newValue
380 }
381 newValue = this.enhancer_(newValue, oldValue)
382 const changed = newValue !== oldValue
383 if (changed) {
384 values[index] = newValue
385 this.notifyArrayChildUpdate_(index, newValue, oldValue)
386 }
387 } else {
388 // For out of bound index, we don't create an actual sparse array,
389 // but rather fill the holes with undefined (same as setArrayLength_).
390 // This could be considered a bug.
391 const newItems = new Array(index + 1 - values.length)
392 for (let i = 0; i < newItems.length - 1; i++) {
393 newItems[i] = undefined
394 } // No Array.fill everywhere...
395 newItems[newItems.length - 1] = newValue
396 this.spliceWithArray_(values.length, 0, newItems)
397 }
398 }
399}
400
401export function createObservableArray<T>(

Callers 4

setFunction · 0.45
setFunction · 0.45
setFunction · 0.45

Calls 6

spliceWithArray_Method · 0.95
dieFunction · 0.85
hasInterceptorsFunction · 0.85
interceptChangeFunction · 0.85

Tested by

no test coverage detected