|
1 | 1 | import * as _ from 'lodash'; |
2 | 2 | import * as firebase from 'firebase'; |
3 | 3 | import { FirebaseEnv } from './env'; |
4 | | -import * as Promise from 'bluebird'; |
5 | 4 |
|
6 | 5 | export interface AuthMode { |
7 | 6 | admin: boolean; |
8 | 7 | variable?: any; |
9 | 8 | } |
10 | 9 |
|
11 | | -export interface RefCounter { |
12 | | - admin: number; |
13 | | - noauth: number; |
14 | | - user: {[uuid: string]: number}; |
15 | | -} |
16 | | - |
17 | 10 | export default class Apps { |
18 | 11 | private static _noauth: firebase.app.App; |
19 | 12 | private static _admin: firebase.app.App; |
| 13 | + |
20 | 14 | private _env: FirebaseEnv; |
21 | | - private _refCounter: RefCounter; |
22 | 15 |
|
23 | 16 | constructor(env: FirebaseEnv) { |
24 | 17 | this._env = env; |
25 | | - this._refCounter = { |
26 | | - admin: 0, |
27 | | - noauth: 0, |
28 | | - user: {}, |
29 | | - }; |
30 | | - } |
31 | | - |
32 | | - _waitToDestroyApp(app: firebase.app.App) { |
33 | | - if (!app) { |
34 | | - console.log('resolved promise since app is empty'); |
35 | | - return Promise.resolve(); |
36 | | - } |
37 | | - return Promise.delay(120000).then(() => { |
38 | | - console.log('deleting app'); |
39 | | - return app.delete(); |
40 | | - }).then(() => { |
41 | | - console.log('app deleted'); |
42 | | - }); |
43 | | - } |
44 | | - |
45 | | - _changeRefCounter() { |
46 | | - return Promise.delay(120000).then(() => { |
47 | | - console.log('time passed'); |
48 | | - this._refCounter = { |
49 | | - admin: 1000, |
50 | | - noauth: 1000, |
51 | | - user: {}, |
52 | | - }; |
53 | | - }); |
54 | | - } |
55 | | - |
56 | | - _waitToDestroyUserApp(key: string) { |
57 | | - try { |
58 | | - let app = firebase.app(key); |
59 | | - console.log('deleting user app...'); |
60 | | - return this._waitToDestroyApp(app); |
61 | | - } catch (e) { |
62 | | - return Promise.resolve(); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - _waitToDestroyAdmin() { |
67 | | - console.log('deleting admin app...'); |
68 | | - return this._waitToDestroyApp(Apps._admin); |
69 | 18 | } |
70 | 19 |
|
71 | | - _waitToDestroyNoauth() { |
72 | | - console.log('deleting noauth app...'); |
73 | | - return this._waitToDestroyApp(Apps._noauth); |
74 | | - } |
75 | | - |
76 | | - retain(payload) { |
77 | | - let auth: AuthMode | null = _.get(payload, 'auth', null); |
78 | | - this._refCounter.admin ++; |
79 | | - if (!auth || typeof auth !== 'object') { |
80 | | - this._refCounter.noauth ++; |
81 | | - } else if (auth.admin) { |
82 | | - this._refCounter.admin ++; |
83 | | - } else if (!auth.variable) { |
84 | | - this._refCounter.noauth ++; |
85 | | - } else { |
86 | | - const key = JSON.stringify(auth.variable); |
87 | | - let count = _.get(this._refCounter.user, key, 0); |
88 | | - _.set(this._refCounter.user, key, count+1); |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - release(payload) { |
93 | | - let auth: AuthMode | null = _.get(payload, 'auth', null); |
94 | | - this._refCounter.admin --; |
95 | | - if (!auth || typeof auth !== 'object') { |
96 | | - this._refCounter.noauth --; |
97 | | - } else if (auth.admin) { |
98 | | - this._refCounter.admin --; |
99 | | - } else if (!auth.variable) { |
100 | | - this._refCounter.noauth --; |
101 | | - } else { |
102 | | - const key = JSON.stringify(auth.variable); |
103 | | - let count = _.get(this._refCounter.user, key, 0); |
104 | | - if (count < 2) { |
105 | | - this._waitToDestroyUserApp(key); |
106 | | - } |
107 | | - _.set(this._refCounter.user, key, count-1); |
108 | | - } |
109 | | - if (this._refCounter.admin === 0) { |
110 | | - this._waitToDestroyAdmin(); |
111 | | - } |
112 | | - if (this._refCounter.noauth === 0) { |
113 | | - this._waitToDestroyNoauth(); |
114 | | - } |
115 | | - } |
116 | 20 | get admin(): firebase.app.App { |
117 | | - try { |
118 | | - return firebase.app('__admin__'); |
119 | | - } catch (e) { |
120 | | - console.log('Need to init admin app'); |
121 | | - Apps._admin = firebase.initializeApp(this.firebaseArgs, '__admin__'); |
122 | | - return Apps._admin; |
123 | | - } |
| 21 | + // TODO(inlined) add credential to env |
| 22 | + Apps._admin = Apps._admin || firebase.initializeApp(this.firebaseArgs, '__admin__'); |
| 23 | + return Apps._admin; |
124 | 24 | } |
125 | 25 |
|
126 | 26 | get noauth(): firebase.app.App { |
127 | | - try { |
128 | | - return firebase.app('__noauth__'); |
129 | | - } catch (e) { |
130 | | - console.log('Need to init noauth app'); |
131 | | - Apps._noauth = firebase.initializeApp(_.omit(this.firebaseArgs, 'credential'), '__noauth__'); |
132 | | - return Apps._noauth; |
133 | | - } |
| 27 | + Apps._noauth = Apps._noauth || |
| 28 | + firebase.initializeApp(_.omit(this.firebaseArgs, 'credential'), '__noauth__'); |
| 29 | + return Apps._noauth; |
134 | 30 | } |
135 | 31 |
|
136 | 32 | forMode(auth: AuthMode): firebase.app.App { |
|
0 commit comments