@monterosa/sdk-interact-kit 0.18.0 → 0.18.2-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.cjs.js CHANGED
@@ -5,6 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var sdkConnectKit = require('@monterosa/sdk-connect-kit');
6
6
  var sdkUtil = require('@monterosa/sdk-util');
7
7
  var sdkCore = require('@monterosa/sdk-core');
8
+ var sdkInteractInterop = require('@monterosa/sdk-interact-interop');
9
+ var sdkStorageKit = require('@monterosa/sdk-storage-kit');
8
10
 
9
11
  /*! *****************************************************************************
10
12
  Copyright (c) Microsoft Corporation.
@@ -497,33 +499,6 @@ function onConnectionHealthState(connectionHealth, callback) {
497
499
  return sdkUtil.subscribe(connectionHealth, 'state', callback);
498
500
  }
499
501
 
500
- /**
501
- * @license
502
- * internal.ts
503
- * interact-kit
504
- *
505
- * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15
506
- * Copyright © 2022 Monterosa. All rights reserved.
507
- *
508
- * More details on the license can be found at https://www.monterosa.co/sdk/license
509
- */
510
- function fetchSettings(host, id) {
511
- return __awaiter(this, void 0, void 0, function () {
512
- var response, data;
513
- return __generator(this, function (_a) {
514
- switch (_a.label) {
515
- case 0: return [4 /*yield*/, fetch("https://" + host + "/projects/" + id.substring(0, 2) + "/" + id + "/settings.json")];
516
- case 1:
517
- response = _a.sent();
518
- return [4 /*yield*/, response.json()];
519
- case 2:
520
- data = _a.sent();
521
- return [2 /*return*/, data];
522
- }
523
- });
524
- });
525
- }
526
-
527
502
  /**
528
503
  * @license
529
504
  * constants.ts
@@ -821,7 +796,7 @@ function buildProject(options, context) {
821
796
  throw new Error("Unable to fetch project " + id + " listings. Possibly wrong project id.");
822
797
  }
823
798
  _a = project;
824
- return [4 /*yield*/, fetchSettings(project.host, project.id)];
799
+ return [4 /*yield*/, sdkInteractInterop.fetchSettings(project.host, project.id)];
825
800
  case 4:
826
801
  _a.fields = _b.sent();
827
802
  return [2 /*return*/, project];
