From cb29cbeb52223d78bdd4e67416f8535aed3699e8 Mon Sep 17 00:00:00 2001 From: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> Date: Sat, 4 Sep 2021 21:38:21 +0800 Subject: [PATCH] feat: add disabled lifecycle groups Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> --- .../src/__tests__/unit/lifecycle-registry.unit.ts | 12 ++++++++++++ packages/core/src/lifecycle-registry.ts | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/core/src/__tests__/unit/lifecycle-registry.unit.ts b/packages/core/src/__tests__/unit/lifecycle-registry.unit.ts index 028aa110bdea..3f5beeecd2a5 100644 --- a/packages/core/src/__tests__/unit/lifecycle-registry.unit.ts +++ b/packages/core/src/__tests__/unit/lifecycle-registry.unit.ts @@ -38,6 +38,14 @@ describe('LifeCycleRegistry', () => { expect(events).to.eql(['1-start', '2-start']); }); + it('skips notification of disabled groups', async () => { + givenObserver('1', 'a'); + givenObserver('2', 'b'); + registry.setDisabledGroups(['a']); + await registry.start(); + expect(events).to.eql(['2-start']); + }); + it('starts/stops all registered observers with param injections', async () => { givenObserverWithParamInjection('1'); givenObserverWithParamInjection('2'); @@ -170,6 +178,10 @@ describe('LifeCycleRegistry', () => { setParallel(parallel?: boolean) { this.options.parallel = parallel; } + + setDisabledGroups(groups?: string[]) { + this.options.disabledGroups = groups; + } } async function givenLifeCycleRegistry() { diff --git a/packages/core/src/lifecycle-registry.ts b/packages/core/src/lifecycle-registry.ts index 3fa25d502a09..233a4eda9bbb 100644 --- a/packages/core/src/lifecycle-registry.ts +++ b/packages/core/src/lifecycle-registry.ts @@ -38,6 +38,12 @@ export type LifeCycleObserverOptions = { * observers are notified in the reverse order during `stop`. */ orderedGroups: string[]; + /** + * Override and disable lifecycle observer groups. This setting applies to + * both ordered groups (i.e. those defined in `orderedGroups`) and unordered + * groups. + */ + disabledGroups?: string[]; /** * Notify observers of the same group in parallel, default to `true` */ @@ -211,6 +217,10 @@ export class LifeCycleObserverRegistry implements LifeCycleObserver { groups = [...groups].reverse(); } for (const group of groups) { + if (this.options.disabledGroups?.includes(group.group)) { + debug('Notification skipped (Group is disabled): %s', group.group); + continue; + } const observersForGroup: LifeCycleObserver[] = []; const bindingsInGroup = reverse ? group.bindings.reverse()