ProAct.AutoProperty Class
Constructs a ProAct.AutoProperty
.
The properties are simple ProAct.Actors with state.
The auto-computed or functional property has a state of a function return value.
Auto-computed properties are functions which are turned to ProAct.Propertys by a ProAct.ObjectCore.
If these functions are reading another fields of ProAct.js objects, they authomatically become dependent on them.
For example:
var obj = { a: 1, b: 2, c: function () { return this.a - this.b; } };If this object - obj is turned to a reactive ProAct.js object, it becomes a simple object with three fields:
{ a: 1, b: 2, c: -1 }But now c is dependent on a and b, so if a is set to 4, obj becomes:
{ a: 1, b: 2, c: 2 }
The logic is the following:
- The property is initialized to be lazy, so its state is init
- On its first read, the currentCaller is set to the listener of the property, so all the properties read in the function body become observed by it. The value of the property is computed using the original function of the field.
- On this first read the state of the property is updated to ready.
- On its following reads it is a simple value, computed from the first read. No re-computations on get.
- If a property, this auto-computed property depends changes, the value of this ProAct.AutoProperty is recomputed.
- Setting the property can be implemented easy, because on set, the original function of the property is called with the new value.
ProAct.AutoProperty
can be dependant on another ProAct.AutoProperty
.
ProAct.AutoProperty
is part of the proact-properties module of ProAct.js.
Constructor
ProAct.AutoProperty
-
queueName
-
proObject
-
property
Parameters:
-
queueName
StringThe name of the queue all the updates should be pushed to.
If this parameter is null/undefined the default queue of flow is used.
If this parameter is not a string it is used as the proObject.
-
proObject
ObjectA plain JavaScript object, holding a field, this property will represent.
-
property
StringThe name of the field of the object, this property should represent.
Item Index
Methods
- accumulate
- accumulation
- addCaller
- afterInit
- beforeDestroy
- canClose
- close
- defaultActions
- defaultListeners
- defer
- destroy
- diff
- doClose
- doInit
- filter
- filtering
- flatMap
- flatMapFirst
- flatMapLast
- flatMapLimited
- fromLambda
- init
- into
- makeCloseListener
- makeErrListener
- makeEvent
- makeListener
- map
- mapping
- off
- offAll
- offClose
- offErr
- on
- onAll
- onClose
- onErr
- out
- reduce
- skip
- skipDuplicates
- skipWhile
- take
- takeWhile
- toProArray
- toProperty
- toStream
- toString
- transform
- transformStored
- type
Properties
Methods
accumulate
-
initVal
-
accumulationFunction
Creates a new ProAct.Property
instance with source this and accumulation
the passed accumulation function.
Some examples:
var prop = ProAct.Property.value(3);
var acc = prop.accumulate(0, function (current, el) {
return current + el;
});
acc.get(); // 3
prop.set(5);
acc.get(); // 8
prop.set(2);
acc.get(); // 10
Parameters:
Returns:
A new ProAct.Property
instance with the accumulation applied.
accumulation
-
initVal
-
accumulationFunction
Adds an accumulation transformation to this actor.
Accumulation is used to compute a value based on the previous one.
Parameters:
Returns:
this
addCaller
()
protected
Uses currentCaller to automatically add a new listener to this property if the caller is set.
This method is used by the default getter to make every reader of the property a listener to it.
afterInit
()
protected
Called automatically after initialization of this property.
For ProAct.AutoProperty
it does nothing -
the real initialization is lazy and is performed on the first read of this.
beforeDestroy
()
protected
A hook that is called right before destruction, the extenders use it to clean up resources.
The ProAct.Property
deletes its state and is removed from its core container.
Don't override it.
canClose
()
protected
Checks if this can be closed.
Defaults to return true.
close
()
ProAct.Actor
Closes this actor => it state becomes closed.
This sends a close
event to all the subscribers to closing.
After closing the actor it can't emit events anymore.
Example:
var actor = new ProAct.Actor();
actor.onClose(function () {
console.log('Done!');
});
actor.close(); // We will see 'Done!' on the console output.
Returns:
This instance - can be chained.
defaultActions
()
Array | String
protected
A list of actions or action to be used when no action is passed for the methods working with actions.
defaultListeners
()
Object
protected
Generates the initial listeners object. It can be overridden for alternative listeners collections. It is used for resetting all the listeners too.
The default types of listeners are:
{
change: [],
error: [],
close: []
}
Returns:
A map containing the default listeners collections.
defer
-
event
-
listener
Defers a ProAct.Actor listener.
By default this means that the listener is put into active ProAct.Flow using it's pushOnce method, but it can be overridden.
This method determines the order of actions, triggered by the changes in the data flow.
The default implementation is executing only one update on this Actor per data flow change.
This means that if the Actor
depends on other three Actors, and all of them get updated,
it is updated only once with the last update value.
Parameters:
Returns:
this
destroy
()
diff
-
seed
-
differ
Creates a new ProAct.Stream with source - this
.
It emits the difference between the last update and the current incomming update from the source.
source.diff(0, function(prev, v) {
return v - prev;
});
// source :
// --3---5------6---|->
// diff:
// --3---2------1---|->
Parameters:
-
seed
ObjectA value to pass the
differ
function as previous on the inital notification from the source. -
differ
FunctionCreates the difference, receives two params - the previous update and the current. It can be skipped - the default
differ
function returns array with two elements - the previous and the curren updates.
doClose
()
protected
This method is called when a close
event is pushed to this Actor
.
It removes all the subscriptions to the Actor
and sets its
state to closed.
Do not call this method; it is private!
doInit
()
protected
Initializes this property.
First the property is defined as a field in its object, using defineProp.
filter
-
filteringFunction
Creates a new ProAct.Actor instance with source this and filtering the passed filtering function.
Should be overridden with creating the right actor.
var actor = sourceActor.filter(function (el) {
return el % 2 == 0;
});
or
var actor = sourceActor.filter('odd');
Parameters:
-
filteringFunction
ObjectThe filtering function or object with a call method, should return boolean.
Returns:
A new ProAct.Actor instance with the filtering applied.
filtering
-
filteringFunction
Adds a filtering transformation to this actor.
Filtering can be used to filter the incoming update values. For example you can filter by only odd numbers as update values.
Parameters:
-
filteringFunction
ObjectThe filtering function or object with a call method, should return boolean.
Returns:
this
flatMap
-
mapper
Creates a new ProAct.Stream with source - this
.
For every update incomming from the source, a new Actor
is created using the mapper
function. All the updates, emitted by the streams, returned by the mapper
are emitted by the
Actor
created by flatMap
source.flatMap(function (v) {
return ProAct.seq(100, [v, v +1 ]);
});
// source:
// -1---2----4-----3-----2-----1---->
// flatMap
// -1-2-2-3--4-5---3-4---2-3---1-2-->
Parameters:
-
mapper
FunctionA function that returns an
ProAct.Actor
using the incomming notification.
flatMapFirst
-
mapper
Creates a new ProAct.Stream with source - this
.
For every update comming from this
, a new ProAct.Actor
is created using the logic
passed through mapper
. The first such Actor
becomes the source of the Actor
, returned by this
method. When it finishes, if a new Actor
is emitted, it becomes the source.
source.flatMapLast(function (v) {
return ProAct.seq(100, [v, v + 1, v + 2, v + 3]);
});
// source:
// -1---2----4-----3-----2-----1----|->
// flatMapFirst
// -1-2-3-4--4-5-6-7-----2-3-4-5-|->
Parameters:
-
mapper
FunctionA function that returns an
ProAct.Actor
using the incomming notification.
flatMapLast
-
mapper
Creates a new ProAct.Stream with source - this
.
For every update comming from this
, a new ProAct.Actor
is created using the logic
passed through mapper
. This new Actor
becomes the current source of the ProAct.Stream
,
returned by this method. The next update will create a new source, which will become
the current one and replace the old one. This is the same as flatMapLimited,
with limit
of 1
.
source.flatMapLast(function (v) {
return ProAct.seq(100, [v, v + 1, v + 2, v + 3]);
});
// source:
// -1---2----4-----3-----2-----1----|->
// flatMapLast
// -1-2-2-3-44-5-6-3-4-5-2-3-4-1-2-3-4-|->
Parameters:
-
mapper
FunctionA function that returns an
ProAct.Actor
using the incomming notification.
flatMapLimited
-
mapper
-
limit
Creates a new ProAct.Stream with source - this
.
For every update incomming from the source, a new Actor
is created using the mapper
function. ALl the updates, emitted by the streams, returned by the mapper
are emitted by the
Actor
created by flatMap
. The number of the currently active sources is limited by the
passed limit
. All the sources created after the limit is reached are queued and reused as sources later.
fromLambda
-
lambda
Creates a new ProAct.Stream with source - this
.
The logic of the stream is implemented through the passed lambda
parameter.
TODO The first parameter of the lambda should be called something else and not stream.
source.fromLambda(function (stream, notification) {
stream.trigger(notification);
});
// Just forwards notifications..
Parameters:
-
lambda
FunctionA function, with two arguments - the returned by this function stream and notification. For every update comming from
this
, the lambda is called with the update and the stream in it. Has thetrigger
,triggerErr
andtriggerClose
methods.
init
()
into
-
[...]
Links source actors into this actor. This means that this actor is listening for changes from the sources.
A good example is one stream to have another as as source -> if data comes into the source stream, it is passed to the listening too. That way the source stream is plugged into the listening one.
The listeners from makeListener, makeErrListener and makeCloseListener are used.
Chaining actors is very powerful operation. It can be used to merge many source actors into one.
var sourceActor1 = <Actor implementation>;
var sourceActor2 = <Actor implementation>;
var actor = <Actor implementation>;
actor.into(sourceActor1, sourceActor2);
actor.on(function (v) {
console.log(v);
});
Now if the any of the source actors is updated, the update will be printed on the console by the actor
.
Parameters:
-
[...]
Object optionalZero or more source ProAct.Actors to set as sources.
Returns:
this
makeCloseListener
()
Object
protected
Creates the closing listener of this actor.
Every actor should have one closing listener that should pass to other actors.
This listener turns the actor in a observer for closing events.
Should be overriden with specific listener, by default it returns N.
Returns:
The closing listener of this observer.
makeErrListener
()
Object
protected
Creates the error listener of this actor.
Every actor should have one error listener that should pass to other actors.
This listener turns the actor in a observer for errors.
Should be overriden with specific listener, by default it returns N.
Returns:
The error listener of this observer.
makeEvent
-
source
Creates the event to be send to the listeners of this ProAct.Property
on update.
The event should be an instance of ProAct.Event.
By default this method returns ProAct.Event.Types/value:property event with target the property name and arguments:
- The object this
ProAct.Property
manages a field for. - The old value of this property.
- The new value of this property.
Parameters:
-
source
ProAct.EventThe source event of the event. It can be null
Returns:
The event, created.
makeListener
()
Object
protected
Creates the listener of this ProAct.AutoProperty
.
This listener turns the observable in a observer.
The listener for ProAct.AutoProperty
is an object defining the call method.
It has a property field set to this.
On value changes the
Returns:
The listener of this ProAct.AutoProperty.
map
-
mappingFunction
Creates a new ProAct.Property
instance with source this and mapping
the passed mapping function.
When the source is changed, the product of this operator is updated too.
var val = ProAct.Property.value(5);
var plusOne = val.map(function (v) {
return v + 1;
});
plusOne.get(); // 6
val.set(4);
plusOne.get(); // 5
or
var positive = val.map('+');
val.set(-4);
positive.get(); // 4
Parameters:
Returns:
A new ProAct.Property
instance with the mapping applied.
mapping
-
mappingFunction
Adds a mapping transformation to this actor.
Mapping transformations just transform one value into another. For example if we get update with the value of 3 and we have mapping transformation that returns the updating value powered by 2, we'll get 9 as actual updating value.
Parameters:
-
mappingFunction
ObjectFunction or object with a call method to use as map function.
Returns:
this
off
-
actions
-
listener
Removes a listener from the passed action.
If this method is called without parameters, all the listeners for all the actions are removed. The listeners are reset using defaultActions.
Examples are:
Removing a listener:
var listener = function (v) {
console.log(v);
};
actor.on(listener);
actor.off(listener);
Or for removing all the listeners attached to an actor:
actor.off();
Or for removing all the listeners of a given type attached to an actor:
actor.off('error');
Or for removing a listener from different type of actions:
var listener = function (v) {
console.log(v);
};
actor.on(listener);
actor.onErr(listener);
actor.off(['error', 'change'], listener);
Parameters:
-
actions
Array | StringThe action/actions to stop listening for. If this parameter is skipped or null/undefined, the actions from defaultActions are used.
The actions can be skipped and on their place as first parameter to be passed the listener.
-
listener
ObjectThe listener to detach. If it is skipped, null or undefined all the listeners are removed from this actor.
Returns:
this
offAll
-
listener
Removes all notifications listener from the passed action.
Parameters:
-
listener
ObjectThe listener to detach. If it is skipped, null or undefined all the listeners are removed from this actor.
Returns:
this
offClose
-
listener
Removes a close notification listener from the passed action.
This is the same as calling off('close', listener)
on an Actor
...
Parameters:
-
listener
ObjectThe listener to detach. If it is skipped, null or undefined all the listeners are removed from this actor.
Returns:
this
offErr
-
listener
Removes an error listener from the passed action.
This is the same as calling off('error', listener)
on an Actor
...
Parameters:
-
listener
ObjectThe listener to detach. If it is skipped, null or undefined all the listeners are removed from this actor.
Returns:
this
on
-
actions
-
listener
Attaches a new listener to this ProAct.Actor
.
The listener may be function or object that defines a call method.
actor.on(function (v) {
console.log(v);
});
actor.on('error', function (v) {
console.error(v);
});
actor.on({
call: function (v) {
console.log(v);
}
});
Parameters:
-
actions
Array | StringThe action/actions to listen for. If this parameter is skipped or null/undefined, the actions from defaultActions are used.
The actions can be skipped and on their place as first parameter to be passed the listener.
-
listener
ObjectThe listener to attach. It must be instance of Function or object with a call method.
Returns:
this
onAll
-
listener
Attaches the passed listener to listen to values, errors and the close notification from this ProAct.Actor
.
The listener may be function or object that defines a call method.
Parameters:
-
listener
ObjectThe listener to attach. It must be instance of Function or object with a call method.
Returns:
this
onClose
-
listener
Attaches a new close notifcation listener to this ProAct.Actor
.
The listener may be function or object that defines a call method.
This is the same as calling on('close', listener)
on an Actor
...
Parameters:
-
listener
ObjectThe listener to attach. It must be instance of Function or object with a call method.
Returns:
this
onErr
-
listener
Attaches a new error listener to this ProAct.Actor.
The listener may be function or object that defines a call method.
This is the same as calling on('error', listener)
on an Actor
...
Parameters:
-
listener
ObjectThe listener to attach. It must be instance of Function or object with a call method.
Returns:
this
out
-
destination
The reverse of into - sets this actor as a source to the passed destination actor.
var sourceActor = <Actor implementation>;
var actor = <Actor implementation>;
sourceActor.out(actor);
actor.on(function (v) {
console.log(v);
});
Now if the any of the source actors is updated, the update will be printed on the console by the actor
.
Parameters:
-
destination
ProAct.ActorThe actor to set as source this to.
Returns:
this
reduce
-
initVal
-
accumulationFunction
Generates a new ProAct.Property containing the state of an accumulations.
The value will be updated with every update coming to this actor.
Parameters:
Returns:
A ProAct.Property instance observing this with the accumulation applied.
skip
-
n
Creates a new ProAct.Stream with source - this
.
It skips the first n
updates incoming from this
.
source : --3---4---5--4---3---4---5--|-> skip(3): -------------4---3---4---5--|->
Parameters:
-
n
NumberThe number of notifications to skip.
skipDuplicates
-
comparator
Creates a new ProAct.Stream with source - this
.
It skips dublicating elements, comming one after another.
source.skipDuplicates();
// source :
// --3---5---5--4---3---3---5--|->
// skipDuplicates:
// --3---5------4---3-------5--|->
Parameters:
-
comparator
FunctionA function used to compare the elements. If nothing is passed it defaults to comparing using
===
.
skipWhile
-
condition
Creates a new ProAct.Stream with source - this
.
It skips notifications from its source, while a condition is true.
source.skipWhile(function (v) {
return v % 2 === 1;
});
// source :
// --3---5---2--4---3---4---5--|->
// skipWhile:
// ----------2--4---3---4---5--|->
Parameters:
-
condition
FunctionA condition function, which is called for each of the incoming values While it returns true, the elements are skipped, after it returns false for the first time, the current and all the following values are emitted.
take
-
limit
Creates a new ProAct.Stream with source - this
.
It takes the first limit
updates incoming from this
.
source : --3---4---5--4---3---4---5--|-> skip(3): --3---4---5--|->
Parameters:
-
limit
NumberThe number of notifications to emit.
takeWhile
-
condition
Creates a new ProAct.Stream with source - this
.
It emits notifications from its source, while a condition is true.
source.takeWhile(function (v) {
return v % 2 === 1;
});
// source :
// --3---5---2--4---3---4---5--|->
// takeWhile:
// --3---5--|->
Parameters:
-
condition
FunctionA condition function, which is called for each of the incoming values While it returns true, the elements are emitted, after it returns false for the first time, the stream created by takeWhile closes.
toProArray
()
ProAct.Array
Creates and returns a ProAct.Array instance, which tracks the changes of this. Uses the current queue for queueing changes.
Returns:
A ProAct.Array
instance tracking the changes of this
.
toProperty
()
Creates a {{{#crossLink "ProAct.Property"}}{{/crossLink}} instance,
dependent on this.
Comes from the proact-properties
module.
toStream
()
Turns this ProAct.Actor
to a ProAct.Stream.
In reality this method creates a new Stream
with source this.
toString
()
The toString() method returns a string representing this ProAct.Property
.
The string representation is the value of this property.
transform
-
transformation
Adds a new transformation to the list of transformations of this actor.
A transformation is a function or an object that has a call method defined. This function or call method should have one argument and to return a transformed version of it. If the returned value is {@link ProAct.Actor.BadValue}, the next transformations are skipped and the updating value/event becomes - bad value.
Every value/event that updates this actor will be transformed using the new transformation.
Parameters:
-
transformation
ObjectThe transformation to add.
Returns:
this
transformStored
-
transformation
-
type
Adds a new transformation to the list of transformations of this actor.
A transformation is a function or an object that has a call method defined. This function or call method should have one argument and to return a transformed version of it. If the returned value is {@link ProAct.Actor.BadValue}, the next transformations are skipped and the updating value/event becomes - bad value.
Every value/event that updates this actor will be transformed using the new transformation.
The idea of this method is that it just calls transform, but it can be overidden from another module.
TODO Maybe transformStored is a bad name
Parameters:
Returns:
this
type
()
Number
Retrieves the ProAct.Property.Types value of this property.
For instances of the ProAct.AutoProperty
class, it is
auto.
Returns:
The right type of the property.