# `JSONRPC2.Spec.Batch`

`JSONRPC2.Spec.Batch` allows you to pack operations (function calls and notification sending) that can later be executed into a single request.

Examples:

    iex> alias JSONRPC2.Spec.Batch
    iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
    %JSONRPC2.Spec.Batch{
      calls: %{
        1 => %JSONRPC2.Spec.Request{
          id: 1,
          method: "func_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      },
      notifies: [
        %JSONRPC2.Spec.Request{
          id: nil,
          method: "event_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      ]
    }

# `t`

```elixir
@type t() :: %JSONRPC2.Spec.Batch{calls: map(), notifies: [map()]}
```

# `call`

```elixir
@spec call(JSONRPC2.Spec.method(), JSONRPC2.Spec.params(), JSONRPC2.Spec.id()) :: t()
```

Prepare data to call function.

The `params` could be either map or list.

The `id` must be unique within `Batch`.

Examples:

    iex> alias JSONRPC2.Spec.Batch
    iex> Batch.call("name", ["arg1", "arg2"], 1)
    %JSONRPC2.Spec.Batch{
      calls: %{
        1 => %JSONRPC2.Spec.Request{
          id: 1,
          method: "name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      },
      notifies: []
    }

Functions can be piped:

    iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
    %JSONRPC2.Spec.Batch{
      calls: %{
        1 => %JSONRPC2.Spec.Request{
          id: 1,
          method: "func_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      },
      notifies: [
        %JSONRPC2.Spec.Request{
          id: nil,
          method: "event_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      ]
    }

# `call`

```elixir
@spec call(t(), JSONRPC2.Spec.method(), JSONRPC2.Spec.params(), JSONRPC2.Spec.id()) ::
  t()
```

# `notify`

```elixir
@spec notify(JSONRPC2.Spec.method(), JSONRPC2.Spec.params()) :: t()
```

Prepare data to send notification.

The `params` could be either map or list.

Examples:

    iex> alias JSONRPC2.Spec.Batch
    iex> Batch.notify("name", ["arg1", "arg2"])
    %JSONRPC2.Spec.Batch{
      calls: %{},
      notifies: [
        %JSONRPC2.Spec.Request{
          id: nil,
          method: "name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      ]
    }

Functions can be piped:

    iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
    %JSONRPC2.Spec.Batch{
      calls: %{
        1 => %JSONRPC2.Spec.Request{
          id: 1,
          method: "func_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      },
      notifies: [
        %JSONRPC2.Spec.Request{
          id: nil,
          method: "event_name",
          params: ["arg1", "arg2"],
          jsonrpc: "2.0"
        }
      ]
    }

# `notify`

```elixir
@spec notify(t(), JSONRPC2.Spec.method(), JSONRPC2.Spec.params()) :: t()
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
