Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit bbded0c

Browse files
committed
feat: return matchedAudiences from top level API
1 parent 982a935 commit bbded0c

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

.prettierrc.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module.exports = {
22
semi: true,
3-
// trailingComma: 'all',
43
singleQuote: true,
54
tabWidth: 2
65
};

packages/core/src/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface IPageFeatureGetter {
1515

1616
// TODO: we need to give a way to consumers to ensure this does not
1717
// run multiple times on a single page load.
18-
const run = async (config: IConfig) => {
18+
const run = async (config: IConfig): Promise<void> => {
1919
const { pageFeatureGetters } = config;
2020
const pageFeatures = await getPageFeatures(pageFeatureGetters);
2121
viewStore.insert(pageFeatures);
@@ -29,6 +29,7 @@ const run = async (config: IConfig) => {
2929
id: audience.id,
3030
matchedAt: timeStampInSecs(),
3131
expiresAt: timeStampInSecs() + audience.ttl,
32+
matchedOnCurrentPageView: true,
3233
matched: engine.check(audience.conditions, viewStore.pageViews),
3334
};
3435
})
@@ -37,11 +38,11 @@ const run = async (config: IConfig) => {
3738
audienceStore.setMatchedAudiences(matchedAudiences);
3839
};
3940

40-
const stats = () => {
41-
return {};
41+
const getMatchedAudiences = () => {
42+
return audienceStore.matchedAudiences;
4243
};
4344

4445
export const edkt = {
4546
run,
46-
stats,
47+
getMatchedAudiences,
4748
};

packages/core/src/store/audience.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface IMatchedAudience {
1010
id: string;
1111
matchedAt: number;
1212
expiresAt: number;
13+
matchedOnCurrentPageView: boolean;
1314
}
1415

1516
// TODO: share the types.
@@ -26,9 +27,14 @@ class AudienceStore {
2627
_load() {
2728
const audiences: IMatchedAudience[] =
2829
storage.get(StorageKeys.MATCHED_AUDIENCES) || [];
29-
const unExpiredAudiences = audiences.filter(
30-
(audience) => audience.expiresAt > timeStampInSecs()
31-
);
30+
const unExpiredAudiences = audiences
31+
.filter((audience) => audience.expiresAt > timeStampInSecs())
32+
.map((audience) => {
33+
return {
34+
...audience,
35+
matchedOnCurrentPageView: false,
36+
};
37+
});
3238
const unExpiredAudienceIds = unExpiredAudiences.map(
3339
(audience) => audience.id
3440
);

packages/core/src/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const get = (key: string) => {
1111
}
1212
};
1313

14-
const set = (key: string, value: object) => {
14+
const set = (key: string, value: any) => {
1515
try {
1616
const serialized = JSON.stringify(value);
1717
localStorage.setItem(key, serialized);

0 commit comments

Comments
 (0)