# cp.rx.go.Statement

A Statement is defined to enable processing of asynchronous resolvable values such as cp.rx.Observable values.

To define a new Statement, you call the named constructor, assigning the result to a constant value and calling the define method.

# Definine a new Statement

To define a new Statement implementation, we use the Statement.named constructor. This gives us a Statement.Definition which allows us to set the rules for the statement before finally "defining" it.

Statements may have an onInit, and must have an onObservable provided, and then the define method must be called.

For example, the First statement is defined like so:

local First = Statement.named("First")
:onInit(function(context, resolvable)
    assert(resolvable ~= nil, "The First `resolveable` may not be `nil`.")
    context.resolvable = resolvable
end)
:onObservable(function(context)
    return toObservable(context.resolvable):first()
end)
:define()

Once you've defined a statement, you then execute it by calling the statement directly, passing in any parameters.

For example:

local First = require("cp.rx.go").First
First(Observable.of(1, 2, 3))
:Now(
    function(value) print("Received: "..tostring(value)) end,
    function(message) print("Error: "..tostring(message)) end,
    function() print("Completed") end
)

This will output:

Received: 1
Completed

The Observable as passed to the onInit function handler as the second parameter. context is always the first parameter, followed by any values passed to the constructor call.

The onObservable function handler is called once the statement is actually executing, typically by calling the Now or After methods.

It is recommended that any conversion of input parameters are converted to Observables as late as possible, typically in the onObservable function handler. Otherwise, input values may get resolved before the user intends.


# Submodules


# API Overview

Functions - API calls offered directly by the extension

  • defaultObserverFactory
  • is
  • toObservable
  • toObservables

Constructors - API calls which return an object, typically one that offers API methods

  • named

Methods - API calls which can only be made on an object returned by a constructor

  • After
  • Catch
  • Debug
  • Finally
  • fullName
  • Label
  • name
  • Now
  • ThenDelay
  • ThenYield
  • TimeoutAfter
  • toObservable

# API Documentation

# Functions

# defaultObserverFactory

Signature cp.rx.go.Statement.defaultObserverFactory([factoryFn]) -> nil
Type Function
Description Gets/sets the factory function which creates a new Observer for Statements which are executed without one being provided. By default, an Observer which only outputs errors via the standard error function is provided.
Parameters
  • factoryFn - if provided, replaces the current default factory function.
Returns
  • A new Observer, or the previous factory function if a new one was provided.
Notes
  • The factory function has no arguments provided and must return a new Observer instance.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 384

# is

Signature cp.rx.go.Statement.is(thing) -> boolean
Type Function
Description Checks if the thing is a Statement.
Parameters
  • thing - The thing to test.
Returns
  • true if the thing is a Statement.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 364

# toObservable

Signature cp.rx.go.Statement.toObservable(thing[, params]) -> cp.rx.Observable
Type Function
Description Converts the thing into an Observable.
Parameters
  • thing - The thing to convert.
  • params - Optional table list to pass as parameters for the thing if it's a function.
Returns
  • The Observable.
Notes
  • It converts the following:
  • Observable - Returned unchanged.
  • cp.rx.go.Statement - Returns the result of the toObservable() method. Note: this will cancel any scheduled executions for the Statement.
  • cp.prop - Returns the cp.prop:toObservable() value.
  • function - Executes the function, passing in the params as a list of values, returning the results converted to an Observable.
  • Other values - Returned via Observable.of(thing).
  • Note that with functions, the function is not executed immediately, but it will be passed the params as
  • a list when the resulting Observable is subscribed to. For example:
  • lua</li><li>-- set up the function</li><li>multiply = toObservable(function(one, two) return onetwo end, {2, 3})</li><li>-- nothing has happened yet</li><li>multiply:subscribe(function(result) print(result) end)</li><li>-- now the function has been executed</li><li>
  • This results in printing 6.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 99

# toObservables

Signature cp.rx.go.Statement.toObservables(things[, params]) -> table
Type Function
Description Converts a list of things into a list of Observables of those things.
Parameters
  • things - a table list of things to convert to Observables.
  • params - an optional table list of parameters to pass to any function things.
Returns
  • A table list of the things, converted to Observable.
Notes
  • For example:
  • lua</li><li>result = toObservables({1, 2, 3})</li><li>for _,o in ipairs(results) do</li><li> o:subscribe(function(x) print x end)</li><li>end</li><li></li><li>If any of the things are `function`s, then the `params` table is unpacked to a list</li><li>and passed into the function when it is called. For example:</li><li></li><li>lua
  • toObservables({function(x) return x2 end}, {3})
  • :subscribe(function(x) print end) -- outputs 6
  • ```
  • Any type supported by toObservable can be included in the things array.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 158

# Constructors

# named

Signature cp.rx.go.Statement.named(name) -> Statement.Definition
Type Constructor
Description Starts the definition of a new Statement with the specified names.
Parameters
  • name - The name of the Statement.
Returns
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 351

# Methods

# After

Signature cp.rx.go.Statement:After(millis[, observer][, scheduler]) -> nil
Type Method
Description Requests the statement to be executed after the specified amount of time in seconds.
Parameters
  • millis - The number of milliseconds to delay the execution.
  • observer - The observer to subscribe to the final result.
  • scheduler - (optional) the cp.rx.Scheduler to use. Uses the cp.rx.util.defaultScheduler() if none is provided.
Returns
  • Nothing.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 657

# Catch

Signature cp.rx.go.Statement:Catch(handler) -> cp.rx.go.Statement
Type Method
Description Assigns a handler which will be applied at the end of the Statement.
Parameters
  • handler - The handler function
Returns
  • The same Statement.
Notes
  • The function will receive the error signal and the returned value will be pass onwards.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 501

# Debug

Signature cp.rx.go.Statement:Debug([label]) -> Statement
Type Method
Description Indicates that the results of the Statement should be output to the Error Log.
Parameters
  • label - If specified, this is output in the log.
Returns
  • The same Statement instance.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 468

# Finally

Signature cp.rx.go.Statement:Finally(handler) -> Statement
Type Method
Description Provides a function handler to get called when the statement is done, either via an onError or onComplete signal.
Parameters
  • handler - The handler function.
Returns
  • The same Statement instance.
Notes
  • The original signal will be passed on without modification. This will trigger after any Catch handler, so will be affected by the results of that.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 483

# fullName

Signature cp.rx.go.Statement:fullName()
Type Method
Description Returns the Statement's full name.
Parameters
  • None
Returns
  • The full Statement name.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 422

# Label

Signature cp.rx.go.Statement:Label(label) -> Statement
Type Method
Description Sets the custom label for the Statement. This will be used instead of the name when outputting it as a string if set. Defaults to nil.
Parameters
  • label - Optional new value for the label. If provided, the Statement is returned.
Returns
  • The Statement if a new lable is specified, otherwise the current label value.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 452

# name

Signature cp.rx.go.Statement:name()
Type Method
Description Returns the Statement name.
Parameters
  • None
Returns
  • The Statement name.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 405

# Now

Signature cp.rx.go.Statement:Now([observer]) -> nil
Type Method
Description Executes the statment immediately.
Parameters
  • observer - An observer to watch the resulting Observable. Defaults to the default observer factory.
Returns
  • Nothing.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 631

# ThenDelay

Signature cp.rx.go.Statement:ThenDelay(millis) -> cp.rx.go.Statement
Type Method
Description Indicates that there will be a delay after this statement by the specified number of millis. This will happen after any TimeoutAfter/Catch/Debug actions.
Parameters
  • millis - the amount of time to delay, in millisecods.
Returns
  • The same Statement.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 531

# ThenYield

Signature cp.rx.go.Statement:ThenYield() -> cp.rx.go.Statement
Type Method
Description Indicates that the Statement will "yield" to allow other pending operations to happen, then pick up as soon as possible afterwards. This will happen after any TimeoutAfter/Catch/Debug actions.
Parameters
  • None
Returns
  • The same Statement.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 518

# TimeoutAfter

Signature cp.rx.go.Statement:TimeoutAfter(millis[, next][, scheduler]) -> cp.rx.go.Statement
Type Method
Description Indicates that this statement should time out after the specified number of milliseconds.
Parameters
  • millis - A number or a function returning the number of milliseconds to wait before timing out.
  • next - Optional string or resolvable value indicating how to handle it.
  • scheduler - The cp.rx.Scheduler to use when timing out. Defaults to cp.rx.defaultScheduler().
Returns
  • The same Statement.
Notes
  • This can be called multiple times before the statement is executed, and the most recent configuration will be used at that time.
  • The next value may be either a string to send as the error, or a resolvable value to pass on instead of failing. If nothing is provided, a default error message is output.
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 545

# toObservable

Signature cp.rx.go.Statement:toObservable([preserveTimer]) -> cp.rx.Observable
Type Method
Description Returns a new Observable instance for the Statement. Unless preserveTimer is true, this will cancel any scheduled execution of the statement via After
Parameters
  • preserveTimer - If a timer has been set via After, don't cancel it. Defaults to false.
Returns
  • The Observable.
Notes None
Examples None
Source src/extensions/cp/rx/go/Statement.lua line 569