DEV Community

Seth T.T
Seth T.T

Posted on

Event-driven cognitive framework

Thought Constructs

  • Everything is information.

  • An event is information about something that happened, or something that changed.

  • A set of causes and a set of effects are associated with each event. Causes can be partially described at the time of the event. Some effects may be impossible to associate with their event, even after its time.

Code Constructs

  • Data is tangible and manipulable information. Everything is data.
data = {};
data = 123;
data = window;
Enter fullscreen mode Exit fullscreen mode
  • Let Event be a generic type of data for explicitly labeling events.
/**
 * A generic type of data for explicitly labeling events.
 * @template What   - The type of data describing what happened.
 * @template Cause  - The type of data that caused this event.
 * @template Effect - The type of data which this event causes.
 **/
type Event<What, Cause, Effect> = {
    // what happened?
    what: What,
    // what caused it?
    cause: Set<C>,
    // what happens after?
    effect: Set<E>
};
Enter fullscreen mode Exit fullscreen mode

Note that not all events may be explicitly labeled. Simple code can benefit from our vocabulary - we just need to think from a fresh perspective!

Let's examine a common TypeScript function - add(a, b).

/**
 * File: addition.ts
 * This code contains one system called `add`.
 **/

function add(a: number, b: number): number {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

This code contains one system called add. The add system has two inputs, called a and b, and it returns a single output.

Recall that we defined an event as information about something that happened, or something that changed. It sounds obvious, but a computer system executing the add function is - you guessed it - something happening!

Our return a + b is actually an event happening! Let's make this explicit using our Event type from earlier.

/**
 * File: addition.ts
 * This code contains one system called `add`, and its corresponding event.
 **/

type AddEvent = Event<number, typeof add, unknown>;

function add(a: number, b: number): AddEvent {

    return {
        what: (a + b),
        cause: new Set<>(add),
        effect: new Set<>(),
    } as const;
}
Enter fullscreen mode Exit fullscreen mode

Obviously, this code is ugly and hard to read. What it communicates, though, is that even simple computer operations can fit within this cognitive framework.

Quest: Using effect to record the truth

Notice the effect section is empty. That's because at this point in the program's execution, we don't know yet what effects this function has on the rest of the program.

Let's fill our AddEvent with effects. Consider the following example:

import { AddEvent, add } from './addition.ts'

function main() {

    const a = 1;
    const b = 2;
    const $add = add(a, b);

    // Output: 3
    console.log($add.what);

    // Since we did something with $add, let's record it
    // as an effect. Now, $add is a complete and accurate
    // record of all that occurred by calling 'add(a, b)'.
    $add.effect.add(console.log);

}
Enter fullscreen mode Exit fullscreen mode

This example adds unnecessary complexity to basic code operations for the sake of demonstration.

This API design also enables mistakes, because effects must be tracked explicitly and might be forgotten.

Conversely, tracing and understanding a program's causal chain is fucking invaluable. That's why developers log things.

Can we alleviate these API issues? What if a computer system could track it's own causal chain, as data? What if, indeed...

Let's try it!

Quest: Using effect to declare the truth

⚠️ TODO


Thank you for reading my article :)
Github

Top comments (0)