web-launch-kit 0.0.5 → 0.0.7

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.
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.LaunchKit = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
- var version$3 = "0.0.5";
7
+ var version$3 = "0.0.7";
8
8
  var packageJSON$3 = {
9
9
  version: version$3};
10
10
 
@@ -1900,6 +1900,7 @@
1900
1900
  var NAVIGATOR = GLOBAL.navigator;
1901
1901
  var CLEANUP_INPUT_ELEMENT = null;
1902
1902
  var DEBUG = false;
1903
+ var APP_IN_FLIGHT = null;
1903
1904
  function createTypeGuard(tag) {
1904
1905
  return function (value) {
1905
1906
  return Object.prototype.toString.call(value) === '[object ' + tag + ']';
@@ -2116,7 +2117,8 @@
2116
2117
  };
2117
2118
  }
2118
2119
  }
2119
- function openURL(tried, url, timeout) {
2120
+ function openURL(tried, url, timeout, terminal) {
2121
+ if (terminal === void 0) { terminal = false; }
2120
2122
  var config = resolveFocusEventConfig();
2121
2123
  var top = getTopmostWindow();
2122
2124
  var topDocument = top.document;
@@ -2154,6 +2156,8 @@
2154
2156
  }
2155
2157
  }
2156
2158
  }
2159
+ if (terminal)
2160
+ return;
2157
2161
  try {
2158
2162
  iframe = createHiddenElement('iframe');
2159
2163
  iframe.src = url;
@@ -2172,6 +2176,17 @@
2172
2176
  catch (_) {
2173
2177
  }
2174
2178
  }
2179
+ if (terminal) {
2180
+ return new Promise(function (resolve, reject) {
2181
+ try {
2182
+ open();
2183
+ resolve();
2184
+ }
2185
+ catch (_) {
2186
+ reject();
2187
+ }
2188
+ });
2189
+ }
2175
2190
  return new Promise(function (resolve, reject) {
2176
2191
  if (DEBUG)
2177
2192
  debugger;
@@ -2253,11 +2268,78 @@
2253
2268
  }
2254
2269
  });
2255
2270
  }
2271
+ var FUNCTION_FALLBACK_LABEL = '[function fallback]';
2272
+ var CANCEL_DELAY = 1000;
2273
+ function createLaunchError(code, message, attempted) {
2274
+ var error = new Error(message);
2275
+ var holder = error;
2276
+ holder['code'] = code;
2277
+ holder['attempted'] = attempted;
2278
+ return error;
2279
+ }
2280
+ function isWebURL(url) {
2281
+ return /^https?:\/\//i.test(url);
2282
+ }
2283
+ function isTerminalCandidate(by, url) {
2284
+ return (by === 'fallback' || by === 'store') && isWebURL(url);
2285
+ }
2286
+ function notifyAttempt(onAttempt, attempt) {
2287
+ if (typeof onAttempt !== 'function')
2288
+ return;
2289
+ try {
2290
+ onAttempt(attempt);
2291
+ }
2292
+ catch (_) {
2293
+ }
2294
+ }
2295
+ function describeAttempts(attempted) {
2296
+ var routes = [];
2297
+ for (var i = 0; i < attempted.length; i++)
2298
+ routes.push(attempted[i].by);
2299
+ return ' ' + attempted.length + ' candidate(s) attempted (' + joining(routes, undefined, ' \u2b62 ') + '). Read error.attempted for the URLs.';
2300
+ }
2301
+ function runCandidates(candidates, timeout, onAttempt, failure) {
2302
+ return new Promise(function (resolve, reject) {
2303
+ var attempted = [];
2304
+ var total = candidates.length;
2305
+ function step(index) {
2306
+ if (index >= total)
2307
+ return reject(createLaunchError('all-failed', failure + describeAttempts(attempted), attempted));
2308
+ var by = candidates[index][0];
2309
+ var url = candidates[index][1];
2310
+ var attempt = {
2311
+ by: by,
2312
+ url: typeof url === 'string' ? url : FUNCTION_FALLBACK_LABEL,
2313
+ index: index,
2314
+ total: total,
2315
+ };
2316
+ attempted.push(attempt);
2317
+ notifyAttempt(onAttempt, attempt);
2318
+ if (typeof url !== 'string') {
2319
+ try {
2320
+ url();
2321
+ }
2322
+ catch (_) {
2323
+ return step(index + 1);
2324
+ }
2325
+ return resolve(by);
2326
+ }
2327
+ openURL(index, url, timeout, isTerminalCandidate(by, url))
2328
+ .then(function () {
2329
+ resolve(by);
2330
+ })
2331
+ .catch(function () {
2332
+ step(index + 1);
2333
+ });
2334
+ }
2335
+ step(0);
2336
+ });
2337
+ }
2256
2338
  function resolveOptions(option) {
2257
2339
  var resolved = [];
2258
2340
  var os = PlatformKit.os.name;
2259
2341
  if (os === 'unknown')
2260
- throw new Error('Cannot resolve app open options: unsupported or undetected OS. (userAgent: "' + PlatformKit.userAgent + '")');
2342
+ throw createLaunchError('unsupported-os', 'Cannot resolve app open options: unsupported or undetected OS. (userAgent: "' + PlatformKit.userAgent + '")', []);
2261
2343
  var scheme;
2262
2344
  var fallback;
2263
2345
  var allowAppStore;
@@ -2267,6 +2349,7 @@
2267
2349
  var timeout;
2268
2350
  var intent;
2269
2351
  var packageName;
2352
+ var allowIntent = true;
2270
2353
  var bundleId;
2271
2354
  var trackId;
2272
2355
  var universal;
@@ -2276,6 +2359,7 @@
2276
2359
  case 'android':
2277
2360
  if (typeof option.android === 'undefined')
2278
2361
  return [[], 0];
2362
+ allowIntent = option.android.allowIntent !== false;
2279
2363
  intent = stripURL(option.android.intent);
2280
2364
  scheme = stripURL(option.android.scheme);
2281
2365
  fallback = stripURL(option.android.fallback);
@@ -2294,11 +2378,11 @@
2294
2378
  if (typeof parsed.fallback !== 'undefined' && typeof fallback === 'undefined')
2295
2379
  fallback = parsed.fallback;
2296
2380
  }
