DEV Community

SudoJunior
SudoJunior

Posted on

[RANDOM] Circular class recurrence

no actual content just code

// file: ./src/classes/User/index.js
const { r } = require('rethinkdb-ts');

/**
 * @typedef {import('discord.js').User} DiscordUser
 */

// [internal]
const Admin = require('./Admin.js');

class User {
  /**
   * @param {DiscordUser} user
   */
  constructor(user) {
    this._client = user.client
    this.user = user;
  }

  get query() {
    return r.table('users').get(this.user.id);
  }

  get r() {
    return r;
  }

  get admin() {
    return new Admin(this.user);
  }
}

module.exports = User;
Enter fullscreen mode Exit fullscreen mode
// file: ./src/classes/User/Admin.js

/**
 * @typedef {import('discord.js').User} DiscordUser
 */

class Admin {
  /**
   * @param {DiscordUser} user
   */
  constructor(user) {
    this._client = user.client;
    this.user = user;
  }

  get _() {
    let Seed = require('./');
    return new Seed(this.user);
  }
}

module.exports = Admin;
Enter fullscreen mode Exit fullscreen mode

efficient? f*ck no
cool? yea, sure

what does it do?
(in short) it is class file recurrence, where the Admin class is getting the 'seed' file to return to its intended state.
(ref to discord.js) the user class instance is passed through both constructors therefore allowing for deeper recurrence.
this is not singleton programming, if it was i would have passed the class instance through... but i couldn't figure that one out at the time.

Updated 2019/03/21 13:13 GMT London
Replaced module support for rethinkdb-ts for better type resolving.

Latest comments (0)