Skip to content

Commit

Permalink
feat(core): introduce onBind callback
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Mar 31, 2022
1 parent 66fe9b0 commit 04c2cfd
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/guides/sharing-the-browser-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ consumer receives history change events only for location changes that affect
its own history.

How the root location is build from the consumer locations, is a problem that
can not be solved generally, since it is dependant on the usecase. This is why
can not be solved generally, since it is dependent on the usecase. This is why
the integrator defines the History Service with a so-called **root location
transformer**. The root location transformer provides functions for merging
consumer locations into a root location, and for extracting a consumer path from
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/__tests__/create-feature-hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,35 @@ describe('createFeatureHub()', () => {
);
});
});

describe('with an onBind callback', () => {
it('calls the onBind callback successfully', () => {
const mockOnBind = jest.fn();
const featureAppId = 'testId';
const featureAppName = 'testName';

const {featureAppManager} = createFeatureHub('test:integrator', {
...featureHubOptions,
onBind: mockOnBind,
});

featureAppManager.createFeatureAppScope(
featureAppId,
mockFeatureAppDefinition,
{featureAppName}
);

expect(mockOnBind.mock.calls).toEqual([
[
{
featureAppDefinition: mockFeatureAppDefinition,
featureAppId,
featureAppName,
},
],
]);
});
});
});

describe('with Feature Service definitions', () => {
Expand Down
56 changes: 56 additions & 0 deletions packages/core/src/__tests__/feature-app-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,62 @@ describe('FeatureAppManager', () => {
});
});

describe('with an onBind callback', () => {
it('calls the onBind callback successfully', () => {
const mockOnBind = jest.fn();
const featureAppId = 'testId';
const featureAppName = 'testName';

featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
logger,
onBind: mockOnBind,
});

featureAppManager.createFeatureAppScope(
featureAppId,
mockFeatureAppDefinition,
{featureAppName}
);

expect(mockOnBind.mock.calls).toEqual([
[
{
featureAppDefinition: mockFeatureAppDefinition,
featureAppId,
featureAppName,
},
],
]);
});

describe('that throws an error', () => {
it('does not throw but logs the error', () => {
const mockError = new Error('mockError');

featureAppManager = new FeatureAppManager(
mockFeatureServiceRegistry,
{
logger,
onBind: () => {
throw mockError;
},
}
);

expect(() =>
featureAppManager.createFeatureAppScope(
'testId',
mockFeatureAppDefinition
)
).not.toThrow();

expect(logger.error.mock.calls).toEqual([
['Failed to execute onBind callback.', mockError],
]);
});
});
});

describe('with a featureAppName', () => {
it('passes the featureAppName as consumerName to bindFeatureServices', () => {
const featureAppId = 'testId';
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/create-feature-hub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {ExternalsValidator, ProvidedExternals} from './externals-validator';
import {FeatureAppManager, ModuleLoader} from './feature-app-manager';
import {
FeatureAppManager,
ModuleLoader,
OnBindParams,
} from './feature-app-manager';
import {
FeatureServiceConsumerDefinition,
FeatureServiceConsumerDependencies,
Expand Down Expand Up @@ -63,6 +67,13 @@ export interface FeatureHubOptions {
* A custom logger that shall be used instead of `console`.
*/
readonly logger?: Logger;

/**
* A function that is called for every Feature App when its dependent Feature
* Services are bound. This allows the integrator to collect information about
* Feature Service usage.
*/
readonly onBind?: (params: OnBindParams) => void;
}

/**
Expand All @@ -84,6 +95,7 @@ export function createFeatureHub(
providedExternals,
moduleLoader,
logger,
onBind,
} = options;

let externalsValidator: ExternalsValidator | undefined;
Expand Down Expand Up @@ -112,6 +124,7 @@ export function createFeatureHub(
externalsValidator,
moduleLoader,
logger,
onBind,
});

const {featureServices} = featureServiceRegistry.bindFeatureServices(
Expand Down
27 changes: 24 additions & 3 deletions packages/core/src/feature-app-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ export interface FeatureAppScopeOptions<
readonly done?: (result?: unknown) => void;
}

export interface OnBindParams {
readonly featureAppDefinition: FeatureServiceConsumerDefinition;
readonly featureAppId: string;
readonly featureAppName: string | undefined;
}

export interface FeatureAppManagerOptions {
/**
* For the `FeatureAppManager` to be able to load Feature Apps from a remote
Expand All @@ -154,6 +160,13 @@ export interface FeatureAppManagerOptions {
* A custom logger that shall be used instead of `console`.
*/
readonly logger?: Logger;

/**
* A function that is called for every Feature App when its dependent Feature
* Services are bound. This allows the integrator to collect information about
* Feature Service usage.
*/
readonly onBind?: (params: OnBindParams) => void;
}

type FeatureAppId = string;
Expand Down Expand Up @@ -422,6 +435,16 @@ export class FeatureAppManager {
featureAppName
);

try {
this.options.onBind?.({
featureAppDefinition,
featureAppId,
featureAppName,
});
} catch (error) {
this.logger.error('Failed to execute onBind callback.', error);
}

const env: FeatureAppEnvironment<TFeatureServices, TConfig> = {
baseUrl,
config,
Expand All @@ -432,9 +455,7 @@ export class FeatureAppManager {
};

try {
if (beforeCreate) {
beforeCreate(env);
}
beforeCreate?.(env);

const featureApp = featureAppDefinition.create(env);

Expand Down

0 comments on commit 04c2cfd

Please sign in to comment.