also looking at this
fix(android): hand the launch link to the page that subscribes for it
#232
17 files
+776
-136
| @@ -2907,24 +2907,35 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2907 | 2907 | // |
| 2908 | 2908 | // Held here instead, and handed to the first subscriber on the |
| 2909 | 2909 | // next turn, so an unsubscribe returned in the same tick still |
| 2910 | // applies. The launch link is withdrawn when the page calls | |
| 2911 | // getInitialURL, its other way of receiving it, so a page that | |
| 2912 | // does both in the same tick, in either order, gets it once. | |
| 2910 | // applies. The launch link belongs to getInitialURL once the page | |
| 2911 | // has called it, whether native has dispatched it yet or not, so a | |
| 2912 | // page that does both in the same tick, in either order, gets it | |
| 2913 | // once. That includes a craftReady handler, which runs before | |
| 2914 | // native dispatches anything. A page that asks getInitialURL only | |
| 2915 | // later, after an await, should skip `initial` in onLink. | |
| 2916 | // | |
| 2917 | // The same block runs on Android (#215), where this script can run | |
| 2918 | // twice in one document, so its state lives on window. | |
| 2913 | 2919 | (function installDeepLinkReplay(craft) { |
| 2914 | var undelivered = []; | |
| 2915 | var subscribed = false; | |
| 2916 | window.addEventListener('craftDeepLink', function(e) { | |
| 2917 | if (!subscribed) undelivered.push(e.detail); | |
| 2918 | }); | |
| 2920 | var replay = window.__craftDeepLinkReplay; | |
| 2921 | if (!replay) { | |
| 2922 | replay = window.__craftDeepLinkReplay = {undelivered: [], subscribed: false, initialClaimed: false}; | |
| 2923 | window.addEventListener('craftDeepLink', function(e) { | |
| 2924 | if (!replay.subscribed && !claimed(e.detail)) replay.undelivered.push(e.detail); | |
| 2925 | }); | |
| 2926 | } | |
| 2927 | function claimed(detail) { | |
| 2928 | return replay.initialClaimed && detail && detail.initial; | |
| 2929 | } | |
| 2919 | 2930 | craft._subscribeDeepLinks = function(callback) { |
| 2920 | 2931 | var active = true; |
| 2921 | var listener = function(e) { callback(e.detail); }; | |
| 2932 | var listener = function(e) { if (!claimed(e.detail)) callback(e.detail); }; | |
| 2922 | 2933 | window.addEventListener('craftDeepLink', listener); |
| 2923 | if (!subscribed) { | |
| 2924 | subscribed = true; | |
| 2934 | if (!replay.subscribed) { | |
| 2935 | replay.subscribed = true; | |
| 2925 | 2936 | setTimeout(function() { |
| 2926 | var pending = undelivered; | |
| 2927 | undelivered = []; | |
| 2937 | var pending = replay.undelivered; | |
| 2938 | replay.undelivered = []; | |
| 2928 | 2939 | if (!active) return; |
| 2929 | 2940 | pending.forEach(function(detail) { callback(detail); }); |
| 2930 | 2941 | }, 0); |
| @@ -2935,7 +2946,8 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2935 | 2946 | }; |
| 2936 | 2947 | }; |
| 2937 | 2948 | craft._claimInitialDeepLink = function() { |
| 2938 | undelivered = undelivered.filter(function(detail) { return !(detail && detail.initial); }); | |
| 2949 | replay.initialClaimed = true; | |
| 2950 | replay.undelivered = replay.undelivered.filter(function(detail) { return !(detail && detail.initial); }); | |
| 2939 | 2951 | }; |
| 2940 | 2952 | })(window.craft); |
| 2941 | 2953 | |