Light application event communication method, system and device based on event bus and medium
By defining the event bus class and implementing event bus object instances in micro-applications, the complexity and inefficiency of event communication in complex nested pages and component structures are solved, and an efficient and easy-to-use event communication mechanism is achieved.
Patent Information
- Application Number
- CN202411522932.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-29
- Publication Date
- 2025-02-25
AI Technical Summary
When the existing technology faces complex nested pages and component structures, it leads to problems such as bloated code, cumbersome call, difficulty in maintaining and waste of resources, making it difficult to effectively manage data sharing and event communication across pages and components.
Define an event bus class, encapsulates event objects and events registration, cancellation, and dispatch methods, and creates object instances of event bus class in the global environment of micro-applications, providing a unified and efficient event communication mechanism for components and pages.
By manually managing the registration and cancellation of events, the coupling between components and pages is reduced, the ease of use and efficiency of event communication is improved, and the code bloat and resource waste in complex nested structures are avoided.
Smart Images

Figure CN119512781A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of light application software development, and specifically relates to a light application event communication method, system, device and medium based on an event bus. Background Art
[0002] A light application is an application that can be used without downloading and installing. It usually runs on a platform such as WeChat to provide users with convenient and efficient services. In the development process of micro applications, communication between components and between pages is a common requirement. The traditional communication methods are as follows: First, define global variables in app.js or use storage global data storage to read and write data in different pages and components. Second, the user's page browsing history is recorded in the micro-application page similar to the browser history. The page stack can be obtained through the getCurrentPages method, and then the data can be obtained through parameters to achieve cross-page communication. Third, use the event transmission mechanism of the micro-application's own components. Fourth, establish a data channel between pages through the micro-application's EventChannel, and achieve cross-page communication through event dispatching and registered listening.
[0003] Although the above methods can meet the communication needs to a certain extent, they often lead to problems such as bloated code, cumbersome calls, difficult maintenance, and waste of resources when faced with complex nested pages and component structures. Specifically, although global variables or storage global data storage can achieve data sharing across pages and components, they are difficult to manage and easily cause data conflicts; although page stack operations can obtain page instances in the page stack, the operations are complex and depend on the structure of the page stack; the event mechanism that comes with the micro-application is mainly used for communication between components, and has limited support for cross-page communication; and although EventChannel can establish data channels between pages, it needs to be created explicitly when the page jumps, which is not flexible enough to use.
[0004] This is a shortcoming of the prior art. Therefore, in view of the above-mentioned defects in the prior art, it is very necessary to provide a light application event communication method, system, device and medium based on an event bus. Summary of the invention
[0005] In a first aspect, an embodiment of the present application provides a light application event communication method based on an event bus, comprising the following steps: S1. Define an event bus class to encapsulate event objects and event registration, deregistration, and dispatching methods; S2. Create an object instance of the event bus class in the global environment of the micro-application, and provide a calling interface in the global scope of the micro-application; S3. In the life cycle function of the component or page of the micro application, use the object instance of the event bus class by calling the interface, register the event listener using the event registration method, and unregister the event listener using the event unregistration method; S4. The event listener listens to events in components or pages. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
[0006] Furthermore, the specific steps of step S1 are as follows: S11. Create a class for the purpose of encapsulating the functions required by the event bus, and define it as an event bus class; S12. Define event objects in the event bus class and save all events and event listeners for each event; S13. Define the function of the event registration method in the event bus class; S14. Define a function of the event deregistration method in the event bus class; S15. Define the function of the event dispatching method in the event bus class.
[0007] Furthermore, the specific steps of step S12 are as follows: S121. Define the structure of the event object in the event bus class, and set each event name to correspond to an array; S122. Set the elements in the array to the page or component's identifier, callback function, and event listener.
[0008] Furthermore, the function of the registration method of the event bus class defined in step S13 has the following specific steps: S131. Receive the event name, page or component identifier, and callback function as parameters; S132. Check whether the received event name exists in the array of saved event objects; If not, proceed to step S133; If yes, go to step S134; S133. Create a new array and add the received page or component identifier and the event object of the callback function to the array, and then assign it to the event object, and end; S134. Check whether the listening object identified by the received page or component exists; If yes, go to step S135; If not, proceed to step S136; S135. Update the callback function of the received page or component identifier, and end; S136. Add the received page or component tag and the event object added by the callback function to the array, and end; The specific steps of the function of the cancellation method of the event bus class defined in step S14 are as follows: S141. Receive the event name, page or component identifier, and callback function as parameters; S142. Searching for an array of corresponding event objects according to the received event name as an array of markers that need to deregister the event listener; If found, go to step S143; If not found, end; S143. Filter out arrays other than the marked array from all arrays as active arrays, and reassign the active array to the event name of the event object to implement the cancellation of the event listener of the page or component, and end; The specific steps of the function of the dispatch method of the event bus class defined in step S15 are as follows: S151 receives the event name and event data as parameters; S152. Search the event listener in the corresponding array by event name; If not found, end; If found, go to step S153; S153. Call the callback function in the event listener and pass in the event data to realize message passing.
[0009] Furthermore, the specific steps of step S2 are as follows: S21. Get the global environment file of the micro application; S22. Introduce the event bus class in the global environment file of the micro application and create an object instance; S23. Assign the object instance of the created event bus class to the extended attribute of the micro application, thereby providing a calling interface of the event bus class in the global environment.
[0010] Furthermore, the specific steps of step S3 are as follows: S31. Build lifecycle functions for components or pages needed in micro applications; If it is a component, go to step S32; If it is a page, go to step S33; S32. Register the event listener in the attached lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the detached lifecycle function by setting the event cancellation method of the event bus class of the micro-application, and proceed to step S4; S33. Register the event listener in the onLoad lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the onUnload lifecycle function by setting the event unregistration method of the event bus class of the micro-application.
[0011] Furthermore, the specific steps of step S4 are as follows: S41. The micro-application page or component monitors events through its own event listener; If an event trigger is detected, proceed to step S43; If no event trigger is detected, proceed to step S42; S42. Wait for the set time period and return to step S41; S43. Send events and transmit event data through the event dispatching method of the object instance of the event object class of the micro-application, and communicate between the pages and components of the micro-application.
[0012] In a second aspect, an embodiment of the present application further provides a light application event communication system based on an event bus, comprising: The event bus class definition module is used to define an event bus class and encapsulate event objects and event registration, deregistration, and dispatch methods; The object instance creation module of the event bus class is used to create an object instance of the event bus class in the global environment of the micro-application and provide a calling interface in the global scope of the micro-application; The event listener registration and deregistration module is used to use the object instance of the event bus class by calling the interface in the life cycle function of the micro-application component or page, register the event listener using the event registration method, and deregister the event listener using the event deregistration method; The event monitoring and dispatching module is used to monitor events in components or pages through event listeners. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
[0013] In a third aspect, an embodiment of the present application further provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the light application event communication method based on the event bus described in the first aspect are implemented.
[0014] In a fourth aspect, an embodiment of the present application further provides a storage medium, and when the computer program is executed by a processor, the steps of the light application event communication method based on the event bus described in the first aspect are implemented.
[0015] It can be seen from the above technical solutions that the present invention has the following advantages: The present application provides a light application event communication method, system, device and medium based on an event bus, which defines an event bus class to encapsulate event objects and event registration, deregistration and dispatching methods; creates an object instance of the event bus class in the global environment of the micro-application, and provides a calling interface in the global scope of the micro-application; uses the object instance of the event bus class through the calling interface in the life cycle function of the component or page of the micro-application, registers the event listener using the event registration method, and deregisters the event listener using the event deregistration method; the event listener listens to the events in the component or page, and when the event is triggered, uses the event dispatching method of the object instance of the event bus class to send the event and transmit data, so as to realize the communication between micro-application components or between pages, thereby establishing communication between different components and pages by manually managing the registration and deregistration of events, reducing the coupling degree, and improving the ease of use, thereby avoiding the problems of bloated code, cumbersome calls, difficult maintenance, and waste of resources in some complex nested pages and components. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solution of the present invention, the accompanying drawings required for use in the description will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0017] Figure 1 The present invention is a flowchart of an embodiment of a light application event communication method based on an event bus.
[0018] Figure 2 The present invention is a flowchart of another embodiment of the light application event communication method based on event bus.
[0019] Figure 3 It is a schematic diagram of a light application event communication system based on an event bus of the present invention.
[0020] Figure 4 An embodiment of an electronic device storing a light application event communication method based on an event bus according to the present invention. DETAILED DESCRIPTION
[0021] In the light application event communication method based on the event bus, which will be described in detail below, various embodiments of the present disclosure will be described in more detail. The present disclosure may have various embodiments, and adjustments and changes may be made therein. However, it should be understood that there is no intention to limit the various embodiments of the present disclosure to the specific embodiments disclosed herein, but the present disclosure should be understood to cover all adjustments, equivalents and / or alternatives that fall within the spirit and scope of the various embodiments of the present disclosure.
[0022] For example, in the development of lightweight applications, taking WeChat applets as an example, the following requirements are often encountered: a. A parent component contains multiple child components. When operating a child component, an event of the parent component or another child component needs to be triggered; b. Enter page B from page A. After performing an operation on page B, an event on page A needs to be triggered; c. Event triggering between the above parent-child components and multiple pages in multiple layers of nesting.
[0023] In response to the above requirements, the common processing methods are mainly the following: 1. Define global variables in app.js or use storage global data storage to read and write data in different pages and components; 2. The page stack of WeChat Mini Program is similar to the history of the browser, which records the user's page browsing history. The page stack can be obtained through the getCurrentPages method, and then the data can be obtained through parameters to achieve cross-page communication; 3. WeChat applet comes with a component event delivery mechanism; 4. Establish a data channel between pages through the WeChat applet's EventChannel, and realize cross-page communication by dispatching events and registering event listeners.
[0024] The above common methods have their own applicable scenarios. Simple businesses can handle them. In some complex nested pages and components, there will be problems such as bloated code, cumbersome calls, difficult maintenance, and waste of resources. By defining an event bus class that encapsulates event objects and registration, cancellation, and dispatch event methods, an event bus instance is created in the micro-application global environment, and a calling interface is provided. In the life cycle of a micro-application component or page, an event listener is registered and cancelled by calling the interface. The event listener listens to events, and when triggered, events are sent through the event bus and data is transmitted to realize communication between components or pages. The present invention solves the complexity and inefficiency of communication between components and components, and between pages and pages in light application development. By defining an event bus class, encapsulating event objects and event registration, cancellation, and dispatch methods, and creating its instance in the micro-application global environment, a unified and efficient event communication mechanism is provided for the components and pages of micro-applications.
[0025] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0026] See also Figure 1 The figure is a flowchart of a method, system, device and medium for communicating light application events based on an event bus in a specific embodiment, and the method includes the following steps: S1. Define an event bus class to encapsulate event objects and event registration, deregistration, and dispatching methods; It should be noted that the function of the event bus is encapsulated by defining the event bus class; S2. Create an object instance of the event bus class in the global environment of the micro-application, and provide a calling interface in the global scope of the micro-application; It should be noted that creating an object instance of the event bus class in the global environment of the micro-application and providing a calling interface in the global scope of the micro-application is actually to provide a global and unified interface so that the event bus class can be easily referenced and used for event communication in pages and components; S3. In the life cycle function of the component or page of the micro application, use the object instance of the event bus class by calling the interface, register the event listener using the event registration method, and unregister the event listener using the event unregistration method; It should be noted that in the micro-application architecture, the life cycle functions of components or pages usually involve communication with other parts. Event bus is a common pattern that allows decoupled communication between different components or pages. S4. The event listener monitors events in components or pages. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages; It should be noted that in the micro-application architecture, communication between components or pages can be achieved through the event bus, where the event listener is responsible for listening to specific events and using the event dispatching method of the event bus to send events and pass data when the event is triggered.
[0027] In this embodiment, by defining an event bus class, encapsulating event objects and event registration, deregistration, and dispatching methods, and creating its instance in the micro-application global environment, a unified and efficient event communication mechanism is provided for the components and pages of the micro-application, thereby solving the complexity and inefficiency of communication between components and between pages in light application development.
[0028] Further, as a refinement and expansion of the specific implementation of the above embodiment, in order to fully illustrate the specific implementation process of this embodiment, as shown below Figure 2 As shown, another light application event communication method based on event bus is provided, and the method includes the following steps: The specific steps of step S1 are as follows: S11. Create a class for the purpose of encapsulating the functions required by the event bus, and define it as an event bus class; S12. Define event objects in the event bus class and save all events and event listeners for each event; S13. Define the function of the event registration method in the event bus class; S14. Define a function of the event deregistration method in the event bus class; S15. Define the function of the event dispatching method in the event bus class; The specific steps of step S2 are as follows: S21. Get the global environment file of the micro application; S22. Introduce the event bus class in the global environment file of the micro application and create an object instance; S23. Assign the created object instance of the event bus class to the extended attribute of the micro-application, thereby providing a calling interface of the event bus class in the global environment; The specific steps of step S3 are as follows: S31. Build lifecycle functions for components or pages needed in micro applications; If it is a component, go to step S32; If it is a page, go to step S33; S32. Register the event listener in the attached lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the detached lifecycle function by setting the event cancellation method of the event bus class of the micro-application, and proceed to step S4; S33. Register the event listener in the onLoad lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the onUnload lifecycle function by setting the event unregistration method of the event bus class of the micro-application; The specific steps of step S4 are as follows: S41. The micro-application page or component monitors events through its own event listener; If an event trigger is detected, proceed to step S43; If no event trigger is detected, proceed to step S42; S42. Wait for the set time period and return to step S41; S43. Send events and transmit event data through the event dispatching method of the object instance of the event object class of the micro-application, and communicate between the pages and components of the micro-application.
[0029] In an embodiment of the present invention, based on step S12, step S13, step S14, and step S15, a possible embodiment is given below to illustrate its specific implementation scheme in a non-limiting manner.
[0030] The specific steps of step S12 are as follows: S121. Define the structure of the event object in the event bus class, and set each event name to correspond to an array; S122. Set the elements in the array to the page or component identifier, callback function and event listener; The specific steps of the function of the registration method of the event bus class defined in step S13 are as follows: S131. Receive the event name, page or component identifier, and callback function as parameters; S132. Check whether the received event name exists in the array of saved event objects; If not, proceed to step S133; If yes, go to step S134; S133. Create a new array and add the received page or component identifier and the event object of the callback function to the array, and then assign it to the event object, and end; S134. Check whether the listening object identified by the received page or component exists; If yes, go to step S135; If not, proceed to step S136; S135. Update the callback function of the received page or component identifier, and end; S136. Add the received page or component tag and the event object added by the callback function to the array, and end; The specific steps of the function of the cancellation method of the event bus class defined in step S14 are as follows: S141. Receive the event name, page or component identifier, and callback function as parameters; S142. Searching for an array of corresponding event objects according to the received event name as an array of markers that need to deregister the event listener; If found, go to step S143; If not found, end; S143. Filter out arrays other than the marked array from all arrays as active arrays, and reassign the active array to the event name of the event object to implement the cancellation of the event listener of the page or component, and end; The specific steps of the function of the dispatch method of the event bus class defined in step S15 are as follows: S151 receives the event name and event data as parameters; S152. Search the event listener in the corresponding array by event name; If not found, end; If found, go to step S153; S153. Call the callback function in the event listener and pass in the event data to realize message passing.
[0031] Taking WeChat applet as an example, first define the event bus class EventBus, save it in a separate .js file, and expose it through export for reference; First, define an events object in the EventBus class to save all events and all listeners corresponding to each event. The data structure is as follows: events={'event1':[{'page':'page1','callback':'funA'}],'event2':[{'page':'page2','callback':'funB'},{'page':'component1','callback':'funC'}, …], …} In the events object, the attribute name corresponds to the event name, event1 and event2 represent different events respectively, and the attribute value is the set of listener objects corresponding to the event. The page of the listener object represents the page or component, and callback is the callback function of the event bound to the page or component. The EventBus class provides three calling functions, namely on, off, and emit, which represent event registration, event deregistration, and event dispatching respectively.
[0032] In the on function, the input parameters are name, page, and callback. First, create an object tuple containing page and callback. Then check whether the attribute corresponding to the current name exists. If not, directly add the tuple to the collection and add the event attribute of the name to events. If it exists, check whether the listening object of the current node page exists. If it exists, overwrite the current node callback. If not, add the node object tuple to the attribute value of the name.
[0033] In the off function, the input parameters are name and page. First, the corresponding event listening set is found by name. If it does not exist, no processing is performed; if it exists, objects that are not the current page node are filtered out in the set, and the filtered set is reassigned to the name attribute of the events object to realize the page event deregistration function; In the emit function, the input parameters are name and data. First, the corresponding event listening set is found by name. If it does not exist, no processing is performed; if it exists, each event listening object in the set is traversed, the call method of the callback function is called, and the data is sent out to realize the function of message transmission; Secondly, in the app.js file, import the EventBus class, then create the object and assign it to the extended properties of the WeChat applet, wx.eventBus = newEventBus(); Again, in the page page, register the event through wx.eventBus.on in the onLoad life cycle function, and unregister the event through wx.eventBus.off in the onUnload life cycle function.
[0034] Again, in the component, register the event in the attached function and unregister the event in the detached function; Finally, in any page or component, events can be sent and data can be passed through wx.eventBus.emit.
[0035] The present invention solves the complexity and inefficiency of communication between components and pages in the development of lightweight applications. By defining an event bus class, encapsulating event objects and event registration, deregistration, and dispatching methods, and creating its instance in the micro-application global environment, a unified and efficient event communication mechanism is provided for the components and pages of the micro-application.
[0036] It should be understood that the order of execution of the steps in the above embodiment does not necessarily mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present invention.
[0037] like Figure 3 As shown, the following is an embodiment of a light application event communication system based on an event bus provided by an embodiment of the present disclosure. This system and the light application event communication method based on an event bus in the above-mentioned embodiments belong to the same inventive concept. For details not described in detail in the embodiment of the light application event communication system based on an event bus, please refer to the embodiment of the light application event communication method based on an event bus.
[0038] The system includes: an event bus class definition module, which is used to define an event bus class and encapsulate event objects and event registration, deregistration and dispatching methods; The object instance creation module of the event bus class is used to create an object instance of the event bus class in the global environment of the micro-application and provide a calling interface in the global scope of the micro-application; The event listener registration and deregistration module is used to use the object instance of the event bus class by calling the interface in the life cycle function of the micro-application component or page, register the event listener using the event registration method, and deregister the event listener using the event deregistration method; The event monitoring and dispatching module is used to monitor events in components or pages through event listeners. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
[0039] The present invention establishes communication between different components and pages by manually managing event registration and deregistration, reduces coupling, and improves usability, thereby avoiding problems such as bloated code, cumbersome calls, difficult maintenance, and waste of resources in some complex nested pages and components. Figure 4 A schematic diagram of the hardware structure of an electronic device for implementing various embodiments of the present invention.
[0040] The light application event communication method based on the event bus provided in the embodiment of the present application can be applied to electronic devices. Those skilled in the art will understand that the electronic device structure involved in the embodiment of the present invention does not constitute a limitation on the electronic device, and the electronic device may include more or fewer components than shown, or combine certain components, or arrange different components. In an embodiment of the present invention, electronic devices include but are not limited to laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the embodiments of the present application described and / or required herein.
[0041] The electronic device may include a processor, an external memory interface, an internal memory, a universal serial bus (USB) interface, a charging management module, a power management module, a battery, a wireless communication module, an audio module, a speaker, a microphone, a sensor module, buttons, a camera, a display, and a SIM card interface, etc.
[0042] It is to be understood that the structure illustrated in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0043] The processor may include one or more processing units, for example, the processor may include a central processing unit (CPU), an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units may be independent devices or integrated into one or more processors.
[0044] The processor can be the nerve center and command center of the electronic device. The controller can generate an operation control signal according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.
[0045] A memory may also be provided in the processor for storing instructions and data. In some embodiments, the memory in the processor is a cache memory. The memory may store instructions or data that the processor has just used or is cyclically used. If the processor needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor, and thus improves system efficiency.
[0046] The external memory interface can be used to connect an external memory card, such as a MicroSD card, to expand the storage capacity of the electronic device. The external memory card communicates with the processor through the external memory interface to implement data storage functions. For example, files such as music and videos can be saved in the external memory card.
[0047] The internal memory can be used to store computer executable program codes, which include instructions. The processor executes various functional applications and data processing of the electronic device by running the instructions stored in the internal memory. The internal memory may include a program storage area and a data storage area. The internal memory may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0048] The wireless communication function of an electronic device can be realized through an antenna, a wireless communication module, a modem processor, and a baseband processor.
[0049] The wireless communication module can provide wireless communication solutions for electronic devices, including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared technology (IR), etc.
[0050] Electronic devices can implement audio functions, etc. through audio modules, speakers, receivers, microphones, headphone jacks, and application processors.
[0051] Electronic devices can achieve shooting functions through ISP, camera, video codec, GPU, display and application processor.
[0052] Electronic devices can achieve display functions through GPU, display screen and application processor.
[0053] The GPU is a microprocessor for image processing that connects the display screen and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor may include one or more GPUs that execute program instructions to generate or change display information.
[0054] The display screen is used to display images, videos, etc. The display screen includes a display panel.
[0055] The above-mentioned electronic device implements the light application event communication method based on the event bus of the present application, defines an event bus class, encapsulates event objects and event registration, deregistration and dispatching methods; creates an object instance of the event bus class in the global environment of the micro-application, and provides a calling interface in the global scope of the micro-application; uses the object instance of the event bus class through the calling interface in the life cycle function of the component or page of the micro-application, registers the event listener using the event registration method, and deregisters the event listener using the event deregistration method; the event listener listens to the event in the component or page, and when the event is triggered, uses the event dispatching method of the object instance of the event bus class to send the event and transmit data, so as to realize the communication between micro-application components or between pages, solve the complexity and inefficiency of communication between components and components and between pages in the development of light applications, and provides a unified and efficient event communication mechanism for the components and pages of the micro-application by defining an event bus class, encapsulating event objects and event registration, deregistration and dispatching methods, and creating its instance in the global environment of the micro-application.
[0056] The storage medium provided in the present application stores a program product that can implement a light application event communication method based on an event bus.
[0057] The event bus-based light application event communication method includes: Define an event bus class to encapsulate event objects and event registration, deregistration, and dispatching methods; Create an object instance of the event bus class in the global environment of the micro-application, and provide a calling interface in the global scope of the micro-application; In the life cycle function of the micro-application component or page, use the object instance of the event bus class by calling the interface, register the event listener using the event registration method, and unregister the event listener using the event unregistration method; The event listener listens to events in components or pages. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
[0058] In some possible implementations, the event bus-based light application event communication method disclosed herein can be implemented in the form of a program product, which includes a program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps described in the above "Exemplary Method" section of this specification according to various exemplary implementations of the present disclosure.
[0059] The storage medium of the present disclosure can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can be, for example, but not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0060] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A light application event communication method based on event bus, characterized in that: The steps include: S1. Define an event bus class to encapsulate event objects and event registration, deregistration, and dispatching methods; S2. Create an object instance of the event bus class in the global environment of the micro-application, and provide a calling interface in the global scope of the micro-application; S3. In the life cycle function of the component or page of the micro application, use the object instance of the event bus class by calling the interface, register the event listener using the event registration method, and unregister the event listener using the event unregistration method; S4. The event listener listens to events in components or pages. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
2. The method for light application event communication based on event bus according to claim 1, characterized in that: The specific steps of step S1 are as follows: S11. Create a class for the purpose of encapsulating the functions required by the event bus, and define it as an event bus class; S12. Define event objects in the event bus class and save all events and event listeners for each event; S13. Define the function of the event registration method in the event bus class; S14. Define a function of the event deregistration method in the event bus class; S15. Define the function of the event dispatching method in the event bus class.
3. The method for light application event communication based on event bus according to claim 2, characterized in that: The specific steps of step S12 are as follows: S121. Define the structure of the event object in the event bus class, and set each event name to correspond to an array; S122. Set the elements in the array to the page or component's identifier, callback function, and event listener.
4. The method for light application event communication based on event bus according to claim 3 is characterized in that: The specific steps of the function of the registration method of the event bus class defined in step S13 are as follows: S131. Receive the event name, page or component identifier, and callback function as parameters; S132. Check whether the received event name exists in the array of saved event objects; If not, proceed to step S133; If yes, go to step S134; S133. Create a new array and add the received page or component identifier and the event object of the callback function to the array, and then assign it to the event object, and end; S134. Check whether the listening object identified by the received page or component exists; If yes, go to step S135; If not, proceed to step S136; S135. Update the callback function of the received page or component identifier, and end; S136. Add the received page or component tag and the event object added by the callback function to the array, and end; The specific steps of the function of the cancellation method of the event bus class defined in step S14 are as follows: S141. Receive the event name, page or component identifier, and callback function as parameters; S142. Searching for an array of corresponding event objects according to the received event name as an array of markers that need to deregister the event listener; If found, go to step S143; If not found, end; S143. Filter out arrays other than the marked array from all arrays as active arrays, and reassign the active array to the event name of the event object to implement the cancellation of the event listener of the page or component, and end; The specific steps of the function of the dispatch method of the event bus class defined in step S15 are as follows: S151 receives the event name and event data as parameters; S152. Search the event listener in the corresponding array by event name; If not found, end; If found, go to step S153; S153. Call the callback function in the event listener and pass in the event data to realize message passing.
5. The method for light application event communication based on event bus according to claim 2, characterized in that: The specific steps of step S2 are as follows: S21. Get the global environment file of the micro application; S22. Introduce the event bus class in the global environment file of the micro application and create an object instance; S23. Assign the object instance of the created event bus class to the extended attribute of the micro application, thereby providing a calling interface of the event bus class in the global environment.
6. The method for light application event communication based on event bus according to claim 5, characterized in that: The specific steps of step S3 are as follows: S31. Build lifecycle functions for components or pages needed in micro applications; If it is a component, go to step S32; If it is a page, go to step S33; S32. Register the event listener in the attached lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the detached lifecycle function by setting the event cancellation method of the event bus class of the micro-application, and proceed to step S4; S33. Register the event listener in the onLoad lifecycle function by setting the event registration method of the event bus class of the micro-application, and unregister the event listener in the onUnload lifecycle function by setting the event unregistration method of the event bus class of the micro-application.
7. The method for light application event communication based on event bus according to claim 6, characterized in that: The specific steps of step S4 are as follows: S41. The micro-application page or component monitors events through its own event listener; If an event trigger is detected, proceed to step S43; If no event trigger is detected, proceed to step S42; S42. Wait for the set time period and return to step S41; S43. Send events and transmit event data through the event dispatching method of the object instance of the event object class of the micro-application, and communicate between the pages and components of the micro-application.
8. A light application event communication system based on event bus, characterized in that: include: The event bus class definition module is used to define an event bus class and encapsulate event objects and event registration, deregistration, and dispatch methods; The object instance creation module of the event bus class is used to create an object instance of the event bus class in the global environment of the micro-application and provide a calling interface in the global scope of the micro-application; The event listener registration and deregistration module is used to use the object instance of the event bus class by calling the interface in the life cycle function of the micro-application component or page, register the event listener using the event registration method, and deregister the event listener using the event deregistration method; The event monitoring and dispatching module is used to monitor events in components or pages through event listeners. When an event is triggered, the event dispatching method of the object instance of the event bus class is used to send the event and transmit data to achieve communication between micro-application components or between pages.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the light application event communication method based on the event bus as described in any one of claims 1 to 7 are implemented.
10. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the light application event communication method based on the event bus as described in any one of claims 1 to 7 are implemented.