@@ -905,7 +880,7 @@ function handleListings(project, data) {
905
880
  _d.trys.push([1, 4, , 5]);
906
881
  if (!(oldDigest !== undefined && newDigest !== oldDigest)) return [3 /*break*/, 3];
907
882
  _b = project;
908
- return [4 /*yield*/, fetchSettings(project.host, project.id)];
883
+ return [4 /*yield*/, sdkInteractInterop.fetchSettings(project.host, project.id)];
909
884
  case 2:
910
885
  _b.fields = _d.sent();
911
886
  _d.label = 3;
@@ -957,25 +932,6 @@ function getProject(sdk) {
957
932
  if (sdk === void 0) { sdk = sdkCore.getSdk(); }
958
933
  return getProjectMemoized(sdk);
959
934
  }
960
- /**
961
- * @internal
962
- */
963
- function fetchListings(host, projectId) {
964
- return __awaiter(this, void 0, void 0, function () {
965
- var response, data;
966
- return __generator(this, function (_a) {
967
- switch (_a.label) {
968
- case 0: return [4 /*yield*/, fetch("https://" + host + "/projects/" + projectId.substring(0, 2) + "/" + projectId + "/listings.json")];
969
- case 1:
970
- response = _a.sent();
971
- return [4 /*yield*/, response.json()];
972
- case 2:
973
- data = (_a.sent());
974
- return [2 /*return*/, data];
975
- }
976
- });
977
- });
978
- }
979
935
  /**
980
936
  * Adds an observer for when {@link InteractProject.fields | project fields}
981
937
  * are updated
@@ -2179,57 +2135,6 @@ var ElementImpl = /** @class */ (function (_super) {
2179
2135
  return ElementImpl;
2180
2136
  }(sdkUtil.Emitter));
2181
2137
 
2182
- /**
2183
- * @license
2184
- * calculate_percentage.ts
2185
- * interact-kit
2186
- *
2187
- * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-04-07
2188
- * Copyright © 2022 Monterosa. All rights reserved.
2189
- *
2190
- * More details on the license can be found at https://www.monterosa.co/sdk/license
2191
- */
2192
- var calculatePercentage = function (values) {
2193
- // Calculate sum of all votes
2194
- var sum = values.reduce(function (memo, value) { return memo + value; }, 0);
2195
- // create array of hashes
2196
- var results = values.map(function (value, idx) { return ({
2197
- idx: idx,
2198
- votes: value,
2199
- percentage: Math.round((100 * value) / sum) || 0,
2200
- }); });
2201
- // Sum them all up - this can be less, equal or greater than a 100%
2202
- var total = results.reduce(function (memo, _a) {
2203
- var percentage = _a.percentage;
2204
- return memo + percentage;
2205
- }, 0);
2206
- total = total || 100;
2207
- // Calculate number of percent that we are missing or that we need to lose
2208
- var delta = 100 - total;
2209
- var sign = delta >= 0 ? 1 : -1;
2210
- // Order all options by number of votes
2211
- results.sort(function (a, b) {
2212
- var result = sign * (b.votes - a.votes);
2213
- if (result !== 0) {
2214
- return result;
2215
- }
2216
- return sign * (a.idx - b.idx);
2217
- });
2218
- // If we have percents to lose then take one percent off lowest options
2219
- // If we are missing percents then add one percent to highest options
2220
- var i = 0;
2221
- while (delta !== 0) {
2222
- if (results[i].votes > 0) {
2223
- results[i].percentage += sign;
2224
- delta -= sign;
2225
- }
2226
- i++;
2227
- }
2228
- // Restore options order
2229
- results.sort(function (a, b) { return a.idx - b.idx; });
2230
- return results.map(function (item) { return item.percentage; });
2231
- };
2232
-
2233
2138
  /**
2234
2139
  * @license
2235
2140
  * factory.ts
@@ -2241,15 +2146,24 @@ var calculatePercentage = function (values) {
2241
2146
  * More details on the license can be found at https://www.monterosa.co/sdk/license
2242
2147
  */
2243
2148
  var elements = new Map();
2244
- var storageAvailable = sdkUtil.checkAvailability();
2245
2149
  function buildElement(options, context) {
2246
- if (elements.has(options.id)) {
2247
- return elements.get(options.id);
2248
- }
2249
- var element = new ElementImpl(options, context);
2250
- restoreAnswer(element);
2251
- elements.set(element.id, element);
2252
- return element;
2150
+ return __awaiter(this, void 0, void 0, function () {
2151
+ var element;
2152
+ return __generator(this, function (_a) {
2153
+ switch (_a.label) {
2154
+ case 0:
2155
+ if (elements.has(options.id)) {
2156
+ return [2 /*return*/, elements.get(options.id)];
2157
+ }
2158
+ element = new ElementImpl(options, context);
2159
+ return [4 /*yield*/, restoreAnswer(element)];
2160
+ case 1:
2161
+ _a.sent();
2162
+ elements.set(element.id, element);
2163
+ return [2 /*return*/, element];
2164
+ }
2165
+ });
2166
+ });
2253
2167
  }
2254
2168
  function getElementByPolld(pollId) {
2255
2169
  return Array.from(elements.values()).find(function (element) { return element.pollId === pollId; });
@@ -2348,19 +2262,21 @@ function handleCreateMessage(message) {
2348
2262
  if (event === null) {
2349
2263
  return [2 /*return*/];
2350
2264
  }
2351
- if (elements.has(options.id)) {
2352
- element = elements.get(options.id);
2353
- if (options.updated_at > element.updatedAt) {
2354
- element.update(options);
2355
- event.emit('update', element);
2356
- }
2357
- }
2358
- else {
2359
- context = __assign(__assign({}, event.context), { event: event });
2360
- element = buildElement(options, context);
2361
- event.emit('publish', element);
2265
+ if (!elements.has(options.id)) return [3 /*break*/, 2];
2266
+ element = elements.get(options.id);
2267
+ if (options.updated_at > element.updatedAt) {
2268
+ element.update(options);
2269
+ event.emit('update', element);
2362
2270
  }
2363
- return [2 /*return*/];
2271
+ return [3 /*break*/, 4];
2272
+ case 2:
2273
+ context = __assign(__assign({}, event.context), { event: event });
2274
+ return [4 /*yield*/, buildElement(options, context)];
2275
+ case 3:
2276
+ element = _a.sent();
2277
+ event.emit('publish', element);
2278
+ _a.label = 4;
2279
+ case 4: return [2 /*return*/];
2364
2280
  }
2365
2281
  });
2366
2282
  });
