@staffbase/plugins-client-sdk 2.0.1 → 2.0.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.
@@ -2,12 +2,12 @@
2
2
  * Bundle of @staffbase/plugins-client-sdk
3
3
  * @file Staffbase plugins client SDK for JavaScript
4
4
  * @see https://github.com/Staffbase/plugins-client-sdk#readme
5
- * @version 2.0.0
5
+ * @version 2.0.1
6
6
  *
7
7
  * @author Stefan Staude <stefan.staude@staffbase.com>
8
8
  * @author Daniel Große <daniel.grosse@staffbase.com>
9
9
  *
10
- * @copyright 2023
10
+ * @copyright 2024
11
11
  * @license Apache-2.0
12
12
  */
13
13
 
@@ -237,6 +237,9 @@
237
237
  self.setLevel(defaultLevel, false);
238
238
  clearPersistedLevel();
239
239
  };
240
+ self.enableAll = function (persist) {
241
+ self.setLevel(self.levels.TRACE, persist);
242
+ };
240
243
  self.disableAll = function (persist) {
241
244
  self.setLevel(self.levels.SILENT, persist);
242
245
  };
@@ -938,7 +941,7 @@
938
941
  }
939
942
  };
940
943
 
941
- let connection$2 = null;
944
+ let connection$1 = null;
942
945
  const fallbackKickIn = 500;
943
946
 
944
947
  /**
@@ -949,15 +952,15 @@
949
952
  * @return {Promise<function>} An appropriate send function
950
953
  */
