Skip to content

Working with Contexts

Cross.stream uses contexts to partition event streams. Every frame belongs to a context, with the system context being the default where operations happen if no specific context is specified.

System Context

When you first start using cross.stream, you’re working in the system context. Let’s see this in action:

Terminal window
.cat
─#─┬──topic───┬────────────id─────────────┬─hash─┬─meta─┬───ttl───
0 │ xs.start │ 03d4q1o70y6ek0ig8hwy9q00n │ │ │
───┴──────────┴───────────────────────────┴──────┴──────┴─────────

The xs.start frame above is in the system context. Let’s add a note:

Terminal window
"system note" | .append notes
───────┬─────────────────────────────────────────────────────
topic │ notes
id │ 03d4q1qhbiv09ovtuhokw5yxv
hash │ sha256-wIcRiyKpOjA1Z8O+wZvoiMXYgGEzPQOhlA8AOptOhBY=
meta │
ttl │ forever
───────┴─────────────────────────────────────────────────────

Creating a New Context

To create a new context, we use the special xs.context topic:

Terminal window
"my project" | .append xs.context
───────┬─────────────────────────────────────────────────────
topic │ xs.context
id │ 03d4qbrxizqgav09m7hicksb0
meta │
ttl │ forever
───────┴─────────────────────────────────────────────────────
# Save the context ID for later use
> let project_context = (ls | last).id

Using Contexts

Now we can add frames to our new context:

Terminal window
"project note" | .append notes -c $project_context
.cat -c $project_context
─#─┬──topic───┬────────────id─────────────┬────────────────────────hash─────────────────────────┬─meta─┬───ttl───
0 │ notes │ 03d4qbrxizqgav09m7hicksb0 │ sha256-KDyb7pypM+8aLiq5obfpCqbMmb6LvvPnCu2+y9eWd0c= │ │ forever
───┴──────────┴───────────────────────────┴─────────────────────────────────────────────────────┴──────┴─────────

Notice how .cat -c $project_context only shows frames from our project context. The system note we created earlier isn’t visible.

The head command is also context-aware:

Terminal window
.head notes -c $project_context | .cas
project note

Setting a Default Context

Instead of specifying the context with each command, you can set a default:

Terminal window
$env.XS_CONTEXT = $project_context
"another project note" | .append notes # Uses project context automatically
.cat # Also uses project context

Viewing All Contexts

Sometimes you may want to see frames across all contexts:

Terminal window
.cat --all
─#─┬──topic───┬────────────id─────────────┬────────────────────────hash─────────────────────────┬─meta─┬───ttl───
0 │ xs.start │ 03d4q1o70y6ek0ig8hwy9q00n │ │ │
1 │ notes │ 03d4q1qhbiv09ovtuhokw5yxv │ sha256-wIcRiyKpOjA1Z8O+wZvoiMXYgGEzPQOhlA8AOptOhBY= │ │ forever
2 │ xs.context│ 03d4qbrxizqgav09m7hicksb0 │ sha256-KDyb7pypM+8aLiq5obfpCqbMmb6LvvPnCu2+y9eWd0c= │ │ forever
3 │ notes │ 03d4qkzpbiv09ovtuhokw5yxv │ sha256-LMcRiyKpOjA1Z8O+wZvoiMXYgGEzPQOhlA8AOptOhBY= │ │ forever
───┴──────────┴───────────────────────────┴─────────────────────────────────────────────────────┴──────┴─────────

Managing Contexts with .ctx Commands

When using the NuShell convenience module (xs.nu), you get access to additional context management commands under the .ctx namespace. These provide a more interactive way to work with contexts.

Listing Contexts

View all available contexts and see which one is active:

Terminal window
.ctx list
─#─┬───────────────id───────────────┬─active─┐
0 │ 0000000000000000000000000 │ true │
1 │ 03d4qbrxizqgav09m7hicksb0 │ false │
───┴──────────────────────────────────┴────────┘

Switching Contexts

Change your active context:

Terminal window
.ctx switch 03d4qbrxizqgav09m7hicksb0
03d4qbrxizqgav09m7hicksb0

You can also switch interactively:

Terminal window
.ctx switch
# (shows interactive list to select from)
03d4qbrxizqgav09m7hicksb0

Creating a New Context

Create and automatically switch to a new context:

Terminal window
.ctx new
03d4qbrxizqgav09m7hicksb0

Viewing Current Context

Check your current context:

Terminal window
.ctx
03d4qbrxizqgav09m7hicksb0