|
1 | 1 | import { assert } from '@ember/debug'; |
2 | 2 | import { ENV } from '@ember/-internals/environment/lib/env'; |
3 | | -import type { |
4 | | - ElementDescriptor, |
5 | | - ExtendedMethodDecorator, |
6 | | -} from '@ember/-internals/metal/lib/decorator'; |
7 | | -import { isElementDescriptor, setClassicDecorator } from '@ember/-internals/metal/lib/decorator'; |
8 | 3 | import expandProperties from '@ember/-internals/metal/lib/expand_properties'; |
9 | 4 | import { getFactoryFor } from '@ember/-internals/container/lib/container'; |
10 | 5 | import { setObservers } from '@ember/-internals/utils/lib/super'; |
@@ -109,120 +104,7 @@ export default EmberObject; |
109 | 104 | @return {PropertyDecorator} property decorator instance |
110 | 105 | */ |
111 | 106 |
|
112 | | -const BINDINGS_MAP = new WeakMap(); |
113 | | - |
114 | | -interface HasProto { |
115 | | - constructor: { |
116 | | - proto(): void; |
117 | | - }; |
118 | | -} |
119 | | - |
120 | | -function hasProto(obj: unknown): obj is HasProto { |
121 | | - return ( |
122 | | - obj != null && |
123 | | - (obj as any).constructor !== undefined && |
124 | | - typeof ((obj as any).constructor as any).proto === 'function' |
125 | | - ); |
126 | | -} |
127 | | - |
128 | | -interface HasActions { |
129 | | - actions: Record<string | symbol, unknown>; |
130 | | -} |
131 | | - |
132 | | -function setupAction( |
133 | | - target: Partial<HasActions>, |
134 | | - key: string | symbol, |
135 | | - actionFn: Function |
136 | | -): TypedPropertyDescriptor<unknown> { |
137 | | - if (hasProto(target)) { |
138 | | - target.constructor.proto(); |
139 | | - } |
140 | | - |
141 | | - if (!Object.prototype.hasOwnProperty.call(target, 'actions')) { |
142 | | - let parentActions = target.actions; |
143 | | - // we need to assign because of the way mixins copy actions down when inheriting |
144 | | - target.actions = parentActions ? Object.assign({}, parentActions) : {}; |
145 | | - } |
146 | | - |
147 | | - assert("[BUG] Somehow the target doesn't have actions!", target.actions != null); |
148 | | - |
149 | | - target.actions[key] = actionFn; |
150 | | - |
151 | | - return { |
152 | | - get() { |
153 | | - let bindings = BINDINGS_MAP.get(this); |
154 | | - |
155 | | - if (bindings === undefined) { |
156 | | - bindings = new Map(); |
157 | | - BINDINGS_MAP.set(this, bindings); |
158 | | - } |
159 | | - |
160 | | - let fn = bindings.get(actionFn); |
161 | | - |
162 | | - if (fn === undefined) { |
163 | | - fn = actionFn.bind(this); |
164 | | - bindings.set(actionFn, fn); |
165 | | - } |
166 | | - |
167 | | - return fn; |
168 | | - }, |
169 | | - }; |
170 | | -} |
171 | | - |
172 | | -export function action( |
173 | | - target: ElementDescriptor[0], |
174 | | - key: ElementDescriptor[1], |
175 | | - desc: ElementDescriptor[2] |
176 | | -): PropertyDescriptor; |
177 | | -export function action(desc: PropertyDescriptor): ExtendedMethodDecorator; |
178 | | -export function action( |
179 | | - ...args: ElementDescriptor | [PropertyDescriptor] |
180 | | -): PropertyDescriptor | ExtendedMethodDecorator { |
181 | | - let actionFn: object | Function; |
182 | | - |
183 | | - if (!isElementDescriptor(args)) { |
184 | | - actionFn = args[0]; |
185 | | - |
186 | | - let decorator: ExtendedMethodDecorator = function ( |
187 | | - target, |
188 | | - key, |
189 | | - _desc, |
190 | | - _meta, |
191 | | - isClassicDecorator |
192 | | - ) { |
193 | | - assert( |
194 | | - 'The @action decorator may only be passed a method when used in classic classes. You should decorate methods directly in native classes', |
195 | | - isClassicDecorator |
196 | | - ); |
197 | | - |
198 | | - assert( |
199 | | - 'The action() decorator must be passed a method when used in classic classes', |
200 | | - typeof actionFn === 'function' |
201 | | - ); |
202 | | - |
203 | | - return setupAction(target, key, actionFn); |
204 | | - }; |
205 | | - |
206 | | - setClassicDecorator(decorator); |
207 | | - |
208 | | - return decorator; |
209 | | - } |
210 | | - |
211 | | - let [target, key, desc] = args; |
212 | | - |
213 | | - actionFn = desc?.value; |
214 | | - |
215 | | - assert( |
216 | | - 'The @action decorator must be applied to methods when used in native classes', |
217 | | - typeof actionFn === 'function' |
218 | | - ); |
219 | | - |
220 | | - // SAFETY: TS types are weird with decorators. This should work. |
221 | | - return setupAction(target, key, actionFn); |
222 | | -} |
223 | | - |
224 | | -// SAFETY: TS types are weird with decorators. This should work. |
225 | | -setClassicDecorator(action as ExtendedMethodDecorator); |
| 107 | +export { action } from './action'; |
226 | 108 |
|
227 | 109 | // .......................................................... |
228 | 110 | // OBSERVER HELPER |
|
0 commit comments