@spacinbox/sdk 2.0.3 → 2.2.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.
@@ -1218,8 +1218,8 @@
1218
1218
  if (!tokenExpiresAt.value) return true;
1219
1219
  return tokenExpiresAt.value - Date.now() < REFRESH_THRESHOLD_MS;
1220
1220
  },
1221
- identify(user) {
1222
- ApiService.identifyVisitor({
1221
+ async identify(user) {
1222
+ return await ApiService.identifyVisitor({
1223
1223
  externalId: user.userId,
1224
1224
  name: user.name,
1225
1225
  email: user.email
@@ -1231,7 +1231,8 @@
1231
1231
  visitorId.value = visitor_id;
1232
1232
  localStorage.setItem(STORAGE_KEY, visitor_id);
1233
1233
  }
1234
- });
1234
+ return { success: true };
1235
+ }).catch(() => ({ success: false }));
1235
1236
  },
1236
1237
  clear() {
1237
1238
  localStorage.removeItem(STORAGE_KEY);
@@ -5933,10 +5934,9 @@
5933
5934
  // src/widget/mount.tsx
5934
5935
  init_service();
5935
5936
  init_state();
5936
- function mountWidget(options) {
5937
+ async function mountWidget(options) {
5937
5938
  const { appId: id, hideButton = false } = options;
5938
5939
  appId.value = id;
5939
- VisitorService.init().then(() => SocketService.connect());
5940
5940
  const host = document.createElement("div");
5941
5941
  host.id = "spacinbox-widget";
5942
5942
  document.body.appendChild(host);
@@ -5947,6 +5947,8 @@
5947
5947
  const mountPoint = document.createElement("div");
5948
5948
  shadow.appendChild(mountPoint);
5949
5949
  J(/* @__PURE__ */ u4(Widget, { hideButton }), mountPoint);
5950
+ await VisitorService.init();
5951
+ await SocketService.connect();
5950
5952
  return {
5951
5953
  open() {
5952
5954
  isOpen.value = true;
@@ -5956,7 +5958,7 @@
5956
5958
  },
5957
5959
  identify(user) {
5958
5960
  currentUser.value = user;
5959
- VisitorService.identify(user);
5961
+ return VisitorService.identify(user);
5960
5962
  },
5961
5963
  setMetadata(data) {
5962
5964
  metadata.value = { ...metadata.value, ...data };
@@ -5978,18 +5980,26 @@
5978
5980
 
5979
5981
  // src/index.ts
5980
5982
  var controller = null;
5983
+ var bootPromise = null;
5981
5984
  var Spacinbox = {
5982
- boot(options) {
5985
+ async boot(options) {
5983
5986
  if (typeof document === "undefined") {
5984
- return;
5987
+ return Promise.resolve({ success: false });
5985
5988
  }
5986
- if (controller) {
5987
- return;
5989
+ if (bootPromise) {
5990
+ return bootPromise;
5988
5991
  }
5989
- controller = mountWidget(options);
5992
+ bootPromise = mountWidget(options).then((instance) => {
5993
+ controller = instance;
5994
+ return { success: true };
5995
+ }).catch(() => ({ success: false }));
5996
+ return bootPromise;
5990
5997
  },
5991
- identify(user) {
5992
- controller?.identify(user);
5998
+ async identify(user) {
5999
+ if (!controller) {
6000
+ return { success: false };
6001
+ }
6002
+ return controller.identify(user);
5993
6003
  },
5994
6004
  setMetadata(data) {
5995
6005
  controller?.setMetadata(data);
@@ -6003,6 +6013,7 @@
6003
6013
  shutdown() {
6004
6014
  controller?.shutdown();
6005
6015
  controller = null;
6016
+ bootPromise = null;
6006
6017
  }
6007
6018
  };
6008
6019
  var src_default = Spacinbox;
@@ -6013,8 +6024,8 @@
6013
6024
  var appId2 = script?.dataset?.appId;
6014
6025
  var opened = typeof script?.dataset?.opened === "string";
6015
6026
  if (appId2) {
6016
- const boot = () => {
6017
- src_default.boot({ appId: appId2 });
6027
+ const boot = async () => {
6028
+ await src_default.boot({ appId: appId2 });
6018
6029
  if (opened) {
6019
6030
  src_default.open();
6020
6031
  }