UNPKG

12.2 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { InjectionToken, PLATFORM_ID, Injectable, Inject, Optional, NgModule } from '@angular/core';
3import { pipe, of, EMPTY, concat, Observable } from 'rxjs';
4import { map, distinctUntilChanged, filter, withLatestFrom, scan, observeOn, switchMap, startWith, shareReplay, groupBy, mergeMap, debounceTime } from 'rxjs/operators';
5import * as i1 from '@angular/fire';
6import { keepUnstableUntilFirst, VERSION } from '@angular/fire';
7import { ɵfirebaseAppFactory, ɵcacheInstance, ɵlazySDKProxy, FIREBASE_OPTIONS, FIREBASE_APP_NAME, ɵapplyMixins } from '@angular/fire/compat';
8import { isSupported } from 'firebase/remote-config';
9import firebase from 'firebase/compat/app';
10
11// DO NOT MODIFY, this file is autogenerated by tools/build.ts
12// Export a null object with the same keys as firebase/compat/remote-config, so Proxy can work with proxy-polyfill in Internet Explorer
13const proxyPolyfillCompat = {
14 app: null,
15 settings: null,
16 defaultConfig: null,
17 fetchTimeMillis: null,
18 lastFetchStatus: null,
19 activate: null,
20 ensureInitialized: null,
21 fetch: null,
22 fetchAndActivate: null,
23 getAll: null,
24 getBoolean: null,
25 getNumber: null,
26 getString: null,
27 getValue: null,
28 setLogLevel: null,
29};
30
31const SETTINGS = new InjectionToken('angularfire2.remoteConfig.settings');
32const DEFAULTS = new InjectionToken('angularfire2.remoteConfig.defaultConfig');
33const AS_TO_FN = { strings: 'asString', numbers: 'asNumber', booleans: 'asBoolean' };
34const STATIC_VALUES = { numbers: 0, booleans: false, strings: undefined };
35// TODO look into the types here, I don't like the anys
36const proxyAll = (observable, as) => new Proxy(observable.pipe(mapToObject(as)), {
37 get: (self, name) => self[name] || observable.pipe(map(all => all.find(p => p.key === name)), map(param => param ? param[AS_TO_FN[as]]() : STATIC_VALUES[as]), distinctUntilChanged())
38});
39// TODO export as implements Partial<...> so minor doesn't break us
40class Value {
41 // tslint:disable-next-line:variable-name
42 constructor(_source, _value) {
43 this._source = _source;
44 this._value = _value;
45 }
46 asBoolean() {
47 return ['1', 'true', 't', 'y', 'yes', 'on'].indexOf(this._value.toLowerCase()) > -1;
48 }
49 asString() {
50 return this._value;
51 }
52 asNumber() {
53 return Number(this._value) || 0;
54 }
55 getSource() {
56 return this._source;
57 }
58}
59// SEMVER use ConstructorParameters when we can support Typescript 3.6
60class Parameter extends Value {
61 constructor(key, fetchTimeMillis, source, value) {
62 super(source, value);
63 this.key = key;
64 this.fetchTimeMillis = fetchTimeMillis;
65 }
66}
67// If it's a Parameter array, test any, else test the individual Parameter
68const filterTest = (fn) => filter(it => Array.isArray(it) ? it.some(fn) : fn(it));
69// Allow the user to bypass the default values and wait till they get something from the server, even if it's a cached copy;
70// if used in conjuntion with first() it will only fetch RC values from the server if they aren't cached locally
71const filterRemote = () => filterTest(p => p.getSource() === 'remote');
72// filterFresh allows the developer to effectively set up a maximum cache time
73const filterFresh = (howRecentInMillis) => filterTest(p => p.fetchTimeMillis + howRecentInMillis >= new Date().getTime());
74// I ditched loading the defaults into RC and a simple map for scan since we already have our own defaults implementation.
75// The idea here being that if they have a default that never loads from the server, they will be able to tell via fetchTimeMillis
76// on the Parameter. Also if it doesn't come from the server it won't emit again in .changes, due to the distinctUntilChanged,
77// which we can simplify to === rather than deep comparison
78const scanToParametersArray = (remoteConfig) => pipe(withLatestFrom(remoteConfig), scan((existing, [all, rc]) => {
79 // SEMVER use "new Set" to unique once we're only targeting es6
80 // at the scale we expect remote config to be at, we probably won't see a performance hit from this unoptimized uniqueness
81 // implementation.
82 // const allKeys = [...new Set([...existing.map(p => p.key), ...Object.keys(all)])];
83 const allKeys = [...existing.map(p => p.key), ...Object.keys(all)].filter((v, i, a) => a.indexOf(v) === i);
84 return allKeys.map(key => {
85 const updatedValue = all[key];
86 return updatedValue ? new Parameter(key, rc ? rc.fetchTimeMillis : -1, updatedValue.getSource(), updatedValue.asString())
87 : existing.find(p => p.key === key);
88 });
89}, []));
90class AngularFireRemoteConfig {
91 constructor(options, name, settings, defaultConfig, zone, schedulers,
92 // tslint:disable-next-line:ban-types
93 platformId) {
94 this.zone = zone;
95 const remoteConfig$ = of(undefined).pipe(observeOn(schedulers.outsideAngular), switchMap(() => isSupported()), switchMap(isSupported => isSupported ? import('firebase/compat/remote-config') : EMPTY), map(() => ɵfirebaseAppFactory(options, zone, name)), map(app => ɵcacheInstance(`${app.name}.remote-config`, 'AngularFireRemoteConfig', app.name, () => {
96 const rc = app.remoteConfig();
97 if (settings) {
98 rc.settings = settings;
99 }
100 if (defaultConfig) {
101 rc.defaultConfig = defaultConfig;
102 }
103 return rc;
104 }, [settings, defaultConfig])), startWith(undefined), shareReplay({ bufferSize: 1, refCount: false }));
105 const loadedRemoteConfig$ = remoteConfig$.pipe(filter(rc => !!rc));
106 const default$ = of(Object.keys(defaultConfig || {}).reduce((c, k) => (Object.assign(Object.assign({}, c), { [k]: new Value('default', defaultConfig[k].toString()) })), {}));
107 // we should filter out the defaults we provided to RC, since we have our own implementation
108 // that gives us a -1 for fetchTimeMillis (so filterFresh can filter them out)
109 const filterOutDefaults = map(all => Object.keys(all)
110 .filter(key => all[key].getSource() !== 'default')
111 .reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: all[key] })), {}));
112 const existing$ = loadedRemoteConfig$.pipe(switchMap(rc => rc.activate()
113 .then(() => rc.ensureInitialized())
114 .then(() => rc.getAll())), filterOutDefaults);
115 const fresh$ = loadedRemoteConfig$.pipe(switchMap(rc => zone.runOutsideAngular(() => rc.fetchAndActivate()
116 .then(() => rc.ensureInitialized())
117 .then(() => rc.getAll()))), filterOutDefaults);
118 this.parameters = concat(default$, existing$, fresh$).pipe(scanToParametersArray(remoteConfig$), keepUnstableUntilFirst, shareReplay({ bufferSize: 1, refCount: true }));
119 this.changes = this.parameters.pipe(switchMap(params => of(...params)), groupBy(param => param.key), mergeMap(group => group.pipe(distinctUntilChanged())));
120 this.strings = proxyAll(this.parameters, 'strings');
121 this.booleans = proxyAll(this.parameters, 'booleans');
122 this.numbers = proxyAll(this.parameters, 'numbers');
123 return ɵlazySDKProxy(this, loadedRemoteConfig$, zone);
124 }
125}
126AngularFireRemoteConfig.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfig, deps: [{ token: FIREBASE_OPTIONS }, { token: FIREBASE_APP_NAME, optional: true }, { token: SETTINGS, optional: true }, { token: DEFAULTS, optional: true }, { token: i0.NgZone }, { token: i1.ɵAngularFireSchedulers }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
127AngularFireRemoteConfig.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfig, providedIn: 'any' });
128i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfig, decorators: [{
129 type: Injectable,
130 args: [{
131 providedIn: 'any'
132 }]
133 }], ctorParameters: function () { return [{ type: undefined, decorators: [{
134 type: Inject,
135 args: [FIREBASE_OPTIONS]
136 }] }, { type: undefined, decorators: [{
137 type: Optional
138 }, {
139 type: Inject,
140 args: [FIREBASE_APP_NAME]
141 }] }, { type: undefined, decorators: [{
142 type: Optional
143 }, {
144 type: Inject,
145 args: [SETTINGS]
146 }] }, { type: undefined, decorators: [{
147 type: Optional
148 }, {
149 type: Inject,
150 args: [DEFAULTS]
151 }] }, { type: i0.NgZone }, { type: i1.ɵAngularFireSchedulers }, { type: Object, decorators: [{
152 type: Inject,
153 args: [PLATFORM_ID]
154 }] }]; } });
155const budget = (interval) => (source) => new Observable(observer => {
156 let timedOut = false;
157 // TODO use scheduler task rather than settimeout
158 const timeout = setTimeout(() => {
159 observer.complete();
160 timedOut = true;
161 }, interval);
162 return source.subscribe({
163 next(val) {
164 if (!timedOut) {
165 observer.next(val);
166 }
167 },
168 error(err) {
169 if (!timedOut) {
170 clearTimeout(timeout);
171 observer.error(err);
172 }
173 },
174 complete() {
175 if (!timedOut) {
176 clearTimeout(timeout);
177 observer.complete();
178 }
179 }
180 });
181});
182const typedMethod = (it) => {
183 switch (typeof it) {
184 case 'string':
185 return 'asString';
186 case 'boolean':
187 return 'asBoolean';
188 case 'number':
189 return 'asNumber';
190 default:
191 return 'asString';
192 }
193};
194function scanToObject(to = 'strings') {
195 return pipe(
196 // TODO cleanup
197 scan((c, p) => (Object.assign(Object.assign({}, c), { [p.key]: typeof to === 'object' ?
198 p[typedMethod(to[p.key])]() :
199 p[AS_TO_FN[to]]() })), typeof to === 'object' ?
200 to :
201 {}), debounceTime(1), budget(10), distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)));
202}
203function mapToObject(to = 'strings') {
204 return pipe(
205 // TODO this is getting a little long, cleanup
206 map((params) => params.reduce((c, p) => (Object.assign(Object.assign({}, c), { [p.key]: typeof to === 'object' ?
207 p[typedMethod(to[p.key])]() :
208 p[AS_TO_FN[to]]() })), typeof to === 'object' ?
209 to :
210 {})), distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)));
211}
212ɵapplyMixins(AngularFireRemoteConfig, [proxyPolyfillCompat]);
213
214class AngularFireRemoteConfigModule {
215 constructor() {
216 firebase.registerVersion('angularfire', VERSION.full, 'rc-compat');
217 }
218}
219AngularFireRemoteConfigModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfigModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
220AngularFireRemoteConfigModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfigModule });
221AngularFireRemoteConfigModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfigModule, providers: [AngularFireRemoteConfig] });
222i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireRemoteConfigModule, decorators: [{
223 type: NgModule,
224 args: [{
225 providers: [AngularFireRemoteConfig]
226 }]
227 }], ctorParameters: function () { return []; } });
228
229/**
230 * Generated bundle index. Do not edit.
231 */
232
233export { AngularFireRemoteConfig, AngularFireRemoteConfigModule, DEFAULTS, Parameter, SETTINGS, Value, budget, filterFresh, filterRemote, mapToObject, scanToObject };
234//# sourceMappingURL=angular-fire-compat-remote-config.js.map