17
17
18
18
import com .diffplug .common .base .Preconditions ;
19
19
import java .util .ArrayList ;
20
- import java .util .List ;
21
20
import java .util .concurrent .atomic .AtomicReference ;
22
21
import java .util .function .Consumer ;
23
22
import org .jetbrains .annotations .Nullable ;
24
23
24
+ /**
25
+ * - immediately on app startup, call `MacDeepLink.startCapturingBeforeSwt()`
26
+ * - once SWT has initialized, call `MacDeepLink.swtHasInitializedBeginReceiving(Consumer<String>)`
27
+ *
28
+ * That's all! Don't do anything else.
29
+ */
25
30
public class MacDeepLink {
26
- private static volatile @ Nullable Consumer <String > urlHandler ;
27
- private static final AtomicReference <@ Nullable List <String >> backlogUrls = new AtomicReference <>();
31
+ private static final AtomicReference <@ Nullable Object > state = new AtomicReference <>();
28
32
29
33
static {
30
34
// Load the native library - try multiple strategies
@@ -36,22 +40,19 @@ public class MacDeepLink {
36
40
}
37
41
}
38
42
39
- public static void setURLHandler (Consumer <String > handler ) {
40
- Preconditions .checkArgument (urlHandler == null , "URL handler can only be set once" );
41
- urlHandler = handler ;
42
- }
43
-
44
- public static void applicationStartBeforeSwt () {
45
- Preconditions .checkArgument (urlHandler != null , "Call `setURLHandler()` first" );
46
- backlogUrls .set (new ArrayList <>());
43
+ public static void startCapturingBeforeSwt () {
44
+ var was = state .getAndSet (new ArrayList <>());
45
+ Preconditions .checkArgument (was == null , "`startCapturingBeforeSwt() should be called first`" );
47
46
nativeBeforeSwt ();
48
47
}
49
48
50
- public static void applicationStartAfterSwt () {
51
- Preconditions .checkArgument (backlogUrls .get () != null , "Call `applicationStartBeforeSwt()` first." );
49
+ public static void swtHasInitializedBeginReceiving (Consumer <String > handler ) {
50
+ var was = state .getAndSet (handler );
51
+ Preconditions .checkArgument (was instanceof ArrayList <?>, "Call `applicationStartBeforeSwt()` first." );
52
+
53
+ var backlog = (ArrayList <String >) was ;
54
+ backlog .forEach (handler );
52
55
nativeAfterSwt ();
53
- var accumulatedBacklog = backlogUrls .getAndSet (null );
54
- accumulatedBacklog .forEach (urlHandler );
55
56
}
56
57
57
58
// Native method declarations - implemented in DeepLinkBridge.m
@@ -66,11 +67,13 @@ public static void applicationStartAfterSwt() {
66
67
* @param url The URL string received from the operating system
67
68
*/
68
69
public static void deliverURL (String url ) {
69
- var backlog = backlogUrls .get ();
70
- if (backlog != null ) {
71
- backlog .add (url );
70
+ var was = state .get ();
71
+ if (was instanceof Consumer ) {
72
+ ((Consumer <String >) was ).accept (url );
73
+ } else if (was instanceof ArrayList <?>) {
74
+ ((ArrayList <String >) was ).add (url );
72
75
} else {
73
- urlHandler . accept ( url );
76
+ throw new IllegalStateException ( "Expected Consumer or ArrayList, was " + was );
74
77
}
75
78
}
76
79
}
0 commit comments