generic_actor

A generic Actor model for Crystal

generic_actor

Generic Actor to build MT safe objects

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      generic_actor:
        github: NeuraLegion/generic_actor
    
  2. Run shards install

Usage

require "generic_actor"

A simple object can look as follows:

  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:

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:

  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
Repository

generic_actor

Owner
Statistic
  • 10
  • 1
  • 0
  • 0
  • 0
  • over 1 year ago
  • February 17, 2021
License

MIT License

Links
Synced at

Sun, 17 Nov 2024 02:33:20 GMT

Languages