generic_actor

Version, currently main branch1 version
  • main branchlatestJul 11, 2023

github.com/NeuraLegion/generic_actor

A generic Actor model for Crystal

10 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  generic_actor:
    github: NeuraLegion/generic_actor
    branch: main

main is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
~> 1
License
MIT
Author
Bar Hofesh <bar.hofesh@gmail.com>

Dependencies

This version declares no dependencies.

README

# generic_actor

Generic Actor to build MT safe objects

## Installation

1. Add the dependency to your `shard.yml`:

   ```yaml
   dependencies:
     generic_actor:
       github: NeuraLegion/generic_actor
   ```

2. Run `shards install`

## Usage

```crystal
require "generic_actor"
```

A simple object can look as follows:

```crystal
  class StringStore
    include GenericActor

    @db = Array(String).new

    call_def get, nil, Array(String) do
      @db.dup
    end

    cast_def set, {string: String} do
      @db << string
    end

    call_def includes?, {string: String}, Bool do
      @db.includes?(string)
    end

    call_def size, nil, Int32 do
      @db.size
    end
  end
```

And the usage can be like:

```crystal
string_store = StringStore.new
100.times do
  spawn do
    string_store.set(string: "adding new string")
    string_store.size # what's the size?
  end
end
```

### Priority

You can specify the priority of the message by using `prioritized_call_def` and `prioritized_cast_def`:

```crystal
  class StringStore
    include GenericActor

    @db = Array(String).new

    # this will be picked up first by the actor
    # and will be executed before any other message
    # that is not prioritized
    prioritized_cast_def set, {string: String} do
      @db << string
    end

  end
```

## Contributing

1. Fork it (<https://github.com/NeuraLegion/generic_actor/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request