@monterosa/sdk-interact-kit 0.19.0-rc.6 → 2.0.0-rc.1

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.esm.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { getConnect as getConnect$1, login as login$1, connect, disconnect, subscribe as subscribe$1, unsubscribe as unsubscribe$1, send, ConnState, onConnected } from '@monterosa/sdk-connect-kit';
2
- import { Emitter, memoizePromise, subscribe as subscribe$2, checksum, onTick, now, checkAvailability, calculatePercentage, getItem, setItem, createError } from '@monterosa/sdk-util';
2
+ import { Emitter, memoizePromise, subscribe as subscribe$2, checksum, onTick, now, calculatePercentage, getErrorMessage, createError } from '@monterosa/sdk-util';
3
3
  import { getSdk, Logger } from '@monterosa/sdk-core';
4
+ import { fetchSettings } from '@monterosa/sdk-interact-interop';
5
+ import { storageRead, storageWrite } from '@monterosa/sdk-storage-kit';
4
6
 
5
7
  /**
6
8
  * @license
@@ -286,20 +288,6 @@ function onConnectionHealthState(connectionHealth, callback) {
286
288
  return subscribe$2(connectionHealth, 'state', callback);
287
289
  }
288
290
 
289
- /**
290
- * @license
291
- * @monterosa/sdk-interact-kit
292
- *
293
- * Copyright © 2022 Monterosa Productions Limited. All rights reserved.
294
- *
295
- * More details on the license can be found at https://www.monterosa.co/sdk/license
296
- */
297
- async function fetchSettings(host, id) {
298
- const response = await fetch(`https://${host}/projects/${id.substring(0, 2)}/${id}/settings.json`);
299
- const data = await response.json();
300
- return data;
301
- }
302
-
303
291
  /**
304
292
  * @license
305
293
  * @monterosa/sdk-interact-kit
@@ -670,14 +658,6 @@ const getProjectMemoized = memoizePromise(async (sdk) => {
670
658
  function getProject(sdk = getSdk()) {
671
659
  return getProjectMemoized(sdk);
672
660
  }
673
- /**
674
- * @internal
675
- */
676
- async function fetchListings(host, projectId) {
677
- const response = await fetch(`https://${host}/projects/${projectId.substring(0, 2)}/${projectId}/listings.json`);
678
- const data = (await response.json());
679
- return data;
680
- }
681
661
  /**
682
662
  * Adds an observer for when {@link InteractProject.fields | project fields}
683
663
  * are updated
@@ -1458,6 +1438,7 @@ class ElementImpl extends Emitter {
1458
1438
  }
1459
1439
  update(data) {
1460
1440
  this._data = data;
1441
+ this.handleState();
1461
1442
  }
1462
1443
  destroy() {
1463
1444
  this.unsubscribeStateHandler();
@@ -1596,13 +1577,12 @@ class ElementImpl extends Emitter {
1596
1577
  * More details on the license can be found at https://www.monterosa.co/sdk/license
1597
1578
  */
1598
1579
  const elements = new Map();
