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