1
0

torture.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * JS-grammar torture fixture (javascript variant: no type machinery, JS class
  3. * fields use `field_definition` with a `property` field).
  4. */
  5. import { EventEmitter } from 'node:events';
  6. const { promisify } = require('node:util');
  7. /** Legacy prototype-style helper. */
  8. function legacyHelper(a, b) {
  9. return a + b;
  10. }
  11. const arrow = (x) => legacyHelper(x, 1);
  12. class Widget extends EventEmitter {
  13. static registry = new Map();
  14. #privateField = 1;
  15. label = 'w';
  16. onTick = () => {
  17. this.render();
  18. };
  19. wrapped = debounce(function () {
  20. expensive();
  21. }, 50);
  22. constructor(opts) {
  23. super();
  24. this.opts = opts;
  25. register(this.onTick);
  26. }
  27. render() {
  28. paint(this.label);
  29. }
  30. static create(opts) {
  31. return new Widget(opts);
  32. }
  33. }
  34. // AMD-style wrapper — anonymous, but inner functions must still surface.
  35. (function () {
  36. function hiddenInner() {
  37. return 7;
  38. }
  39. hiddenInner();
  40. })();
  41. module.exports.makeWidget = function makeWidget(opts) {
  42. return Widget.create(opts);
  43. };
  44. // Vuex module shape (store-file signals: mutations + actions + getters).
  45. const mutations = {
  46. SET_USER(state, user) {
  47. state.user = user;
  48. },
  49. };
  50. const actions = {
  51. async loadUser({ commit }, id) {
  52. const user = await fetchUser(id);
  53. commit('SET_USER', user);
  54. },
  55. };
  56. export default {
  57. namespaced: true,
  58. state: () => ({ user: null }),
  59. mutations,
  60. actions,
  61. getters: {
  62. userName(state) {
  63. return state.user?.name;
  64. },
  65. },
  66. };