Skip to content

Commit

Permalink
fix(js): Bypass cache during novu.notifications.list()
Browse files Browse the repository at this point in the history
Introduce novu.notifications.list({ useCache: false }) option to always fetch the latest notifications and bypass the cache.
  • Loading branch information
SokratisVidros committed Oct 14, 2024
1 parent 7624e94 commit c95c76c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/js/src/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ export class Notifications extends BaseModule {
this.#useCache = useCache;
}

async list({ limit = 10, ...restOptions }: ListNotificationsArgs = {}): Result<ListNotificationsResponse> {
async list({
limit = 10,
useCache = true,
...restOptions
}: ListNotificationsArgs = {}): Result<ListNotificationsResponse> {
return this.callWithSession(async () => {
const args = { limit, ...restOptions };
try {
let data: ListNotificationsResponse | undefined = this.#useCache ? this.cache.getAll(args) : undefined;
let data: ListNotificationsResponse | undefined =
this.#useCache || useCache ? this.cache.getAll(args) : undefined;
this._emitter.emit('notifications.list.pending', { args, data });

if (!data) {
Expand All @@ -78,7 +83,7 @@ export class Notifications extends BaseModule {
notifications: response.data.map((el) => new Notification(el, this._emitter, this._inboxService)),
};

if (this.#useCache) {
if (this.#useCache || useCache) {
this.cache.set(args, data);
data = this.cache.getAll(args);
}
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/notifications/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type ListNotificationsArgs = {
limit?: number;
after?: string;
offset?: number;
useCache?: boolean;
};

export type ListNotificationsResponse = { notifications: Notification[]; hasMore: boolean; filter: NotificationFilter };
Expand Down

0 comments on commit c95c76c

Please sign in to comment.