Skip to content

Commit b7b8dcd

Browse files
committed
allow additional initialization arguments be passed to actor.
1 parent 5be04c5 commit b7b8dcd

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

src/actor-api.lisp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:use :cl)
33
(:nicknames :act)
44
(:export #:make-actor
5+
#:other-init-args
56
#:actor
67
#:tell
78
#:!
@@ -27,7 +28,11 @@
2728
(in-package :sento.actor)
2829

2930
(defclass actor (actor-cell)
30-
((receive :initarg :receive
31+
((other-init-args :initarg :other-init-args
32+
:initform nil
33+
:reader other-init-args
34+
:documentation "Other init args passed to `make-actor`")
35+
(receive :initarg :receive
3136
:initform (error "'receive' must be specified!")
3237
:reader receive
3338
:documentation
@@ -66,7 +71,7 @@ There is asynchronous `tell`, a synchronous `ask-s` and asynchronous `ask` which
6671
6772
To stop an actors message processing in order to cleanup resouces you should `tell` (or `ask-s`) the `:stop` message. It will respond with `:stopped` (in case of `ask(-s)`)."))
6873

69-
(defgeneric make-actor (receive &key name state type init destroy)
74+
(defgeneric make-actor (receive &key name state type init destroy other-init-args)
7075
(:documentation
7176
"Constructs an `actor`.
7277
@@ -84,7 +89,9 @@ If you have additional initializations to make you can do so in `initialize-inst
8489
- `state`: initialize an actor with a state. (default is `nil`)
8590
8691
- `init` and `destroy`: are functions that take one argument, the actor instance.
87-
Those hooks are called on (after) initialization and (after) stop respectively."))
92+
Those hooks are called on (after) initialization and (after) stop respectively.
93+
94+
- `other-init-args`: are additional arguments passed to `initialize-instance` of the actor class."))
8895

8996
(defgeneric pre-start (actor)
9097
(:documentation

src/actor-context-api.lisp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(in-package :sento.actor-context)
1919

2020
(defgeneric actor-of (context
21-
&key receive init destroy dispatcher state type name)
21+
&key receive init destroy dispatcher state type name other-args)
2222
(:documentation "Interface for creating an actor.
2323
2424
**!!! Attention:** this factory function wraps the `act:make-actor` functionality to something more simple to use.
@@ -37,7 +37,8 @@ This function allows to unsubsribe from event-stream or such.
3737
- `:dispatcher` key can be used to define the message dispatcher manually.
3838
Options are `:shared` (default) and `:pinned`.
3939
- `:type` can specify a custom actor class. See `act:make-actor` for more info.
40-
- `:name` to set a specific name to the actor, otherwise a random name will be used."))
40+
- `:name` to set a specific name to the actor, otherwise a random name will be used.
41+
- `:other-args` are in the actor instance as `other-init-args` and can be acted upon in i.e. `pre-start` for additional initialization."))
4142

4243
(defgeneric find-actors (context path &key test key)
4344
(:documentation "Returns actors to be found by the criteria of:

src/actor-context.lisp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ An `act:actor` contains an `actor-context`."
110110
&key receive
111111
(init nil) (destroy nil)
112112
(dispatcher :shared) (state nil)
113-
(type 'act:actor) (name nil))
113+
(type 'act:actor) (name nil)
114+
(other-args nil))
114115
"See `ac:actor-of`."
115116
(check-type receive function "a function!")
116117
(%actor-of context
@@ -119,7 +120,8 @@ An `act:actor` contains an `actor-context`."
119120
:name name
120121
:type type
121122
:init init
122-
:destroy destroy))
123+
:destroy destroy
124+
:other-init-args other-args))
123125
:dispatcher dispatcher))
124126

125127
;; test 2-arity function with 'path' and 'act-cell-name' (default)

src/actor-system.lisp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ See `config:config-from`."
131131
state
132132
(type 'act:actor)
133133
name
134-
(context-key :user))
134+
(context-key :user)
135+
(other-args nil))
135136
"Private API to create system actors. Context-key is either `:internal` or `:user`
136137
Users should use `actor-of`."
137138
(ac:actor-of (actor-context-for-key context-key system)
@@ -141,7 +142,8 @@ Users should use `actor-of`."
141142
:dispatcher dispatcher
142143
:state state
143144
:type type
144-
:name name))
145+
:name name
146+
:other-args other-args))
145147

146148
(defun %find-actors (system path &key test key context-key)
147149
"Private API to find actors in both contexts the actor-system supports.
@@ -192,7 +194,8 @@ Users should use `ac:find-actors`."
192194
&key receive
193195
(init nil) (destroy nil)
194196
(dispatcher :shared) (state nil)
195-
(type 'act:actor) (name nil))
197+
(type 'act:actor) (name nil)
198+
(other-args nil))
196199
"See `ac:actor-of`"
197200
(%actor-of system
198201
:receive receive
@@ -202,7 +205,8 @@ Users should use `ac:find-actors`."
202205
:state state
203206
:type type
204207
:name name
205-
:context-key :user))
208+
:context-key :user
209+
:other-args other-args))
206210

207211
(defmethod find-actors ((self actor-system) path &key (test #'string=) (key #'act-cell:name))
208212
"See `ac:find-actors`"

src/actor.lisp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
(setf (symbol-function '!) #'act:tell)
2222
(setf (symbol-function '?) #'act:ask)
2323

24-
(defmethod make-actor (receive &key name state (type 'actor) (init nil) (destroy nil))
24+
(defmethod make-actor (receive &key name state (type 'actor) (init nil) (destroy nil) (other-init-args nil))
2525
(make-instance type
2626
:name name
2727
:state state
2828
:receive receive
2929
:init init
30-
:destroy destroy))
30+
:destroy destroy
31+
:other-init-args other-init-args))
3132

3233
(defun finalize-initialization (actor message-box actor-context)
3334
"Private API: finalize initialization of the actor with a `mesgb:message-box` and an `ac:actor-context`."
@@ -252,7 +253,8 @@ Use this from within receive function to reply to a sender."
252253
&key receive
253254
(init nil) (destroy nil)
254255
(dispatcher :shared) (state nil)
255-
(type 'act:actor) (name nil))
256+
(type 'act:actor) (name nil)
257+
(other-args nil))
256258
"`ac:actor-context` protocol implementation"
257259
(ac:actor-of (context actor)
258260
:receive receive
@@ -261,4 +263,5 @@ Use this from within receive function to reply to a sender."
261263
:dispatcher dispatcher
262264
:state state
263265
:type type
264-
:name name))
266+
:name name
267+
:other-args other-args))

0 commit comments

Comments
 (0)