@tech4good/angular-schematics 17.0.3 → 18.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/README.md +8 -0
  2. package/package.json +4 -4
  3. package/src/collection.json +0 -16
  4. package/src/component/files/__name@dasherize__/__name@dasherize__.component.ts.template +22 -8
  5. package/src/component/index.js +2 -45
  6. package/src/component/index.js.map +1 -1
  7. package/src/component/schema.json +0 -5
  8. package/src/entity/files/__name@dasherize__/__name@dasherize__.mock.ts.template +12 -0
  9. package/src/entity/files/__name@dasherize__/__name@dasherize__.model.ts.template +1 -0
  10. package/src/entity/files/__name@dasherize__/__name@dasherize__.store.ts.template +41 -0
  11. package/src/entity/index.js +29 -50
  12. package/src/entity/index.js.map +1 -1
  13. package/src/init/files/.firebaserc.template +8 -0
  14. package/src/init/files/README.md.template +6 -6
  15. package/src/init/files/angular.json.template +18 -11
  16. package/src/init/files/package.json.template +28 -29
  17. package/src/init/files/src/app/admin/{waitlist/waitlist.animations.ts.template → admin-panel/admin-panel.animations.ts.template} +1 -1
  18. package/src/init/files/src/app/admin/{waitlist/waitlist.component.html.template → admin-panel/admin-panel.component.html.template} +3 -3
  19. package/src/init/files/src/app/admin/{waitlist/waitlist.component.scss.template → admin-panel/admin-panel.component.scss.template} +1 -1
  20. package/src/init/files/src/app/admin/admin-panel/admin-panel.component.spec.ts.template +25 -0
  21. package/src/init/files/src/app/admin/admin-panel/admin-panel.component.ts.template +99 -0
  22. package/src/init/files/src/app/app.component.html.template +1 -0
  23. package/src/init/files/src/app/app.component.ts.template +14 -8
  24. package/src/init/files/src/app/app.config.ts.template +63 -0
  25. package/src/init/files/src/app/app.routes.ts.template +32 -0
  26. package/src/init/files/src/app/core/analytics/log.service.ts.template +122 -89
  27. package/src/init/files/src/app/core/analytics/time-analytics.service.ts.template +9 -17
  28. package/src/init/files/src/app/core/auth/auth.guard.ts.template +25 -22
  29. package/src/init/files/src/app/core/firebase/cached-listeners.service.ts.template +291 -0
  30. package/src/init/files/src/app/core/firebase/database.service.ts.template +56 -0
  31. package/src/init/files/src/app/core/firebase/firebase.mock.service.ts.template +276 -0
  32. package/src/init/files/src/app/core/firebase/firebase.service.ts.template +265 -215
  33. package/src/init/files/src/app/core/local-storage/session.service.ts.template +3 -4
  34. package/src/init/files/src/app/core/store/app.model.ts.template +19 -0
  35. package/src/init/files/src/app/core/store/app.store.ts.template +1074 -0
  36. package/src/init/files/src/app/core/store/auth/auth.mock.ts.template +20 -0
  37. package/src/init/files/src/app/core/store/auth/auth.store.ts.template +136 -0
  38. package/src/init/files/src/app/core/store/batch-write.firebase.service.ts.template +58 -0
  39. package/src/init/files/src/app/core/store/batch-write.mock.service.ts.template +105 -0
  40. package/src/init/files/src/app/core/store/batch-write.service.ts.template +126 -0
  41. package/src/init/files/src/app/core/store/user/user.mock.ts.template +24 -0
  42. package/src/init/files/src/app/core/store/user/user.model.ts.template +1 -1
  43. package/src/init/files/src/app/core/store/user/user.store.ts.template +41 -0
  44. package/src/init/files/src/app/core/store/user-context/user-context.mock.ts.template +24 -0
  45. package/src/init/files/src/app/core/store/user-context/user-context.model.ts.template +1 -0
  46. package/src/init/files/src/app/core/store/user-context/user-context.store.ts.template +41 -0
  47. package/src/init/files/src/app/core/utils/rand.utils.ts.template +13 -0
  48. package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.html.template +8 -13
  49. package/src/init/files/src/app/first-time/consent/consent.component.ts.template +90 -0
  50. package/src/init/files/src/app/first-time/early-access/early-access.animations.ts.template +4 -0
  51. package/src/init/files/src/app/first-time/early-access/early-access.component.html.template +69 -0
  52. package/src/init/files/src/app/{onboarding/waiting/waiting.component.scss.template → first-time/early-access/early-access.component.scss.template} +1 -1
  53. package/src/init/files/src/app/first-time/early-access/early-access.component.spec.ts.template +25 -0
  54. package/src/init/files/src/app/first-time/early-access/early-access.component.ts.template +194 -0
  55. package/src/init/files/src/app/first-time/early-access/early-access.model.ts.template +7 -0
  56. package/src/init/files/src/app/{general → first-time}/landing/landing.component.html.template +1 -1
  57. package/src/init/files/src/app/first-time/landing/landing.component.ts.template +44 -0
  58. package/src/init/files/src/app/first-time/login/login.component.html.template +10 -0
  59. package/src/init/files/src/app/first-time/login/login.component.ts.template +45 -0
  60. package/src/init/files/src/app/first-time/onboarding/onboarding.animations.ts.template +4 -0
  61. package/src/init/files/src/app/first-time/onboarding/onboarding.component.html.template +2 -0
  62. package/src/init/files/src/app/first-time/onboarding/onboarding.component.scss.template +5 -0
  63. package/src/init/files/src/app/{admin/waitlist/waitlist.component.spec.ts.template → first-time/onboarding/onboarding.component.spec.ts.template} +6 -6
  64. package/src/init/files/src/app/first-time/onboarding/onboarding.component.ts.template +41 -0
  65. package/src/init/files/src/app/main/home/home.component.html.template +5 -0
  66. package/src/init/files/src/app/main/home/home.component.ts.template +51 -0
  67. package/src/init/files/src/app/shared/navbar/navbar.component.html.template +15 -0
  68. package/src/init/files/src/app/shared/navbar/navbar.component.scss.template +43 -0
  69. package/src/init/files/src/app/shared/navbar/navbar.component.spec.ts.template +25 -0
  70. package/src/init/files/src/app/shared/navbar/navbar.component.ts.template +40 -0
  71. package/src/init/files/src/app/shared/redeployment-warning/redeployment-warning.component.html.template +9 -0
  72. package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.ts.template +17 -6
  73. package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.service.ts.template +2 -2
  74. package/src/init/files/src/assets/images/close-x.svg +3 -0
  75. package/src/init/files/src/environments/environment.ts.template +1 -0
  76. package/src/init/files/src/index.html.template +1 -1
  77. package/src/init/files/tsconfig.json.template +0 -1
  78. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.actions.ts.template +0 -26
  79. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.effects.ts.template +0 -48
  80. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.model.ts.template +0 -0
  81. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.selectors.ts.template +0 -20
  82. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.state.ts.template +0 -13
  83. package/src/container/files/__name@dasherize__/__name@dasherize__.animations.ts.template +0 -4
  84. package/src/container/files/__name@dasherize__/__name@dasherize__.component.html.template +0 -3
  85. package/src/container/files/__name@dasherize__/__name@dasherize__.component.scss.template +0 -5
  86. package/src/container/files/__name@dasherize__/__name@dasherize__.component.spec.ts.template +0 -25
  87. package/src/container/files/__name@dasherize__/__name@dasherize__.component.ts.template +0 -90
  88. package/src/container/index.d.ts +0 -2
  89. package/src/container/index.js +0 -127
  90. package/src/container/index.js.map +0 -1
  91. package/src/container/index_spec.d.ts +0 -1
  92. package/src/container/index_spec.js +0 -23
  93. package/src/container/index_spec.js.map +0 -1
  94. package/src/container/schema.json +0 -50
  95. package/src/entity/files/__name@dasherize__/__name@dasherize__.actions.ts.template +0 -249
  96. package/src/entity/files/__name@dasherize__/__name@dasherize__.effects.ts.template +0 -131
  97. package/src/entity/files/__name@dasherize__/__name@dasherize__.reducer.spec.ts.template +0 -13
  98. package/src/entity/files/__name@dasherize__/__name@dasherize__.reducer.ts.template +0 -51
  99. package/src/entity/files/__name@dasherize__/__name@dasherize__.service.ts.template +0 -50
  100. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.actions.ts.template +0 -270
  101. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.effects.ts.template +0 -131
  102. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.model.ts.template +0 -3
  103. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.spec.ts.template +0 -13
  104. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.ts.template +0 -51
  105. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.service.ts.template +0 -50
  106. package/src/entity-in-example/index.d.ts +0 -2
  107. package/src/entity-in-example/index.js +0 -141
  108. package/src/entity-in-example/index.js.map +0 -1
  109. package/src/entity-in-example/index_spec.d.ts +0 -1
  110. package/src/entity-in-example/index_spec.js +0 -23
  111. package/src/entity-in-example/index_spec.js.map +0 -1
  112. package/src/entity-in-example/schema.json +0 -45
  113. package/src/init/files/CHANGELOG.md.template +0 -10
  114. package/src/init/files/src/app/admin/admin-routing.module.ts.template +0 -17
  115. package/src/init/files/src/app/admin/admin.module.spec.ts.template +0 -13
  116. package/src/init/files/src/app/admin/admin.module.ts.template +0 -28
  117. package/src/init/files/src/app/admin/waitlist/+state/waitlist.actions.ts.template +0 -23
  118. package/src/init/files/src/app/admin/waitlist/+state/waitlist.effects.ts.template +0 -50
  119. package/src/init/files/src/app/admin/waitlist/+state/waitlist.model.ts.template +0 -0
  120. package/src/init/files/src/app/admin/waitlist/+state/waitlist.selectors.ts.template +0 -39
  121. package/src/init/files/src/app/admin/waitlist/+state/waitlist.state.ts.template +0 -13
  122. package/src/init/files/src/app/admin/waitlist/waitlist.component.ts.template +0 -141
  123. package/src/init/files/src/app/app-routing.module.ts.template +0 -26
  124. package/src/init/files/src/app/app.module.ts.template +0 -34
  125. package/src/init/files/src/app/core/core.module.ts.template +0 -75
  126. package/src/init/files/src/app/core/setup/firebase-setup.ts.template +0 -25
  127. package/src/init/files/src/app/core/setup/module-import-guard.ts.template +0 -5
  128. package/src/init/files/src/app/core/snackbar/snackbar.actions.ts.template +0 -24
  129. package/src/init/files/src/app/core/snackbar/snackbar.effects.ts.template +0 -41
  130. package/src/init/files/src/app/core/store/app-state/app-state.actions.ts.template +0 -249
  131. package/src/init/files/src/app/core/store/app-state/app-state.effects.ts.template +0 -131
  132. package/src/init/files/src/app/core/store/app-state/app-state.model.ts.template +0 -8
  133. package/src/init/files/src/app/core/store/app-state/app-state.reducer.spec.ts.template +0 -13
  134. package/src/init/files/src/app/core/store/app-state/app-state.reducer.ts.template +0 -51
  135. package/src/init/files/src/app/core/store/app-state/app-state.service.ts.template +0 -50
  136. package/src/init/files/src/app/core/store/app.actions.ts.template +0 -92
  137. package/src/init/files/src/app/core/store/app.effects.ts.template +0 -175
  138. package/src/init/files/src/app/core/store/app.reducer.ts.template +0 -36
  139. package/src/init/files/src/app/core/store/app.selectors.ts.template +0 -42
  140. package/src/init/files/src/app/core/store/auth/auth.actions.ts.template +0 -77
  141. package/src/init/files/src/app/core/store/auth/auth.effects.ts.template +0 -163
  142. package/src/init/files/src/app/core/store/auth/auth.reducer.spec.ts.template +0 -13
  143. package/src/init/files/src/app/core/store/auth/auth.reducer.ts.template +0 -26
  144. package/src/init/files/src/app/core/store/auth/auth.selectors.ts.template +0 -25
  145. package/src/init/files/src/app/core/store/cached-load-connections.service.ts.template +0 -405
  146. package/src/init/files/src/app/core/store/cached-selectors.service.ts.template +0 -376
  147. package/src/init/files/src/app/core/store/effects.helpers.ts.template +0 -66
  148. package/src/init/files/src/app/core/store/user/user.actions.ts.template +0 -248
  149. package/src/init/files/src/app/core/store/user/user.effects.ts.template +0 -149
  150. package/src/init/files/src/app/core/store/user/user.reducer.spec.ts.template +0 -13
  151. package/src/init/files/src/app/core/store/user/user.reducer.ts.template +0 -50
  152. package/src/init/files/src/app/core/store/user/user.service.ts.template +0 -50
  153. package/src/init/files/src/app/core/store/user-context/user-context.actions.ts.template +0 -248
  154. package/src/init/files/src/app/core/store/user-context/user-context.effects.ts.template +0 -131
  155. package/src/init/files/src/app/core/store/user-context/user-context.reducer.spec.ts.template +0 -13
  156. package/src/init/files/src/app/core/store/user-context/user-context.reducer.ts.template +0 -51
  157. package/src/init/files/src/app/core/store/user-context/user-context.service.ts.template +0 -50
  158. package/src/init/files/src/app/core/utils/string-case.utils.ts.template +0 -96
  159. package/src/init/files/src/app/general/general-routing.module.ts.template +0 -25
  160. package/src/init/files/src/app/general/general.module.spec.ts.template +0 -13
  161. package/src/init/files/src/app/general/general.module.ts.template +0 -36
  162. package/src/init/files/src/app/general/home/+state/home.actions.ts.template +0 -26
  163. package/src/init/files/src/app/general/home/+state/home.effects.ts.template +0 -48
  164. package/src/init/files/src/app/general/home/+state/home.model.ts.template +0 -0
  165. package/src/init/files/src/app/general/home/+state/home.selectors.ts.template +0 -20
  166. package/src/init/files/src/app/general/home/+state/home.state.ts.template +0 -13
  167. package/src/init/files/src/app/general/home/home.component.html.template +0 -5
  168. package/src/init/files/src/app/general/home/home.component.ts.template +0 -88
  169. package/src/init/files/src/app/general/landing/+state/landing.actions.ts.template +0 -26
  170. package/src/init/files/src/app/general/landing/+state/landing.effects.ts.template +0 -48
  171. package/src/init/files/src/app/general/landing/+state/landing.model.ts.template +0 -0
  172. package/src/init/files/src/app/general/landing/+state/landing.selectors.ts.template +0 -20
  173. package/src/init/files/src/app/general/landing/+state/landing.state.ts.template +0 -13
  174. package/src/init/files/src/app/general/landing/landing.component.ts.template +0 -98
  175. package/src/init/files/src/app/general/login/login.component.html.template +0 -11
  176. package/src/init/files/src/app/general/login/login.component.ts.template +0 -80
  177. package/src/init/files/src/app/onboarding/consent/+state/consent.actions.ts.template +0 -23
  178. package/src/init/files/src/app/onboarding/consent/+state/consent.effects.ts.template +0 -47
  179. package/src/init/files/src/app/onboarding/consent/+state/consent.model.ts.template +0 -0
  180. package/src/init/files/src/app/onboarding/consent/+state/consent.selectors.ts.template +0 -20
  181. package/src/init/files/src/app/onboarding/consent/+state/consent.state.ts.template +0 -13
  182. package/src/init/files/src/app/onboarding/consent/consent.component.ts.template +0 -151
  183. package/src/init/files/src/app/onboarding/onboarding-routing.module.ts.template +0 -19
  184. package/src/init/files/src/app/onboarding/onboarding.module.spec.ts.template +0 -13
  185. package/src/init/files/src/app/onboarding/onboarding.module.ts.template +0 -32
  186. package/src/init/files/src/app/onboarding/waiting/+state/waiting.actions.ts.template +0 -26
  187. package/src/init/files/src/app/onboarding/waiting/+state/waiting.effects.ts.template +0 -52
  188. package/src/init/files/src/app/onboarding/waiting/+state/waiting.model.ts.template +0 -9
  189. package/src/init/files/src/app/onboarding/waiting/+state/waiting.selectors.ts.template +0 -42
  190. package/src/init/files/src/app/onboarding/waiting/+state/waiting.state.ts.template +0 -13
  191. package/src/init/files/src/app/onboarding/waiting/waiting.animations.ts.template +0 -4
  192. package/src/init/files/src/app/onboarding/waiting/waiting.component.html.template +0 -57
  193. package/src/init/files/src/app/onboarding/waiting/waiting.component.spec.ts.template +0 -25
  194. package/src/init/files/src/app/onboarding/waiting/waiting.component.ts.template +0 -281
  195. package/src/init/files/src/app/shared/components/redeployment-warning/redeployment-warning.component.html.template +0 -10
  196. package/src/init/files/src/app/shared/shared.module.ts.template +0 -87
  197. package/src/init/files/src/main.ts.template +0 -12
  198. package/src/module/files/__name@dasherize__/__name@dasherize__-routing.module.ts.template +0 -15
  199. package/src/module/files/__name@dasherize__/__name@dasherize__.module.spec.ts.template +0 -13
  200. package/src/module/files/__name@dasherize__/__name@dasherize__.module.ts.template +0 -24
  201. package/src/module/index.d.ts +0 -2
  202. package/src/module/index.js +0 -97
  203. package/src/module/index.js.map +0 -1
  204. package/src/module/index_spec.d.ts +0 -1
  205. package/src/module/index_spec.js +0 -23
  206. package/src/module/index_spec.js.map +0 -1
  207. package/src/module/schema.json +0 -38
  208. /package/src/init/files/src/app/{onboarding → first-time}/consent/consent.animations.ts.template +0 -0
  209. /package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.scss.template +0 -0
  210. /package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.spec.ts.template +0 -0
  211. /package/src/init/files/src/app/{general → first-time}/landing/landing.animations.ts.template +0 -0
  212. /package/src/init/files/src/app/{general → first-time}/landing/landing.component.scss.template +0 -0
  213. /package/src/init/files/src/app/{general → first-time}/landing/landing.component.spec.ts.template +0 -0
  214. /package/src/init/files/src/app/{general → first-time}/login/login.component.scss.template +0 -0
  215. /package/src/init/files/src/app/{general → first-time}/login/login.component.spec.ts.template +0 -0
  216. /package/src/init/files/src/app/{general → main}/home/home.animations.ts.template +0 -0
  217. /package/src/init/files/src/app/{general → main}/home/home.component.scss.template +0 -0
  218. /package/src/init/files/src/app/{general → main}/home/home.component.spec.ts.template +0 -0
  219. /package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.scss.template +0 -0
  220. /package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.spec.ts.template +0 -0
