createApp()
Creates a GlyphUI application with top-level state management, reducers, and a view function. This is the main entry point for building applications with GlyphUI.Signature
Parameters
The application configuration object.
The initial state of the application.
The top-level view function that renders the application UI.Signature:
(state, emit) => vnodestate: The current application stateemit: Function to dispatch actions
An object mapping action names to reducer functions.Reducer signature:
(state, payload) => newStatestate: The current statepayload: The action payload
Return Value
Returns an application object with the following methods:
mount(parentEl): Mounts the application to a DOM elementunmount(): Unmounts the application and cleans upemit(eventName, payload): Dispatches an action
Methods
mount()
Mounts the application to a DOM element. Signature:The DOM element to mount the application to.
unmount()
Unmounts the application from the DOM and unsubscribes all event listeners. Signature:emit()
Dispatches an action to the application, triggering the corresponding reducer. Signature:The name of the action to dispatch.
The payload to pass to the reducer.
Complete Example
How It Works
- Initialization:
createApp()sets up a dispatcher and subscribes reducers to actions - Mounting: When
mount()is called, the view function is invoked with the current state - Rendering: The virtual DOM is created and mounted to the target element
- State Updates: When
emit()is called:- The corresponding reducer updates the state
- The view function is called with the new state
- The DOM is efficiently patched with the changes
Notes
- Reducers should be pure functions that don’t mutate the state
- The view function is called automatically whenever the state changes
- GlyphUI uses a virtual DOM diffing algorithm to efficiently update only the parts that changed
- You can emit actions from within event handlers, reducers, or anywhere you have access to the
emitfunction