ReviewOS

also looking at this

craft-native/craft

fix(android): hand the launch link to the page that subscribes for it

#232
Merged glennmichael123 wants to merge fix/android-deep-link-replay into main
17 files +776 -136
packages/ios/templates/CraftApp.swiftmodified+26-14
Changes to packages/ios/templates/CraftApp.swift
@@ -2907,24 +2907,35 @@ struct CraftWebView: UIViewRepresentable {
29072907 //
29082908 // Held here instead, and handed to the first subscriber on the
29092909 // 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.
29132919 (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 }
29192930 craft._subscribeDeepLinks = function(callback) {
29202931 var active = true;
2921 var listener = function(e) { callback(e.detail); };
2932 var listener = function(e) { if (!claimed(e.detail)) callback(e.detail); };
29222933 window.addEventListener('craftDeepLink', listener);
2923 if (!subscribed) {
2924 subscribed = true;
2934 if (!replay.subscribed) {
2935 replay.subscribed = true;
29252936 setTimeout(function() {
2926 var pending = undelivered;
2927 undelivered = [];
2937 var pending = replay.undelivered;
2938 replay.undelivered = [];
29282939 if (!active) return;
29292940 pending.forEach(function(detail) { callback(detail); });
29302941 }, 0);
@@ -2935,7 +2946,8 @@ struct CraftWebView: UIViewRepresentable {
29352946 };
29362947 };
29372948 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); });
29392951 };
29402952 })(window.craft);
29412953