|
| 1 | +package fpgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | +) |
| 6 | + |
| 7 | +// ActorDef Actor model inspired by Erlang/Akka |
| 8 | +type ActorDef struct { |
| 9 | + isClosed bool |
| 10 | + ch *chan interface{} |
| 11 | + effect func(*ActorDef, interface{}) |
| 12 | + |
| 13 | + context map[string]interface{} |
| 14 | + |
| 15 | + children map[time.Time]*ActorDef |
| 16 | + parent *ActorDef |
| 17 | +} |
| 18 | + |
| 19 | +var defaultActor *ActorDef |
| 20 | + |
| 21 | +// GetDefault Get Default Actor |
| 22 | +func (actorSelf *ActorDef) GetDefault() *ActorDef { |
| 23 | + return defaultActor |
| 24 | +} |
| 25 | + |
| 26 | +// New New Actor instance |
| 27 | +func (actorSelf *ActorDef) New(effect func(*ActorDef, interface{})) *ActorDef { |
| 28 | + ch := make(chan interface{}) |
| 29 | + return actorSelf.NewByOptions(effect, &ch, map[string]interface{}{}) |
| 30 | +} |
| 31 | + |
| 32 | +// NewByOptions New Actor by its options |
| 33 | +func (actorSelf *ActorDef) NewByOptions(effect func(*ActorDef, interface{}), ioCh *chan interface{}, context map[string]interface{}) *ActorDef { |
| 34 | + newOne := ActorDef{ch: ioCh, effect: effect, context: context, children: map[time.Time]*ActorDef{}} |
| 35 | + go newOne.run() |
| 36 | + |
| 37 | + return &newOne |
| 38 | +} |
| 39 | + |
| 40 | +// Send Send a message to the Actor |
| 41 | +func (actorSelf *ActorDef) Send(message interface{}) { |
| 42 | + if actorSelf.isClosed { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + *(actorSelf.ch) <- message |
| 47 | +} |
| 48 | + |
| 49 | +// Spawn Spawn a new Actor with parent(this actor) |
| 50 | +func (actorSelf *ActorDef) Spawn(effect func(*ActorDef, interface{})) *ActorDef { |
| 51 | + newOne := Actor.New(effect) |
| 52 | + if actorSelf.isClosed { |
| 53 | + return newOne |
| 54 | + } |
| 55 | + |
| 56 | + newOne.parent = actorSelf |
| 57 | + actorSelf.children[time.Now()] = newOne |
| 58 | + |
| 59 | + return newOne |
| 60 | +} |
| 61 | + |
| 62 | +// GetChild Get a child Actor by ID |
| 63 | +func (actorSelf *ActorDef) GetChild(id time.Time) *ActorDef { |
| 64 | + return actorSelf.children[id] |
| 65 | +} |
| 66 | + |
| 67 | +// GetParent Get its parent Actor |
| 68 | +func (actorSelf *ActorDef) GetParent() *ActorDef { |
| 69 | + return actorSelf.parent |
| 70 | +} |
| 71 | + |
| 72 | +// Close Close the Actor |
| 73 | +func (actorSelf *ActorDef) Close() { |
| 74 | + actorSelf.isClosed = true |
| 75 | + |
| 76 | + close(*actorSelf.ch) |
| 77 | +} |
| 78 | + |
| 79 | +// IsClosed Check is Closed |
| 80 | +func (actorSelf *ActorDef) IsClosed() bool { |
| 81 | + return actorSelf.isClosed |
| 82 | +} |
| 83 | + |
| 84 | +func (actorSelf *ActorDef) run() { |
| 85 | + for message := range *actorSelf.ch { |
| 86 | + actorSelf.effect(actorSelf, message) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Actor Actor utils instance |
| 91 | +var Actor ActorDef |
| 92 | + |
| 93 | +func init() { |
| 94 | + Actor = *Actor.New(func(_ *ActorDef, _ interface{}) {}) |
| 95 | + Actor.Close() |
| 96 | + defaultActor = &Actor |
| 97 | +} |
0 commit comments