-
Notifications
You must be signed in to change notification settings - Fork 376
fix: Remove throwing "initWithContext was not called or timed out", introduced in 5.4.0 #2408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
580b0bc
4c8a9e7
7e71e06
b51b6b9
fb729bb
6e2531e
23a763f
124f849
23ac7f8
fd7f6c0
dddaa67
920189a
6f27c46
900abf9
854c277
dfda60d
28bf106
bddd49c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,7 +263,6 @@ internal class OneSignalImp( | |
| suspendifyOnIO { | ||
| internalInit(context, appId) | ||
| } | ||
| initState = InitState.SUCCESS | ||
| return true | ||
| } | ||
|
|
||
|
|
@@ -306,22 +305,48 @@ internal class OneSignalImp( | |
| ) { | ||
| Logging.log(LogLevel.DEBUG, "Calling deprecated login(externalId: $externalId, jwtBearerToken: $jwtBearerToken)") | ||
|
|
||
| if (!initState.isSDKAccessible()) { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'login'") | ||
| // Check state and provide appropriate error messages | ||
| when (initState) { | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| InitState.NOT_STARTED -> { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'login'") | ||
| } | ||
| InitState.IN_PROGRESS, InitState.SUCCESS -> { | ||
| // Continue - these states allow proceeding (will wait if needed) | ||
| } | ||
| } | ||
|
|
||
| waitForInit() | ||
jinliu9508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| suspendifyOnIO { loginHelper.login(externalId, jwtBearerToken) } | ||
| } | ||
|
|
||
| override fun logout() { | ||
| Logging.log(LogLevel.DEBUG, "Calling deprecated logout()") | ||
|
|
||
| if (!initState.isSDKAccessible()) { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'logout'") | ||
| // Check state and provide appropriate error messages | ||
| when (initState) { | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| InitState.NOT_STARTED -> { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'logout'") | ||
| } | ||
| InitState.IN_PROGRESS, InitState.SUCCESS -> { | ||
| // Continue - these states allow proceeding (will wait if needed) | ||
| } | ||
|
||
| } | ||
|
|
||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| suspendifyOnIO { logoutHelper.logout() } | ||
| } | ||
|
|
||
|
|
@@ -358,6 +383,10 @@ internal class OneSignalImp( | |
| withTimeout(MAX_TIMEOUT_TO_INIT) { | ||
| initAwaiter.awaitSuspend() | ||
| } | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } catch (e: TimeoutCancellationException) { | ||
| throw IllegalStateException("initWithContext was timed out after $MAX_TIMEOUT_TO_INIT ms") | ||
| } | ||
|
|
@@ -384,13 +413,21 @@ internal class OneSignalImp( | |
| InitState.IN_PROGRESS -> { | ||
| Logging.debug("Waiting for init to complete...") | ||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| else -> { | ||
| // SUCCESS | ||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,8 +150,19 @@ class SDKInitTests : FunSpec({ | |
| accessorThread.join(500) | ||
|
|
||
| // Then | ||
| // should complete even SharedPreferences is unavailable | ||
| // should complete even SharedPreferences is unavailable (non-blocking) | ||
| accessorThread.isAlive shouldBe false | ||
|
|
||
| // Release the SharedPreferences lock so internalInit can complete | ||
| trigger.complete() | ||
|
|
||
| // Wait for initialization to complete (internalInit runs asynchronously) | ||
| var attempts = 0 | ||
|
||
| while (!os.isInitialized && attempts < 50) { | ||
| Thread.sleep(20) | ||
| attempts++ | ||
| } | ||
|
|
||
| os.isInitialized shouldBe true | ||
| } | ||
|
|
||
|
|
@@ -224,12 +235,23 @@ class SDKInitTests : FunSpec({ | |
| accessorThread.start() | ||
| accessorThread.join(500) | ||
|
|
||
| os.isInitialized shouldBe true | ||
| // initWithContext should return immediately (non-blocking) | ||
| // but isInitialized won't be true until internalInit completes | ||
| // which requires SharedPreferences to be unblocked | ||
| accessorThread.isAlive shouldBe true | ||
|
|
||
| // release the lock on SharedPreferences | ||
| // release the lock on SharedPreferences so internalInit can complete | ||
| trigger.complete() | ||
|
|
||
| // Wait for initialization to complete (internalInit runs asynchronously) | ||
| var initAttempts = 0 | ||
| while (!os.isInitialized && initAttempts < 50) { | ||
| Thread.sleep(20) | ||
| initAttempts++ | ||
| } | ||
|
|
||
| os.isInitialized shouldBe true | ||
|
|
||
| accessorThread.join(500) | ||
| accessorThread.isAlive shouldBe false | ||
| os.user.externalId shouldBe externalId | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.