Skip to content

Commit d97eb1a

Browse files
authored
Merge pull request #10 from yahoo/sal/optionalvalues
Value is a nullable return type
2 parents f46e22d + b8e4566 commit d97eb1a

File tree

6 files changed

+21
-36
lines changed

6 files changed

+21
-36
lines changed

behavior-graph/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import com.vanniktech.maven.publish.SonatypeHost
4242
mavenPublishing {
4343
configure(new KotlinJvm(new JavadocJar.Dokka("dokkaHtml"), true))
4444
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
45-
coordinates("com.yahoo.behaviorgraph", "bgjvm", "0.8.0")
45+
coordinates("com.yahoo.behaviorgraph", "bgjvm", "0.9.0")
4646
pom {
4747
name = 'Behavior Graph'
4848
description = 'Behavior Graph lets you build your programs out of small, easily understood pieces in a way that lets the computer do more of the work for you.'

behavior-graph/src/main/kotlin/behaviorgraph/TypedMoment.kt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,19 @@ class TypedMoment<T> @JvmOverloads constructor(extent: Extent<*>, debugName: Str
1616

1717
/**
1818
* Is there a current event and if the moment updated then what is the associated data.
19-
* Otherwise throws an error.
19+
* Will return null if the moment did not update this event.
20+
* Be careful: It is possible that T is also an optional type itself
21+
* So this typedMoment could be called with .update(null).
22+
* In that case justUpdated will be true and value will also be null.
2023
* A behavior must demand this resource to access its value.
2124
*/
2225
@get:JvmName("value")
23-
val value: T
26+
val value: T?
2427
get() {
2528
assertValidAccessor()
26-
if (!_happened) { throw BehaviorGraphException("Cannot access moment value when it did not update.")
27-
}
28-
return this._happenedValue!!
29+
return this._happenedValue
2930
}
3031

31-
/**
32-
* Optional version value() property which may be more convenient.
33-
* Returns T if justUpdated is true, null otherwise
34-
* Be careful: It is possible that T is also an optional type itself
35-
* So this typedMoment could be called with .update(null).
36-
* In that case justUpdated will be true but justUpdatedValue would be null.
37-
*/
38-
@get:JvmName("justUpdatedValue")
39-
val justUpdatedValue: T?
40-
get() = _happenedValue
41-
4232
/**
4333
* If this Moment has ever been update what was the last Event it was updated.
4434
* A behavior must demand this resource to access this property.

behavior-graph/src/test/kotlin/behaviorgraph/MomentTest.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class MomentTest : AbstractBehaviorGraphTest() {
5959
// |> Then the data is visible in subsequent behaviors
6060
assertEquals(1, afterUpdate)
6161
assertTrue(updatedToOne)
62-
// but is an Exception outside event loop
63-
assertBehaviorGraphException { mr1.value }
62+
// but null after event
63+
assertNull(mr1.value)
6464
}
6565

6666
@Test
@@ -81,19 +81,14 @@ class MomentTest : AbstractBehaviorGraphTest() {
8181
}
8282

8383
@Test
84-
fun `updatedValue returns optional wrapped value`() {
85-
// In some cases justUpdatedValue may be easier to work with
86-
// than checking for justUpdated and getting the value
87-
// however kotlin collapses the nulls, so there's no notion of
88-
// unwrapping as you might find in
84+
fun `value is optional`() {
8985
val mr1: TypedMoment<String> = ext.typedMoment()
9086
val mr2: TypedMoment<String?> = ext.typedMoment()
9187

9288
g.action {
9389
g.sideEffect {
94-
// mr1 was not just updated, so justUpdatedValue is null
95-
assertNull(mr1.justUpdatedValue)
96-
var run = false
90+
// mr1 was not just updated, so value is null
91+
assertNull(mr1.value)
9792
}
9893
}
9994

@@ -102,15 +97,15 @@ class MomentTest : AbstractBehaviorGraphTest() {
10297
mr2.update(null)
10398

10499
g.sideEffect {
105-
// mr1 was updated and it's value was hello so justUpdatedValue is hello
106-
assertEquals("hello", mr1.justUpdatedValue)
100+
// mr1 was updated, and it's value was hello so value is hello
101+
assertEquals("hello", mr1.value)
107102
// mr2 was also just updated, but with a value of null
108103
assertTrue(mr2.justUpdated)
109-
// however justUpdatedValue is null, so ?.let pattern will not run
104+
assertNull(mr2.value)
105+
// because is null, so ?.let pattern will not run
110106
var run = false
111-
mr2.justUpdatedValue?.let {
107+
mr2.value?.let {
112108
run = true
113-
assertNull(it)
114109
}
115110
assertFalse(run)
116111
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
}
1111
dependencies {
1212
// Build system
13-
classpath 'com.android.tools.build:gradle:8.2.0'
13+
classpath 'com.android.tools.build:gradle:8.5.2'
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1515
classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.9.20"
1616
}

example/src/main/kotlin/com/example/myapplication/ui/login/LoginExtent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class LoginExtent(var loginActivityBG: LoginActivityBG, graph: Graph) : Extent<L
8686
if (loggingIn.value) {
8787
status = "Logging in...";
8888
} else if (loggingIn.justUpdatedTo(false)) {
89-
if (loginComplete.justUpdated && loginComplete.value) {
89+
if (loginComplete.value == true) {
9090
status = "Login Success";
91-
} else if (loginComplete.justUpdated && !loginComplete.value) {
91+
} else if (loginComplete.value == false) {
9292
status = "Login Failed";
9393
}
9494
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Mon Aug 23 11:41:29 PDT 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)