systemlynx 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -11,8 +11,8 @@ const { App, Service, Client, LoadBalancer } = require("systemlynx");
|
|
|
11
11
|
Call `require("systemlynx")` and de-concatenate from the object it returns. The main abstractions used for client-to-server interactions are the following:
|
|
12
12
|
|
|
13
13
|
- **Service** - Used to create and host objects that can be loaded and used by a SystemLynx Client.
|
|
14
|
-
- **Client** - Used in a client application to load a
|
|
15
|
-
- **App** - Provides a modular interface and lifecycle methods for asynchronously creating and loading
|
|
14
|
+
- **Client** - Used in a client application to load a **Service**, which contains all the objects added to the **Service**.
|
|
15
|
+
- **App** - Provides a modular interface and lifecycle methods for asynchronously creating and loading **Services**.
|
|
16
16
|
|
|
17
17
|
Find the full [API Documentation](https://github.com/Odion100/SystemLynx/blob/tasksjs2.0/API.md#tasksjs-api-documentation) here.
|
|
18
18
|
|
|
@@ -22,16 +22,16 @@ Find the full [API Documentation](https://github.com/Odion100/SystemLynx/blob/ta
|
|
|
22
22
|
|
|
23
23
|
## Service.ServerModule(name, constructor [,options])
|
|
24
24
|
|
|
25
|
-
Use the `Service.ServerModule(name, constructor/object)` method to register an object to be hosted by a
|
|
25
|
+
Use the `Service.ServerModule(name, constructor/object)` method to register an object to be hosted by a **SystemLynx Service**. This will allows you to load an instance of that object onto a client application, and call any methods on that object remotely.
|
|
26
26
|
|
|
27
27
|
```javascript
|
|
28
28
|
const { Service } = require("systemlynx");
|
|
29
29
|
|
|
30
30
|
const Users = {};
|
|
31
31
|
|
|
32
|
-
Users.add = function (data
|
|
32
|
+
Users.add = function (data) {
|
|
33
33
|
console.log(data);
|
|
34
|
-
|
|
34
|
+
return { message: "You have successfully called the Users.add method" };
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
Service.ServerModule("Users", Users);
|
|
@@ -39,17 +39,17 @@ Service.ServerModule("Users", Users);
|
|
|
39
39
|
|
|
40
40
|
In the code above we assigned an object to the variable `Users` and gave it an add method. The `Service.ServerModule(name, constructor/object)` function takes the name assigned to the object as the first argument and the object itself as the second argument.
|
|
41
41
|
|
|
42
|
-
Alternatively, you can use a constructor function instead of an object as the second argument. In the example below we create another
|
|
43
|
-
"Orders".
|
|
42
|
+
Alternatively, you can use a constructor function instead of an object as the second argument. In the example below we create another **ServerModule** called
|
|
43
|
+
"Orders". This time we use a constructor function as the second argument of the to **ServerModule** function. The `this` value is the initial instance of the **ServerModule** object. Every method added to the `this` value will be accessible when the object is loaded by a **SystemLynx Client**. Note: **ServerModule** methods can be synchronous or asynchronous functions.
|
|
44
44
|
|
|
45
45
|
```javascript
|
|
46
46
|
const { Service } = require("systemlynx");
|
|
47
47
|
|
|
48
48
|
const Users = {};
|
|
49
49
|
|
|
50
|
-
Users.add = function (data
|
|
50
|
+
Users.add = function (data) {
|
|
51
51
|
console.log(data);
|
|
52
|
-
|
|
52
|
+
return { message: "You have successfully called the Users.add method" };
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
Service.ServerModule("Users", Users);
|
|
@@ -57,27 +57,25 @@ Service.ServerModule("Users", Users);
|
|
|
57
57
|
Service.ServerModule("Orders", function () {
|
|
58
58
|
const Orders = this;
|
|
59
59
|
|
|
60
|
-
Orders.find = function (arg1, arg2
|
|
60
|
+
Orders.find = async function (arg1, arg2) {
|
|
61
61
|
console.log(data);
|
|
62
|
-
|
|
62
|
+
return { message: "You have successfully called the Orders.find method" };
|
|
63
63
|
};
|
|
64
64
|
});
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
In the _ServerModule_ constructor function above, the `this` value is the initial instance of the _ServerModule_ object. Every method added to the `this` value will be accessible when the object is loaded by a _SystemLynx Client_. Notice that the method we created, `Orders.find = function(arg1, arg2, callback)...`, has 3 parameters including a callback function as the last argument. By defualt all _ServerModule_ methods will recieve a callback function as its last argument. Use the first parameter of the callback function to respond with an error, and the second parameter to send a success response. Note: _ServerModule_ methods can be configured to work with synchronous return values instead of asynchronous callbacks (read more about Service configuration [here](https://github.com/Odion100/SystemLynx/blob/tasksjs2.0/API.md#apploadserviceurl)).
|
|
68
|
-
|
|
69
67
|
## Service.startService(options)
|
|
70
68
|
|
|
71
|
-
Before we can access the objects hosted by this
|
|
69
|
+
Before we can access the objects hosted by this **Service** from a client application, we need to call the `Service.startService(options)` function. This will start an **ExpressJS** Server and a **Socket.io** WebSocket Server, and set up routing for the **Service**. In the example below we added the `Service.startService(options)` function at the bottom, but the order does not matter.
|
|
72
70
|
|
|
73
71
|
```javascript
|
|
74
72
|
const { Service } = require("systemlynx");
|
|
75
73
|
|
|
76
74
|
const Users = {};
|
|
77
75
|
|
|
78
|
-
Users.add = function (data
|
|
76
|
+
Users.add = function (data) {
|
|
79
77
|
console.log(data);
|
|
80
|
-
|
|
78
|
+
return { message: "You have successfully called the Users.add method" };
|
|
81
79
|
};
|
|
82
80
|
|
|
83
81
|
Service.ServerModule("Users", Users);
|
|
@@ -85,9 +83,9 @@ Service.ServerModule("Users", Users);
|
|
|
85
83
|
Service.ServerModule("Orders", function () {
|
|
86
84
|
const Orders = this;
|
|
87
85
|
|
|
88
|
-
Orders.find = function (arg1, arg2
|
|
86
|
+
Orders.find = function (arg1, arg2) {
|
|
89
87
|
console.log(data);
|
|
90
|
-
|
|
88
|
+
return { message: "You have successfully called the Orders.find method" };
|
|
91
89
|
};
|
|
92
90
|
});
|
|
93
91
|
|
|
@@ -98,7 +96,7 @@ Now lets see how these objects can be loaded into a client application.
|
|
|
98
96
|
|
|
99
97
|
## Client.loadService(url, [options])
|
|
100
98
|
|
|
101
|
-
The `Client.loadService(url)` function can be used to load a SystemLynx
|
|
99
|
+
The `Client.loadService(url)` function can be used to load a SystemLynx **Service**. This method requires the url (string) of the **Service** you want to load as the first argument, and will return a promise that will resolve into an object that containing all the modules hosted by that service. See below. **NOTE: You must be within an async function in order to use the `await` keyword when returning a promise.**
|
|
102
100
|
|
|
103
101
|
```javascript
|
|
104
102
|
const { Client } = require("systemlynx");
|
|
@@ -108,7 +106,7 @@ const { Users, Orders } = await Client.loadService("http://localhost:4400/test/s
|
|
|
108
106
|
console.log(Users, Orders);
|
|
109
107
|
```
|
|
110
108
|
|
|
111
|
-
Now that we've loaded the
|
|
109
|
+
Now that we've loaded the **Service** that we created in the previous example, and have a handle on the **Users** and **Orders** objects hosted by the **Service**, we can now call any method on those objects in the same way we would remotely. In the example below, noticed that both the `User.add` and `Orders.find` methods will return a promise.
|
|
112
110
|
|
|
113
111
|
```javascript
|
|
114
112
|
const { Client } = require("systemlynx");
|
|
@@ -117,10 +115,9 @@ const { Users, Orders } = await Client.loadService("http://localhost:4400/test/s
|
|
|
117
115
|
|
|
118
116
|
console.log(Users, Orders);
|
|
119
117
|
|
|
120
|
-
Users.add({ message: "Users.add Test" }
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
118
|
+
const results = await Users.add({ message: "Users.add Test" });
|
|
119
|
+
|
|
120
|
+
console.log(results);
|
|
124
121
|
|
|
125
122
|
const response = await Orders.find("hello", "world");
|
|
126
123
|
|
|
@@ -129,7 +126,7 @@ console.log(response);
|
|
|
129
126
|
|
|
130
127
|
## Sending and Receiving Websocket Events
|
|
131
128
|
|
|
132
|
-
We can also receive WebSocket events emitted from the remote objects we've loaded using the `Client.loadService(url)` function. In the example below we're using the `Users.on(event_name, callback)` method to listen for events coming from the "Users"
|
|
129
|
+
We can also receive WebSocket events emitted from the remote objects we've loaded using the `Client.loadService(url)` function. In the example below we're using the `Users.on(event_name, callback)` method to listen for events coming from the "Users" **ServerModule**.
|
|
133
130
|
|
|
134
131
|
```javascript
|
|
135
132
|
const { Client } = require("systemlynx");
|
|
@@ -138,10 +135,9 @@ const { Users, Orders } = await Client.loadService("http://localhost:4400/test/s
|
|
|
138
135
|
|
|
139
136
|
console.log(Users, Orders);
|
|
140
137
|
|
|
141
|
-
Users.add({ message: "Users.add Test" }
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
});
|
|
138
|
+
const results = await Users.add({ message: "Users.add Test" });
|
|
139
|
+
|
|
140
|
+
console.log(results);
|
|
145
141
|
|
|
146
142
|
Users.on("new_user", function (event) {
|
|
147
143
|
console.log(event);
|
|
@@ -152,17 +148,17 @@ const response = await Orders.find("hello", "world");
|
|
|
152
148
|
console.log(response);
|
|
153
149
|
```
|
|
154
150
|
|
|
155
|
-
Now let's go to our server application and call the `
|
|
151
|
+
Now let's go to our server application and call the `this.emit(event_name, data)` method to emit a websocket event that can be received by its corresponding Clients. Below, notice that we've added `this.emit("new_user", { message:"new_user event test" })` at the end of the `Users.add` method, so the `new_user` event will be emitted every time this method is called. The `this` value of a **ServerModule** method will always be scoped to the **ServerModule** itself.
|
|
156
152
|
|
|
157
153
|
```javascript
|
|
158
154
|
const { Service } = require("systemlynx");
|
|
159
155
|
|
|
160
156
|
const Users = {};
|
|
161
157
|
|
|
162
|
-
Users.add = function (data
|
|
158
|
+
Users.add = function (data) {
|
|
163
159
|
console.log(data);
|
|
164
|
-
|
|
165
|
-
|
|
160
|
+
return { message: "You have successfully called the Users.add method" };
|
|
161
|
+
this.emit("new_user", { message: "new_user event test" });
|
|
166
162
|
};
|
|
167
163
|
|
|
168
164
|
Service.ServerModule("Users", Users);
|
|
@@ -170,9 +166,9 @@ Service.ServerModule("Users", Users);
|
|
|
170
166
|
Service.ServerModule("Orders", function () {
|
|
171
167
|
const Orders = this;
|
|
172
168
|
|
|
173
|
-
Orders.find = function (arg1, arg2
|
|
169
|
+
Orders.find = function (arg1, arg2) {
|
|
174
170
|
console.log(data);
|
|
175
|
-
|
|
171
|
+
return { message: "You have successfully called the Orders.find method" };
|
|
176
172
|
};
|
|
177
173
|
});
|
|
178
174
|
|
package/package.json
CHANGED
|
@@ -10,5 +10,5 @@ module.exports = async function loadModules(system) {
|
|
|
10
10
|
system.Service.ServerModule(name, __constructor)
|
|
11
11
|
);
|
|
12
12
|
if (system.routing) await system.Service.startService(system.routing);
|
|
13
|
-
system.App.emit("
|
|
13
|
+
system.App.emit("ready", system);
|
|
14
14
|
};
|
|
@@ -45,7 +45,7 @@ describe("App: Loading Services", () => {
|
|
|
45
45
|
await new Promise((resolve) => {
|
|
46
46
|
const App = AppFactory();
|
|
47
47
|
App.loadService("test", `http://localhost:${port}/${route}`).on(
|
|
48
|
-
"
|
|
48
|
+
"ready",
|
|
49
49
|
(system) => {
|
|
50
50
|
expect(system.Services[0]).to.be.an("object");
|
|
51
51
|
|
|
@@ -151,7 +151,7 @@ describe("App: Loading Services", () => {
|
|
|
151
151
|
.that.respondsTo("resetConnection");
|
|
152
152
|
resolve();
|
|
153
153
|
})
|
|
154
|
-
.on("
|
|
154
|
+
.on("ready", resolve);
|
|
155
155
|
});
|
|
156
156
|
});
|
|
157
157
|
});
|
|
@@ -189,7 +189,7 @@ describe("App SystemObjects: Initializing Modules, ServerModules and configurat
|
|
|
189
189
|
const url = `http://localhost:${port}/${route}`;
|
|
190
190
|
|
|
191
191
|
await new Promise((resolve) =>
|
|
192
|
-
App.startService({ route, port }).on("
|
|
192
|
+
App.startService({ route, port }).on("ready", resolve)
|
|
193
193
|
);
|
|
194
194
|
const connData = await HttpClient.request({ url });
|
|
195
195
|
|
|
@@ -223,7 +223,7 @@ describe("App SystemObjects: Initializing Modules, ServerModules and configurat
|
|
|
223
223
|
this.test = () => {};
|
|
224
224
|
this.test2 = () => {};
|
|
225
225
|
})
|
|
226
|
-
.on("
|
|
226
|
+
.on("ready", resolve)
|
|
227
227
|
);
|
|
228
228
|
|
|
229
229
|
const connData = await HttpClient.request({ url });
|
|
@@ -253,7 +253,7 @@ describe("App SystemObjects: Initializing Modules, ServerModules and configurat
|
|
|
253
253
|
]);
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
-
it('should be able to use App.on("
|
|
256
|
+
it('should be able to use App.on("ready", callback) fire a callback when App initialization is complete', async () => {
|
|
257
257
|
const App = AppFactory();
|
|
258
258
|
|
|
259
259
|
App.ServerModule("mod", function () {
|
|
@@ -265,7 +265,7 @@ describe("App SystemObjects: Initializing Modules, ServerModules and configurat
|
|
|
265
265
|
});
|
|
266
266
|
|
|
267
267
|
await new Promise((resolve) =>
|
|
268
|
-
App.on("
|
|
268
|
+
App.on("ready", (system) => {
|
|
269
269
|
expect(system)
|
|
270
270
|
.to.be.an("object")
|
|
271
271
|
.that.has.all.keys(
|
|
@@ -303,7 +303,7 @@ describe("App SystemObjects: Initializing Modules, ServerModules and configurat
|
|
|
303
303
|
});
|
|
304
304
|
|
|
305
305
|
await new Promise((resolve) =>
|
|
306
|
-
App.on("
|
|
306
|
+
App.on("ready", ({ configurations }) => {
|
|
307
307
|
expect(configurations)
|
|
308
308
|
.to.be.an("object")
|
|
309
309
|
.that.has.all.keys("module", "__constructor")
|
|
@@ -349,6 +349,6 @@ describe("SystemObjects", () => {
|
|
|
349
349
|
this.configPassed = true;
|
|
350
350
|
next();
|
|
351
351
|
});
|
|
352
|
-
return new Promise((resolve) => App.on("
|
|
352
|
+
return new Promise((resolve) => App.on("ready", () => resolve()));
|
|
353
353
|
});
|
|
354
354
|
});
|
|
@@ -16,10 +16,7 @@ module.exports = function SystemLynxServerManager() {
|
|
|
16
16
|
useREST: false,
|
|
17
17
|
useService: true,
|
|
18
18
|
staticRouting: false,
|
|
19
|
-
validateArgs: true,
|
|
20
19
|
middleware: [],
|
|
21
|
-
useCallbacks: true,
|
|
22
|
-
useReturnValues: false,
|
|
23
20
|
};
|
|
24
21
|
const server = SystemLynxServer();
|
|
25
22
|
const router = SystemLynxRouter(server, () => serverConfigurations);
|