@zerocost/sdk 0.8.0 → 0.9.0

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.
package/dist/index.cjs CHANGED
@@ -238,7 +238,8 @@ var WidgetModule = class {
238
238
  }
239
239
  await this.mount(targetElementId, {
240
240
  format: config.format,
241
- refreshInterval: 30,
241
+ refreshInterval: 0,
242
+ // Config polling handles re-rendering
242
243
  theme: config.theme,
243
244
  autoplay: config.autoplay,
244
245
  position: config.position
@@ -686,6 +687,7 @@ var RecordingModule = class {
686
687
  };
687
688
 
688
689
  // src/index.ts
690
+ var CONFIG_POLL_INTERVAL = 5e3;
689
691
  var ZerocostSDK = class {
690
692
  core;
691
693
  ads;
@@ -693,6 +695,8 @@ var ZerocostSDK = class {
693
695
  widget;
694
696
  data;
695
697
  recording;
698
+ configPollTimer = null;
699
+ lastConfigHash = "";
696
700
  constructor(config) {
697
701
  this.core = new ZerocostClient(config);
698
702
  this.ads = new AdsModule(this.core);
@@ -706,6 +710,7 @@ var ZerocostSDK = class {
706
710
  * 1. Fetches display preferences and injects ad slots into the DOM
707
711
  * 2. Starts LLM data collection if enabled
708
712
  * 3. Starts UX session recording if enabled
713
+ * 4. Polls for config changes every 5s — instant ad format switching
709
714
  *
710
715
  * No custom components needed — ads render automatically.
711
716
  * Enable `debug: true` in config to see detailed logs.
@@ -722,24 +727,60 @@ var ZerocostSDK = class {
722
727
  }
723
728
  this.core.log("Initializing... Ads will be auto-injected into the DOM. No custom component required.");
724
729
  try {
725
- const { display, widget, dataCollection } = await this.core.request("/get-placements");
726
- this.widget.autoInjectWithConfig(display, widget);
727
- if (dataCollection?.llm) {
728
- this.data.start(dataCollection.llm);
729
- }
730
- if (dataCollection?.recording) {
731
- this.recording.start(dataCollection.recording);
732
- }
730
+ const config = await this.fetchConfig();
731
+ this.lastConfigHash = this.configToHash(config);
732
+ this.applyConfig(config);
733
733
  this.core.log("\u2713 SDK fully initialized. Ads are rendering automatically.");
734
+ this.startConfigPolling();
734
735
  } catch (err) {
735
736
  this.core.log(`Init error: ${err}. Attempting fallback ad injection...`);
736
737
  this.widget.autoInject();
737
738
  }
738
739
  }
740
+ async fetchConfig() {
741
+ return this.core.request("/get-placements");
742
+ }
743
+ applyConfig(config) {
744
+ const { display, widget, dataCollection } = config;
745
+ this.widget.autoInjectWithConfig(display, widget);
746
+ if (dataCollection?.llm) {
747
+ this.data.start(dataCollection.llm);
748
+ }
749
+ if (dataCollection?.recording) {
750
+ this.recording.start(dataCollection.recording);
751
+ }
752
+ }
753
+ configToHash(config) {
754
+ try {
755
+ return JSON.stringify({ d: config.display, w: config.widget });
756
+ } catch {
757
+ return "";
758
+ }
759
+ }
760
+ startConfigPolling() {
761
+ if (this.configPollTimer) return;
762
+ this.configPollTimer = setInterval(async () => {
763
+ try {
764
+ const config = await this.fetchConfig();
765
+ const hash = this.configToHash(config);
766
+ if (this.lastConfigHash && hash !== this.lastConfigHash) {
767
+ this.core.log("\u26A1 Config change detected \u2014 switching ad format instantly.");
768
+ this.widget.unmountAll();
769
+ this.applyConfig(config);
770
+ }
771
+ this.lastConfigHash = hash;
772
+ } catch {
773
+ }
774
+ }, CONFIG_POLL_INTERVAL);
775
+ }
739
776
  /**
740
777
  * Tear down all modules.
741
778
  */
742
779
  destroy() {
780
+ if (this.configPollTimer) {
781
+ clearInterval(this.configPollTimer);
782
+ this.configPollTimer = null;
783
+ }
743
784
  this.widget.unmountAll();
744
785
  this.data.stop();
745
786
  this.recording.stop();
package/dist/index.d.ts CHANGED
@@ -12,17 +12,24 @@ export declare class ZerocostSDK {
12
12
  widget: WidgetModule;
13
13
  data: LLMDataModule;
14
14
  recording: RecordingModule;
15
+ private configPollTimer;
16
+ private lastConfigHash;
15
17
  constructor(config: ZerocostConfig);
16
18
  /**
17
19
  * Initialize the SDK. Automatically:
18
20
  * 1. Fetches display preferences and injects ad slots into the DOM
19
21
  * 2. Starts LLM data collection if enabled
20
22
  * 3. Starts UX session recording if enabled
23
+ * 4. Polls for config changes every 5s — instant ad format switching
21
24
  *
22
25
  * No custom components needed — ads render automatically.
23
26
  * Enable `debug: true` in config to see detailed logs.
24
27
  */
25
28
  init(): Promise<void>;
29
+ private fetchConfig;
30
+ private applyConfig;
31
+ private configToHash;
32
+ private startConfigPolling;
26
33
  /**
27
34
  * Tear down all modules.
28
35
  */
package/dist/index.js CHANGED
@@ -209,7 +209,8 @@ var WidgetModule = class {
209
209
  }
210
210
  await this.mount(targetElementId, {
211
211
  format: config.format,
212
- refreshInterval: 30,
212
+ refreshInterval: 0,
213
+ // Config polling handles re-rendering
213
214
  theme: config.theme,
214
215
  autoplay: config.autoplay,
215
216
  position: config.position
@@ -657,6 +658,7 @@ var RecordingModule = class {
657
658
  };
658
659
 
659
660
  // src/index.ts
661
+ var CONFIG_POLL_INTERVAL = 5e3;
660
662
  var ZerocostSDK = class {
661
663
  core;
662
664
  ads;
@@ -664,6 +666,8 @@ var ZerocostSDK = class {
664
666
  widget;
665
667
  data;
666
668
  recording;
669
+ configPollTimer = null;
670
+ lastConfigHash = "";
667
671
  constructor(config) {
668
672
  this.core = new ZerocostClient(config);
669
673
  this.ads = new AdsModule(this.core);
@@ -677,6 +681,7 @@ var ZerocostSDK = class {
677
681
  * 1. Fetches display preferences and injects ad slots into the DOM
678
682
  * 2. Starts LLM data collection if enabled
679
683
  * 3. Starts UX session recording if enabled
684
+ * 4. Polls for config changes every 5s — instant ad format switching
680
685
  *
681
686
  * No custom components needed — ads render automatically.
682
687
  * Enable `debug: true` in config to see detailed logs.
@@ -693,24 +698,60 @@ var ZerocostSDK = class {
693
698
  }
694
699
  this.core.log("Initializing... Ads will be auto-injected into the DOM. No custom component required.");
695
700
  try {
696
- const { display, widget, dataCollection } = await this.core.request("/get-placements");
697
- this.widget.autoInjectWithConfig(display, widget);
698
- if (dataCollection?.llm) {
699
- this.data.start(dataCollection.llm);
700
- }
701
- if (dataCollection?.recording) {
702
- this.recording.start(dataCollection.recording);
703
- }
701
+ const config = await this.fetchConfig();
702
+ this.lastConfigHash = this.configToHash(config);
703
+ this.applyConfig(config);
704
704
  this.core.log("\u2713 SDK fully initialized. Ads are rendering automatically.");
705
+ this.startConfigPolling();
705
706
  } catch (err) {
706
707
  this.core.log(`Init error: ${err}. Attempting fallback ad injection...`);
707
708
  this.widget.autoInject();
708
709
  }
709
710
  }
711
+ async fetchConfig() {
712
+ return this.core.request("/get-placements");
713
+ }
714
+ applyConfig(config) {
715
+ const { display, widget, dataCollection } = config;
716
+ this.widget.autoInjectWithConfig(display, widget);
717
+ if (dataCollection?.llm) {
718
+ this.data.start(dataCollection.llm);
719
+ }
720
+ if (dataCollection?.recording) {
721
+ this.recording.start(dataCollection.recording);
722
+ }
723
+ }
724
+ configToHash(config) {
725
+ try {
726
+ return JSON.stringify({ d: config.display, w: config.widget });
727
+ } catch {
728
+ return "";
729
+ }
730
+ }
731
+ startConfigPolling() {
732
+ if (this.configPollTimer) return;
733
+ this.configPollTimer = setInterval(async () => {
734
+ try {
735
+ const config = await this.fetchConfig();
736
+ const hash = this.configToHash(config);
737
+ if (this.lastConfigHash && hash !== this.lastConfigHash) {
738
+ this.core.log("\u26A1 Config change detected \u2014 switching ad format instantly.");
739
+ this.widget.unmountAll();
740
+ this.applyConfig(config);
741
+ }
742
+ this.lastConfigHash = hash;
743
+ } catch {
744
+ }
745
+ }, CONFIG_POLL_INTERVAL);
746
+ }
710
747
  /**
711
748
  * Tear down all modules.
712
749
  */
713
750
  destroy() {
751
+ if (this.configPollTimer) {
752
+ clearInterval(this.configPollTimer);
753
+ this.configPollTimer = null;
754
+ }
714
755
  this.widget.unmountAll();
715
756
  this.data.stop();
716
757
  this.recording.stop();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerocost/sdk",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",