1599
- const storageAvailable = checkAvailability();
1600
- function buildElement(options, context) {
1580
+ async function buildElement(options, context) {
1601
1581
  if (elements.has(options.id)) {
1602
1582
  return elements.get(options.id);
1603
1583
  }
1604
1584
  const element = new ElementImpl(options, context);
1605
- restoreAnswer(element);
1585
+ await restoreAnswer(element);
1606
1586
  elements.set(element.id, element);
1607
1587
  return element;
1608
1588
  }
@@ -1653,14 +1633,21 @@ async function handleCreateMessage(message) {
1653
1633
  }
1654
1634
  if (elements.has(options.id)) {
1655
1635
  const element = elements.get(options.id);
1656
- if (options.updated_at > element.updatedAt) {
1636
+ if (options.updated_at > element.updatedAt ||
1637
+ // This is a workaround to handle a Studio issue when two /create/ messages
1638
+ // are sent upon correct option reveal. The first /create/ message contains
1639
+ // the same duration but a different updated_at timestamp. The second /create/
1640
+ // message contains the same updated_at timestamp but a different duration.
1641
+ // Therefore, we need to check if the duration is different to ensure that
1642
+ // the element is updated correctly.
1643
+ options.duration !== element.duration) {
1657
1644
  element.update(options);
1658
1645
  event.emit('update', element);
1659
1646
  }
1660
1647
  }
1661
1648
  else {
1662
1649
  const context = Object.assign(Object.assign({}, event.context), { event });
1663
- const element = buildElement(options, context);
1650
+ const element = await buildElement(options, context);
1664
1651
  event.emit('publish', element);
1665
1652
  }
1666
1653
  }
@@ -1705,10 +1692,13 @@ function handleRevealMessage(message) {
1705
1692
  if (!element) {
1706
1693
  return;
1707
1694
  }
1708
- element.correctOption = correctOption;
1709
- element.context.event.emit('update', element);
1695
+ if (element.correctOption !== correctOption) {
1696
+ element.correctOption = correctOption;
1697
+ element.context.event.emit('update', element);
1698
+ }
1710
1699
  }
1711
1700
  async function handleEventHistory(history) {
1701
+ var _a, e_1, _b, _c;
1712
1702
  if (history.timeline.length === 0) {
1713
1703
  return;
1714
1704
  }
@@ -1717,11 +1707,28 @@ async function handleEventHistory(history) {
1717
1707
  return;
1718
1708
  }
1719
1709
  const context = Object.assign(Object.assign({}, event.context), { event });
1720
- for (const snapshot of history.timeline) {
1721
- if (!elements.has(snapshot.id)) {
1722
- buildElement(snapshot, context);
1710
+ try {
1711
+ for (var _d = true, _e = __asyncValues(history.timeline), _f; _f = await _e.next(), _a = _f.done, !_a;) {
1712
+ _c = _f.value;
1713
+ _d = false;
1714
+ try {
1715
+ const snapshot = _c;
1716
+ if (!elements.has(snapshot.id)) {
1717
+ await buildElement(snapshot, context);
1718
+ }
1719
+ }
1720
+ finally {
1721
+ _d = true;
1722
+ }
1723
1723
  }
1724
1724
  }
1725
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1726
+ finally {
1727
+ try {
1728
+ if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
1729
+ }
1730
+ finally { if (e_1) throw e_1.error; }
1731
+ }
1725
1732
  }
1726
1733
  async function sendAnswer(element, userAnswer) {
1727
1734
  const { project, event } = element.context;
@@ -1732,27 +1739,19 @@ async function sendAnswer(element, userAnswer) {
1732
1739
  .join(':');
1733
1740
  connect.sendVote(event.id, element.pollId, answer);
1734
1741
  }
1735
- function storeAnswer(element, userAnswer) {
1742
+ async function storeAnswer(element, userAnswer) {
1736
1743
  try {
1737
1744
  element.userAnswer = userAnswer;
1738
- if (storageAvailable) {
1739
- // Can throw a "QuotaExceededError" DOMException exception
1740
- // if the quota has been exceeded.
1741
- setItem(`element_${element.id}_vote`, JSON.stringify(userAnswer));
1742
- }
1745
+ await storageWrite(`element_${element.id}_vote`, JSON.stringify(userAnswer));
1743
1746
  }
1744
1747
  catch (err) {
1745
- if (err instanceof Error) {
1746
- console.warn(`Can't store user answer: ${err.message}`);
1747
- }
1748
+ console.warn(`Failed to store user answer: ${getErrorMessage(err)}`);
1748
1749
  }
1749
1750
  }
1750
- function restoreAnswer(element) {
1751
- if (storageAvailable) {
1752
- const userAnswer = getItem(`element_${element.id}_vote`);
1753
- if (userAnswer !== null) {
1754
- element.userAnswer = Answer.fromJSON(userAnswer);
1755
- }
1751
+ async function restoreAnswer(element) {
1752
+ const userAnswer = await storageRead(`element_${element.id}_vote`);
1753
+ if (userAnswer !== null) {
1754
+ element.userAnswer = Answer.fromJSON(userAnswer);
1756
1755
  }
1757
1756
  }
1758
1757
 
@@ -2058,5 +2057,5 @@ function onPresenceCounterClose(presenceCounter, callback) {
2058
2057
  return () => presenceCounter.off('state', handler);
2059
2058
  }
2060
2059
 
2061
- export { Answer, AnswerError, Channel, ConnectionHealthState, ElementImpl, ElementState, ElementType, EventImpl, EventState, Klass, PresenceCounterState, ProjectImpl, State, answer, fetchListings, getConnect, getConnectionHealth, getElement, getElementMemoized, getElements, getElementsMemoized, getEvent, getEventMemoized, getEvents, getEventsMemoized, getPresenceCounter, getProject, getProjectMemoized, login, onConnectionHealthState, onElementPublished, onElementResults, onElementRevoked, onElementStateChanged, onElementUpdated, onEventAdded, onEventPublished, onEventRemoved, onEventState, onEventUpdated, onPresenceCounterClose, onPresenceCounterOpen, onPresenceCounterUpdate, onProjectFieldsUpdated, onProjectListingsUpdated, validateAnswer };
2060
+ export { Answer, AnswerError, Channel, ConnectionHealthState, ElementImpl, ElementState, ElementType, EventImpl, EventState, Klass, PresenceCounterState, ProjectImpl, State, answer, getConnect, getConnectionHealth, getElement, getElementMemoized, getElements, getElementsMemoized, getEvent, getEventMemoized, getEvents, getEventsMemoized, getPresenceCounter, getProject, getProjectMemoized, login, onConnectionHealthState, onElementPublished, onElementResults, onElementRevoked, onElementStateChanged, onElementUpdated, onEventAdded, onEventPublished, onEventRemoved, onEventState, onEventUpdated, onPresenceCounterClose, onPresenceCounterOpen, onPresenceCounterUpdate, onProjectFieldsUpdated, onProjectListingsUpdated, validateAnswer };
2062
2061
  //# sourceMappingURL=index.esm.js.map