@magiclabs.ai/magicbook-client 0.7.1 → 0.7.2

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/index.cjs CHANGED
@@ -4224,11 +4224,9 @@ var DesignRequestOptions = {
4224
4224
  textStickerLevel: textStickerLevels
4225
4225
  };
4226
4226
  var DesignRequest = class {
4227
- // eslint-disable-next-line no-unused-vars
4228
4227
  constructor(parentId, client, designRequestProps) {
4229
- this.client = client;
4230
4228
  this.parentId = parentId;
4231
- this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4229
+ this.client = client;
4232
4230
  this.state = designRequestProps?.state || states[0];
4233
4231
  this.title = designRequestProps?.title || "";
4234
4232
  this.subtitle = designRequestProps?.subtitle;
@@ -4244,9 +4242,7 @@ var DesignRequest = class {
4244
4242
  this.images = new Images(this.client, this.parentId);
4245
4243
  this.userId = designRequestProps?.userId;
4246
4244
  }
4247
- webSocket;
4248
4245
  state;
4249
- parentId;
4250
4246
  title;
4251
4247
  subtitle;
4252
4248
  occasion;
@@ -4259,6 +4255,7 @@ var DesignRequest = class {
4259
4255
  embellishmentLevel;
4260
4256
  textStickerLevel;
4261
4257
  images;
4258
+ webSocket;
4262
4259
  userId;
4263
4260
  guid;
4264
4261
  timeout;
@@ -4280,6 +4277,7 @@ var DesignRequest = class {
4280
4277
  throw new Error("Design request already submitted");
4281
4278
  } else {
4282
4279
  submitDesignRequestProps && Object.assign(this, submitDesignRequestProps);
4280
+ this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4283
4281
  this.updateDesignRequest(
4284
4282
  (await this.client.engineAPI.books.update(this.parentId, this.toBook())).toDesignRequestProps()
4285
4283
  );
@@ -4328,7 +4326,7 @@ var DesignRequest = class {
4328
4326
  async eventHandler(detail, type = "MagicBook.designRequestUpdated") {
4329
4327
  const customEvent = new CustomEvent(type, { detail });
4330
4328
  if (statesToCloseWS.includes(detail.slug)) {
4331
- this.webSocket.close();
4329
+ this.webSocket?.close();
4332
4330
  if (statesToReport.includes(detail.slug)) {
4333
4331
  await this.client.engineAPI.books.report(this.parentId, {
4334
4332
  error: detail.slug === "error" ? "design" : "timeout",
@@ -4349,16 +4347,18 @@ var DesignRequest = class {
4349
4347
  }
4350
4348
  }
4351
4349
  async getProgress() {
4352
- let timeout;
4353
- this.webSocket.onmessage = async (event) => {
4354
- const detail = JSON.parse(event.data);
4355
- if (this.state !== detail.slug) {
4356
- timeout && clearTimeout(timeout);
4357
- timeout = this.timeoutHandler();
4358
- await this.eventHandler(detail);
4359
- }
4360
- };
4361
- this.webSocket.onclose = () => clearTimeout(timeout);
4350
+ if (this.webSocket) {
4351
+ let timeout;
4352
+ this.webSocket.onmessage = async (event) => {
4353
+ const detail = JSON.parse(event.data);
4354
+ if (this.state !== detail.slug) {
4355
+ timeout && clearTimeout(timeout);
4356
+ timeout = this.timeoutHandler();
4357
+ await this.eventHandler(detail);
4358
+ }
4359
+ };
4360
+ this.webSocket.onclose = () => clearTimeout(timeout);
4361
+ }
4362
4362
  }
4363
4363
  toBook() {
4364
4364
  const designRequest = { ...this };