Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix update with handlers #7 #8

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/concepts/handlers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# aOS Library: Handlers (Version 0.0.3)

## Overview

The Handlers library provides a flexible way to manage and execute a series of handlers based on patterns. Each handler consists of a pattern function, a handle function, and a name. This library is suitable for scenarios where different actions need to be taken based on varying input criteria.

## Module Structure

- `handlers._version`: String representing the version of the Handlers library.
- `handlers.list`: Table storing the list of registered handlers.

## Functions

### `handlers.add(name, pattern, handler)`

adds a new handler or updates an existing handler by name

### `handlers.append(name, pattern, handle)`

Appends a new handler to the end of the handlers list.

#### Parameters

- `pattern` (function): Function that determines if the handler should be executed.
- `handle` (function): The handler function to execute.
- `name` (string): A unique name for the handler.

### `handlers.prepend(name, pattern, handle)`

Prepends a new handler to the beginning of the handlers list.

#### Parameters

- Same as `handlers.append`.

### `handlers.before(handleName)`

Returns an object that allows adding a new handler before a specified handler.

#### Parameters

- `handleName` (string): The name of the handler before which the new handler will be added.

#### Returns

- An object with an `add` method to insert the new handler.

### `handlers.after(handleName)`

Returns an object that allows adding a new handler after a specified handler.

#### Parameters

- `handleName` (string): The name of the handler after which the new handler will be added.

#### Returns

- An object with an `add` method to insert the new handler.

### `handlers.remove(name)`

Removes a handler from the handlers list by name.

#### Parameters

- `name` (string): The name of the handler to be removed.

### `handlers.evaluate(msg, env)`

Evaluates each handler against a given message and environment. Handlers are called in the order they appear in the handlers list.

#### Parameters

- `msg` (table): The message to be processed by the handlers.
- `env` (table): The environment in which the handlers are executed.

#### Returns

- `response` (varies): The response from the handler(s). Returns a default message if no handler matches.

## Usage Example

```lua
local handlers = require "handlers_module_path"

-- Define pattern and handle functions
local function myPattern(msg)
-- Determine if the handler should be executed
end

local function myHandle(msg, env, response)
-- Handler logic
end

-- Append a new handler
handlers.append("myHandler", myPattern, myHandle)

-- Evaluate a message
local response = handlers.evaluate({ key = "value" }, { envKey = "envValue" })
```

## Notes

- Handlers are executed in the order they appear in `handlers.list`.
- The pattern function should return `0` to skip the handler, `-1` to break after the handler is executed, or `1` to continue with the next handler.
- The `evaluate` function can concatenate responses from multiple handlers.
2 changes: 1 addition & 1 deletion src/getting-started/aos.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ You should see `pong`

:tada:

For more information about `handlers` check out the handlers [docs](process/handlers.md)
For more information about `handlers` check out the handlers [docs](../concepts/handlers)

## Summary

Expand Down