Properties of a the same values, in terminology of mol is polymorphic methods with the following type of interface:
myProperty< Value >() : Value // getter
myProperty< Value >( next? : Value ) : Value // getter/setter
myProperty< Value >( next? : Value , force? : $mol_mem_force ) : Value // getter/setter with force support
The example of declaring a not cached property:
/// getter
userName() { return 'jin' }
/// getter/setter
userName( next? : string ) {
if( next === void 0 ) { // check for undefined for switch between getter and setter
return localStorage.getItem( 'name' ) // pull value
} else {
localStorage.setItem( 'name' , next ) // put value
return next // return next value
}
}
Examples of usage:
// Set user name 'jin'
userName( 'jin' )
// Get user name 'jin'
userName()
To do a property is cached it is enough to use decorator $mol_mem, which uses under the hood $mol_atom for automatic invalidation of cache:
/// getter
@ $mol_mem
userName() { return $mol_state_local.value( 'name' ) }
/// getter/setter
@ $mol_mem
userName( next? : string ) {
if( next === void ) { // check for undefined for switch between getter and setter
return $mol_state_local.value( 'name' ) // pull value
} else {
$mol_state_local.value( 'name' , next ) // put value
return next // return next value
}
}
/// Delegated getter/setter
@ $mol_mem
userName( next? : string ) {
return $mol_state_local.value( 'name' , next )
}
Additional examples of usage reactive properties:
@ $mol_mem
userName( next? : string , force? : $mol_mem_force ) { // Added force support
return 'mary'
}
/// Try to set new value
userName( 'jin' ) // returns 'mary'
/// Force push value to cache
userName( 'jin' , $mol_mem_force_cache ) // returns 'jin'
/// Force cache ignoring and pulling fresh value
userName( void 0 , $mol_mem_force_update ) // returns 'mary'
Multi-value properties has the following interface:
myProperty< Key , Value >( key : Key ) : Value // getter
myProperty< Key , Value >( key : Key , next? : Value ) : Value // getter/setter
myProperty< Key , Value >( key : Key , next? : Value , force? : $mol_mem_force ) : Value // getter/setter with force support
$mol_mem_key uses $mol_dict inside.
Examples of declarations:
userName( pos : number ) {
return `User #${pos}` // value based on key
}
@ $mol_mem_key
userNames( pos : number , next? : string ) {
return $mol_state_local.value( 'name' , next ) || `User #${pos}`
}
Examples of usages:
/// Set user#0 name to 'jin'
userName( 0 , 'jin' )
/// Get user#0 name 'jin'
userName( 0 )
As a key it is allowed to use any value, which can be serialized into JSON, for example:
@ $mol_mem_key
usersSearch( query : {
name? : string
minAge? : number
} ) {
var users = this.users()
if( query.name ) users = users.filter( ({ name })=> !!name.match( query.name ) )
if( query.minAge ) users = users.filter( ({ age })=> age > query.minAge )
return users
}
adults() {
return this.usersSearch({ minAge : 18 })
}
jins() {
return this.usersSearch({ name : 'jin' })
}
- $mol_atom: теория и практика реактивности - Object oriented reactive programming
- Объектное Реактивное Программирование - Features of Object Reactive Programming