Skip to content

Commit ded82ee

Browse files
authored
Merge pull request #2 from OpenScriptJs/develop
Readme updated and helper function for eventparsing added to list of exports.
2 parents f31aa14 + e956490 commit ded82ee

7 files changed

Lines changed: 31 additions & 26 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,13 @@ Listen to global application events dispatched via the Broker. Methods prefixed
566566
- `eventName`: The specific event name triggered.
567567
568568
```javascript
569-
import { EventData } from "modular-openscriptjs";
569+
import { parsePayload } from "modular-openscriptjs";
570570
571571
export default class UserProfile extends Component {
572572
// Listen for 'auth:login' event
573573
async $$auth_login(eventData, eventName) {
574574
// 1. Parse the payload
575-
const data = EventData.parse(eventData);
575+
const data = parsePayload(eventData);
576576
577577
console.log("User Logged In:", data.message.get("userId"));
578578
}
@@ -1069,12 +1069,12 @@ When you listen for an event (e.g., in a Mediator or Component), you receive the
10691069
**Why a string?** It ensures that data remains immutable during transit and can be easily serialized for logging or debugging.
10701070

10711071
```javascript
1072-
import { EventData } from "modular-openscriptjs";
1072+
import { parsePayload } from "modular-openscriptjs";
10731073

