forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputed.ts
More file actions
66 lines (59 loc) · 2.77 KB
/
Copy pathcomputed.ts
File metadata and controls
66 lines (59 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { comparer } from "../utils/comparer"
import { IComputedValueOptions } from "../core/computedvalue"
import { defineComputedProperty } from "../types/observableobject"
import { invariant } from "../utils/utils"
import { ComputedValue, IComputedValue } from "../core/computedvalue"
import { createPropDecorator } from "../utils/decorators2"
export interface IComputed {
<T>(options: IComputedValueOptions<T>): any // decorator
<T>(func: () => T, setter: (v: T) => void): IComputedValue<T> // normal usage
<T>(func: () => T, options?: IComputedValueOptions<T>): IComputedValue<T> // normal usage
(target: Object, key: string | symbol, baseDescriptor?: PropertyDescriptor): void // decorator
struct(target: Object, key: string | symbol, baseDescriptor?: PropertyDescriptor): void // decorator
}
export const computedDecorator = createPropDecorator(
false,
(
instance: any,
propertyName: string,
descriptor: any,
decoratorTarget: any,
decoratorArgs: any[]
) => {
const { get, set } = descriptor // initialValue is the descriptor for get / set props
// Optimization: faster on decorator target or instance? Assuming target
// Optimization: find out if declaring on instance isn't just faster. (also makes the property descriptor simpler). But, more memory usage..
// Forcing instance now, fixes hot reloadig issues on React Native:
const options = decoratorArgs[0] || {}
defineComputedProperty(instance, propertyName, { get, set, ...options })
}
)
const computedStructDecorator = computedDecorator({ equals: comparer.structural })
/**
* Decorator for class properties: @computed get value() { return expr; }.
* For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`;
*/
export var computed: IComputed = function computed(arg1, arg2, arg3) {
if (typeof arg2 === "string") {
// @computed
return computedDecorator.apply(null, arguments)
}
if (arg1 !== null && typeof arg1 === "object" && arguments.length === 1) {
// @computed({ options })
return computedDecorator.apply(null, arguments)
}
// computed(expr, options?)
if (process.env.NODE_ENV !== "production") {
invariant(
typeof arg1 === "function",
"First argument to `computed` should be an expression."
)
invariant(arguments.length < 3, "Computed takes one or two arguments if used as function")
}
const opts: IComputedValueOptions<any> = typeof arg2 === "object" ? arg2 : {}
opts.get = arg1
opts.set = typeof arg2 === "function" ? arg2 : opts.set
opts.name = opts.name || arg1.name || "" /* for generated name */
return new ComputedValue(opts)
} as any
computed.struct = computedStructDecorator