|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @fileoverview Provides the universal link subscriber utility to allow |
| 19 | + * multiple subscriptions for incoming universal link detection. |
| 20 | + */ |
| 21 | +goog.provide('fireauth.UniversalLinkSubscriber'); |
| 22 | + |
| 23 | +goog.require('fireauth.util'); |
| 24 | +goog.require('goog.array'); |
| 25 | + |
| 26 | +/** |
| 27 | + * Defines the universal link subscriber class used to allow multiple universal |
| 28 | + * link subscriptions since the underlying plugin only works with one. |
| 29 | + * This utility is needed since the universal link cordova plugin can only allow |
| 30 | + * one subscriber and multiple app instances can subscribe to this. |
| 31 | + * @constructor @final @struct |
| 32 | + */ |
| 33 | +fireauth.UniversalLinkSubscriber = function() { |
| 34 | + /** |
| 35 | + * @private {?function(?Object)} The master callback that subscribes directly |
| 36 | + * to universalLinks. |
| 37 | + */ |
| 38 | + this.masterCb_ = null; |
| 39 | + /** |
| 40 | + * @private {!Array<function(?Object)>} The list of external subscribers that |
| 41 | + * are triggered every time the master callback is triggered. |
| 42 | + */ |
| 43 | + this.cb_ = []; |
| 44 | +}; |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * @return {!fireauth.UniversalLinkSubscriber} The default universal link |
| 49 | + * subscriber instance. |
| 50 | + */ |
| 51 | +fireauth.UniversalLinkSubscriber.getInstance = function() { |
| 52 | + if (!fireauth.UniversalLinkSubscriber.instance_) { |
| 53 | + fireauth.UniversalLinkSubscriber.instance_ = |
| 54 | + new fireauth.UniversalLinkSubscriber(); |
| 55 | + } |
| 56 | + return fireauth.UniversalLinkSubscriber.instance_; |
| 57 | +}; |
| 58 | + |
| 59 | + |
| 60 | +/** Clears singleton instance. Useful for testing. */ |
| 61 | +fireauth.UniversalLinkSubscriber.clear = function() { |
| 62 | + fireauth.UniversalLinkSubscriber.instance_ = null; |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +/** |
| 67 | + * @private {?fireauth.UniversalLinkSubscriber} The singleton universal |
| 68 | + * link subscriber instance. |
| 69 | + */ |
| 70 | +fireauth.UniversalLinkSubscriber.instance_ = null; |
| 71 | + |
| 72 | + |
| 73 | +/** |
| 74 | + * Subscribes a callback to the universal link plugin listener. |
| 75 | + * @param {function(?Object)} cb The callback to subscribe to the universal |
| 76 | + * link plugin. |
| 77 | + */ |
| 78 | +fireauth.UniversalLinkSubscriber.prototype.subscribe = function(cb) { |
| 79 | + var self = this; |
| 80 | + this.cb_.push(cb); |
| 81 | + if (!this.masterCb_) { |
| 82 | + this.masterCb_ = function(event) { |
| 83 | + for (var i = 0; i < self.cb_.length; i++) { |
| 84 | + self.cb_[i](event); |
| 85 | + } |
| 86 | + }; |
| 87 | + var subscribe = fireauth.util.getObjectRef( |
| 88 | + 'universalLinks.subscribe', goog.global); |
| 89 | + // For iOS environments, this plugin is not used, therefore this is a no-op |
| 90 | + // and no error needs to be thrown. |
| 91 | + if (typeof subscribe === 'function') { |
| 92 | + subscribe(null, this.masterCb_); |
| 93 | + } |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | + |
| 98 | +/** |
| 99 | + * Unsubscribes a callback from the universal link plugin listener. |
| 100 | + * @param {function(?Object)} cb The callback to unsubscribe from the universal |
| 101 | + * link plugin. |
| 102 | + */ |
| 103 | +fireauth.UniversalLinkSubscriber.prototype.unsubscribe = function(cb) { |
| 104 | + goog.array.removeAllIf(this.cb_, function(ele) { |
| 105 | + return ele == cb; |
| 106 | + }); |
| 107 | +}; |
| 108 | + |
0 commit comments