(
target: A,
properties: B,
annotations?: AnnotationsMap<B, never>,
options?: CreateObservableOptions
)
| 14 | } from "../internal" |
| 15 | |
| 16 | export 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 | } |
searching dependent graphs…