headless.service.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.HeadlessService = exports.NOTIFICATION_CENTER_TOKEN_KEY = void 0;
  16. const query_core_1 = require("@tanstack/query-core");
  17. const socket_io_client_1 = __importDefault(require("socket.io-client"));
  18. const client_1 = require("@novu/client");
  19. const shared_1 = require("@novu/shared");
  20. const query_service_1 = require("./query.service");
  21. const utils_1 = require("../utils");
  22. exports.NOTIFICATION_CENTER_TOKEN_KEY = 'nc_token';
  23. const isBrowser = typeof window !== 'undefined';
  24. const SESSION_NOT_INITIALIZED_ERROR = 'Session is not initialized, please use the initializeSession method first';
  25. const getToken = () => {
  26. if (isBrowser) {
  27. return localStorage.getItem(exports.NOTIFICATION_CENTER_TOKEN_KEY);
  28. }
  29. return null;
  30. };
  31. class HeadlessService {
  32. constructor(options) {
  33. var _a, _b, _c, _d, _e, _f, _g, _h, _j;
  34. this.options = options;
  35. this.queryClient = null;
  36. this.session = null;
  37. this.socket = null;
  38. this.sessionQueryOptions = {
  39. queryKey: utils_1.SESSION_QUERY_KEY,
  40. cacheTime: Infinity,
  41. staleTime: Infinity,
  42. queryFn: () => this.api.initializeSession(this.options.applicationIdentifier, this.options.subscriberId, this.options.subscriberHash),
  43. };
  44. this.organizationQueryOptions = {
  45. queryKey: utils_1.ORGANIZATION_QUERY_KEY,
  46. cacheTime: Infinity,
  47. staleTime: Infinity,
  48. queryFn: () => this.api.getOrganization(),
  49. };
  50. this.unseenCountQueryOptions = {
  51. queryKey: utils_1.UNSEEN_COUNT_QUERY_KEY,
  52. queryFn: () => this.api.getUnseenCount(),
  53. };
  54. this.unreadCountQueryOptions = {
  55. queryKey: utils_1.UNREAD_COUNT_QUERY_KEY,
  56. queryFn: () => this.api.getUnreadCount(),
  57. };
  58. this.userPreferencesQueryOptions = {
  59. queryKey: utils_1.USER_PREFERENCES_QUERY_KEY,
  60. queryFn: () => this.api.getUserPreference(),
  61. };
  62. this.userGlobalPreferencesQueryOptions = {
  63. queryKey: utils_1.USER_GLOBAL_PREFERENCES_QUERY_KEY,
  64. queryFn: () => this.api.getUserGlobalPreference(),
  65. };
  66. this.callFetchListener = (result, listener) => listener({
  67. data: result.data,
  68. error: result.error,
  69. status: result.status,
  70. isLoading: result.isLoading,
  71. isFetching: result.isFetching,
  72. isError: result.isError,
  73. });
  74. this.callFetchListenerWithPagination = (result, listener) => listener({
  75. data: result.data,
  76. error: result.error,
  77. status: result.status,
  78. isLoading: result.isLoading,
  79. isFetching: result.isFetching,
  80. isError: result.isError,
  81. });
  82. this.callUpdateListener = (result, listener) => listener({
  83. data: result.data,
  84. error: result.error,
  85. status: result.status,
  86. isLoading: result.isLoading,
  87. isError: result.isError,
  88. });
  89. const backendUrl = (_a = options.backendUrl) !== null && _a !== void 0 ? _a : 'https://api.novu.co';
  90. const token = getToken();
  91. this.api = new client_1.ApiService(backendUrl);
  92. this.applyToken(token);
  93. this.queryClient = new query_core_1.QueryClient({
  94. defaultOptions: {
  95. queries: Object.assign({ refetchOnWindowFocus: false, retry: (_c = (_b = options === null || options === void 0 ? void 0 : options.config) === null || _b === void 0 ? void 0 : _b.retry) !== null && _c !== void 0 ? _c : 3 }, (((_d = options === null || options === void 0 ? void 0 : options.config) === null || _d === void 0 ? void 0 : _d.retryDelay) && {
  96. retryDelay: (_e = options === null || options === void 0 ? void 0 : options.config) === null || _e === void 0 ? void 0 : _e.retryDelay,
  97. })),
  98. mutations: Object.assign({ retry: (_g = (_f = options === null || options === void 0 ? void 0 : options.config) === null || _f === void 0 ? void 0 : _f.retry) !== null && _g !== void 0 ? _g : 3 }, (((_h = options === null || options === void 0 ? void 0 : options.config) === null || _h === void 0 ? void 0 : _h.retryDelay) && {
  99. retryDelay: (_j = options === null || options === void 0 ? void 0 : options.config) === null || _j === void 0 ? void 0 : _j.retryDelay,
  100. })),
  101. },
  102. });
  103. this.queryService = new query_service_1.QueryService(this.queryClient);
  104. }
  105. assertSessionInitialized() {
  106. if (!this.session) {
  107. throw new Error(SESSION_NOT_INITIALIZED_ERROR);
  108. }
  109. }
  110. applyToken(newToken) {
  111. if (newToken) {
  112. isBrowser &&
  113. localStorage.setItem(exports.NOTIFICATION_CENTER_TOKEN_KEY, newToken);
  114. this.api.setAuthorizationToken(newToken);
  115. }
  116. else {
  117. isBrowser && localStorage.removeItem(exports.NOTIFICATION_CENTER_TOKEN_KEY);
  118. this.api.disposeAuthorizationToken();
  119. }
  120. }
  121. initializeSocket(token) {
  122. var _a;
  123. const socketUrl = (_a = this.options.socketUrl) !== null && _a !== void 0 ? _a : 'https://ws.novu.co';
  124. if (this.socket) {
  125. this.socket.disconnect();
  126. }
  127. if (token) {
  128. this.socket = (0, socket_io_client_1.default)(socketUrl, {
  129. reconnectionDelayMax: 10000,
  130. transports: ['websocket'],
  131. query: {
  132. token: `${token}`,
  133. },
  134. });
  135. this.socket.on('connect_error', (error) => {
  136. console.error(error.message);
  137. });
  138. }
  139. }
  140. initializeSession({ listener, onSuccess, onError, }) {
  141. this.session = null;
  142. this.queryClient.clear();
  143. const { unsubscribe } = this.queryService.subscribeQuery({
  144. options: Object.assign(Object.assign({}, this.sessionQueryOptions), { onSuccess: (session) => {
  145. this.session = session;
  146. this.applyToken(session.token);
  147. this.initializeSocket(session.token);
  148. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(session);
  149. }, onError }),
  150. listener: (result) => this.callFetchListener(result, listener),
  151. });
  152. return unsubscribe;
  153. }
  154. fetchOrganization({ listener, onSuccess, onError, }) {
  155. return __awaiter(this, void 0, void 0, function* () {
  156. this.assertSessionInitialized();
  157. const { unsubscribe } = this.queryService.subscribeQuery({
  158. options: Object.assign(Object.assign({}, this.organizationQueryOptions), { onSuccess,
  159. onError }),
  160. listener: (result) => this.callFetchListener(result, listener),
  161. });
  162. return unsubscribe;
  163. });
  164. }
  165. fetchUnseenCount({ listener, onSuccess, onError, }) {
  166. this.assertSessionInitialized();
  167. const { unsubscribe } = this.queryService.subscribeQuery({
  168. options: Object.assign(Object.assign({}, this.unseenCountQueryOptions), { onSuccess,
  169. onError }),
  170. listener: (result) => this.callFetchListener(result, listener),
  171. });
  172. return unsubscribe;
  173. }
  174. fetchUnreadCount({ listener, onSuccess, onError, }) {
  175. this.assertSessionInitialized();
  176. const { unsubscribe } = this.queryService.subscribeQuery({
  177. options: Object.assign(Object.assign({}, this.unreadCountQueryOptions), { onSuccess,
  178. onError }),
  179. listener: (result) => this.callFetchListener(result, listener),
  180. });
  181. return unsubscribe;
  182. }
  183. listenNotificationReceive({ listener, }) {
  184. this.assertSessionInitialized();
  185. if (this.socket) {
  186. this.socket.on(shared_1.WebSocketEventEnum.RECEIVED, (data) => {
  187. if (data === null || data === void 0 ? void 0 : data.message) {
  188. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  189. exact: false,
  190. });
  191. listener(data.message);
  192. }
  193. });
  194. }
  195. return () => {
  196. if (this.socket) {
  197. this.socket.off(shared_1.WebSocketEventEnum.RECEIVED);
  198. }
  199. };
  200. }
  201. listenUnseenCountChange({ listener, }) {
  202. this.assertSessionInitialized();
  203. if (this.socket) {
  204. this.socket.on(shared_1.WebSocketEventEnum.UNSEEN, (data) => {
  205. if (Number.isInteger(data === null || data === void 0 ? void 0 : data.unseenCount)) {
  206. this.queryClient.setQueryData(utils_1.UNSEEN_COUNT_QUERY_KEY, (oldData) => { var _a; return ({ count: (_a = data === null || data === void 0 ? void 0 : data.unseenCount) !== null && _a !== void 0 ? _a : oldData.count }); });
  207. listener(data.unseenCount);
  208. }
  209. });
  210. }
  211. return () => {
  212. if (this.socket) {
  213. this.socket.off(shared_1.WebSocketEventEnum.UNSEEN);
  214. }
  215. };
  216. }
  217. listenUnreadCountChange({ listener, }) {
  218. this.assertSessionInitialized();
  219. if (this.socket) {
  220. this.socket.on(shared_1.WebSocketEventEnum.UNREAD, (data) => {
  221. if (Number.isInteger(data === null || data === void 0 ? void 0 : data.unreadCount)) {
  222. this.queryClient.setQueryData(utils_1.UNREAD_COUNT_QUERY_KEY, (oldData) => { var _a; return ({ count: (_a = data === null || data === void 0 ? void 0 : data.unreadCount) !== null && _a !== void 0 ? _a : oldData.count }); });
  223. listener(data.unreadCount);
  224. }
  225. });
  226. }
  227. return () => {
  228. if (this.socket) {
  229. this.socket.off(shared_1.WebSocketEventEnum.UNREAD);
  230. }
  231. };
  232. }
  233. fetchNotifications({ page = 0, storeId = 'default_store', query = {}, listener, onSuccess, onError, }) {
  234. this.assertSessionInitialized();
  235. const { unsubscribe } = this.queryService.subscribeQuery({
  236. options: {
  237. queryKey: [...utils_1.NOTIFICATIONS_QUERY_KEY, storeId, page, query],
  238. queryFn: () => this.api.getNotificationsList(page, query),
  239. onSuccess,
  240. onError,
  241. },
  242. listener: (result) => this.callFetchListenerWithPagination(result, listener),
  243. });
  244. return unsubscribe;
  245. }
  246. fetchUserPreferences({ listener, onSuccess, onError, }) {
  247. this.assertSessionInitialized();
  248. const { unsubscribe } = this.queryService.subscribeQuery({
  249. options: Object.assign(Object.assign({}, this.userPreferencesQueryOptions), { onSuccess,
  250. onError }),
  251. listener: (result) => this.callFetchListener(result, listener),
  252. });
  253. return unsubscribe;
  254. }
  255. fetchUserGlobalPreferences({ listener, onSuccess, onError, }) {
  256. this.assertSessionInitialized();
  257. const { unsubscribe } = this.queryService.subscribeQuery({
  258. options: Object.assign(Object.assign({}, this.userGlobalPreferencesQueryOptions), { onSuccess,
  259. onError }),
  260. listener: (result) => this.callFetchListener(result, listener),
  261. });
  262. return unsubscribe;
  263. }
  264. updateUserPreferences({ templateId, channelType, checked, listener, onSuccess, onError, }) {
  265. return __awaiter(this, void 0, void 0, function* () {
  266. this.assertSessionInitialized();
  267. const { result, unsubscribe } = this.queryService.subscribeMutation({
  268. options: {
  269. mutationFn: (variables) => this.api.updateSubscriberPreference(variables.templateId, variables.channelType, variables.checked),
  270. onSuccess: (data) => {
  271. this.queryClient.setQueryData(utils_1.USER_PREFERENCES_QUERY_KEY, (oldUserPreferences) => oldUserPreferences.map((setting) => {
  272. if (setting.template._id === data.template._id) {
  273. return data;
  274. }
  275. return setting;
  276. }));
  277. },
  278. },
  279. listener: (res) => this.callUpdateListener(res, listener),
  280. });
  281. result
  282. .mutate({ templateId, channelType, checked })
  283. .then((data) => {
  284. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  285. return data;
  286. })
  287. .catch((error) => {
  288. onError === null || onError === void 0 ? void 0 : onError(error);
  289. })
  290. .finally(() => {
  291. unsubscribe();
  292. });
  293. });
  294. }
  295. updateUserGlobalPreferences({ preferences, enabled, listener, onSuccess, onError, }) {
  296. return __awaiter(this, void 0, void 0, function* () {
  297. this.assertSessionInitialized();
  298. const { result, unsubscribe } = this.queryService.subscribeMutation({
  299. options: {
  300. mutationFn: (variables) => this.api.updateSubscriberGlobalPreference(variables.preferences, variables.enabled),
  301. onSuccess: (data) => {
  302. this.queryClient.setQueryData(utils_1.USER_GLOBAL_PREFERENCES_QUERY_KEY, () => [data]);
  303. },
  304. },
  305. listener: (res) => this.callUpdateListener(res, listener),
  306. });
  307. result
  308. .mutate({ preferences, enabled })
  309. .then((data) => {
  310. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  311. return data;
  312. })
  313. .catch((error) => {
  314. onError === null || onError === void 0 ? void 0 : onError(error);
  315. })
  316. .finally(() => {
  317. unsubscribe();
  318. });
  319. });
  320. }
  321. markNotificationsAsRead({ messageId, listener, onSuccess, onError, }) {
  322. return __awaiter(this, void 0, void 0, function* () {
  323. this.assertSessionInitialized();
  324. const { result, unsubscribe } = this.queryService.subscribeMutation({
  325. options: {
  326. mutationFn: (variables) => this.api.markMessageAs(variables.messageId, {
  327. seen: true,
  328. read: true,
  329. }),
  330. onSuccess: (data) => {
  331. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  332. exact: false,
  333. });
  334. },
  335. },
  336. listener: (res) => this.callUpdateListener(res, listener),
  337. });
  338. result
  339. .mutate({ messageId })
  340. .then((data) => {
  341. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  342. return data;
  343. })
  344. .catch((error) => {
  345. onError === null || onError === void 0 ? void 0 : onError(error);
  346. })
  347. .finally(() => {
  348. unsubscribe();
  349. });
  350. });
  351. }
  352. markNotificationsAsSeen({ messageId, listener, onSuccess, onError, }) {
  353. return __awaiter(this, void 0, void 0, function* () {
  354. this.assertSessionInitialized();
  355. const { result, unsubscribe } = this.queryService.subscribeMutation({
  356. options: {
  357. mutationFn: (variables) => this.api.markMessageAs(variables.messageId, {
  358. seen: true,
  359. }),
  360. onSuccess: (data) => {
  361. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  362. exact: false,
  363. });
  364. },
  365. },
  366. listener: (res) => this.callUpdateListener(res, listener),
  367. });
  368. result
  369. .mutate({ messageId })
  370. .then((data) => {
  371. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  372. return data;
  373. })
  374. .catch((error) => {
  375. onError === null || onError === void 0 ? void 0 : onError(error);
  376. })
  377. .finally(() => {
  378. unsubscribe();
  379. });
  380. });
  381. }
  382. markNotificationsAs({ messageId, mark, listener, onSuccess, onError, }) {
  383. return __awaiter(this, void 0, void 0, function* () {
  384. this.assertSessionInitialized();
  385. const { result, unsubscribe } = this.queryService.subscribeMutation({
  386. options: {
  387. mutationFn: (variables) => this.api.markMessageAs(variables.messageId, mark),
  388. onSuccess: (data) => {
  389. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  390. exact: false,
  391. });
  392. },
  393. },
  394. listener: (res) => this.callUpdateListener(res, listener),
  395. });
  396. result
  397. .mutate({ messageId })
  398. .then((data) => {
  399. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  400. return data;
  401. })
  402. .catch((error) => {
  403. onError === null || onError === void 0 ? void 0 : onError(error);
  404. })
  405. .finally(() => {
  406. unsubscribe();
  407. });
  408. });
  409. }
  410. removeNotification({ messageId, listener, onSuccess, onError, }) {
  411. return __awaiter(this, void 0, void 0, function* () {
  412. this.assertSessionInitialized();
  413. const { result, unsubscribe } = this.queryService.subscribeMutation({
  414. options: {
  415. mutationFn: (variables) => this.api.removeMessage(variables.messageId),
  416. onSuccess: (data) => {
  417. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  418. exact: false,
  419. });
  420. },
  421. },
  422. listener: (res) => this.callUpdateListener(res, listener),
  423. });
  424. result
  425. .mutate({ messageId })
  426. .then((data) => {
  427. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  428. return data;
  429. })
  430. .catch((error) => {
  431. onError === null || onError === void 0 ? void 0 : onError(error);
  432. })
  433. .finally(() => {
  434. unsubscribe();
  435. });
  436. });
  437. }
  438. removeNotifications({ messageIds, listener, onSuccess, onError, }) {
  439. return __awaiter(this, void 0, void 0, function* () {
  440. this.assertSessionInitialized();
  441. const { result, unsubscribe } = this.queryService.subscribeMutation({
  442. options: {
  443. mutationFn: (variables) => this.api.removeMessages(variables.messageIds),
  444. onSuccess: (data) => {
  445. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  446. exact: false,
  447. });
  448. },
  449. },
  450. listener: (res) => this.callUpdateListener(res, listener),
  451. });
  452. result
  453. .mutate({ messageIds })
  454. .then((data) => {
  455. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  456. return data;
  457. })
  458. .catch((error) => {
  459. onError === null || onError === void 0 ? void 0 : onError(error);
  460. })
  461. .finally(() => {
  462. unsubscribe();
  463. });
  464. });
  465. }
  466. updateAction({ messageId, actionButtonType, status, payload, listener, onSuccess, onError, }) {
  467. return __awaiter(this, void 0, void 0, function* () {
  468. this.assertSessionInitialized();
  469. const { result, unsubscribe } = this.queryService.subscribeMutation({
  470. options: {
  471. mutationFn: (variables) => this.api.updateAction(variables.messageId, variables.actionButtonType, variables.status, variables.payload),
  472. onSuccess: (data) => {
  473. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  474. exact: false,
  475. });
  476. },
  477. },
  478. listener: (res) => this.callUpdateListener(res, listener),
  479. });
  480. result
  481. .mutate({ messageId, actionButtonType, status, payload })
  482. .then((data) => {
  483. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  484. return data;
  485. })
  486. .catch((error) => {
  487. onError === null || onError === void 0 ? void 0 : onError(error);
  488. })
  489. .finally(() => {
  490. unsubscribe();
  491. });
  492. });
  493. }
  494. markAllMessagesAsRead({ listener, onSuccess, onError, feedId, }) {
  495. return __awaiter(this, void 0, void 0, function* () {
  496. this.assertSessionInitialized();
  497. const { result, unsubscribe } = this.queryService.subscribeMutation({
  498. options: {
  499. mutationFn: (variables) => this.api.markAllMessagesAsRead(variables === null || variables === void 0 ? void 0 : variables.feedId),
  500. onSuccess: (data) => {
  501. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  502. exact: false,
  503. });
  504. },
  505. },
  506. listener: (res) => this.callUpdateListener(res, listener),
  507. });
  508. result
  509. .mutate({ feedId })
  510. .then((data) => {
  511. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  512. return data;
  513. })
  514. .catch((error) => {
  515. onError === null || onError === void 0 ? void 0 : onError(error);
  516. })
  517. .finally(() => {
  518. unsubscribe();
  519. });
  520. });
  521. }
  522. markAllMessagesAsSeen({ listener, onSuccess, onError, feedId, }) {
  523. return __awaiter(this, void 0, void 0, function* () {
  524. this.assertSessionInitialized();
  525. const { result, unsubscribe } = this.queryService.subscribeMutation({
  526. options: {
  527. mutationFn: (variables) => this.api.markAllMessagesAsSeen(variables === null || variables === void 0 ? void 0 : variables.feedId),
  528. onSuccess: (data) => {
  529. this.queryClient.refetchQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  530. exact: false,
  531. });
  532. },
  533. },
  534. listener: (res) => this.callUpdateListener(res, listener),
  535. });
  536. result
  537. .mutate({ feedId })
  538. .then((data) => {
  539. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
  540. return data;
  541. })
  542. .catch((error) => {
  543. onError === null || onError === void 0 ? void 0 : onError(error);
  544. })
  545. .finally(() => {
  546. unsubscribe();
  547. });
  548. });
  549. }
  550. removeAllNotifications({ feedId, listener, onSuccess, onError, }) {
  551. return __awaiter(this, void 0, void 0, function* () {
  552. this.assertSessionInitialized();
  553. const { result, unsubscribe } = this.queryService.subscribeMutation({
  554. options: {
  555. mutationFn: (variables) => this.api.removeAllMessages(variables === null || variables === void 0 ? void 0 : variables.feedId),
  556. onSuccess: (data) => {
  557. this.queryClient.removeQueries(utils_1.NOTIFICATIONS_QUERY_KEY, {
  558. exact: false,
  559. });
  560. },
  561. },
  562. listener: (res) => this.callUpdateListener(res, listener),
  563. });
  564. result
  565. .mutate({ feedId })
  566. .then((data) => {
  567. onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
  568. return data;
  569. })
  570. .catch((error) => {
  571. onError === null || onError === void 0 ? void 0 : onError(error);
  572. })
  573. .finally(() => {
  574. unsubscribe();
  575. });
  576. });
  577. }
  578. }
  579. exports.HeadlessService = HeadlessService;