@coframe/sdk 1.1.45 → 1.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4194,17 +4194,39 @@ function setupListeners(Coframe, executionStartTime) {
4194
4194
  });
4195
4195
  };
4196
4196
  document.addEventListener('click', handleClick, true);
4197
- const handleEventFlush = debounce((eventType) => {
4197
+ let scheduledEventFlush = null;
4198
+ const flushEvent = (eventType) => {
4198
4199
  queueLeaveEvent(Coframe, eventType, eventType === 'tab_show' ? undefined : leaveFields());
4199
4200
  flushQueuedEvents(Coframe.projectId);
4200
4201
  notifyEventsFlushed();
4201
- }, 1);
4202
- window.addEventListener('beforeunload', () => handleEventFlush('before_unload'));
4203
- window.addEventListener('pagehide', () => handleEventFlush('page_hide'));
4202
+ };
4203
+ const scheduleEventFlush = (eventType) => {
4204
+ if (scheduledEventFlush !== null) {
4205
+ clearTimeout(scheduledEventFlush);
4206
+ }
4207
+ scheduledEventFlush = setTimeout(() => {
4208
+ scheduledEventFlush = null;
4209
+ flushEvent(eventType);
4210
+ }, 1);
4211
+ };
4212
+ const flushEventImmediately = (eventType) => {
4213
+ if (scheduledEventFlush !== null) {
4214
+ clearTimeout(scheduledEventFlush);
4215
+ scheduledEventFlush = null;
4216
+ }
4217
+ flushEvent(eventType);
4218
+ };
4219
+ window.addEventListener('beforeunload', () => scheduleEventFlush('before_unload'));
4220
+ window.addEventListener('pagehide', () => flushEventImmediately('page_hide'));
4204
4221
  window.addEventListener('visibilitychange', () => {
4205
4222
  const isPageHidden = document.hidden;
4206
4223
  const eventType = isPageHidden ? 'tab_hide' : 'tab_show';
4207
- handleEventFlush(eventType);
4224
+ if (isPageHidden) {
4225
+ flushEventImmediately(eventType);
4226
+ }
4227
+ else {
4228
+ scheduleEventFlush(eventType);
4229
+ }
4208
4230
  });
4209
4231
  }
4210
4232
  function queueLeaveEvent(Coframe, eventType, fields) {
@@ -4192,17 +4192,39 @@ function setupListeners(Coframe, executionStartTime) {
4192
4192
  });
4193
4193
  };
4194
4194
  document.addEventListener('click', handleClick, true);
4195
- const handleEventFlush = debounce((eventType) => {
4195
+ let scheduledEventFlush = null;
4196
+ const flushEvent = (eventType) => {
4196
4197
  queueLeaveEvent(Coframe, eventType, eventType === 'tab_show' ? undefined : leaveFields());
4197
4198
  flushQueuedEvents(Coframe.projectId);
4198
4199
  notifyEventsFlushed();
4199
- }, 1);
4200
- window.addEventListener('beforeunload', () => handleEventFlush('before_unload'));
4201
- window.addEventListener('pagehide', () => handleEventFlush('page_hide'));
4200
+ };
4201
+ const scheduleEventFlush = (eventType) => {
4202
+ if (scheduledEventFlush !== null) {
4203
+ clearTimeout(scheduledEventFlush);
4204
+ }
4205
+ scheduledEventFlush = setTimeout(() => {
4206
+ scheduledEventFlush = null;
4207
+ flushEvent(eventType);
4208
+ }, 1);
4209
+ };
4210
+ const flushEventImmediately = (eventType) => {
4211
+ if (scheduledEventFlush !== null) {
4212
+ clearTimeout(scheduledEventFlush);
4213
+ scheduledEventFlush = null;
4214
+ }
4215
+ flushEvent(eventType);
4216
+ };
4217
+ window.addEventListener('beforeunload', () => scheduleEventFlush('before_unload'));
4218
+ window.addEventListener('pagehide', () => flushEventImmediately('page_hide'));
4202
4219
  window.addEventListener('visibilitychange', () => {
4203
4220
  const isPageHidden = document.hidden;
4204
4221
  const eventType = isPageHidden ? 'tab_hide' : 'tab_show';
4205
- handleEventFlush(eventType);
4222
+ if (isPageHidden) {
4223
+ flushEventImmediately(eventType);
4224
+ }
4225
+ else {
4226
+ scheduleEventFlush(eventType);
4227
+ }
4206
4228
  });
4207
4229
  }
4208
4230
  function queueLeaveEvent(Coframe, eventType, fields) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coframe/sdk",
3
- "version": "1.1.45",
3
+ "version": "1.1.46",
4
4
  "description": "Coframe optimizes your website using generative AI to deliver maximum impact to your audience and allow your site to improve itself as you sleep. Learn more at coframe.com.",
5
5
  "main": "dist/pkg/cf.cjs.js",
6
6
  "module": "dist/pkg/cf.esm.mjs",