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