Skip to content

Commit c8403f3

Browse files
Johennesrichvdh
andauthored
Fix build failure in node.js example (#4394)
* Fix node.js example Relates to: element-hq/element-web#26922 Signed-off-by: Johannes Marbach <[email protected]> * Update examples/node/app.js Co-authored-by: Richard van der Hoff <[email protected]> * Update examples/node/package.json Co-authored-by: Richard van der Hoff <[email protected]> * Move imports to the top of the file --------- Co-authored-by: Richard van der Hoff <[email protected]>
1 parent 8cf5df7 commit c8403f3

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

examples/node/app.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import clc from "cli-color";
2+
import fs from "fs";
3+
import readline from "readline";
4+
import sdk, { ClientEvent, EventType, MsgType, RoomEvent } from "matrix-js-sdk";
5+
import { KnownMembership } from "matrix-js-sdk/lib/@types/membership.js";
6+
7+
var myHomeServer = "http://localhost:8008";
18
var myUserId = "@example:localhost";
29
var myAccessToken = "QGV4YW1wbGU6bG9jYWxob3N0.qPEvLuYfNBjxikiCjP";
3-
var sdk = require("matrix-js-sdk");
4-
var clc = require("cli-color");
10+
511
var matrixClient = sdk.createClient({
6-
baseUrl: "http://localhost:8008",
12+
baseUrl: myHomeServer,
713
accessToken: myAccessToken,
814
userId: myUserId,
915
});
@@ -15,7 +21,6 @@ var numMessagesToShow = 20;
1521

1622
// Reading from stdin
1723
var CLEAR_CONSOLE = "\x1B[2J";
18-
var readline = require("readline");
1924
var rl = readline.createInterface({
2025
input: process.stdin,
2126
output: process.stdout,
@@ -97,7 +102,7 @@ rl.on("line", function (line) {
97102
})
98103
.then(function (url) {
99104
var content = {
100-
msgtype: "m.file",
105+
msgtype: MsgType.File,
101106
body: filename,
102107
url: JSON.parse(url).content_uri,
103108
};
@@ -138,7 +143,7 @@ rl.on("line", function (line) {
138143
// ==== END User input
139144

140145
// show the room list after syncing.
141-
matrixClient.on("sync", function (state, prevState, data) {
146+
matrixClient.on(ClientEvent.Sync, function (state, prevState, data) {
142147
switch (state) {
143148
case "PREPARED":
144149
setRoomList();
@@ -149,7 +154,7 @@ matrixClient.on("sync", function (state, prevState, data) {
149154
}
150155
});
151156

152-
matrixClient.on("Room", function () {
157+
matrixClient.on(ClientEvent.Room, function () {
153158
setRoomList();
154159
if (!viewingRoom) {
155160
printRoomList();
@@ -158,7 +163,7 @@ matrixClient.on("Room", function () {
158163
});
159164

160165
// print incoming messages.
161-
matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline) {
166+
matrixClient.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
162167
if (toStartOfTimeline) {
163168
return; // don't print paginated results
164169
}
@@ -305,7 +310,7 @@ function printRoomInfo(room) {
305310
print(eTypeHeader + sendHeader + contentHeader);
306311
print(new Array(100).join("-"));
307312
eventMap.keys().forEach(function (eventType) {
308-
if (eventType === "m.room.member") {
313+
if (eventType === EventType.RoomMember) {
309314
return;
310315
} // use /members instead.
311316
var eventEventMap = eventMap.get(eventType);
@@ -343,7 +348,7 @@ function printLine(event) {
343348
name = name.slice(0, maxNameWidth - 1) + "\u2026";
344349
}
345350

346-
if (event.getType() === "m.room.message") {
351+
if (event.getType() === EventType.RoomMessage) {
347352
body = event.getContent().body;
348353
} else if (event.isState()) {
349354
var stateName = event.getType();
@@ -394,4 +399,4 @@ function fixWidth(str, len) {
394399
return str;
395400
}
396401

397-
matrixClient.startClient(numMessagesToShow); // messages for each room.
402+
matrixClient.startClient({ initialSyncLimit: numMessagesToShow });

examples/node/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"main": "app.js",
66
"author": "",
77
"license": "Apache 2.0",
8+
"type": "module",
89
"dependencies": {
910
"cli-color": "^1.0.0",
10-
"matrix-js-sdk": "^32.0.0"
11+
"matrix-js-sdk": "^34.5.0"
1112
}
1213
}

0 commit comments

Comments
 (0)