UNPKG

1.49 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { stringify } from './facade/lang';
9/**
10 * Allows to refer to references which are not yet defined.
11 *
12 * For instance, `forwardRef` is used when the `token` which we need to refer to for the purposes of
13 * DI is declared,
14 * but not yet defined. It is also used when the `token` which we use when creating a query is not
15 * yet defined.
16 *
17 * ### Example
18 * {@example core/di/ts/forward_ref/forward_ref_spec.ts region='forward_ref'}
19 * @experimental
20 */
21export function forwardRef(forwardRefFn) {
22 forwardRefFn.__forward_ref__ = forwardRef;
23 forwardRefFn.toString = function () {
24 return stringify(this());
25 };
26 return forwardRefFn;
27}
28/**
29 * Lazily retrieves the reference value from a forwardRef.
30 *
31 * Acts as the identity function when given a non-forward-ref value.
32 *
33 * ### Example ([live demo](http://plnkr.co/edit/GU72mJrk1fiodChcmiDR?p=preview))
34 *
35 * {@example core/di/ts/forward_ref/forward_ref_spec.ts region='resolve_forward_ref'}
36 *
37 * See: {@link forwardRef}
38 * @experimental
39 */
40export function resolveForwardRef(type) {
41 if (typeof type === 'function' && type.hasOwnProperty('__forward_ref__') && type.__forward_ref__ === forwardRef) {
42 return type();
43 }
44 else {
45 return type;
46 }
47}
48//# sourceMappingURL=forward_ref.js.map
\No newline at end of file