@@ -1877,7 +1877,40 @@ test("can make non-extenible objects observable", () => {
18771877 expect ( mobx . isObservableProp ( o , "x" ) ) . toBeTruthy ( )
18781878} )
18791879
1880- test ( "keeping computed properties alive works" , ( ) => {
1880+ test ( "keeping computed properties alive does not run before access" , ( ) => {
1881+ let calcs = 0
1882+ observable (
1883+ {
1884+ x : 1 ,
1885+ get y ( ) {
1886+ calcs ++
1887+ return this . x * 2
1888+ }
1889+ } ,
1890+ {
1891+ y : mobx . computed ( { keepAlive : true } )
1892+ }
1893+ )
1894+
1895+ expect ( calcs ) . toBe ( 0 ) // initially there is no calculation done
1896+ } )
1897+
1898+ test ( "(for objects) keeping computed properties alive does not run before access" , ( ) => {
1899+ let calcs = 0
1900+ class Foo {
1901+ @observable x = 1
1902+ @computed ( { keepAlive : true } )
1903+ get y ( ) {
1904+ calcs ++
1905+ return this . x * 2
1906+ }
1907+ }
1908+ new Foo ( )
1909+
1910+ expect ( calcs ) . toBe ( 0 ) // initially there is no calculation done
1911+ } )
1912+
1913+ test ( "keeping computed properties alive runs on first access" , ( ) => {
18811914 let calcs = 0
18821915 const x = observable (
18831916 {
@@ -1892,18 +1925,47 @@ test("keeping computed properties alive works", () => {
18921925 }
18931926 )
18941927
1895- expect ( calcs ) . toBe ( 0 )
1896- expect ( x . y ) . toBe ( 2 )
1928+ expect ( x . y ) . toBe ( 2 ) // perform calculation on access
18971929 expect ( calcs ) . toBe ( 1 )
1898- expect ( x . y ) . toBe ( 2 )
1899- expect ( calcs ) . toBe ( 1 ) // kept alive!
1930+ } )
19001931
1901- x . x = 3
1902- expect ( calcs ) . toBe ( 2 ) // reactively updated
1903- expect ( x . y ) . toBe ( 6 )
1932+ test ( "(for objects) keeping computed properties alive runs on first access" , ( ) => {
1933+ let calcs = 0
1934+ class Foo {
1935+ @observable x = 1
1936+ @computed ( { keepAlive : true } )
1937+ get y ( ) {
1938+ calcs ++
1939+ return this . x * 2
1940+ }
1941+ }
1942+ const x = new Foo ( )
1943+
1944+ expect ( x . y ) . toBe ( 2 ) // perform calculation on access
1945+ expect ( calcs ) . toBe ( 1 )
1946+ } )
1947+
1948+ test ( "keeping computed properties alive caches values on subsequent accesses" , ( ) => {
1949+ let calcs = 0
1950+ const x = observable (
1951+ {
1952+ x : 1 ,
1953+ get y ( ) {
1954+ calcs ++
1955+ return this . x * 2
1956+ }
1957+ } ,
1958+ {
1959+ y : mobx . computed ( { keepAlive : true } )
1960+ }
1961+ )
1962+
1963+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
1964+ expect ( x . y ) . toBe ( 2 ) // second access: use cached value, no calculation
1965+ expect ( calcs ) . toBe ( 1 ) // only one calculation: cached!
19041966} )
19051967
1906- test ( "keeping computed properties alive works for objects " , ( ) => {
1968+ test ( "(for objects) keeping computed properties alive caches values on subsequent accesses " , ( ) => {
19071969 let calcs = 0
19081970 class Foo {
19091971 @observable x = 1
@@ -1915,17 +1977,89 @@ test("keeping computed properties alive works for objects", () => {
19151977 }
19161978 const x = new Foo ( )
19171979
1918- expect ( calcs ) . toBe ( 0 )
1919- expect ( x . y ) . toBe ( 2 )
1980+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
1981+ expect ( x . y ) . toBe ( 2 ) // second access: use cached value, no calculation
1982+ expect ( calcs ) . toBe ( 1 ) // only one calculation: cached!
1983+ } )
1984+
1985+ test ( "keeping computed properties alive does not recalculate when dirty" , ( ) => {
1986+ let calcs = 0
1987+ const x = observable (
1988+ {
1989+ x : 1 ,
1990+ get y ( ) {
1991+ calcs ++
1992+ return this . x * 2
1993+ }
1994+ } ,
1995+ {
1996+ y : mobx . computed ( { keepAlive : true } )
1997+ }
1998+ )
1999+
2000+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
2001+ x . x = 3 // mark as dirty: no calculation
19202002 expect ( calcs ) . toBe ( 1 )
1921- expect ( x . y ) . toBe ( 2 )
1922- expect ( calcs ) . toBe ( 1 ) // kept alive!
2003+ expect ( x . y ) . toBe ( 6 )
2004+ } )
19232005
1924- x . x = 3
1925- expect ( calcs ) . toBe ( 2 ) // reactively updated
2006+ test ( "(for objects) keeping computed properties alive does not recalculate when dirty" , ( ) => {
2007+ let calcs = 0
2008+ class Foo {
2009+ @observable x = 1
2010+ @computed ( { keepAlive : true } )
2011+ get y ( ) {
2012+ calcs ++
2013+ return this . x * 2
2014+ }
2015+ }
2016+ const x = new Foo ( )
2017+
2018+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
2019+ x . x = 3 // mark as dirty: no calculation
2020+ expect ( calcs ) . toBe ( 1 )
19262021 expect ( x . y ) . toBe ( 6 )
19272022} )
19282023
2024+ test ( "keeping computed properties alive recalculates when accessing it dirty" , ( ) => {
2025+ let calcs = 0
2026+ const x = observable (
2027+ {
2028+ x : 1 ,
2029+ get y ( ) {
2030+ calcs ++
2031+ return this . x * 2
2032+ }
2033+ } ,
2034+ {
2035+ y : mobx . computed ( { keepAlive : true } )
2036+ }
2037+ )
2038+
2039+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
2040+ x . x = 3 // mark as dirty: no calculation
2041+ expect ( x . y ) . toBe ( 6 ) // second access: do calculation because it is dirty
2042+ expect ( calcs ) . toBe ( 2 )
2043+ } )
2044+
2045+ test ( "(for objects) keeping computed properties alive recalculates when accessing it dirty" , ( ) => {
2046+ let calcs = 0
2047+ class Foo {
2048+ @observable x = 1
2049+ @computed ( { keepAlive : true } )
2050+ get y ( ) {
2051+ calcs ++
2052+ return this . x * 2
2053+ }
2054+ }
2055+ const x = new Foo ( )
2056+
2057+ expect ( x . y ) . toBe ( 2 ) // first access: do calculation
2058+ x . x = 3 // mark as dirty: no calculation
2059+ expect ( x . y ) . toBe ( 6 ) // second access: do calculation because it is dirty
2060+ expect ( calcs ) . toBe ( 2 )
2061+ } )
2062+
19292063test ( "tuples" , ( ) => {
19302064 // See #1391
19312065 function tuple ( ) {
0 commit comments