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.
@@ -1,4 +1,4 @@
1
- var version$3 = "0.0.5";
1
+ var version$3 = "0.0.7";
2
2
  var packageJSON$3 = {
3
3
  version: version$3};
4
4
 
@@ -1894,6 +1894,7 @@ var GLOBAL = getGlobal$1();
1894
1894
  var NAVIGATOR = GLOBAL.navigator;
1895
1895
  var CLEANUP_INPUT_ELEMENT = null;
1896
1896
  var DEBUG = false;
1897
+ var APP_IN_FLIGHT = null;
1897
1898
  function createTypeGuard(tag) {
1898
1899
  return function (value) {
1899
1900
  return Object.prototype.toString.call(value) === '[object ' + tag + ']';
@@ -2110,7 +2111,8 @@ function resolveFocusEventConfig() {
2110
2111
  };
2111
2112
  }
2112
2113
  }
2113
- function openURL(tried, url, timeout) {
2114
+ function openURL(tried, url, timeout, terminal) {
2115
+ if (terminal === void 0) { terminal = false; }
2114
2116
  var config = resolveFocusEventConfig();
2115
2117
  var top = getTopmostWindow();
2116
2118
  var topDocument = top.document;
@@ -2148,6 +2150,8 @@ function openURL(tried, url, timeout) {
2148
2150
  }
2149
2151
  }
2150
2152
  }
2153
+ if (terminal)
2154
+ return;
2151
2155
  try {
2152
2156
  iframe = createHiddenElement('iframe');
2153
2157
  iframe.src = url;
@@ -2166,6 +2170,17 @@ function openURL(tried, url, timeout) {
2166
2170
  catch (_) {
2167
2171
  }
2168
2172
  }
2173
+ if (terminal) {
2174
+ return new Promise(function (resolve, reject) {
2175
+ try {
2176
+ open();
2177
+ resolve();
2178
+ }
2179
+ catch (_) {
2180
+ reject();
2181
+ }
2182
+ });
2183
+ }
2169
2184
  return new Promise(function (resolve, reject) {
2170
2185
  if (DEBUG)
2171
2186
  debugger;
@@ -2247,11 +2262,78 @@ function openURL(tried, url, timeout) {
2247
2262
  }
2248
2263
  });
2249
2264
  }
