mobx-keystone 1.20.0 → 1.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +864 -853
- package/README.md +61 -61
- package/dist/mobx-keystone.esm.js +11336 -11148
- package/dist/mobx-keystone.esm.mjs +11336 -11148
- package/dist/mobx-keystone.umd.js +12229 -11738
- package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
- package/dist/types/modelShared/prop.d.ts +5 -0
- package/dist/types/redux/connectReduxDevTools.d.ts +17 -4
- package/dist/types/ref/Ref.d.ts +1 -1
- package/dist/types/treeUtils/sandbox.d.ts +1 -1
- package/dist/types/types/TypeChecker.d.ts +1 -1
- package/dist/types/types/arrayBased/typesArray.d.ts +1 -1
- package/dist/types/types/arrayBased/typesTuple.d.ts +1 -1
- package/dist/types/types/objectBased/typesArraySet.d.ts +1 -1
- package/dist/types/types/objectBased/typesDataModelData.d.ts +1 -1
- package/dist/types/types/objectBased/typesModel.d.ts +1 -1
- package/dist/types/types/objectBased/typesObject.d.ts +3 -3
- package/dist/types/types/objectBased/typesObjectMap.d.ts +1 -1
- package/dist/types/types/objectBased/typesRecord.d.ts +1 -1
- package/dist/types/types/primitiveBased/typesPrimitive.d.ts +1 -1
- package/dist/types/types/types.d.ts +3 -3
- package/dist/types/types/utility/typesCodec.d.ts +1 -6
- package/dist/types/types/utility/typesCodecCore.d.ts +1 -1
- package/dist/types/types/utility/typesOr.d.ts +1 -1
- package/dist/types/types/utility/typesRefinement.d.ts +1 -1
- package/dist/types/types/utility/typesSkipCheck.d.ts +1 -1
- package/dist/types/types/utility/typesTag.d.ts +1 -1
- package/dist/types/wrappers/ObjectMap.d.ts +2 -0
- package/dist/types/wrappers/asMap.d.ts +10 -6
- package/package.json +14 -12
- package/src/action/applyAction.ts +125 -114
- package/src/action/applyMethodCall.ts +37 -37
- package/src/action/isModelAction.ts +16 -16
- package/src/action/middleware.ts +137 -137
- package/src/action/modelFlow.ts +240 -240
- package/src/action/modelFlowPromiseGenerator.ts +127 -125
- package/src/action/pendingActions.ts +42 -42
- package/src/action/wrapInAction.ts +156 -156
- package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +1 -1
- package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +172 -172
- package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +20 -20
- package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +17 -17
- package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +42 -42
- package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +50 -50
- package/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.ts +21 -21
- package/src/actionMiddlewares/actionSerialization/plainObjectSerializer.ts +24 -32
- package/src/actionMiddlewares/actionSerialization/primitiveSerializer.ts +51 -51
- package/src/actionMiddlewares/actionSerialization/setSerializer.ts +37 -37
- package/src/actionMiddlewares/actionTrackingMiddleware.ts +3 -3
- package/src/actionMiddlewares/onActionMiddleware.ts +2 -2
- package/src/actionMiddlewares/readonlyMiddleware.ts +2 -2
- package/src/actionMiddlewares/transactionMiddleware.ts +4 -4
- package/src/actionMiddlewares/undoMiddleware.ts +804 -796
- package/src/actionMiddlewares/utils.ts +2 -2
- package/src/computedTree/computedTree.ts +143 -143
- package/src/context/context.ts +1 -1
- package/src/dataModel/BaseDataModel.ts +194 -192
- package/src/dataModel/actions.ts +1 -1
- package/src/dataModel/newDataModel.ts +57 -57
- package/src/deepChange/index.ts +1 -1
- package/src/deepChange/onDeepChange.ts +244 -244
- package/src/frozen/Frozen.ts +146 -146
- package/src/globalConfig/globalConfig.ts +107 -107
- package/src/index.ts +24 -24
- package/src/model/BaseModel.ts +275 -273
- package/src/model/ModelConstructorOptions.ts +15 -15
- package/src/model/getModelMetadata.ts +51 -51
- package/src/model/index.ts +6 -6
- package/src/model/mixins.ts +288 -288
- package/src/model/newModel.ts +301 -301
- package/src/model/utils.ts +249 -249
- package/src/modelShared/modelDecorator.ts +260 -253
- package/src/modelShared/modelInfo.ts +50 -50
- package/src/modelShared/modelSymbols.ts +3 -3
- package/src/modelShared/newModel.ts +25 -25
- package/src/modelShared/prop.ts +713 -684
- package/src/modelShared/sharedInternalModel.ts +427 -417
- package/src/modelShared/utils.ts +24 -24
- package/src/parent/core.ts +65 -65
- package/src/parent/coreObjectChildren.ts +180 -180
- package/src/parent/detach.ts +47 -47
- package/src/parent/findParent.ts +1 -1
- package/src/parent/getChildrenObjects.ts +25 -25
- package/src/parent/path.ts +386 -381
- package/src/parent/setParent.ts +149 -149
- package/src/parent/walkTree.ts +1 -1
- package/src/patch/applyPatches.ts +187 -187
- package/src/patch/jsonPatch.ts +108 -108
- package/src/patch/patchRecorder.ts +6 -1
- package/src/redux/connectReduxDevTools.ts +39 -7
- package/src/redux/redux.ts +6 -2
- package/src/ref/Ref.ts +1 -1
- package/src/ref/core.ts +339 -345
- package/src/ref/customRef.ts +3 -3
- package/src/ref/rootRef.ts +100 -100
- package/src/rootStore/attachDetach.ts +89 -89
- package/src/rootStore/rootStore.ts +1 -1
- package/src/snapshot/SnapshotOf.ts +104 -104
- package/src/snapshot/SnapshotProcessingError.ts +45 -45
- package/src/snapshot/applySnapshot.ts +177 -181
- package/src/snapshot/cloneTreeValue.ts +30 -30
- package/src/snapshot/fromArraySnapshot.ts +27 -33
- package/src/snapshot/fromModelSnapshot.ts +70 -70
- package/src/snapshot/fromPlainObjectSnapshot.ts +47 -42
- package/src/snapshot/fromSnapshot.ts +198 -198
- package/src/snapshot/getSnapshot.ts +80 -79
- package/src/snapshot/index.ts +8 -8
- package/src/snapshot/internal.ts +230 -230
- package/src/snapshot/reconcileArraySnapshot.ts +70 -70
- package/src/snapshot/reconcileModelSnapshot.ts +155 -150
- package/src/snapshot/reconcilePlainObjectSnapshot.ts +72 -72
- package/src/snapshot/reconcileSnapshot.ts +88 -88
- package/src/standardActions/actions.ts +64 -64
- package/src/standardActions/objectActions.ts +60 -60
- package/src/transforms/ImmutableDate.ts +93 -93
- package/src/transforms/asMap.ts +50 -50
- package/src/transforms/asSet.ts +22 -22
- package/src/transforms/bigint.ts +20 -20
- package/src/transforms/date.ts +40 -40
- package/src/treeUtils/deepEquals.ts +55 -55
- package/src/treeUtils/sandbox.ts +6 -3
- package/src/tweaker/TweakerPriority.ts +9 -9
- package/src/tweaker/tweak.ts +4 -3
- package/src/tweaker/tweakArray.ts +530 -475
- package/src/tweaker/tweakFrozen.ts +42 -42
- package/src/tweaker/tweakModel.ts +1 -1
- package/src/tweaker/tweakPlainObject.ts +355 -355
- package/src/tweaker/typeChecking.ts +101 -101
- package/src/types/SnapshotTypeMismatchError.ts +46 -46
- package/src/types/TypeCheckError.ts +183 -184
- package/src/types/TypeChecker.ts +202 -185
- package/src/types/arrayBased/typesArray.ts +136 -137
- package/src/types/arrayBased/typesTuple.ts +138 -139
- package/src/types/createPerEntryCachedCheck.ts +166 -160
- package/src/types/index.ts +6 -6
- package/src/types/objectBased/typesArraySet.ts +133 -134
- package/src/types/objectBased/typesDataModelData.ts +203 -204
- package/src/types/objectBased/typesModel.ts +220 -221
- package/src/types/objectBased/typesObject.ts +308 -310
- package/src/types/objectBased/typesObjectMap.ts +142 -143
- package/src/types/objectBased/typesRecord.ts +156 -157
- package/src/types/objectBased/typesRef.ts +89 -89
- package/src/types/primitiveBased/typesEnum.ts +63 -63
- package/src/types/primitiveBased/typesPrimitive.ts +219 -220
- package/src/types/registerDefaultStandardTypeResolvers.ts +21 -21
- package/src/types/resolveTypeChecker.ts +78 -78
- package/src/types/schemas.ts +208 -208
- package/src/types/tProp.ts +277 -268
- package/src/types/typeCheck.ts +21 -21
- package/src/types/typeCheckErrorUtils.ts +23 -23
- package/src/types/types.ts +142 -142
- package/src/types/utility/typeToStoredData.ts +37 -37
- package/src/types/utility/typesBigInt.ts +15 -15
- package/src/types/utility/typesCodec.ts +41 -38
- package/src/types/utility/typesCodecCore.ts +178 -178
- package/src/types/utility/typesCodecSupport.ts +806 -806
- package/src/types/utility/typesDate.ts +27 -27
- package/src/types/utility/typesMap.ts +253 -222
- package/src/types/utility/typesOr.ts +263 -259
- package/src/types/utility/typesRefinement.ts +131 -125
- package/src/types/utility/typesSet.ts +110 -110
- package/src/types/utility/typesSkipCheck.ts +69 -70
- package/src/types/utility/typesTag.ts +88 -82
- package/src/types/utility/typesUnchecked.ts +40 -40
- package/src/utils/AnyFunction.ts +4 -4
- package/src/utils/ModelPool.ts +59 -59
- package/src/utils/chainFns.ts +18 -18
- package/src/utils/decorators.ts +274 -274
- package/src/utils/errorDiagnostics.ts +204 -204
- package/src/utils/index.ts +443 -372
- package/src/wrappers/ArraySet.ts +156 -156
- package/src/wrappers/ObjectMap.ts +155 -136
- package/src/wrappers/asMap.ts +65 -30
- package/src/wrappers/asSet.ts +3 -3
package/src/utils/decorators.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
1
|
import type { WrapInActionOverrideContextFn } from "../action/wrapInAction"
|
|
2
|
-
import { setDataModelAction } from "../dataModel/actions"
|
|
3
|
-
import { BaseDataModel } from "../dataModel/BaseDataModel"
|
|
4
|
-
import { isDataModel, isDataModelClass } from "../dataModel/utils"
|
|
5
|
-
import { BaseModel } from "../model/BaseModel"
|
|
6
|
-
import { modelInfoByClass } from "../modelShared/modelInfo"
|
|
7
|
-
import { runAfterModelDecoratorSymbol } from "../modelShared/modelSymbols"
|
|
8
|
-
import { addLateInitializationFunction, failure, inDevMode, runAfterNewSymbol } from "./index"
|
|
9
|
-
|
|
10
|
-
type WrapFunction = (
|
|
11
|
-
data: {
|
|
12
|
-
actionName: string | (() => string)
|
|
13
|
-
overrideContext: WrapInActionOverrideContextFn | undefined
|
|
14
|
-
},
|
|
15
|
-
fn: any
|
|
16
|
-
) => any
|
|
17
|
-
|
|
18
|
-
const unboundMethodSymbol = Symbol("unboundMethod")
|
|
19
|
-
|
|
20
|
-
const functionPropertiesToSkip = new Set<PropertyKey>([
|
|
21
|
-
"arguments",
|
|
22
|
-
"caller",
|
|
23
|
-
"length",
|
|
24
|
-
"name",
|
|
25
|
-
"prototype",
|
|
26
|
-
unboundMethodSymbol,
|
|
27
|
-
])
|
|
28
|
-
|
|
29
|
-
type ReflectWithMetadata = typeof Reflect & {
|
|
30
|
-
getOwnMetadataKeys?: (target: object) => any[]
|
|
31
|
-
getOwnMetadata?: (metadataKey: any, target: object) => any
|
|
32
|
-
defineMetadata?: (metadataKey: any, metadataValue: any, target: object) => void
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @internal
|
|
37
|
-
*/
|
|
38
|
-
export function copyFunctionMetadata(source: unknown, target: unknown): void {
|
|
39
|
-
if (typeof source !== "function" || typeof target !== "function" || source === target) {
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const keys = Reflect.ownKeys(source)
|
|
44
|
-
for (let i = 0, len = keys.length; i < len; i++) {
|
|
45
|
-
const key = keys[i]
|
|
46
|
-
if (functionPropertiesToSkip.has(key)) {
|
|
47
|
-
continue
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const descriptor = Object.getOwnPropertyDescriptor(source, key)
|
|
51
|
-
if (!descriptor) {
|
|
52
|
-
continue
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
Object.defineProperty(target, key, descriptor)
|
|
57
|
-
} catch {
|
|
58
|
-
// ignore non-copyable properties
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const reflect = Reflect as ReflectWithMetadata
|
|
63
|
-
if (
|
|
64
|
-
typeof reflect.getOwnMetadataKeys === "function" &&
|
|
65
|
-
typeof reflect.getOwnMetadata === "function" &&
|
|
66
|
-
typeof reflect.defineMetadata === "function"
|
|
67
|
-
) {
|
|
68
|
-
try {
|
|
69
|
-
const metadataKeys = reflect.getOwnMetadataKeys(source)
|
|
70
|
-
for (let i = 0, len = metadataKeys.length; i < len; i++) {
|
|
71
|
-
try {
|
|
72
|
-
reflect.defineMetadata!(
|
|
73
|
-
metadataKeys[i],
|
|
74
|
-
reflect.getOwnMetadata!(metadataKeys[i], source),
|
|
75
|
-
target
|
|
76
|
-
)
|
|
77
|
-
} catch {
|
|
78
|
-
// ignore non-copyable metadata entries
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
} catch {
|
|
82
|
-
// ignore metadata providers that throw during key discovery
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
2
|
+
import { setDataModelAction } from "../dataModel/actions"
|
|
3
|
+
import { BaseDataModel } from "../dataModel/BaseDataModel"
|
|
4
|
+
import { isDataModel, isDataModelClass } from "../dataModel/utils"
|
|
5
|
+
import { BaseModel } from "../model/BaseModel"
|
|
6
|
+
import { modelInfoByClass } from "../modelShared/modelInfo"
|
|
7
|
+
import { runAfterModelDecoratorSymbol } from "../modelShared/modelSymbols"
|
|
8
|
+
import { addLateInitializationFunction, failure, inDevMode, runAfterNewSymbol } from "./index"
|
|
9
|
+
|
|
10
|
+
type WrapFunction = (
|
|
11
|
+
data: {
|
|
12
|
+
actionName: string | (() => string)
|
|
13
|
+
overrideContext: WrapInActionOverrideContextFn | undefined
|
|
14
|
+
},
|
|
15
|
+
fn: any
|
|
16
|
+
) => any
|
|
17
|
+
|
|
18
|
+
const unboundMethodSymbol = Symbol("unboundMethod")
|
|
19
|
+
|
|
20
|
+
const functionPropertiesToSkip = new Set<PropertyKey>([
|
|
21
|
+
"arguments",
|
|
22
|
+
"caller",
|
|
23
|
+
"length",
|
|
24
|
+
"name",
|
|
25
|
+
"prototype",
|
|
26
|
+
unboundMethodSymbol,
|
|
27
|
+
])
|
|
28
|
+
|
|
29
|
+
type ReflectWithMetadata = typeof Reflect & {
|
|
30
|
+
getOwnMetadataKeys?: (target: object) => any[]
|
|
31
|
+
getOwnMetadata?: (metadataKey: any, target: object) => any
|
|
32
|
+
defineMetadata?: (metadataKey: any, metadataValue: any, target: object) => void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export function copyFunctionMetadata(source: unknown, target: unknown): void {
|
|
39
|
+
if (typeof source !== "function" || typeof target !== "function" || source === target) {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const keys = Reflect.ownKeys(source)
|
|
44
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
45
|
+
const key = keys[i]
|
|
46
|
+
if (functionPropertiesToSkip.has(key)) {
|
|
47
|
+
continue
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const descriptor = Object.getOwnPropertyDescriptor(source, key)
|
|
51
|
+
if (!descriptor) {
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
Object.defineProperty(target, key, descriptor)
|
|
57
|
+
} catch {
|
|
58
|
+
// ignore non-copyable properties
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const reflect = Reflect as ReflectWithMetadata
|
|
63
|
+
if (
|
|
64
|
+
typeof reflect.getOwnMetadataKeys === "function" &&
|
|
65
|
+
typeof reflect.getOwnMetadata === "function" &&
|
|
66
|
+
typeof reflect.defineMetadata === "function"
|
|
67
|
+
) {
|
|
68
|
+
try {
|
|
69
|
+
const metadataKeys = reflect.getOwnMetadataKeys(source)
|
|
70
|
+
for (let i = 0, len = metadataKeys.length; i < len; i++) {
|
|
71
|
+
try {
|
|
72
|
+
reflect.defineMetadata!(
|
|
73
|
+
metadataKeys[i],
|
|
74
|
+
reflect.getOwnMetadata!(metadataKeys[i], source),
|
|
75
|
+
target
|
|
76
|
+
)
|
|
77
|
+
} catch {
|
|
78
|
+
// ignore non-copyable metadata entries
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
// ignore metadata providers that throw during key discovery
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
87
|
const bindMethod = (method: any, instance: any) => {
|
|
88
|
-
const unboundMethod = unboundMethodSymbol in method ? method[unboundMethodSymbol] : method
|
|
89
|
-
|
|
90
|
-
const boundMethod = unboundMethod.bind(instance)
|
|
91
|
-
copyFunctionMetadata(unboundMethod, boundMethod)
|
|
92
|
-
boundMethod[unboundMethodSymbol] = unboundMethod
|
|
93
|
-
|
|
94
|
-
return boundMethod
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
export function decorateWrapMethodOrField(
|
|
101
|
-
decoratorName: string,
|
|
102
|
-
args: any[],
|
|
103
|
-
wrap: WrapFunction
|
|
104
|
-
): any {
|
|
105
|
-
if (typeof args[1] !== "object") {
|
|
106
|
-
// non-standard decorators
|
|
107
|
-
|
|
108
|
-
const target = args[0]
|
|
109
|
-
const propertyKey: string = args[1]
|
|
110
|
-
const baseDescriptor: PropertyDescriptor | undefined = args[2]
|
|
111
|
-
|
|
112
|
-
checkModelDecoratorTaget(decoratorName, target)
|
|
113
|
-
checkDecoratorContext("transaction", propertyKey, false)
|
|
114
|
-
|
|
115
|
-
const data = getActionNameAndContextOverride(target, propertyKey, true)
|
|
116
|
-
|
|
88
|
+
const unboundMethod = unboundMethodSymbol in method ? method[unboundMethodSymbol] : method
|
|
89
|
+
|
|
90
|
+
const boundMethod = unboundMethod.bind(instance)
|
|
91
|
+
copyFunctionMetadata(unboundMethod, boundMethod)
|
|
92
|
+
boundMethod[unboundMethodSymbol] = unboundMethod
|
|
93
|
+
|
|
94
|
+
return boundMethod
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export function decorateWrapMethodOrField(
|
|
101
|
+
decoratorName: string,
|
|
102
|
+
args: any[],
|
|
103
|
+
wrap: WrapFunction
|
|
104
|
+
): any {
|
|
105
|
+
if (typeof args[1] !== "object") {
|
|
106
|
+
// non-standard decorators
|
|
107
|
+
|
|
108
|
+
const target = args[0]
|
|
109
|
+
const propertyKey: string = args[1]
|
|
110
|
+
const baseDescriptor: PropertyDescriptor | undefined = args[2]
|
|
111
|
+
|
|
112
|
+
checkModelDecoratorTaget(decoratorName, target)
|
|
113
|
+
checkDecoratorContext("transaction", propertyKey, false)
|
|
114
|
+
|
|
115
|
+
const data = getActionNameAndContextOverride(target, propertyKey, true)
|
|
116
|
+
|
|
117
117
|
const addFieldDecorator = () => {
|
|
118
118
|
addLateInitializationFunction(target, runAfterNewSymbol, (instance) => {
|
|
119
119
|
const method = wrap(data, instance[propertyKey])
|
|
@@ -121,16 +121,16 @@ export function decorateWrapMethodOrField(
|
|
|
121
121
|
// all of this is to make method destructuring work
|
|
122
122
|
instance[propertyKey] = bindMethod(method, instance)
|
|
123
123
|
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (baseDescriptor) {
|
|
127
|
-
if (baseDescriptor.get !== undefined) {
|
|
128
|
-
throw failure(`@${decoratorName} cannot be used with getters`)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (baseDescriptor.value) {
|
|
132
|
-
// babel / typescript - method decorator
|
|
133
|
-
// @action method() { }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (baseDescriptor) {
|
|
127
|
+
if (baseDescriptor.get !== undefined) {
|
|
128
|
+
throw failure(`@${decoratorName} cannot be used with getters`)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (baseDescriptor.value) {
|
|
132
|
+
// babel / typescript - method decorator
|
|
133
|
+
// @action method() { }
|
|
134
134
|
return {
|
|
135
135
|
enumerable: false,
|
|
136
136
|
writable: true,
|
|
@@ -138,163 +138,163 @@ export function decorateWrapMethodOrField(
|
|
|
138
138
|
value: wrap(data, baseDescriptor.value),
|
|
139
139
|
}
|
|
140
140
|
} else {
|
|
141
|
-
// babel - field decorator: @action method = () => {}
|
|
142
|
-
addFieldDecorator()
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
// typescript - field decorator: @action method = () => {}
|
|
146
|
-
addFieldDecorator()
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
// standard decorators
|
|
150
|
-
const ctx = args[1] as ClassMethodDecoratorContext | ClassFieldDecoratorContext
|
|
151
|
-
|
|
152
|
-
checkDecoratorContext(decoratorName, ctx.name, ctx.static)
|
|
153
|
-
switch (ctx.kind) {
|
|
154
|
-
case "method": {
|
|
155
|
-
// @action method() { }
|
|
156
|
-
const value = args[0]
|
|
157
|
-
const propertyKey = ctx.name as string
|
|
158
|
-
|
|
159
|
-
let inited = false
|
|
160
|
-
|
|
161
|
-
ctx.addInitializer(function (this: any) {
|
|
162
|
-
// only do one override on first initialization for the whole class
|
|
163
|
-
if (inited) {
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
inited = true
|
|
167
|
-
|
|
168
|
-
const target = this
|
|
169
|
-
checkModelDecoratorTaget(decoratorName, target)
|
|
170
|
-
|
|
171
|
-
// find the deepest proto that matches the value
|
|
172
|
-
let proto = this
|
|
173
|
-
let nextProto = Object.getPrototypeOf(proto)
|
|
174
|
-
while (nextProto && nextProto[propertyKey] === value) {
|
|
175
|
-
proto = nextProto
|
|
176
|
-
nextProto = Object.getPrototypeOf(proto)
|
|
177
|
-
}
|
|
178
|
-
|
|
141
|
+
// babel - field decorator: @action method = () => {}
|
|
142
|
+
addFieldDecorator()
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
// typescript - field decorator: @action method = () => {}
|
|
146
|
+
addFieldDecorator()
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
// standard decorators
|
|
150
|
+
const ctx = args[1] as ClassMethodDecoratorContext | ClassFieldDecoratorContext
|
|
151
|
+
|
|
152
|
+
checkDecoratorContext(decoratorName, ctx.name, ctx.static)
|
|
153
|
+
switch (ctx.kind) {
|
|
154
|
+
case "method": {
|
|
155
|
+
// @action method() { }
|
|
156
|
+
const value = args[0]
|
|
157
|
+
const propertyKey = ctx.name as string
|
|
158
|
+
|
|
159
|
+
let inited = false
|
|
160
|
+
|
|
161
|
+
ctx.addInitializer(function (this: any) {
|
|
162
|
+
// only do one override on first initialization for the whole class
|
|
163
|
+
if (inited) {
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
inited = true
|
|
167
|
+
|
|
168
|
+
const target = this
|
|
169
|
+
checkModelDecoratorTaget(decoratorName, target)
|
|
170
|
+
|
|
171
|
+
// find the deepest proto that matches the value
|
|
172
|
+
let proto = this
|
|
173
|
+
let nextProto = Object.getPrototypeOf(proto)
|
|
174
|
+
while (nextProto && nextProto[propertyKey] === value) {
|
|
175
|
+
proto = nextProto
|
|
176
|
+
nextProto = Object.getPrototypeOf(proto)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
179
|
proto[propertyKey] = wrap(
|
|
180
180
|
getActionNameAndContextOverride(target, propertyKey, false),
|
|
181
181
|
proto[propertyKey]
|
|
182
182
|
)
|
|
183
183
|
})
|
|
184
|
-
|
|
185
|
-
break
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
case "field": {
|
|
189
|
-
// @action method = () => {}
|
|
190
|
-
const propertyKey = ctx.name as string
|
|
191
|
-
|
|
192
|
-
let data: ReturnType<typeof getActionNameAndContextOverride> | undefined
|
|
193
|
-
|
|
194
|
-
return function (this: any, value: any) {
|
|
195
|
-
const instance = this
|
|
196
|
-
|
|
197
|
-
if (!data) {
|
|
198
|
-
checkModelDecoratorTaget(decoratorName, instance)
|
|
199
|
-
data = getActionNameAndContextOverride(instance, propertyKey, false)
|
|
200
|
-
}
|
|
201
|
-
|
|
184
|
+
|
|
185
|
+
break
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
case "field": {
|
|
189
|
+
// @action method = () => {}
|
|
190
|
+
const propertyKey = ctx.name as string
|
|
191
|
+
|
|
192
|
+
let data: ReturnType<typeof getActionNameAndContextOverride> | undefined
|
|
193
|
+
|
|
194
|
+
return function (this: any, value: any) {
|
|
195
|
+
const instance = this
|
|
196
|
+
|
|
197
|
+
if (!data) {
|
|
198
|
+
checkModelDecoratorTaget(decoratorName, instance)
|
|
199
|
+
data = getActionNameAndContextOverride(instance, propertyKey, false)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
202
|
const method = wrap(data, value)
|
|
203
203
|
|
|
204
204
|
// all of this is to make method destructuring work
|
|
205
205
|
return bindMethod(method, instance)
|
|
206
206
|
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
default:
|
|
210
|
-
throw failure(`@${decoratorName} can only be used on fields or methods}`)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* @internal
|
|
217
|
-
*/
|
|
218
|
-
export function checkDecoratorContext(
|
|
219
|
-
decoratorName: string,
|
|
220
|
-
propertyKey: string | symbol,
|
|
221
|
-
isStatic: boolean
|
|
222
|
-
) {
|
|
223
|
-
if (!inDevMode) {
|
|
224
|
-
return
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (typeof propertyKey !== "string") {
|
|
228
|
-
throw failure(`@${decoratorName} cannot decorate symbol properties`)
|
|
229
|
-
}
|
|
230
|
-
if (isStatic) {
|
|
231
|
-
throw failure(`@${decoratorName} cannot be used with static fields or methods`)
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const dataModelOverrideContext: WrapInActionOverrideContextFn = (ctx, self) => {
|
|
236
|
-
ctx.target = self.$
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function getActionNameAndContextOverride(
|
|
240
|
-
target: any,
|
|
241
|
-
propertyKey: string,
|
|
242
|
-
runLate: boolean
|
|
243
|
-
): {
|
|
244
|
-
actionName: string | (() => string)
|
|
245
|
-
overrideContext: WrapInActionOverrideContextFn | undefined
|
|
246
|
-
} {
|
|
247
|
-
if (isDataModelClass(target) || isDataModel(target)) {
|
|
248
|
-
const modelClass: any = isDataModelClass(target) ? target : target.constructor
|
|
249
|
-
|
|
250
|
-
let fullActionName: string
|
|
251
|
-
|
|
252
|
-
const lateInit = (finalClass: any) => {
|
|
253
|
-
const modelInfo = modelInfoByClass.get(finalClass)!
|
|
254
|
-
fullActionName = `fn::${modelInfo.name}::${propertyKey}`
|
|
255
|
-
setDataModelAction(fullActionName, modelInfo.class, propertyKey)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (runLate) {
|
|
259
|
-
addLateInitializationFunction(modelClass, runAfterModelDecoratorSymbol, lateInit)
|
|
260
|
-
} else {
|
|
261
|
-
lateInit(modelClass)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return {
|
|
265
|
-
actionName: () => fullActionName,
|
|
266
|
-
overrideContext: dataModelOverrideContext,
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
return { actionName: propertyKey, overrideContext: undefined }
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function checkModelDecoratorTaget(decoratorName: string, target: any) {
|
|
274
|
-
if (!inDevMode) {
|
|
275
|
-
return
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const errMessage = `@${decoratorName} must be used over model classes or instances`
|
|
279
|
-
|
|
280
|
-
if (!target) {
|
|
281
|
-
throw failure(errMessage)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// check target is a model object or extended class
|
|
285
|
-
const isModel =
|
|
286
|
-
target instanceof BaseModel || target === BaseModel || target.prototype instanceof BaseModel
|
|
287
|
-
if (isModel) {
|
|
288
|
-
return
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const isDataModel =
|
|
292
|
-
target instanceof BaseDataModel ||
|
|
293
|
-
target === BaseDataModel ||
|
|
294
|
-
target.prototype instanceof BaseDataModel
|
|
295
|
-
if (isDataModel) {
|
|
296
|
-
return
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
throw failure(errMessage)
|
|
300
|
-
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
default:
|
|
210
|
+
throw failure(`@${decoratorName} can only be used on fields or methods}`)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @internal
|
|
217
|
+
*/
|
|
218
|
+
export function checkDecoratorContext(
|
|
219
|
+
decoratorName: string,
|
|
220
|
+
propertyKey: string | symbol,
|
|
221
|
+
isStatic: boolean
|
|
222
|
+
) {
|
|
223
|
+
if (!inDevMode) {
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (typeof propertyKey !== "string") {
|
|
228
|
+
throw failure(`@${decoratorName} cannot decorate symbol properties`)
|
|
229
|
+
}
|
|
230
|
+
if (isStatic) {
|
|
231
|
+
throw failure(`@${decoratorName} cannot be used with static fields or methods`)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const dataModelOverrideContext: WrapInActionOverrideContextFn = (ctx, self) => {
|
|
236
|
+
ctx.target = self.$
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function getActionNameAndContextOverride(
|
|
240
|
+
target: any,
|
|
241
|
+
propertyKey: string,
|
|
242
|
+
runLate: boolean
|
|
243
|
+
): {
|
|
244
|
+
actionName: string | (() => string)
|
|
245
|
+
overrideContext: WrapInActionOverrideContextFn | undefined
|
|
246
|
+
} {
|
|
247
|
+
if (isDataModelClass(target) || isDataModel(target)) {
|
|
248
|
+
const modelClass: any = isDataModelClass(target) ? target : target.constructor
|
|
249
|
+
|
|
250
|
+
let fullActionName: string
|
|
251
|
+
|
|
252
|
+
const lateInit = (finalClass: any) => {
|
|
253
|
+
const modelInfo = modelInfoByClass.get(finalClass)!
|
|
254
|
+
fullActionName = `fn::${modelInfo.name}::${propertyKey}`
|
|
255
|
+
setDataModelAction(fullActionName, modelInfo.class, propertyKey)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (runLate) {
|
|
259
|
+
addLateInitializationFunction(modelClass, runAfterModelDecoratorSymbol, lateInit)
|
|
260
|
+
} else {
|
|
261
|
+
lateInit(modelClass)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
actionName: () => fullActionName,
|
|
266
|
+
overrideContext: dataModelOverrideContext,
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
return { actionName: propertyKey, overrideContext: undefined }
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function checkModelDecoratorTaget(decoratorName: string, target: any) {
|
|
274
|
+
if (!inDevMode) {
|
|
275
|
+
return
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const errMessage = `@${decoratorName} must be used over model classes or instances`
|
|
279
|
+
|
|
280
|
+
if (!target) {
|
|
281
|
+
throw failure(errMessage)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// check target is a model object or extended class
|
|
285
|
+
const isModel =
|
|
286
|
+
target instanceof BaseModel || target === BaseModel || target.prototype instanceof BaseModel
|
|
287
|
+
if (isModel) {
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const isDataModel =
|
|
292
|
+
target instanceof BaseDataModel ||
|
|
293
|
+
target === BaseDataModel ||
|
|
294
|
+
target.prototype instanceof BaseDataModel
|
|
295
|
+
if (isDataModel) {
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
throw failure(errMessage)
|
|
300
|
+
}
|