@@ -1,405 +0,0 @@
1
- import { Injectable } from '@angular/core';
2
- import { Subject, Observable, merge, forkJoin, of, from, pipe } from 'rxjs';
3
- import { publishLast, publish, publishReplay, refCount, tap, pluck, mergeMap, filter, takeUntil, skipUntil, buffer, skip, switchMap, map, take } from 'rxjs/operators';
4
- import { Actions, ofType } from '@ngrx/effects';
5
- import { GeneralActionTypes, Unsubscribe, LoadAction } from './app.actions';
6
- import { Action, Store, select } from '@ngrx/store';
7
- import * as fromStore from '../../core/store/app.reducer';
8
- import * as fromAuth from '../../core/store/auth/auth.reducer';
9
- import { FirebaseService } from '../firebase/firebase.service';
10
- import { lowerCamelCase } from '../utils/string-case.utils';
11
-
12
- export interface CachedConnection {
13
- loadKey: string;
14
- loadStream: Observable<Action>;
15
- correlationIds: string[];
16
- }
17
-
18
- @Injectable({
19
- providedIn: 'root',
20
- })
21
- export class CachedLoadConnectionsService {
22
- /** Cached connections by correlationId. */
23
- private connectionsByCorrelationId: { [cId: string]: CachedConnection[]; } = {};
24
-
25
- /** Cached connections by loadKey. */
26
- private connectionByLoadKey: { [loadKey: string]: CachedConnection; } = {};
27
-
28
- /** Close connection events. */
29
- private closeConnection$: Subject<string> = new Subject<string>();
30
-
31
- /** Load ready events. */
32
- private loadReady$: Subject<string> = new Subject<string>();
33
-
34
- /**
35
- * Get entityName and collection from action.
36
- * TODO: Fragile: depends on correct syntax for type
37
- */
38
- private getActionAttributes(action: LoadAction): [string, string] {
39
- const l = action.type.indexOf('[');
40
- const r = action.type.indexOf(']');
41
- const entityName = action.type.slice(l + 1, r);
42
- const collection = lowerCamelCase(entityName) + 's';
43
- return [entityName, collection];
44
- }
45
-
46
- /**
47
- * Function to determine whether it is a Stream action or a Load action.
48
- * TODO: Fragile: depends on correct syntax for type
49
- */
50
- public isStream(action: LoadAction): boolean {
51
- const [entityName, collection] = this.getActionAttributes(action);
52
- if (action.type.indexOf(`[${entityName}] stream`) === -1) {
53
- return false;
54
- } else {
55
- return true;
56
- }
57
- }
58
-
59
- /** Define a unique key for each connection. */
60
- // NOTE: The order matters, so we could potentially execute the same query if in some case
61
- // we had one where query first then the other and in the other case it was reversed
62
- private getLoadKey(action: LoadAction): string {
63
- let queryOptionsString = '';
64
- if (action.queryOptions) {
65
- if (action.queryOptions.orderBy) {
66
- queryOptionsString = queryOptionsString.concat(`orderBy${action.queryOptions.orderBy}`);
67
- }
68
- if (action.queryOptions.limit) {
69
- queryOptionsString = queryOptionsString.concat(`limit${action.queryOptions.limit}`);
70
- }
71
- if (action.queryOptions.startAt) {
72
- queryOptionsString = queryOptionsString.concat(`startAt${action.queryOptions.startAt}`);
73
- }
74
- if (action.queryOptions.startAfter) {
75
- queryOptionsString = queryOptionsString.concat(`startAfter${action.queryOptions.startAfter}`);
76
- }
77
- if (action.queryOptions.endAt) {
78
- queryOptionsString = queryOptionsString.concat(`endAt${action.queryOptions.endAt}`);
79
- }
80
- if (action.queryOptions.endBefore) {
81
- queryOptionsString = queryOptionsString.concat(`endBefore${action.queryOptions.endBefore}`);
82
- }
83
- }
84
- const [entityName, collection] = this.getActionAttributes(action);
85
- return `${entityName}.${action.queryParams}.${queryOptionsString}`;
86
- }
87
-
88
- /** Gets a cached connection if one exists. */
89
- private getConnection(action: LoadAction): CachedConnection {
90
- const loadKey = this.getLoadKey(action);
91
- return this.connectionByLoadKey[loadKey];
92
- }
93
-
94
- /**
95
- * Disconnects all "virtual" connections with that correlationId.
96
- * For connections where this correlationId was the only one, disconnect
97
- * it in actuality.
98
- */
99
- public disconnect(action: Unsubscribe) {
100
- // console.log("PROCESS DISCONNECT: " + action.correlationId);
101
-
102
- // Add a 5-second delay so we don't have to reconnect multiple times
103
- // if the next view happens to use the same loading
104
- setTimeout(() => {
105
- const correlationId = action.correlationId;
106
- const connections = this.connectionsByCorrelationId[correlationId];
107
-
108
- // If there are no connections, just return
109
- if (!connections) {
110
- return;
111
- }
112
-
113
- while (connections.length > 0) {
114
- const c = connections.pop();
115
-
116
- // Remove the correlationId from the list in that connection
117
- const index = c.correlationIds.findIndex((cId) => cId === correlationId);
118
- c.correlationIds.splice(index, 1);
119
-
120
- // If there are no more correlationIds, then dispatch close connection for that particular connection
121
- if (c.correlationIds.length === 0) {
122
- this.closeConnection$.next(c.loadKey);
123
-
124
- // Update connectionByLoadKey
125
- this.connectionByLoadKey[c.loadKey] = undefined;
126
- }
127
- }
128
- }, 5000);
129
- }
130
-
131
- /**
132
- * Main function called by the outside. Some notes:
133
- * -- followupActions are only computed for initial loads and adds. They
134
- * are not recomputed if the fields that are being used change
135
- */
136
- public processLoadAction(action: LoadAction, isFollowup: boolean = false, initiatingAction?: LoadAction, loadWaitingQueue?: string[]): CachedConnection {
137
- // console.log("PROCESS LOAD: " + this.getLoadKey(action));
138
-
139
- // Check whether this action is to just load once or create an open connection
140
- const keepConnected: boolean = this.isStream(action);
141
-
142
- // Set initiating action if needed
143
- if (!isFollowup) {
144
- initiatingAction = action;
145
- loadWaitingQueue = [];
146
- }
147
-
148
- let connection;
149
-
150
- // If we're just loading once, then we don't need all the caching logic
151
- if (!keepConnected) {
152
- connection = this.createSingleLoadConnection(action, initiatingAction);
153
- } else {
154
- // get connection and process the immediate load connection required
155
- connection = this.getConnection(action);
156
-
157
- if (connection) {
158
- // if the connection already exists, don't need to create a new load connection,
159
- // but may need to add the correlationId if it doesn't already exist
160
- if (!connection.correlationIds.some((cId) => cId === action.correlationId)) {
161
- connection.correlationIds.push(action.correlationId);
162
- this.connectionsByCorrelationId[action.correlationId] = this.connectionsByCorrelationId[action.correlationId] || [];
163
- this.connectionsByCorrelationId[action.correlationId].push(connection);
164
- }
165
- } else {
166
- // if the connection does not exist, create it
167
- connection = this.createConnection(action, initiatingAction);
168
- this.connectionByLoadKey[connection.loadKey] = connection;
169
- this.connectionsByCorrelationId[action.correlationId] = this.connectionsByCorrelationId[action.correlationId] || [];
170
- this.connectionsByCorrelationId[action.correlationId].push(connection);
171
- }
172
- }
173
-
174
- // indicator that we're making another connection and waiting for firebase response
175
- if (loadWaitingQueue) {
176
- loadWaitingQueue.push('waiting');
177
- }
178
-
179
- let followupStream$;
180
-
181
- if (!keepConnected) {
182
- followupStream$ = connection.loadStream.pipe(
183
- take(1),
184
- );
185
- } else {
186
- followupStream$ = connection.loadStream;
187
- }
188
- followupStream$.subscribe((fbPayload: any) => {
189
- if (fbPayload.type === 'initial' || fbPayload.type === 'load') {
190
- // process the followupActions
191
- if (action.followupActions) {
192
- for (const entity of fbPayload.results) {
193
- const followupLoadActions = action.followupActions(entity);
194
- for (const fAction of followupLoadActions) {
195
- this.processLoadAction(fAction, true, initiatingAction, loadWaitingQueue);
196
- }
197
- }
198
- }
199
- if (fbPayload.type === 'initial' && loadWaitingQueue) {
200
- loadWaitingQueue.pop();
201
- if (loadWaitingQueue.length === 0) {
202
- console.log('READY!');
203
- // Note: if there's some strange reason you have a nested query that
204
- // is exactly the same as the original query, it could trigger early
205
- this.loadReady$.next(this.getLoadKey(initiatingAction));
206
- }
207
- }
208
- } else if (fbPayload.type === 'added') {
209
- if (action.followupActions) {
210
- const entity = fbPayload.result;
211
- const followupLoadActions = action.followupActions(entity);
212
- for (const fAction of followupLoadActions) {
213
- this.processLoadAction(fAction, true, initiatingAction, loadWaitingQueue);
214
- }
215
- }
216
- }
217
- });
218
-
219
- return connection;
220
- }
221
-
222
- private createSingleLoadConnection(action: LoadAction, initiatingAction: LoadAction): CachedConnection {
223
- // Unlike the createConnection logic, we don't need a disconnectObs$ since
224
- // we aren't keeping a persistent stream
225
-
226
- // ------------ DISPATCH ACTIONS ONLY AFTER START DISPATCH EMITS ------------------
227
-
228
- // When loadReady$ for the initiatingAction, we can startDispatching$ since all connections
229
- // have had responses from Firebase
230
- const initiatingLoadKey = this.getLoadKey(initiatingAction);
231
- const startDispatching$ = this.loadReady$.pipe(
232
- filter((lk) => lk === initiatingLoadKey),
233
- take(1),
234
- );
235
-
236
- // Define the raw stream
237
- const [entityName, collection] = this.getActionAttributes(action);
238
- const rawStream$ = this.db.queryListValueChanges(collection, action.queryParams, action.queryOptions).pipe(
239
- map((results) => {
240
- return { type: 'initial', results };
241
- }),
242
- // Is this 2 or 1? There is that issue with cached values that could be a problem still...
243
- take(1),
244
- );
245
-
246
- // Once startDispatching emits, we can start dispatching
247
- forkJoin([startDispatching$, rawStream$]).pipe(
248
- map(([_, fbPayload]) => {
249
- return fbPayload;
250
- }),
251
- take(1),
252
- ).subscribe((fbPayload) => {
253
- this.store.dispatch( {
254
- type: `[${entityName}] loaded`,
255
- payload: fbPayload.results,
256
- correlationId: action.correlationId,
257
- } );
258
- });
259
-
260
- // Return a replayed version for followupActions
261
- const stream$ = rawStream$.pipe(
262
- publishReplay(1),
263
- refCount(),
264
- );
265
-
266
- // We're returning it in this form just so processLoadAction can be defined generally
267
- return {
268
- loadKey: null,
269
- loadStream: stream$,
270
- correlationIds: [],
271
- };
272
- }
273
-
274
- private createConnection(action: LoadAction, initiatingAction: LoadAction): CachedConnection {
275
- // console.log("CONNECT " + this.getLoadKey(action));
276
-
277
- // ------------ DEFINE OBSERVABLES FOR DISCONNECTING AND DISPATCHING --------------
278
-
279
- const loadKey = this.getLoadKey(action);
280
- const disconnectObs$ = this.closeConnection$.pipe(
281
- filter((lk) => lk === loadKey),
282
- take(1),
283
- // tap(a => { console.log("DISCONNECT: " + loadKey) }),
284
- publish(),
285
- refCount(),
286
- );
287
-
288
- // When loadReady$ for the initiatingAction, we can startDispatching$ since all connections
289
- // have had responses from Firebase
290
- const initiatingLoadKey = this.getLoadKey(initiatingAction);
291
- const startDispatching$ = this.loadReady$.pipe(
292
- filter((lk) => lk === initiatingLoadKey),
293
- take(1),
294
- // tap(a => { console.log("START DISPATCHING: " + initiatingLoadKey) }),
295
- publish(),
296
- refCount(),
297
- );
298
-
299
- // ------------ MULTICAST THE RAW ACTION STREAM -----------------------------------
300
-
301
- const [entityName, collection] = this.getActionAttributes(action);
302
-
303
- // Create a multicasted stream emitting until disonnectObs$ fires
304
- const rawStream$ = this.loadStream(collection, action).pipe(
305
- takeUntil(disconnectObs$),
306
- publishReplay(1),
307
- refCount(),
308
- );
309
-
310
- // ------------ DISPATCH ACTIONS ONLY AFTER START DISPATCH EMITS ------------------
311
-
312
- // This buffers the stream of actions until startDispatching$ fires
313
- const bufferedStream$ = rawStream$.pipe(
314
- buffer(startDispatching$),
315
- take(1),
316
- mergeMap((array) => array),
317
- takeUntil(disconnectObs$),
318
- );
319
-
320
- // This stores the stream of actions after buffering is finished
321
- const postBufferedStream$ = rawStream$.pipe(
322
- skipUntil(startDispatching$),
323
- takeUntil(disconnectObs$),
324
- );
325
-
326
- // Merge the buffered and post-buffered streams together and dispatch actions
327
- merge(bufferedStream$, postBufferedStream$).pipe(
328
- takeUntil(disconnectObs$),
329
- ).subscribe((fbPayload) => {
330
- if (fbPayload.type === 'initial' || fbPayload.type === 'load') {
331
- this.store.dispatch( {
332
- type: `[${entityName}] loaded`,
333
- payload: fbPayload.results,
334
- correlationId: action.correlationId,
335
- } );
336
- } else {
337
- this.store.dispatch( {
338
- type: `[${entityName}] ${fbPayload.type}`,
339
- payload: fbPayload.result,
340
- correlationId: action.correlationId,
341
- } );
342
- }
343
- });
344
-
345
- // ------------ REPLAY ONLY THE INITIAL/LOAD/ADD ACTIONS --------------------------
346
-
347
- // multicast the WHOLE stream of initial and added actions. This is so that "followupActions" in future
348
- // loads also works. Don't need to replay "modify" actions since this will store way too many actions.
349
- // takeUntil is required both before (defined in rawStream$) and after publishReplay
350
- // to make sure both the connection to firebase stops as well as subscriptions to the multicasted observable
351
- const stream$ = rawStream$.pipe(
352
- filter((fbPayload: any) => fbPayload.type === 'initial' || fbPayload.type === 'load' || fbPayload.type === 'added'),
353
- publishReplay(),
354
- refCount(),
355
- takeUntil(disconnectObs$),
356
- );
357
-
358
- return {
359
- loadKey: this.getLoadKey(action),
360
- loadStream: stream$,
361
- correlationIds: [action.correlationId],
362
- };
363
- }
364
-
365
- // Returns an observable with the initial as a list
366
- // and the remainder as deltas
367
- private loadStream = (collection: string, action: LoadAction): Observable<{ type: string; results?: any; result?: any; }> => {
368
- const queryParams = action.queryParams;
369
- const queryOptions = action.queryOptions;
370
-
371
- const loadKey = this.getLoadKey(action);
372
- const disconnectObs$ = this.closeConnection$.pipe(
373
- filter((lk) => lk === loadKey),
374
- take(1),
375
- );
376
- return this.db.queryListValueChanges(collection, queryParams, queryOptions).pipe(
377
- take(1),
378
- mergeMap((results) => {
379
- return merge(
380
- of({ type: 'initial', results }),
381
- this.db.queryListStateChanges(collection, queryParams, queryOptions).pipe(
382
- skip(1),
383
- // results is an array of state changes.
384
- mergeMap((changeResults: any[]) => {
385
- // group 'added' actions together to load simultaneously into store
386
- const addedResults = changeResults.filter((r) => r.type === 'added').map((r) => r.result);
387
-
388
- // group remaining actions together
389
- const otherResults = changeResults.filter((r) => r.type !== 'added');
390
-
391
- return [{ type: 'load', results: addedResults }, ...otherResults];
392
- }),
393
- ) as Observable<{ type: string; results?: any; result?: any; }>,
394
- );
395
- }),
396
- takeUntil(disconnectObs$),
397
- );
398
- };
399
-
400
- constructor(
401
- private actions$: Actions,
402
- private store: Store<fromStore.State>,
403
- private db: FirebaseService,
404
- ) { }
405
- }