2297
- if (typeof scheme !== 'undefined' && typeof intent === 'undefined')
2381
+ if (allowIntent && typeof scheme !== 'undefined' && typeof intent === 'undefined')
2298
2382
  intent = createIntentURL(scheme, packageName, fallback);
2299
2383
  if (typeof timeout === 'undefined')
2300
2384
  timeout = ANDROID_DEFAULT_TIMEOUT;
2301
- if (typeof intent !== 'undefined' && canOpenIntent())
2385
+ if (allowIntent && typeof intent !== 'undefined' && canOpenIntent())
2302
2386
  resolved.push(['intent', intent]);
2303
2387
  if (typeof scheme !== 'undefined' && allowScheme)
2304
2388
  resolved.push(['scheme', scheme]);
@@ -2687,7 +2771,7 @@
2687
2771
  done(true);
2688
2772
  else
2689
2773
  done(false);
2690
- }, 1000);
2774
+ }, CANCEL_DELAY);
2691
2775
  }
2692
2776
  function onvisibilitychange() {
2693
2777
  if (!isDocumentHidden())
@@ -2819,6 +2903,8 @@
2819
2903
  },
2820
2904
  app: function (options) {
2821
2905
  if (options === void 0) { options = {}; }
2906
+ if (APP_IN_FLIGHT !== null)
2907
+ return APP_IN_FLIGHT;
2822
2908
  var resolved;
2823
2909
  try {
2824
2910
  resolved = resolveOptions(options);
@@ -2829,39 +2915,15 @@
2829
2915
  var urls = resolved[0];
2830
2916
  var timeout = resolved[1];
2831
2917
  if (urls.length === 0)
2832
- return Promise.reject(new Error('No openable URL candidates were resolved for the current OS ("' + PlatformKit.os.name + '"). Provide at least one of: scheme, intent, universal, fallback, or store id for this platform.'));
2833
- return new Promise(function (resolve, reject) {
2834
- var tried = [];
2835
- function openURLSequential(index) {
2836
- if (index === void 0) { index = 0; }
2837
- if (index >= urls.length)
2838
- return reject(new Error('Failed to open the application using all available URLs.\n\n' + 'Attempted URLs:\n' + joining(tried, undefined, '\n↓\n')));
2839
- var entry = urls[index];
2840
- var by = entry[0];
2841
- var url = entry[1];
2842
- if (typeof url === 'string') {
2843
- tried[index] = url;
2844
- return openURL(index, url, timeout)
2845
- .then(function () {
2846
- resolve(by);
2847
- })
2848
- .catch(function () {
2849
- openURLSequential(index + 1);
2850
- });
2851
- }
2852
- else {
2853
- tried[index] = '[function fallback]';
2854
- try {
2855
- url();
2856
- }
2857
- catch (_) {
2858
- return openURLSequential(index + 1);
2859
- }
2860
- resolve(by);
2861
- }
2862
- }
2863
- return openURLSequential();
2864
- });
2918
+ return Promise.reject(createLaunchError('no-candidates', 'No openable URL candidates were resolved for the current OS ("' + PlatformKit.os.name + '"). Provide at least one of: scheme, intent, universal, fallback, or store id for this platform.', []));
2919
+ var attempt = runCandidates(urls, timeout, options.onAttempt, 'Failed to open the application.');
2920
+ function release() {
2921
+ if (APP_IN_FLIGHT === attempt)
2922
+ APP_IN_FLIGHT = null;
2923
+ }
2924
+ APP_IN_FLIGHT = attempt;
2925
+ attempt.then(release, release);
2926
+ return attempt;
2865
2927
  },
2866
2928
  telephone: function (options) {
2867
2929
  if (options === void 0) { options = {}; }
@@ -2911,7 +2973,7 @@
2911
2973
  map: function (options) {
2912
2974
  if (options === void 0) { options = {}; }
2913
2975
  var params = [];
2914
- var urls = [];
2976
+ var candidates = [];
2915
2977
  var url = '';
2916
2978
  switch (PlatformKit.os.name) {
2917
2979
  case 'android':
@@ -2929,7 +2991,7 @@
2929
2991
  params.push('z=' + options.zoom);
2930
2992
  if (params.length > 0)
2931
2993
  url = url + '?' + joining(params, undefined, '&');
2932
- urls.push(url);
2994
+ candidates.push(['scheme', url]);
2933
2995
  break;
2934
2996
  case 'ios':
2935
2997
  case 'macos':
@@ -2949,7 +3011,7 @@
2949
3011
  }
2950
3012
  if (typeof options.zoom !== 'undefined')
2951
3013
  params.push('z=' + options.zoom);
2952
- urls.push('maps://?' + joining(params, undefined, '&'));
3014
+ candidates.push(['scheme', 'maps://?' + joining(params, undefined, '&')]);
2953
3015
  break;
2954
3016
  case 'windows':
2955
3017
  if (typeof options.directions !== 'undefined') {
@@ -2965,10 +3027,10 @@
2965
3027
  }
2966
3028
  if (typeof options.zoom !== 'undefined')
2967
3029
  params.push('lvl=' + options.zoom);
2968
- urls.push('bingmaps:?' + joining(params, undefined, '&'));
3030
+ candidates.push(['scheme', 'bingmaps:?' + joining(params, undefined, '&')]);
2969
3031
  }
2970
3032
  if (typeof options.fallback !== 'undefined') {
2971
- urls.push(stripURL(options.fallback));
3033
+ candidates.push(['fallback', stripURL(options.fallback)]);
2972
3034
  }
2973
3035
  else {
2974
3036
  var params_1 = ['api=1'];
@@ -2976,7 +3038,7 @@
2976
3038
  params_1.push('destination=' + encodeMapValue(options.directions.destination));
2977
3039
  if (typeof options.directions.origin !== 'undefined')
2978
3040
  params_1.push('origin=' + encodeMapValue(options.directions.origin));
2979
- urls.push('https://www.google.com/maps/dir/?' + joining(params_1, undefined, '&'));
3041
+ candidates.push(['fallback', 'https://www.google.com/maps/dir/?' + joining(params_1, undefined, '&')]);
2980
3042
  }
2981
3043
  else {
2982
3044
  if (typeof options.coordinate !== 'undefined')
@@ -2985,34 +3047,10 @@
2985
3047
  params_1.push('query=' + escapeURIComponentString(options.query));
2986
3048
  if (typeof options.zoom !== 'undefined')
2987
3049
  params_1.push('zoom=' + options.zoom);
2988
- urls.push('https://www.google.com/maps/search/?' + joining(params_1, undefined, '&'));
3050
+ candidates.push(['fallback', 'https://www.google.com/maps/search/?' + joining(params_1, undefined, '&')]);
2989
3051
  }
2990
3052
  }
2991
- return new Promise(function (resolve, reject) {
2992
- var tried = [];
2993
- function openURLSequential(index) {
2994
- if (index === void 0) { index = 0; }
2995
- if (index >= urls.length)
2996
- return reject(new Error('Failed to open a map using all available URLs.\n\nAttempted URLs:\n' + joining(tried, undefined, '\n↓\n')));
2997
- var url = urls[index];
2998
- if (typeof url === 'string') {
2999
- tried[index] = url;
3000
- return openURL(index, url, getDefaultTimeout())
3001
- .then(function () {
3002
- resolve();
3003
- })
3004
- .catch(function () {
3005
- openURLSequential(index + 1);
3006
- });
3007
- }
3008
- else {
3009
- tried[index] = '[function fallback]';
3010
- url();
3011
- resolve();
3012
- }
3013
- }
3014
- return openURLSequential();
3015
- });
3053
+ return runCandidates(candidates, getDefaultTimeout(), options.onAttempt, 'Failed to open a map.');
3016
3054
  },
3017
3055
  filepicker: function (options) {
3018
3056
  if (options === void 0) { options = {}; }