Giter VIP home page Giter VIP logo

actix-lua2's Introduction

actix-lua

#orig https://github.com/geofmureithi/actix-lua

[dependencies]
actix-lua = { git = "https://github.com/devg1120/actix-lua2", branch = "main"}

Build Status Latest Version API Documentation

A safe scripting environment for actix with the Lua Programming Language:

  • Each LuaActor is an isolated Lua VM.
  • Communicate between actors with predefined message types: String, Integer, Number, Boolean, Nil, and Table.
  • Asynchronous send between actors with Lua coroutine.

For more info about the "safety", check rlua's README.

Synopsis

A basic Lua actor

extern crate actix_lua;
use actix_lua::{LuaActorBuilder, LuaMessage};

fn main () {
    let addr = LuaActorBuilder::new()
        .on_handle_with_lua(r#"return ctx.msg + 42"#)
        .build()
        .unwrap()
        .start();

    let res = addr.send(LuaMessage:from(100));
    // return: 142
}

You can send messages to other actor asynchronously with ctx.send

struct Callback;
impl Actor for Callback {
    type Context = Context<Self>;
}

impl Handler<LuaMessage> for Callback {
    type Result = LuaMessage;

    fn handle(&mut self, msg: LuaMessage, _ctx: &mut Context<Self>) -> Self::Result {
        LuaMessage::String("hello".to_string())
    }
}

let mut actor = LuaActorBuilder::new()
    // create a new LuaActor from a lua script when the actor is started.
    // send message to the newly created actor with `ctx.send`, block and wait for its response.
    .on_started_with_lua(
        r#"
    local result = ctx.send("callback, "Hello")
    print(result) -- print "hello"
    "#).build()
    .unwrap();

actor.add_recipients("callback", Callback.start().recipient());

actor.start();

Install

Add actix-lua to your Cargo.toml:

[dependencies]
actix-lua = "0.7"

Example

Check examples directory.

There's also a write-up about analyzing streaming data with actix-lua. link

Lua Actor

Use LuaActor to integrate Lua scripts to your system with actor model.

Message

In actor model, actors communicate with messages. LuaMessage is the only message type accepted by LuaActor:

  • LuaMessage can be converted to/from primitive types with LuaMessage::from().
  • Lua types(e.g. number, table) will be convert to LuaMessage automatically.

Lua API

Note: Avoid declaring global variables in your Lua script. It might conflict with future actix-lua update and break your program.

ctx.msg

The message sent to Lua actor.

ctx.notify(msg)

Send message msg to self.

ctx.notify_later(msg, seconds)

Send message msg to self after specified period of time.

local result = ctx.send(recipient, msg)

Send message msg to `recipient asynchronously and wait for response.

Equivalent to actix::Recipient.send.

ctx.do_send(recipient, msg)

Send message msg to recipient.

Equivalent to actix::Recipient.do_send.

ctx.terminate()

Terminate actor execution.

License

The MIT License

actix-lua2's People

Contributors

devg1120 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.