forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.ts
More file actions
101 lines (86 loc) · 3.67 KB
/
action.ts
File metadata and controls
101 lines (86 loc) · 3.67 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
IAction,
addHiddenProp,
boundActionDecorator,
createAction,
executeAction,
fail,
invariant,
namedActionDecorator
} from "../internal"
export interface IActionFactory {
// nameless actions
<A1, R, T extends (a1: A1) => R>(fn: T): T & IAction
<A1, A2, R, T extends (a1: A1, a2: A2) => R>(fn: T): T & IAction
<A1, A2, A3, R, T extends (a1: A1, a2: A2, a3: A3) => R>(fn: T): T & IAction
<A1, A2, A3, A4, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4) => R>(fn: T): T & IAction
<A1, A2, A3, A4, A5, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R>(fn: T): T &
IAction
<A1, A2, A3, A4, A5, A6, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4, a6: A6) => R>(fn: T): T &
IAction
// named actions
<A1, R, T extends (a1: A1) => R>(name: string, fn: T): T & IAction
<A1, A2, R, T extends (a1: A1, a2: A2) => R>(name: string, fn: T): T & IAction
<A1, A2, A3, R, T extends (a1: A1, a2: A2, a3: A3) => R>(name: string, fn: T): T & IAction
<A1, A2, A3, A4, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4) => R>(name: string, fn: T): T &
IAction
<A1, A2, A3, A4, A5, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R>(
name: string,
fn: T
): T & IAction
<A1, A2, A3, A4, A5, A6, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4, a6: A6) => R>(
name: string,
fn: T
): T & IAction
// generic forms
<T extends Function>(fn: T): T & IAction
<T extends Function>(name: string, fn: T): T & IAction
// named decorator
(customName: string): (
target: Object,
key: string | symbol,
baseDescriptor?: PropertyDescriptor
) => void
// unnamed decorator
(target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor): void
// @action.bound decorator
bound(target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor): void
}
export var action: IActionFactory = function action(arg1, arg2?, arg3?, arg4?): any {
// action(fn() {})
if (arguments.length === 1 && typeof arg1 === "function")
return createAction(arg1.name || "<unnamed action>", arg1)
// action("name", fn() {})
if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2)
// @action("name") fn() {}
if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1)
// @action fn() {}
if (arg4 === true) {
// apply to instance immediately
addHiddenProp(arg1, arg2, createAction(arg1.name || arg2, arg3.value))
} else {
return namedActionDecorator(arg2).apply(null, arguments as any)
}
} as any
action.bound = boundActionDecorator as any
export function runInAction<T>(block: () => T): T
export function runInAction<T>(name: string, block: () => T): T
export function runInAction(arg1, arg2?) {
const actionName = typeof arg1 === "string" ? arg1 : arg1.name || "<unnamed action>"
const fn = typeof arg1 === "function" ? arg1 : arg2
if (process.env.NODE_ENV !== "production") {
invariant(
typeof fn === "function" && fn.length === 0,
"`runInAction` expects a function without arguments"
)
if (typeof actionName !== "string" || !actionName)
fail(`actions should have valid names, got: '${actionName}'`)
}
return executeAction(actionName, fn, this, undefined)
}
export function isAction(thing: any) {
return typeof thing === "function" && thing.isMobxAction === true
}
export function defineBoundAction(target: any, propertyName: string, fn: Function) {
addHiddenProp(target, propertyName, createAction(propertyName, fn.bind(target)))
}