@@ -2388,7 +2304,7 @@ function handleFeedbackMessage(message) {
2388
2304
  return [2 /*return*/];
2389
2305
  }
2390
2306
  votes = counters.split(':').map(function (value) { return +value; });
2391
- percentages = calculatePercentage(votes);
2307
+ percentages = sdkUtil.calculatePercentage(votes);
2392
2308
  results = votes.map(function (item, idx) { return ({
2393
2309
  percentage: percentages[idx],
2394
2310
  votes: item,
@@ -2413,28 +2329,55 @@ function handleRevealMessage(message) {
2413
2329
  element.context.event.emit('update', element);
2414
2330
  }
2415
2331
  function handleEventHistory(history) {
2332
+ var e_1, _a;
2416
2333
  return __awaiter(this, void 0, void 0, function () {
2417
- var event, context, _i, _a, snapshot;
2418
- return __generator(this, function (_b) {
2419
- switch (_b.label) {
2334
+ var event, context, _b, _c, snapshot, e_1_1;
2335
+ return __generator(this, function (_d) {
2336
+ switch (_d.label) {
2420
2337
  case 0:
2421
2338
  if (history.timeline.length === 0) {
2422
2339
  return [2 /*return*/];
2423
2340
  }
2424
2341
  return [4 /*yield*/, getEvent(history.config.id)];
2425
2342
  case 1:
2426
- event = _b.sent();
2343
+ event = _d.sent();
2427
2344
  if (event === null) {
2428
2345
  return [2 /*return*/];
2429
2346
  }
2430
2347
  context = __assign(__assign({}, event.context), { event: event });
2431
- for (_i = 0, _a = history.timeline; _i < _a.length; _i++) {
2432
- snapshot = _a[_i];
2433
- if (!elements.has(snapshot.id)) {
2434
- buildElement(snapshot, context);
2435
- }
2436
- }
2437
- return [2 /*return*/];
2348
+ _d.label = 2;
2349
+ case 2:
2350
+ _d.trys.push([2, 8, 9, 14]);
2351
+ _b = __asyncValues(history.timeline);
2352
+ _d.label = 3;
2353
+ case 3: return [4 /*yield*/, _b.next()];
2354
+ case 4:
2355
+ if (!(_c = _d.sent(), !_c.done)) return [3 /*break*/, 7];
2356
+ snapshot = _c.value;
2357
+ if (!!elements.has(snapshot.id)) return [3 /*break*/, 6];
2358
+ return [4 /*yield*/, buildElement(snapshot, context)];
2359
+ case 5:
2360
+ _d.sent();
2361
+ _d.label = 6;
2362
+ case 6: return [3 /*break*/, 3];
2363
+ case 7: return [3 /*break*/, 14];
2364
+ case 8:
2365
+ e_1_1 = _d.sent();
2366
+ e_1 = { error: e_1_1 };
2367
+ return [3 /*break*/, 14];
2368
+ case 9:
2369
+ _d.trys.push([9, , 12, 13]);
2370
+ if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 11];
2371
+ return [4 /*yield*/, _a.call(_b)];
2372
+ case 10:
2373
+ _d.sent();
2374
+ _d.label = 11;
2375
+ case 11: return [3 /*break*/, 13];
2376
+ case 12:
2377
+ if (e_1) throw e_1.error;
2378
+ return [7 /*endfinally*/];
2379
+ case 13: return [7 /*endfinally*/];
2380
+ case 14: return [2 /*return*/];
2438
2381
  }
2439
2382
  });
2440
2383
  });
@@ -2463,27 +2406,43 @@ function sendAnswer(element, userAnswer) {
2463
2406
  });
2464
2407
  }
2465
2408
  function storeAnswer(element, userAnswer) {
2466
- try {
2467
- element.userAnswer = userAnswer;
2468
- if (storageAvailable) {
2469
- // Can throw a "QuotaExceededError" DOMException exception
2470
- // if the quota has been exceeded.
2471
- sdkUtil.setItem("element_" + element.id + "_vote", JSON.stringify(userAnswer));
2472
- }
2473
- }
2474
- catch (err) {
2475
- if (err instanceof Error) {
2476
- console.warn("Can't store user answer: " + err.message);
2477
- }
2478
- }
2409
+ return __awaiter(this, void 0, void 0, function () {
2410
+ var err_1;
2411
+ return __generator(this, function (_a) {
2412
+ switch (_a.label) {
2413
+ case 0:
2414
+ _a.trys.push([0, 2, , 3]);
2415
+ element.userAnswer = userAnswer;
2416
+ return [4 /*yield*/, sdkStorageKit.storageWrite("element_" + element.id + "_vote", JSON.stringify(userAnswer))];
2417
+ case 1:
2418
+ _a.sent();
2419
+ return [3 /*break*/, 3];
2420
+ case 2:
2421
+ err_1 = _a.sent();
2422
+ if (err_1 instanceof Error) {
2423
+ console.warn("Failed to store user answer: " + err_1.message);
2424
+ }
2425
+ return [3 /*break*/, 3];
2426
+ case 3: return [2 /*return*/];
2427
+ }
2428
+ });
2429
+ });
2479
2430
  }
2480
2431
  function restoreAnswer(element) {
2481
- if (storageAvailable) {
2482
- var userAnswer = sdkUtil.getItem("element_" + element.id + "_vote");
2483
- if (userAnswer !== null) {
2484
- element.userAnswer = Answer.fromJSON(userAnswer);
2485
- }
2486
- }
2432
+ return __awaiter(this, void 0, void 0, function () {
2433
+ var userAnswer;
2434
+ return __generator(this, function (_a) {
2435
+ switch (_a.label) {
2436
+ case 0: return [4 /*yield*/, sdkStorageKit.storageRead("element_" + element.id + "_vote")];
2437
+ case 1:
2438
+ userAnswer = _a.sent();
2439
+ if (userAnswer !== null) {
2440
+ element.userAnswer = Answer.fromJSON(userAnswer);
2441
+ }
2442
+ return [2 /*return*/];
2443
+ }
2444
+ });
2445
+ });
2487
2446
  }
2488
2447
 
2489
2448
  /**
@@ -2678,7 +2637,6 @@ exports.ElementImpl = ElementImpl;
2678
2637
  exports.EventImpl = EventImpl;
2679
2638
  exports.ProjectImpl = ProjectImpl;
2680
2639
  exports.answer = answer;
2681
- exports.fetchListings = fetchListings;
2682
2640
  exports.getConnect = getConnect;
2683
2641
  exports.getConnectionHealth = getConnectionHealth;
2684
2642
  exports.getElement = getElement;