2265
+ var FUNCTION_FALLBACK_LABEL = '[function fallback]';
2266
+ var CANCEL_DELAY = 1000;
2267
+ function createLaunchError(code, message, attempted) {
2268
+ var error = new Error(message);
2269
+ var holder = error;
2270
+ holder['code'] = code;
2271
+ holder['attempted'] = attempted;
2272
+ return error;
2273
+ }
2274
+ function isWebURL(url) {
2275
+ return /^https?:\/\//i.test(url);
2276
+ }
2277
+ function isTerminalCandidate(by, url) {
2278
+ return (by === 'fallback' || by === 'store') && isWebURL(url);
2279
+ }
2280
+ function notifyAttempt(onAttempt, attempt) {
2281
+ if (typeof onAttempt !== 'function')
2282
+ return;
2283
+ try {
2284
+ onAttempt(attempt);
2285
+ }
2286
+ catch (_) {
2287
+ }
2288
+ }
2289
+ function describeAttempts(attempted) {
2290
+ var routes = [];
2291
+ for (var i = 0; i < attempted.length; i++)
2292
+ routes.push(attempted[i].by);
2293
+ return ' ' + attempted.length + ' candidate(s) attempted (' + joining(routes, undefined, ' \u2b62 ') + '). Read error.attempted for the URLs.';
2294
+ }
2295
+ function runCandidates(candidates, timeout, onAttempt, failure) {
2296
+ return new Promise(function (resolve, reject) {
2297
+ var attempted = [];
2298
+ var total = candidates.length;
2299
+ function step(index) {
2300
+ if (index >= total)
2301
+ return reject(createLaunchError('all-failed', failure + describeAttempts(attempted), attempted));
2302
+ var by = candidates[index][0];
2303
+ var url = candidates[index][1];
2304
+ var attempt = {
2305
+ by: by,
2306
+ url: typeof url === 'string' ? url : FUNCTION_FALLBACK_LABEL,
2307
+ index: index,
2308
+ total: total,
2309
+ };
2310
+ attempted.push(attempt);
2311
+ notifyAttempt(onAttempt, attempt);
2312
+ if (typeof url !== 'string') {
2313
+ try {
2314
+ url();
2315
+ }
2316
+ catch (_) {
2317
+ return step(index + 1);
2318
+ }
2319
+ return resolve(by);
2320
+ }
2321
+ openURL(index, url, timeout, isTerminalCandidate(by, url))
2322
+ .then(function () {
2323
+ resolve(by);
2324
+ })
2325
+ .catch(function () {
2326
+ step(index + 1);
2327
+ });
2328
+ }
2329
+ step(0);
2330
+ });
2331
+ }
2250
2332
  function resolveOptions(option) {
2251
2333
  var resolved = [];
2252
2334
  var os = PlatformKit.os.name;
2253
2335
  if (os === 'unknown')
2254
- throw new Error('Cannot resolve app open options: unsupported or undetected OS. (userAgent: "' + PlatformKit.userAgent + '")');
2336
+ throw createLaunchError('unsupported-os', 'Cannot resolve app open options: unsupported or undetected OS. (userAgent: "' + PlatformKit.userAgent + '")', []);
2255
2337
  var scheme;
2256
2338
  var fallback;
2257
2339
  var allowAppStore;
@@ -2261,6 +2343,7 @@ function resolveOptions(option) {
2261
2343
  var timeout;
2262
2344
  var intent;
2263
2345
  var packageName;
2346
+ var allowIntent = true;
2264
2347
  var bundleId;
2265
2348
  var trackId;
2266
2349
  var universal;
@@ -2270,6 +2353,7 @@ function resolveOptions(option) {
2270
2353
  case 'android':
2271
2354
  if (typeof option.android === 'undefined')
2272
2355
  return [[], 0];
2356
+ allowIntent = option.android.allowIntent !== false;
2273
2357
  intent = stripURL(option.android.intent);
2274
2358
  scheme = stripURL(option.android.scheme);
2275
2359
  fallback = stripURL(option.android.fallback);
@@ -2288,11 +2372,11 @@ function resolveOptions(option) {
2288
2372
  if (typeof parsed.fallback !== 'undefined' && typeof fallback === 'undefined')
2289
2373
  fallback = parsed.fallback;
2290
2374
  }
2291
- if (typeof scheme !== 'undefined' && typeof intent === 'undefined')
2375
+ if (allowIntent && typeof scheme !== 'undefined' && typeof intent === 'undefined')
2292
2376
  intent = createIntentURL(scheme, packageName, fallback);
2293
2377
  if (typeof timeout === 'undefined')
2294
2378
  timeout = ANDROID_DEFAULT_TIMEOUT;
2295
- if (typeof intent !== 'undefined' && canOpenIntent())
2379
+ if (allowIntent && typeof intent !== 'undefined' && canOpenIntent())
2296
2380
  resolved.push(['intent', intent]);
2297
2381
  if (typeof scheme !== 'undefined' && allowScheme)
2298
2382
  resolved.push(['scheme', scheme]);
@@ -2681,7 +2765,7 @@ function resolveFile(module) {
2681
2765
  done(true);
2682
2766
  else
2683
2767
  done(false);
2684
- }, 1000);
2768
+ }, CANCEL_DELAY);
2685
2769
  }
