|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC 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 | + */ |
| 8 | + |
| 9 | +import {Constructor} from './constructor'; |
| 10 | +import {Observable} from 'rxjs/Observable'; |
| 11 | +import {Subscriber} from 'rxjs/Subscriber'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Mixin that adds an initialized property to a directive which, when subscribed to, will emit a |
| 15 | + * value once markInitialized has been called, which should be done during the ngOnInit function. |
| 16 | + * If the subscription is made after it has already been marked as initialized, then it will trigger |
| 17 | + * an emit immediately. |
| 18 | + * @docs-private |
| 19 | + */ |
| 20 | +export interface HasInitialized { |
| 21 | + /** Stream that emits once during the directive/component's ngOnInit. */ |
| 22 | + initialized: Observable<void>; |
| 23 | + |
| 24 | + /** |
| 25 | + * Sets the state as initialized and must be called during ngOnInit to notify subscribers that |
| 26 | + * the directive has been initialized. |
| 27 | + * @docs-private |
| 28 | + */ |
| 29 | + _markInitialized: () => void; |
| 30 | +} |
| 31 | + |
| 32 | +/** Mixin to augment a directive with an initialized property that will emits when ngOnInit ends. */ |
| 33 | +export function mixinInitialized<T extends Constructor<{}>>(base: T): |
| 34 | + Constructor<HasInitialized> & T { |
| 35 | + return class extends base { |
| 36 | + /** Whether this directive has been marked as initialized. */ |
| 37 | + _isInitialized = false; |
| 38 | + |
| 39 | + /** |
| 40 | + * List of subscribers that subscribed before the directive was initialized. Should be notified |
| 41 | + * during _markInitialized. Set to null after pending subscribers are notified, and should |
| 42 | + * not expect to be populated after. |
| 43 | + */ |
| 44 | + _pendingSubscribers: Subscriber<void>[] | null = []; |
| 45 | + |
| 46 | + /** |
| 47 | + * Observable stream that emits when the directive initializes. If already initialized, the |
| 48 | + * subscriber is stored to be notified once _markInitialized is called. |
| 49 | + */ |
| 50 | + initialized = new Observable<void>(subscriber => { |
| 51 | + // If initialized, immediately notify the subscriber. Otherwise store the subscriber to notify |
| 52 | + // when _markInitialized is called. |
| 53 | + if (this._isInitialized) { |
| 54 | + this._notifySubscriber(subscriber); |
| 55 | + } else { |
| 56 | + this._pendingSubscribers!.push(subscriber); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + constructor(...args: any[]) { super(...args); } |
| 61 | + |
| 62 | + /** |
| 63 | + * Marks the state as initialized and notifies pending subscribers. Should be called at the end |
| 64 | + * of ngOnInit. |
| 65 | + * @docs-private |
| 66 | + */ |
| 67 | + _markInitialized(): void { |
| 68 | + if (this._isInitialized) { |
| 69 | + throw Error('This directive has already been marked as initialized and ' + |
| 70 | + 'should not be called twice.'); |
| 71 | + } |
| 72 | + |
| 73 | + this._isInitialized = true; |
| 74 | + |
| 75 | + this._pendingSubscribers!.forEach(this._notifySubscriber); |
| 76 | + this._pendingSubscribers = null; |
| 77 | + } |
| 78 | + |
| 79 | + /** Emits and completes the subscriber stream (should only emit once). */ |
| 80 | + _notifySubscriber(subscriber: Subscriber<void>): void { |
| 81 | + subscriber.next(); |
| 82 | + subscriber.complete(); |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
0 commit comments