Skip to content

Commit

Permalink
feat(analytics-provider-data-layer,core)!: support async data layer t…
Browse files Browse the repository at this point in the history
…rackers (#3045)

* feat(core): add `MaybeAsync`

* feat(analytics-provider-data-layer)!: support async trackers

BREAKING CHANGE: `DataLayerTracker` return type is now `MaybeAsync`
  • Loading branch information
griest024 authored Aug 29, 2024
1 parent 3988924 commit 3e1c93f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion libs/analytics-provider-data-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@daffodil/analytics": "0.0.0-PLACEHOLDER"
},
"devDependencies": {
"@daffodil/analytics": "0.0.0-PLACEHOLDER"
"@daffodil/analytics": "0.0.0-PLACEHOLDER",
"@daffodil/core": "0.0.0-PLACEHOLDER"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import {
FactoryProvider,
inject,
} from '@angular/core';
import { of } from 'rxjs';
import { map } from 'rxjs';

import {
DaffAnalyticsEvent,
DaffAnalyticsServices,
} from '@daffodil/analytics';
import {
MaybeAsync,
observe,
} from '@daffodil/core';

import { DataLayerItem } from './data-layer';
import { DaffAnalyticsDataLayer } from './data-layer.service';



export type DataLayerTracker<T extends DaffAnalyticsEvent> = (
action: T
) => DataLayerItem;
) => MaybeAsync<DataLayerItem>;

export function provideDataLayerTracker<T extends DaffAnalyticsEvent>(
tracker: DataLayerTracker<T>,
Expand All @@ -25,7 +27,9 @@ export function provideDataLayerTracker<T extends DaffAnalyticsEvent>(
provide: DaffAnalyticsServices,
useFactory: () => {
const dataLayer = inject(DaffAnalyticsDataLayer);
return (action: T) => of(dataLayer.push(tracker(action)));
return (action: T) => observe(tracker(action)).pipe(
map((data) => dataLayer.push(data)),
);
},
multi: true,
};
Expand Down
3 changes: 3 additions & 0 deletions libs/core/src/async/maybe.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Observable } from 'rxjs';

export type MaybeAsync<T> = Promise<T> | Observable<T> | T;
1 change: 1 addition & 0 deletions libs/core/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './filters/public_api';
export * from './injection-tokens/public_api';

export { DaffOrderable } from './orderable/orderable';
export { MaybeAsync } from './async/maybe.type';
4 changes: 3 additions & 1 deletion libs/core/src/utils/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
from,
} from 'rxjs';

import { MaybeAsync } from '../async/maybe.type';

/**
* Converts a value to an observable.
* If the value is an observable, just returns that observable.
* If the value is a promise, converts it to an observable (see rxjs `from`).
* If the value is neither, just returns an observable that immediately emits the value.
*/
export function observe<T>(val: T | Promise<T> | Observable<T>): Observable<T> {
export function observe<T>(val: MaybeAsync<T>): Observable<T> {
return isObservable(val) ? val : from(Promise.resolve(val));
}

0 comments on commit 3e1c93f

Please sign in to comment.