10741074
// In a Component or Mediator
1075-
async $$auth_login(eventDataString, eventName) {
1075+
async $$auth_login(eventData, eventName) {
10761076
// 1. Parse the string back into an EventData object
1077-
const data = EventData.parse(eventDataString);
1077+
const data = parsePayload(eventData);
10781078

10791079
// 2. Access the message
10801080
const userId = data.message.get("id"); // 42
@@ -1120,7 +1120,7 @@ A Mediator is just a class that extends `Mediator`. It doesn't have a UI. It jus
11201120

11211121
```javascript
11221122
// src/mediators/AuthMediator.js
1123-
import { Mediator, EventData, payload } from "modular-openscriptjs";
1123+
import { Mediator, parsePayload, payload } from "modular-openscriptjs";
11241124

11251125
export default class AuthMediator extends Mediator {
11261126
// REQUIRED: This tells the framework to scan this class for listeners
@@ -1129,8 +1129,8 @@ export default class AuthMediator extends Mediator {
11291129
}
11301130

11311131
// Logic: Listen for 'auth' and 'login' events
1132-
async $$auth_login(eventDataString, eventName) {
1133-
const data = EventData.parse(eventDataString);
1132+
async $$auth_login(eventData, eventName) {
1133+
const data = parsePayload(eventData);
11341134
const credentials = data.message.getAll();
11351135

11361136
try {

docs/2-components.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,20 @@ Methods starting with `$$` are treated as listeners for global events emitted vi
165165

166166
**Signature**: `(eventData, eventName)`
167167

168-
- `eventData`: The JSON stringified payload (needs `EventData.parse()`).
168+
- `eventData`: The JSON stringified payload (needs `parsePayload()`).
169169
- `eventName`: The string name of the event that triggered this listener.
170170

171171
- `$$app_started(eventData, event)`: Listens for `app:started`.
172172
- `$$user_login(eventData, event)`: Listens for `user:login`.
173173

174174
```javascript
175-
import { EventData, component } from "modular-openscriptjs";
175+
import { parsePayload, component } from "modular-openscriptjs";
176176

177177
export default class UserProfile extends Component {
178178
// Listen to global 'auth:logout' event
179179
async $$auth_logout(eventData, event) {
180180
// 1. Parse Data
181-
const data = EventData.parse(eventData);
181+
const data = parsePayload(eventData);
182182
console.log(`Received ${event}`);
183183
}
184184
}

docs/8-events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ async $$auth_login(eventData, event) {
8585
When an event is received (e.g., in a Mediator), the payload is often a **JSON string**. You must parse it back into an `EventData` object to access the helpers.
8686

8787
```javascript
88-
import { EventData } from "modular-openscriptjs";
88+
import { parsePayload } from "modular-openscriptjs";
8989

9090
// ... in a listener
91-
const eventData = EventData.parse(payloadString);
91+
const eventData = parsePayload(payloadString);
9292
```
9393

9494
### EventData Helper Methods

docs/9-mediators.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The `$$` prefix is used to define event listeners. OpenScript interprets these p
5252
If you use an underscore in the method name after `$$`, it acts as an **OR** operator. The method will key off **multiple independent events**.
5353

5454
```javascript
55-
import { EventData } from "modular-openscriptjs";
55+
import { parsePayload } from "modular-openscriptjs";
5656

5757
/*
5858
* Listens for:
@@ -62,7 +62,7 @@ import { EventData } from "modular-openscriptjs";
6262
*/
6363
async $$user_login(eventData, event) {
6464
// Parse the JSON string payload
65-
const data = EventData.parse(eventData);
65+
const data = parsePayload(eventData);
6666

6767
console.log(`Triggered by '${event}'`);
6868
console.log("User ID:", data.message.get("id"));
@@ -78,7 +78,7 @@ To listen to namespaced events (e.g., `user:login`, `user:logout`), you should u
7878
$$auth = {
7979
// Listens for 'auth:login'
8080
login: async (eventData, event) => {
81-
const data = EventData.parse(eventData);
81+
const data = parsePayload(eventData);
8282
this.handleLogin(data);
8383
},
8484

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-openscriptjs",
3-
"version": "2.0.14",
3+
"version": "2.0.15",
44
"description": "OpenScriptJs Framework - A lightweight, reactive JavaScript framework for building modern web applications",
55
"type": "module",
66
"main": "./dist/modular-openscriptjs.umd.js",

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const putContext = (referenceName, qualifiedName) =>
6464
contextProvider.load(referenceName, qualifiedName);
6565
const lazyFor = Utils.lazyFor;
6666
const each = Utils.each;
67+
const parsePayload = Utils.parsePayload;
6768
const component = (componentId) => app("repository").findComponent(componentId);
6869
const eData = (meta = {}, message = {}) => {
6970
return new EventData().meta(meta).message(message).encode();
@@ -189,6 +190,7 @@ export {
189190
component,
190191
eData,
191192
payload,
193+
parsePayload,
192194
ojsRouterEvents,
193195
removeNodeModifications,
194196
removeNode,
@@ -233,6 +235,7 @@ export default {
233235
component,
234236
eData,
235237
payload,
238+
parsePayload,
236239
ojsRouterEvents,
237240
removeNodeModifications,
238241
removeNode,

src/utils/Utils.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@ export default class Utils {
77
/**
88
* Runs a foreach on an array
99
* @param {Iterable} array
10-
* @param {Function} callback
10+
* @param {(v: any, index: Number) => any} callback
1111
*/
12-
static each = (array, callback = (v, index) => v) => {
12+
static each = (array, callback = null) => {
13+
if (!callback) callback = (v) => v;
14+
1315
let output = [];
14-
if (Array.isArray(array)) {
15-
array.forEach((v, i) => output.push(callback(v, i)));
16-
} else {
17-
for (let k in array) output.push(callback(array[k], k));
18-
}
16+
17+
for (let k in array) output.push(callback(array[k], k));
18+
1919
return output;
2020
};
2121

2222
/**
2323
* Iterates over array elements using setTimeout
2424
* @param {Iterable} array
25-
* @param {Function} callback
25+
* @param {(v: any) => any} callback
2626
*/
27-
static lazyFor = (array, callback = (v) => v) => {
27+
static lazyFor = (array, callback = null) => {
28+
if (!callback) callback = (v) => v;
29+
2830
let index = 0;
2931

3032
if (array.length < 1) return;

0 commit comments

Comments
 (0)