@@ -12,6 +12,14 @@ import (
1212)
1313
1414// New initialises a Core instance using the provided options and performs the necessary setup.
15+ // It is the primary entry point for creating a new Core application.
16+ //
17+ // Example:
18+ //
19+ // core, err := core.New(
20+ // core.WithService(&MyService{}),
21+ // core.WithAssets(assets),
22+ // )
1523func New (opts ... Option ) (* Core , error ) {
1624 c := & Core {
1725 services : make (map [string ]any ),
@@ -37,6 +45,20 @@ func New(opts ...Option) (*Core, error) {
3745// WithService creates an Option that registers a service. It automatically discovers
3846// the service name from its package path and registers its IPC handler if it
3947// implements a method named `HandleIPCEvents`.
48+ //
49+ // Example:
50+ //
51+ // // In myapp/services/calculator.go
52+ // package services
53+ //
54+ // type Calculator struct{}
55+ //
56+ // func (s *Calculator) Add(a, b int) int { return a + b }
57+ //
58+ // // In main.go
59+ // import "myapp/services"
60+ //
61+ // core.New(core.WithService(services.NewCalculator))
4062func WithService (factory func (* Core ) (any , error )) Option {
4163 return func (c * Core ) error {
4264 serviceInstance , err := factory (c )
@@ -83,20 +105,27 @@ func WithName(name string, factory func(*Core) (any, error)) Option {
83105 }
84106}
85107
108+ // WithWails creates an Option that injects the Wails application instance into the Core.
109+ // This is essential for services that need to interact with the Wails runtime.
86110func WithWails (app * application.App ) Option {
87111 return func (c * Core ) error {
88112 c .App = app
89113 return nil
90114 }
91115}
92116
117+ // WithAssets creates an Option that registers the application's embedded assets.
118+ // This is necessary for the application to be able to serve its frontend.
93119func WithAssets (fs embed.FS ) Option {
94120 return func (c * Core ) error {
95121 c .assets = fs
96122 return nil
97123 }
98124}
99125
126+ // WithServiceLock creates an Option that prevents any further services from being
127+ // registered after the Core has been initialized. This is a security measure to
128+ // prevent late-binding of services that could have unintended consequences.
100129func WithServiceLock () Option {
101130 return func (c * Core ) error {
102131 c .serviceLock = true
@@ -106,14 +135,20 @@ func WithServiceLock() Option {
106135
107136// --- Core Methods ---
108137
138+ // ServiceStartup is the entry point for the Core service's startup lifecycle.
139+ // It is called by Wails when the application starts.
109140func (c * Core ) ServiceStartup (ctx context.Context , options application.ServiceOptions ) error {
110141 return c .ACTION (ActionServiceStartup {})
111142}
112143
144+ // ServiceShutdown is the entry point for the Core service's shutdown lifecycle.
145+ // It is called by Wails when the application shuts down.
113146func (c * Core ) ServiceShutdown (ctx context.Context ) error {
114147 return c .ACTION (ActionServiceShutdown {})
115148}
116149
150+ // ACTION dispatches a message to all registered IPC handlers.
151+ // This is the primary mechanism for services to communicate with each other.
117152func (c * Core ) ACTION (msg Message ) error {
118153 c .ipcMu .RLock ()
119154 handlers := append ([]func (* Core , Message ) error (nil ), c .ipcHandlers ... )
@@ -128,18 +163,21 @@ func (c *Core) ACTION(msg Message) error {
128163 return agg
129164}
130165
166+ // RegisterAction adds a new IPC handler to the Core.
131167func (c * Core ) RegisterAction (handler func (* Core , Message ) error ) {
132168 c .ipcMu .Lock ()
133169 c .ipcHandlers = append (c .ipcHandlers , handler )
134170 c .ipcMu .Unlock ()
135171}
136172
173+ // RegisterActions adds multiple IPC handlers to the Core.
137174func (c * Core ) RegisterActions (handlers ... func (* Core , Message ) error ) {
138175 c .ipcMu .Lock ()
139176 c .ipcHandlers = append (c .ipcHandlers , handlers ... )
140177 c .ipcMu .Unlock ()
141178}
142179
180+ // RegisterService adds a new service to the Core.
143181func (c * Core ) RegisterService (name string , api any ) error {
144182 if c .servicesLocked {
145183 return fmt .Errorf ("core: service %q is not permitted by the serviceLock setting" , name )
@@ -156,6 +194,8 @@ func (c *Core) RegisterService(name string, api any) error {
156194 return nil
157195}
158196
197+ // Service retrieves a registered service by name.
198+ // It returns nil if the service is not found.
159199func (c * Core ) Service (name string ) any {
160200 c .serviceMu .RLock ()
161201 api , ok := c .services [name ]
@@ -191,6 +231,7 @@ func MustServiceFor[T any](c *Core, name string) T {
191231}
192232
193233// App returns the global application instance.
234+ // It panics if the Core has not been initialized.
194235func App () * application.App {
195236 if instance == nil {
196237 panic ("core.App() called before core.Setup() was successfully initialized" )
@@ -210,8 +251,10 @@ func (c *Core) Display() Display {
210251 return display
211252}
212253
254+ // Core returns the Core instance itself.
213255func (c * Core ) Core () * Core { return c }
214256
257+ // Assets returns the embedded filesystem containing the application's assets.
215258func (c * Core ) Assets () embed.FS {
216259 return c .assets
217260}
0 commit comments