2686
2770
  function onvisibilitychange() {
2687
2771
  if (!isDocumentHidden())
@@ -2813,6 +2897,8 @@ var LaunchKit = {
2813
2897
  },
2814
2898
  app: function (options) {
2815
2899
  if (options === void 0) { options = {}; }
2900
+ if (APP_IN_FLIGHT !== null)
2901
+ return APP_IN_FLIGHT;
2816
2902
  var resolved;
2817
2903
  try {
2818
2904
  resolved = resolveOptions(options);
@@ -2823,39 +2909,15 @@ var LaunchKit = {
2823
2909
  var urls = resolved[0];
2824
2910
  var timeout = resolved[1];
2825
2911
  if (urls.length === 0)
2826
- 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.'));
2827
- return new Promise(function (resolve, reject) {
2828
- var tried = [];
2829
- function openURLSequential(index) {
2830
- if (index === void 0) { index = 0; }
2831
- if (index >= urls.length)
2832
- return reject(new Error('Failed to open the application using all available URLs.\n\n' + 'Attempted URLs:\n' + joining(tried, undefined, '\n↓\n')));
2833
- var entry = urls[index];
2834
- var by = entry[0];
2835
- var url = entry[1];
2836
- if (typeof url === 'string') {
2837
- tried[index] = url;
2838
- return openURL(index, url, timeout)
2839
- .then(function () {
2840
- resolve(by);
2841
- })
2842
- .catch(function () {
2843
- openURLSequential(index + 1);
2844
- });
2845
- }
2846
- else {
2847
- tried[index] = '[function fallback]';
2848
- try {
2849
- url();
2850
- }
2851
- catch (_) {
2852
- return openURLSequential(index + 1);
2853
- }
2854
- resolve(by);
2855
- }
2856
- }
2857
- return openURLSequential();
2858
- });
2912
+ 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.', []));
2913
+ var attempt = runCandidates(urls, timeout, options.onAttempt, 'Failed to open the application.');
2914
+ function release() {
2915
+ if (APP_IN_FLIGHT === attempt)
2916
+ APP_IN_FLIGHT = null;
2917
+ }
2918
+ APP_IN_FLIGHT = attempt;
2919
+ attempt.then(release, release);
2920
+ return attempt;
2859
2921
  },
2860
2922
  telephone: function (options) {
2861
2923
  if (options === void 0) { options = {}; }
@@ -2905,7 +2967,7 @@ var LaunchKit = {
2905
2967
  map: function (options) {
2906
2968
  if (options === void 0) { options = {}; }
2907
2969
  var params = [];
2908
- var urls = [];
2970
+ var candidates = [];
2909
2971
  var url = '';
2910
2972
  switch (PlatformKit.os.name) {
2911
2973
  case 'android':
@@ -2923,7 +2985,7 @@ var LaunchKit = {
2923
2985
  params.push('z=' + options.zoom);
2924
2986
  if (params.length > 0)
2925
2987
  url = url + '?' + joining(params, undefined, '&');
2926
- urls.push(url);
2988
+ candidates.push(['scheme', url]);
2927
2989
  break;
2928
2990
  case 'ios':
2929
2991
  case 'macos':
@@ -2943,7 +3005,7 @@ var LaunchKit = {
2943
3005
  }
2944
3006
  if (typeof options.zoom !== 'undefined')
2945
3007
  params.push('z=' + options.zoom);
2946
- urls.push('maps://?' + joining(params, undefined, '&'));
3008
+ candidates.push(['scheme', 'maps://?' + joining(params, undefined, '&')]);
2947
3009
  break;
2948
3010
  case 'windows':
2949
3011
  if (typeof options.directions !== 'undefined') {
@@ -2959,10 +3021,10 @@ var LaunchKit = {
2959
3021
  }
2960
3022
  if (typeof options.zoom !== 'undefined')
2961
3023
  params.push('lvl=' + options.zoom);
2962
- urls.push('bingmaps:?' + joining(params, undefined, '&'));
3024
+ candidates.push(['scheme', 'bingmaps:?' + joining(params, undefined, '&')]);
2963
3025
  }
2964
3026
  if (typeof options.fallback !== 'undefined') {
2965
- urls.push(stripURL(options.fallback));
3027
+ candidates.push(['fallback', stripURL(options.fallback)]);
2966
3028
  }
2967
3029
  else {
2968
3030
  var params_1 = ['api=1'];
@@ -2970,7 +3032,7 @@ var LaunchKit = {
2970
3032
  params_1.push('destination=' + encodeMapValue(options.directions.destination));
2971
3033
  if (typeof options.directions.origin !== 'undefined')
2972
3034
  params_1.push('origin=' + encodeMapValue(options.directions.origin));
2973
- urls.push('https://www.google.com/maps/dir/?' + joining(params_1, undefined, '&'));
3035
+ candidates.push(['fallback', 'https://www.google.com/maps/dir/?' + joining(params_1, undefined, '&')]);
2974
3036
  }
2975
3037
  else {
2976
3038
  if (typeof options.coordinate !== 'undefined')
@@ -2979,34 +3041,10 @@ var LaunchKit = {
2979
3041
  params_1.push('query=' + escapeURIComponentString(options.query));
2980
3042
  if (typeof options.zoom !== 'undefined')
2981
3043
  params_1.push('zoom=' + options.zoom);
2982
- urls.push('https://www.google.com/maps/search/?' + joining(params_1, undefined, '&'));
3044
+ candidates.push(['fallback', 'https://www.google.com/maps/search/?' + joining(params_1, undefined, '&')]);
2983
3045
  }
2984
3046
  }
2985
- return new Promise(function (resolve, reject) {
2986
- var tried = [];
2987
- function openURLSequential(index) {
2988
- if (index === void 0) { index = 0; }
2989
- if (index >= urls.length)
2990
- return reject(new Error('Failed to open a map using all available URLs.\n\nAttempted URLs:\n' + joining(tried, undefined, '\n↓\n')));
2991
- var url = urls[index];
2992
- if (typeof url === 'string') {
2993
- tried[index] = url;
2994
- return openURL(index, url, getDefaultTimeout())
2995
- .then(function () {
2996
- resolve();
2997
- })
2998
- .catch(function () {
2999
- openURLSequential(index + 1);
3000
- });
3001
- }
3002
- else {
3003
- tried[index] = '[function fallback]';
3004
- url();
3005
- resolve();
3006
- }
3007
- }
3008
- return openURLSequential();
3009
- });
3047
+ return runCandidates(candidates, getDefaultTimeout(), options.onAttempt, 'Failed to open a map.');
3010
3048
  },
3011
3049
  filepicker: function (options) {
3012
3050
  if (options === void 0) { options = {}; }