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

Function extendObservable

packages/mobx/src/api/extendobservable.ts:16–55  ·  view source on GitHub ↗
(
    target: A,
    properties: B,
    annotations?: AnnotationsMap<B, never>,
    options?: CreateObservableOptions
)

Source from the content-addressed store, hash-verified

14} from "../internal"
15
16export function extendObservable<A extends Object, B extends Object>(
17 target: A,
18 properties: B,
19 annotations?: AnnotationsMap<B, never>,
20 options?: CreateObservableOptions
21): A & B {
22 if (__DEV__) {
23 if (arguments.length > 4) {
24 die("'extendObservable' expected 2-4 arguments")
25 }
26 if (typeof target !== "object") {
27 die("'extendObservable' expects an object as first argument")
28 }
29 if (isObservableMap(target)) {
30 die("'extendObservable' should not be used on maps, use map.merge instead")
31 }
32 if (!isPlainObject(properties)) {
33 die(`'extendObservable' only accepts plain objects as second argument`)
34 }
35 if (isObservable(properties) || isObservable(annotations)) {
36 die(`Extending an object with another observable (object) is not supported`)
37 }
38 }
39 // Pull descriptors first, so we don't have to deal with props added by administration ($mobx)
40 const descriptors = getOwnPropertyDescriptors(properties)
41
42 initObservable(() => {
43 const adm: ObservableObjectAdministration = asObservableObject(target, options)[$mobx]
44 ownKeys(descriptors).forEach(key => {
45 adm.extend_(
46 key,
47 descriptors[key as any],
48 // must pass "undefined" for { key: undefined }
49 !annotations ? true : key in annotations ? annotations[key] : true
50 )
51 })
52 })
53
54 return target as any
55}

Callers 15

ExampleStateFunction · 0.90
proxies.jsFile · 0.90
ThingFunction · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
ItemFunction · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
MyClassFunction · 0.90
observable.tsFile · 0.70
makeAutoObservableFunction · 0.70

Calls 9

dieFunction · 0.85
isObservableMapFunction · 0.85
initObservableFunction · 0.85
ownKeysFunction · 0.85
extend_Method · 0.80
isObservableFunction · 0.70
forEachMethod · 0.65
isPlainObjectFunction · 0.50
asObservableObjectFunction · 0.50

Tested by 11

ExampleStateFunction · 0.72
ThingFunction · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
ItemFunction · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
MyClassFunction · 0.72
TimeFunction · 0.40
TimeFunction · 0.40
ItemFunction · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…