DEV: Upgrade modifyClass syntax to remove object-literal decorators (#50)

Decorators on object literal properties are unsupported in most modern JS tooling, including ts/glint and Prettier 3.0. Using the new native-class-based modifyClass syntax means we can use decorators safely
This commit is contained in:
David Taylor 2025-03-05 10:36:25 +00:00 committed by GitHub
parent 63f9de0119
commit 5f12e99dd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,81 +42,84 @@ export default {
}, },
}); });
api.modifyClass("controller:users", { api.modifyClass(
pluginId: "user-card-directory", "controller:users",
cachedUserCardInfo: null, (Superclass) =>
class extends Superclass {
init() { init() {
this.set("cachedUserCardInfo", {}); this.set("cachedUserCardInfo", {});
this._super(...arguments); super.init(...arguments);
},
@discourseComputed("model.content.@each")
userCards(allUsers) {
if (!allUsers) {
return [];
}
const toLoad = [];
if (settings.hide_current_user && this.currentUser) {
allUsers = allUsers.filter((u) => u.id !== this.currentUser.id);
}
const userCardInfos = allUsers.map((u) => {
if (this.cachedUserCardInfo[u.id]) {
return this.cachedUserCardInfo[u.id];
} }
const userCardInfo = (this.cachedUserCardInfo[u.id] = @discourseComputed("model.content.@each")
EmberObject.create({ userCards(allUsers) {
user: this.store.createRecord("user", u.user), if (!allUsers) {
directoryItem: u, return [];
loading: true, }
})); const toLoad = [];
if (settings.hide_current_user && this.currentUser) {
allUsers = allUsers.filter((u) => u.id !== this.currentUser.id);
}
const userCardInfos = allUsers.map((u) => {
if (this.cachedUserCardInfo[u.id]) {
return this.cachedUserCardInfo[u.id];
}
toLoad.push(userCardInfo); const userCardInfo = (this.cachedUserCardInfo[u.id] =
EmberObject.create({
user: this.store.createRecord("user", u.user),
directoryItem: u,
loading: true,
}));
return userCardInfo; toLoad.push(userCardInfo);
});
const loadMax = 50; return userCardInfo;
while (toLoad.length > 0) {
const thisBatch = toLoad.splice(0, loadMax);
const promise = ajax("/user-cards.json", {
data: { user_ids: thisBatch.map((uc) => uc.user.id).join(",") },
});
thisBatch.forEach((uc) => {
// Each user card expects its own promise
// Rather than making a separate AJAX request for each
// We re-use the `user-cards.json` promise, and manipulate the data
const convertedPromise = promise.then((data) => {
// Find the correct user from users, and put it in the user attribute
// Use Object.assign to avoid contaminating the source object
return Object.assign({}, data, {
user: data.users.find((u) => u.id === uc.user.id),
});
}); });
return uc.user
.findDetails({ existingRequest: convertedPromise }) const loadMax = 50;
.finally(() => uc.set("loading", false));
}); while (toLoad.length > 0) {
const thisBatch = toLoad.splice(0, loadMax);
const promise = ajax("/user-cards.json", {
data: {
user_ids: thisBatch.map((uc) => uc.user.id).join(","),
},
});
thisBatch.forEach((uc) => {
// Each user card expects its own promise
// Rather than making a separate AJAX request for each
// We re-use the `user-cards.json` promise, and manipulate the data
const convertedPromise = promise.then((data) => {
// Find the correct user from users, and put it in the user attribute
// Use Object.assign to avoid contaminating the source object
return Object.assign({}, data, {
user: data.users.find((u) => u.id === uc.user.id),
});
});
return uc.user
.findDetails({ existingRequest: convertedPromise })
.finally(() => uc.set("loading", false));
});
}
return userCardInfos;
}
@action
userCardShowUser(user) {
DiscourseURL.routeTo(userPath(user.username_lower));
}
@action
updateOrder(field, asc) {
this.setProperties({
order: field,
asc,
});
}
} }
);
return userCardInfos;
},
@action
userCardShowUser(user) {
DiscourseURL.routeTo(userPath(user.username_lower));
},
@action
updateOrder(field, asc) {
this.setProperties({
order: field,
asc,
});
},
});
}); });
}, },
}; };