You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { appEvents } from"./events.js"; // We will create this in the next section
112
+
113
+
/*----------------------------------
114
+
| Do OpenScript Configurations Here
115
+
|----------------------------------
116
+
*/
107
117
108
118
constrouter=app("router");
109
119
constbroker=app("broker");
110
120
111
121
exportfunctionconfigureApp() {
112
-
// Router Config
122
+
/*-----------------------------------
123
+
| Set the global runtime prefix.
124
+
| This prefix will be appended
125
+
| to every path before resolution.
126
+
| So ensure when defining routes,
127
+
| you have it as the main prefix.
128
+
|------------------------------------
129
+
*/
113
130
router.runtimePrefix("");
131
+
132
+
/**----------------------------------
133
+
*
134
+
* Set the default route path here
135
+
* ----------------------------------
136
+
*/
114
137
router.basePath("");
115
138
116
-
// Broker Config
139
+
/*--------------------------------
140
+
| Set the logs clearing interval
141
+
| for the broker to remove stale
142
+
| events. (milliseconds)
143
+
|--------------------------------
144
+
*/
117
145
broker.CLEAR_LOGS_AFTER=30000;
146
+
147
+
/*--------------------------------
148
+
| Set how old an event must be
149
+
| to be deleted from the broker's
150
+
| event log during logs clearing
151
+
|--------------------------------
152
+
*/
118
153
broker.TIME_TO_GC=10000;
154
+
155
+
/*-------------------------------------------
156
+
| Start the garbage
157
+
| collector for the broker
158
+
|-------------------------------------------
159
+
*/
119
160
broker.removeStaleEvents();
120
161
121
-
// Enable logs in dev
162
+
/*------------------------------------------
163
+
| Should the broker display events
164
+
| in the console as they are fired
165
+
|------------------------------------------
166
+
*/
122
167
if (/^(127\.0\.0\.1|localhost|.*\.test)$/.test(router.url().hostname)) {
123
-
broker.withLogs(true);
168
+
broker.withLogs(false); // Enable logs for development
124
169
}
125
170
126
-
// Strict Event Registration
171
+
/**
172
+
* ---------------------------------------------
173
+
* Should the broker require events registration.
174
+
* This ensures that only registered events
175
+
* can be listened to and fire by the broker.
176
+
* ---------------------------------------------
177
+
*/
127
178
broker.requireEventsRegistration(true);
179
+
180
+
/**
181
+
* ---------------------------------------------
182
+
* Register events with the broker
183
+
* ---------------------------------------------
184
+
*/
185
+
128
186
broker.registerEvents(appEvents);
129
187
130
-
// Register appEvents in container for easy access
188
+
/**
189
+
* ---------------------------------------------
190
+
* Register core services in IoC container
191
+
* ---------------------------------------------
192
+
*/
131
193
app().value("appEvents", appEvents);
132
194
133
-
// Cleanup callback for third-party libs
195
+
/**
196
+
* ---------------------------------------------
197
+
* Node Disposal Callback
198
+
* ---------------------------------------------
199
+
* Use this to clean up external library instances
200
+
* attached to DOM nodes when they are removed.
201
+
*/
134
202
registerNodeDisposalCallback((node) => {
135
-
// e.g. clean up tooltips
203
+
// Example: Dispose Bootstrap tooltips/popovers
204
+
// if bootstrap.Tooltip.getInstance(node) {
205
+
// bootstrap.Tooltip.getInstance(node).dispose();
206
+
// }
136
207
});
137
208
}
138
209
210
+
// execute configuration
139
211
configureApp();
140
212
```
141
213
214
+
> **Note**: `registerNodeDisposalCallback` is crucial for preventing memory leaks when using third-party libraries that attach instances to DOM elements (like Bootstrap, Tippy.js, etc.). The callback **MUST** be synchronous and stateless.
215
+
216
+
> **Note**: In the configuration above, we are using `appEvents` imported from `events.js`. We will cover the creation of `events.js` and how to handle events in the subsequent sections.
217
+
218
+
### Define Application Events
219
+
220
+
OpenScript uses a centralized event broker. It's best practice to define all your application events in a single file, typically `events.js` (or `src/events.js`).
221
+
222
+
If you configured `broker.requireEventsRegistration(true)` in your `ojs.config.js`, only events defined here and registered will be allowed.
223
+
224
+
Create a `src/events.js` file:
225
+
226
+
```javascript
227
+
/**
228
+
* Application Events
229
+
* Structure: Nested object where keys become namespaced event names
230
+
* Example: app.started becomes "app:started"
231
+
* todo.added -> "todo:added"
232
+
*/
233
+
exportconstappEvents= {
234
+
app: {
235
+
started:true,
236
+
ready:true,
237
+
},
238
+
239
+
// Example for a Todo App
240
+
todo: {
241
+
added:true,
242
+
deleted:true,
243
+
completed:true,
244
+
245
+
// Nested events
246
+
needs: {
247
+
refresh:true,
248
+
},
249
+
},
250
+
251
+
ui: {
252
+
modal: {
253
+
opened:true,
254
+
closed:true,
255
+
},
256
+
},
257
+
};
258
+
```
259
+
260
+
This structure allows you to use `appEvents.todo.added` to refer to the event in your code, providing strict typing and avoiding magic strings.
261
+
262
+
### Configure Contexts
263
+
264
+
Contexts are used to manage state and share data across your application. Create an `ojs.contexts.js` file (or `src/ojs.contexts.js`) to initialize them.
0 commit comments