| 56 | * A context-based registry for life cycle observers |
| 57 | */ |
| 58 | export class LifeCycleObserverRegistry implements LifeCycleObserver { |
| 59 | constructor( |
| 60 | @inject.context() |
| 61 | protected readonly context: Context, |
| 62 | @inject.view(lifeCycleObserverFilter) |
| 63 | protected readonly observersView: ContextView<LifeCycleObserver>, |
| 64 | @inject(CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS, {optional: true}) |
| 65 | protected readonly options: LifeCycleObserverOptions = { |
| 66 | parallel: true, |
| 67 | orderedGroups: DEFAULT_ORDERED_GROUPS, |
| 68 | }, |
| 69 | ) {} |
| 70 | |
| 71 | setOrderedGroups(groups: string[]) { |
| 72 | this.options.orderedGroups = groups; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get observer groups ordered by the group |
| 77 | */ |
| 78 | public getObserverGroupsByOrder(): LifeCycleObserverGroup[] { |
| 79 | const bindings = this.observersView.bindings; |
| 80 | const groups = this.sortObserverBindingsByGroup(bindings); |
| 81 | if (debug.enabled) { |
| 82 | debug( |
| 83 | 'Observer groups: %j', |
| 84 | groups.map(g => ({ |
| 85 | group: g.group, |
| 86 | bindings: g.bindings.map(b => b.key), |
| 87 | })), |
| 88 | ); |
| 89 | } |
| 90 | return groups; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the group for a given life cycle observer binding |
| 95 | * @param binding - Life cycle observer binding |
| 96 | */ |
| 97 | protected getObserverGroup( |
| 98 | binding: Readonly<Binding<LifeCycleObserver>>, |
| 99 | ): string { |
| 100 | // First check if there is an explicit group name in the tag |
| 101 | let group = binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]; |
| 102 | if (!group) { |
| 103 | // Fall back to a tag that matches one of the groups |
| 104 | group = this.options.orderedGroups.find(g => binding.tagMap[g] === g); |
| 105 | } |
| 106 | group = group || ''; |
| 107 | debug( |
| 108 | 'Binding %s is configured with observer group %s', |
| 109 | binding.key, |
| 110 | group, |
| 111 | ); |
| 112 | return group; |
| 113 | } |
| 114 | |
| 115 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected