Modules

Modules allow you to organise your code into nested namespaces. A module can contain types, functions and other modules.

Reactive.Module('Text', function () {

  Reactive.Function('Trim', function () {
    var s = Reactive.Argument(Text);
    return Reactive.Calculate(function () { return String(Reactive.Value(s)).trim(); });
  });

  Reactive.Function('Upper', function () {
    var s = Reactive.Argument(Text);
    return Reactive.Calculate(function () { return String(Reactive.Value(s)).toUpperCase(); });
  });

});
Defining a Text module with two functions

Defining a module

Call Reactive.Module(name, body) and, inside the body, declare the functions and types that belong together. The name becomes a top-level global, such as Text above, and everything you declare in the body lands under it. Nest a Reactive.Module call inside another and the namespaces nest with it, so Maths.Real.Add is a module within a module.

Using a module

Once declared, a module is a top-level global, ready to use, so you reach its operations by their dotted path and call them like any other function, passing reactive values in and getting reactive values back.

var name = Reactive.State(Text)('ada');

Content.Text(Text.Upper(name));   // ADA — updates when name does
Calling a module function by its dotted path

Reading the dotted names is how you navigate the framework: the same path that groups an operation is the path you call it by.

A context of the same name

A module holds structs, unions, types, functions, nested modules, state, and lets. A let in a module is a constant and does not call a function. A context may share the module's name. The native operations are holes of that context, and the driver provides them. Replacing an implementation is that driver, or a Reactive.With. Reopening the module does not swap it.