Skip to content

Commit 36fb698

Browse files
authored
feat(rc) Add Remote Config
1 parent daa4c25 commit 36fb698

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/remote-config/interfaces.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import firebase from 'firebase/app';
1+
// TODO(team): This should be available in the regular package
2+
import { Settings, ValueSource } from '@firebase/remote-config-types';
23

3-
export type Settings = firebase.remoteConfig.Settings;
4+
export { Settings, ValueSource };

src/remote-config/remote-config.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
shareReplay,
1313
startWith,
1414
switchMap,
15-
tap,
1615
withLatestFrom
1716
} from 'rxjs/operators';
1817
import {
@@ -27,9 +26,16 @@ import {
2726
ɵPromiseProxy,
2827
ɵapplyMixins
2928
} from '@angular/fire';
30-
import { isPlatformBrowser } from '@angular/common';
31-
import firebase from 'firebase/app';
32-
import { Settings } from './interfaces';
29+
import {
30+
RemoteConfig,
31+
ValueType,
32+
getRemoteConfig,
33+
activate,
34+
fetchAndActivate,
35+
getAll,
36+
ensureInitialized
37+
} from 'firebase/remote-config';
38+
import { Settings, ValueSource } from './interfaces';
3339
import { proxyPolyfillCompat } from './base';
3440
import { ɵfetchInstance } from '@angular/fire';
3541

@@ -40,7 +46,7 @@ export interface ConfigTemplate {
4046
export const SETTINGS = new InjectionToken<Settings>('angularfire2.remoteConfig.settings');
4147
export const DEFAULTS = new InjectionToken<ConfigTemplate>('angularfire2.remoteConfig.defaultConfig');
4248

43-
export interface AngularFireRemoteConfig extends ɵPromiseProxy<firebase.remoteConfig.RemoteConfig> {
49+
export interface AngularFireRemoteConfig extends ɵPromiseProxy<RemoteConfig> {
4450
}
4551

4652
const AS_TO_FN = { strings: 'asString', numbers: 'asNumber', booleans: 'asBoolean' };
@@ -58,7 +64,7 @@ const proxyAll = (observable: Observable<Parameter[]>, as: 'numbers' | 'booleans
5864
) as any;
5965

6066
// TODO export as implements Partial<...> so minor doesn't break us
61-
export class Value implements firebase.remoteConfig.Value {
67+
export class Value implements ValueType {
6268
asBoolean() {
6369
return ['1', 'true', 't', 'y', 'yes', 'on'].indexOf(this._value.toLowerCase()) > -1;
6470
}
@@ -76,13 +82,13 @@ export class Value implements firebase.remoteConfig.Value {
7682
}
7783

7884
// tslint:disable-next-line:variable-name
79-
constructor(public _source: firebase.remoteConfig.ValueSource, public _value: string) {
85+
constructor(public _source: ValueSource, public _value: string) {
8086
}
8187
}
8288

8389
// SEMVER use ConstructorParameters when we can support Typescript 3.6
8490
export class Parameter extends Value {
85-
constructor(public key: string, public fetchTimeMillis: number, source: firebase.remoteConfig.ValueSource, value: string) {
91+
constructor(public key: string, public fetchTimeMillis: number, source: ValueSource, value: string) {
8692
super(source, value);
8793
}
8894
}
@@ -103,8 +109,8 @@ export const filterFresh = (howRecentInMillis: number) => filterTest(p => p.fetc
103109
// on the Parameter. Also if it doesn't come from the server it won't emit again in .changes, due to the distinctUntilChanged,
104110
// which we can simplify to === rather than deep comparison
105111
const scanToParametersArray = (
106-
remoteConfig: Observable<firebase.remoteConfig.RemoteConfig | undefined>
107-
): OperatorFunction<{ [key: string]: firebase.remoteConfig.Value }, Parameter[]> => pipe(
112+
remoteConfig: Observable<RemoteConfig | undefined>
113+
): OperatorFunction<{ [key: string]: ValueType }, Parameter[]> => pipe(
108114
withLatestFrom(remoteConfig),
109115
scan((existing, [all, rc]) => {
110116
// SEMVER use "new Set" to unique once we're only targeting es6
@@ -146,12 +152,12 @@ export class AngularFireRemoteConfig {
146152

147153
const remoteConfig$ = of(undefined).pipe(
148154
observeOn(schedulers.outsideAngular),
149-
switchMap(() => isPlatformBrowser(platformId) ? import('firebase/remote-config') : EMPTY),
150-
switchMap(() => import('@firebase/remote-config')),
151-
tap(rc => rc.registerRemoteConfig && rc.registerRemoteConfig(firebase as any)),
155+
// switchMap(() => isPlatformBrowser(platformId) ? import('firebase/remote-config') : EMPTY),
156+
// switchMap(() => import('@firebase/remote-config')),
157+
// tap(rc => rc.registerRemoteConfig && rc.registerRemoteConfig(firebase as any)),
152158
map(() => ɵfirebaseAppFactory(options, zone, nameOrConfig)),
153159
map(app => ɵfetchInstance(`${app.name}.remote-config`, 'AngularFireRemoteConfig', app, () => {
154-
const rc = app.remoteConfig();
160+
const rc = getRemoteConfig(app);
155161
if (settings) {
156162
rc.settings = settings;
157163
}
@@ -166,35 +172,35 @@ export class AngularFireRemoteConfig {
166172
);
167173

168174
const loadedRemoteConfig$ = remoteConfig$.pipe(
169-
filter<firebase.remoteConfig.RemoteConfig>(rc => !!rc)
175+
filter<RemoteConfig>(rc => !!rc)
170176
);
171177

172-
const default$: Observable<{ [key: string]: firebase.remoteConfig.Value }> = of(Object.keys(defaultConfig || {}).reduce(
178+
const default$: Observable<{ [key: string]: ValueType }> = of(Object.keys(defaultConfig || {}).reduce(
173179
(c, k) => ({ ...c, [k]: new Value('default', defaultConfig[k].toString()) }), {}
174180
));
175181

176182
// we should filter out the defaults we provided to RC, since we have our own implementation
177183
// that gives us a -1 for fetchTimeMillis (so filterFresh can filter them out)
178-
const filterOutDefaults = map<{ [key: string]: firebase.remoteConfig.Value }, { [key: string]: firebase.remoteConfig.Value }>(all =>
184+
const filterOutDefaults = map<{ [key: string]: ValueType }, { [key: string]: ValueType }>(all =>
179185
Object.keys(all)
180186
.filter(key => all[key].getSource() !== 'default')
181187
.reduce((acc, key) => ({ ...acc, [key]: all[key] }), {})
182188
);
183189

184190
const existing$ = loadedRemoteConfig$.pipe(
185191
switchMap(rc =>
186-
rc.activate()
187-
.then(() => rc.ensureInitialized())
188-
.then(() => rc.getAll())
192+
activate(rc)
193+
.then(() => ensureInitialized(rc))
194+
.then(() => getAll(rc))
189195
),
190196
filterOutDefaults
191197
);
192198

193199
const fresh$ = loadedRemoteConfig$.pipe(
194200
switchMap(rc => zone.runOutsideAngular(() =>
195-
rc.fetchAndActivate()
196-
.then(() => rc.ensureInitialized())
197-
.then(() => rc.getAll())
201+
fetchAndActivate(rc)
202+
.then(() => ensureInitialized(rc))
203+
.then(() => getAll(rc))
198204
)),
199205
filterOutDefaults
200206
);

0 commit comments

Comments
 (0)