How to implement a similar C # event mechanism in Lua

Source: Internet
Author: User

LuaIs similarC # eventsThe Mechanism learning tutorial is the content to be introduced in this article, mainly to learnEventInLuaFor more information, see.LuaThe syntax of metamethod is very flexible. It can be used to simulate the features of many languages.

C #We use the event as follows:

 
 
  1. xxx.Click += new System.EventHandler(xxx_Click);  
  2.  
  3. private void xxx_Click(object sender, EventArgs e)  
  4. {  
  5.     /**/  

To achieve the same effect in Lua and support event multicast mechanism, the key is to rewrite metamethod _ call so that not only functions can be called, but also tables can be called.

The main idea is to use a table to save several response functions for event registration, and then use the table as the function to call it. After rewriting _ call, implement traversal and execution of the Registration Method in the table when calling the table.

It needs to be executed on lua5.0 or lua.net, and lua 5.1 is slightly changed.

 
 
  1. test.lua  
  2. do 

Event prototype object. All events are generated by this prototype.

 
 
  1. Event = {}
  2.  
  3. Function Event: New ()
  4. Local event = {}
  5. Setretriable (event, self)
  6. -- Overwrite _ index Logic
  7. Selfself. _ index = self
  8. -- Overwrite _ call logic
  9. Selfself. _ call = self. Call
  10. Return event
  11. End

Event registration: registers the response method to the event.

@ Source: the object to which the response method belongs

@ Func: Response Method

 
 
  1. function Event:Add(source, func)  
  2.     table.insert(self, {source, func})      
  3. end 

Internal method. The default _ call logic is rewritten. When an event is triggered, the response method registered in the event is cyclically executed.

@ Table: when an object is called, it is passed in.

@: Call Parameters

 
 
  1. Function Event. Call (table ,)
  2. For _, item in ipairs (table) do
  3. -- Item [1] is the source, item [2] is the func RESPONSE METHOD
  4. -- Unpack (arg) is not required in lua 5.1 and can be directly used.
  5. Item [2] (item [1], unpack (arg ))
  6. End
  7. End

The following are test cases:

Create a window object and register the Click Event of the button

 
 
  1. Window = {  
  2.     Name = "Simonw's Window",      
  3. }  
  4. function Window:Init() 

Register the event. self is the Window object source.

 
 
  1. Button.ClickEvent:Add(self, self.Button_OnClick)      
  2.  

Event Response Method. sender is the passed Button object.

 
 
  1. function Window:Button_OnClick(sender)      
  2.     print(sender.Name.." Click On "..self.Name)  
  3. end 

Create a button object with events such as ClickEvent

 
 
  1. Button = {
  2. Name = "A Button ",
  3. -- Create an event
  4. ClickEvent = Event: New (),
  5. }

Execute the button action

 
 
  1. Function Button: Click ()
  2. Print ('click begin ')
  3. -- Trigger the event. self is the sender parameter.
  4. Self. ClickEvent (self)
  5. Print ('click end ')
  6. End

Execute from here

 
 
  1. Window:Init()  
  2. Button:Click()  
  3. --[[ 

Execution result:

 
 
  1. > dofile 'test.lua'  
  2. Click begin  
  3. A Button Click On Simonw's Window  
  4. Click end  
  5. ]]  
  6.  
  7. end 

Summary: AnalysisLuaIs similarC # eventsThe content of the Mechanism learning tutorial is complete. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.