951
954
  var fallback = (() => {
952
- if (connection$2) {
953
- return connection$2;
955
+ if (connection$1) {
956
+ return connection$1;
954
957
  }
955
- connection$2 = new Promise(resolve => {
958
+ connection$1 = new Promise(resolve => {
956
959
  setTimeout(function () {
957
- resolve(sendMessage$3);
960
+ resolve(sendMessage$2);
958
961
  }, fallbackKickIn);
959
962
  });
960
- return connection$2;
963
+ return connection$1;
961
964
  });
962
965
 
963
966
  /**
@@ -969,7 +972,7 @@
969
972
  * @return {Promise<any>} which awaits the response of the Staffbase App
970
973
  * @throws {Error} on commands not supported by protocol
971
974
  */
972
- const sendMessage$3 = async function (cmd) {
975
+ const sendMessage$2 = async function (cmd) {
973
976
  for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
974
977
  payload[_key - 1] = arguments[_key];
975
978
  }
@@ -1077,11 +1080,9 @@
1077
1080
  *
1078
1081
  * @param {string} id of the promise
1079
1082
  * @param {any} msg the message which will will be passed to resolve
1080
- *
1081
- * @throws {Error} on unknown id
1082
1083
  */
1083
1084
  const resolve = (id, msg) => {
1084
- if (!(id in promiseMap)) throw new Error('Tried to resolve an unknown [' + id + '] promise.');
1085
+ if (!(id in promiseMap)) return;
1085
1086
  promiseMap[id].resolve(msg);
1086
1087
  delete promiseMap[id];
1087
1088
  };
@@ -1091,10 +1092,9 @@
1091
1092
  *
1092
1093
  * @param {string} id of the promise
1093
1094
  * @param {any} err the error which will will be passed to reject
1094
- * @throws {Error} on unknown id
1095
1095
  */
1096
1096
  const reject = (id, err) => {
1097
- if (!(id in promiseMap)) throw new Error('Tried to reject an unknown [' + id + '] promise.');
1097
+ if (!(id in promiseMap)) return;
1098
1098
  promiseMap[id].reject(err);
1099
1099
  delete promiseMap[id];
1100
1100
  };
@@ -1129,7 +1129,7 @@
1129
1129
  * @static
1130
1130
  * @return {StaticValueStore}
1131
1131
  */
1132
- const dataStore$1 = _ref => {
1132
+ const dataStore = _ref => {
1133
1133
  let {
1134
1134
  platform,
1135
1135
  language
@@ -1144,26 +1144,53 @@
1144
1144
  branchDefaultLanguage: language.branchDefaultLanguage
1145
1145
  };
1146
1146
  };
1147
- let connection$1 = null;
1147
+ let connection = null;
1148
1148
  const targetOrigin = '*';
1149
1149
 
1150
1150
  /**
1151
1151
  * Connect to the Staffbase App.
1152
1152
  *
1153
- * Create a connection to a Staffbase app 3.6
1154
- * @return {Promise<function>} An appropriate send function
1153
+ * Create a connection to a Staffbase app
1154
+ * Tries to reconnect until an answer is received
1155
+ *
1156
+ * @return {Promise<Function>} An appropriate send function
1155
1157
  */
1156
- const connect$2 = () => {
1157
- if (connection$1) {
1158
- return connection$1;
1158
+ const connect$1 = () => {
1159
+ if (connection) {
1160
+ return connection;
1159
1161
  }
1160
1162
  const connectId = create();
1161
- connection$1 = get(connectId).then(function (payload) {
1162
- return sendMessage$2(dataStore$1(payload));
1163
+ let timeout;
1164
+ const delayFactor = 1.2;
1165
+ connection = get(connectId).then(payload => {
1166
+ window.clearTimeout(timeout);
1167
+ return sendMessage$1(dataStore(payload));
1163
1168
  });
1164
1169
  window.addEventListener('message', receiveMessage);
1165
- window.parent.postMessage([protocol.HELLO, connectId, []], targetOrigin);
1166
- return connection$1;
1170
+ const recurringConnect = function () {
1171
+ let delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 500;
1172
+ timeout = window.setTimeout(() => {
1173
+ if (delay < 1200) {
1174
+ recurringConnect(delay * delayFactor);
1175
+ } else {
1176
+ reject(connectId, 'No answer from Staffbase App');
1177
+ disconnect();
1178
+ }
1179
+ }, delay);
1180
+ window.parent.postMessage([protocol.HELLO, connectId, []], targetOrigin);
1181
+ };
1182
+ recurringConnect();
1183
+ return connection;
1184
+ };
1185
+
1186
+ /**
1187
+ * Disconnect from the Staffbase App
1188
+ *
1189
+ * Only useful for tests.
1190
+ */
1191
+ const disconnect = () => {
1192
+ window.removeEventListener('message', receiveMessage);
1193
+ connection = null;
1167
1194
  };
1168
1195
 
1169
1196
  /**
@@ -1215,7 +1242,7 @@
1215
1242
  * @return {Promise<any>} which awaits the response of the Staffbase App
1216
1243
  * @throws {Error} on commands not supported by protocol
1217
1244
  */
1218
- const sendMessage$2 = store => async function (cmd) {
1245
+ const sendMessage$1 = store => async function (cmd) {
1219
1246
  switch (cmd) {
1220
1247
  case commands.version:
1221
1248
  case commands.native:
@@ -1232,7 +1259,7 @@
1232
1259
  for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1233
1260
  payload[_key - 1] = arguments[_key];
1234
1261
  }
1235
- return sendInvocationCall$1(create())(invocationMapping[cmd], payload);
1262
+ return sendInvocationCall(create())(invocationMapping[cmd], payload);
1236
1263
  default:
1237
1264
  throw new Error('Command ' + cmd + ' not supported by driver');
1238
1265
  }
@@ -1247,164 +1274,16 @@
1247
1274
  *
1248
1275
  * @return {Promise}
1249
1276
  */
1250
- const sendInvocationCall$1 = promiseID => (process, args) => {
1277
+ const sendInvocationCall = promiseID => (process, args) => {
1251
1278
  window.parent.postMessage([protocol.INVOCATION, promiseID, process, args], targetOrigin);
1252
1279
  return get(promiseID);
1253
1280
  };
1254
1281
 
1255
- let connection = null;
1256
- let outMsgQueue = [];
1257
-
1258
- /**
1259
- * Simple store solution to make the initial data available
1260
- * as static values
1261
- *
1262
- * @param {InitialValues} initial the initial data from the frontend
1263
- * @static
1264
- * @return {StaticValueStore}
1265
- */
1266
- const dataStore = _ref => {
1267
- let {
1268
- platform,
1269
- language
1270
- } = _ref;
1271
- return {
1272
- mobile: platform.mobile,
1273
- version: platform.version,
1274
- native: platform.native,
1275
- ios: platform.native === 'ios',
1276
- android: platform.native === 'android',
1277
- langInfos: language,
1278
- branchDefaultLang: language.branchDefaultLanguage
1279
- };
1280
- };
1281
- window.Staffbase = window.Staffbase || {};
1282
- window.Staffbase.plugins = {
1283
- getMessages: multiMessageProvider,
1284
- putMessage: singleMessageReceiver
1285
- };
1286
-
1287
- /**
1288
- * Connect to the Staffbase App.
1289
- *
1290
- * Create a connection to a Staffbase app 3.6 from a native tab
1291
- * @return {Promise<function>} An appropriate send function
1292
- */
1293
- const connect$1 = () => {
1294
- if (connection) {
1295
- return connection;
1296
- }
1297
- const connectId = create();
1298
- connection = get(connectId).then(function (payload) {
1299
- return sendMessage$1(dataStore(payload));
1300
- });
1301
- outMsgQueue.push([protocol.HELLO, connectId, []]);
1302
- return connection;
1303
- };
1304
-
1305
- /**
1306
- * A function which returns an array of messages
1307
- *
1308
- * The return value holds all messages in the order the were
1309
- * received over time by sendMessage
1310
- *
1311
- * @return {Array} ordered list of messages
1312
- */
1313
- function multiMessageProvider() {
1314
- const queueRef = outMsgQueue;
1315
- if (queueRef.length) ;
1316
- outMsgQueue = [];
1317
- return queueRef;
1318
- }
1319
-
1320
- /**
1321
- * A function which can receive a single message.
1322
- *
1323
- * Can be attached to window.onPostMessage
1324
- * @param {Array} msg Staffbase 3.6 message
1325
- */
1326
- function singleMessageReceiver(msg) {
1327
- let type;
1328
- let id;
1329
- let payload;
1330
-
1331
- // safe destructure
1332
- try {
1333
- [type, id, payload] = msg;
1334
- switch (type) {
1335
- case protocol.SUCCESS:
1336
- resolve(id, payload);
1337
- break;
1338
- case protocol.ERROR:
1339
- reject(id, payload);
1340
- break;
1341
- default:
1342
- // even thought catch-ignore is a bad style
1343
- // there may be other participants listening
1344
- // to messages in a different format so we
1345
- // silently ignore here
1346
- return;
1347
- }
1348
- } catch (e) {
1349
- // even thought catch-ignore is a bad style
1350
- // there may be other participants listening
1351
- // to messages in a different format so we
1352
- // silently ignore here
1353
- return;
1354
- }
1355
- }
1356
-
1357
- /**
1358
- * Send a SDK command to the Staffbase App.
1359
- *
1360
- * Translates SDK commands into protocol native commands.
1361
- * @param {StaticValueStore} store the store object
1362
- * @param {String} cmd an SDK command
1363
- * @param {array} payload for the command
1364
- * @return {Promise<any>} which awaits the response of the Staffbase App
1365
- * @throws {Error} on commands not supported by protocol
1366
- */
1367
- const sendMessage$1 = store => async function (cmd) {
1368
- switch (cmd) {
1369
- case commands.version:
1370
- case commands.native:
1371
- case commands.mobile:
1372
- case commands.ios:
1373
- case commands.android:
1374
- case commands.langInfos:
1375
- case commands.branchDefaultLang:
1376
- return store[reversedCommands[cmd]];
1377
- case commands.openLink:
1378
- case commands.prefContentLang:
1379
- for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1380
- payload[_key - 1] = arguments[_key];
1381
- }
1382
- return sendInvocationCall(invocationMapping[cmd], payload);
1383
- default:
1384
- throw new Error('Command ' + cmd + ' not supported by driver');
1385
- }
1386
- };
1387
-
1388
- /**
1389
- * Create a promise and send an invocation call to the frontend
1390
- *
1391
- * @param {string} process the name of the process to call
1392
- * @param {array} args an array of arguments
1393
- *
1394
- * @return {Promise}
1395
- */
1396
- const sendInvocationCall = (process, args) => {
1397
- const promiseID = create();
1398
- outMsgQueue.push([protocol.INVOCATION, promiseID, process, args]);
1399
- return get(promiseID);
1400
- };
1401
-
1402
1282
  let connector;
1403
1283
  const connect = async () => {
1404
- const putMessageConnection = connect$1();
1405
- const postMessageConnection = connect$2();
1284
+ const postMessageConnection = connect$1();
1406
1285
  const fallbackConnection = fallback();
1407
- const realConnectionBucket = [putMessageConnection, postMessageConnection];
1286
+ const realConnectionBucket = [postMessageConnection];
1408
1287
  const fallbackConnectionBucket = realConnectionBucket.concat(fallbackConnection);
1409
1288
 
1410
1289
  // Wait on the real communication and replace the connector with
@@ -1673,6 +1552,7 @@
1673
1552
  * Interface exports
1674
1553
  */
1675
1554
 
1555
+
1676
1556
  /**
1677
1557
  * Check if device is able to perform a download.
1678
1558
  * @function