From b43a96177f13a4438dae2c1f1c8319920d290f40 Mon Sep 17 00:00:00 2001 From: dperolio Date: Sun, 27 Feb 2022 12:03:26 -0500 Subject: [PATCH] clean up core a bit and add deprecation messages for `vizality` and `discord` --- .eslintrc.json | 7 +- injector/preload.js | 3 +- renderer/src/index.js | 167 +++++++++++++++++++++++------------------- 3 files changed, 101 insertions(+), 76 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index cee45036..a4a08084 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,10 +1,15 @@ { "globals": { + // @deprecated "vizality": true, + // @deprecated + "discord": true, + "$discord": true, "$vz": true, "webpackChunkdiscord_app": true, "DiscordNative": false, - "GLOBAL_ENV": true + "GLOBAL_ENV": true, + "_": true }, "parserOptions": { "ecmaVersion": 2020, diff --git a/injector/preload.js b/injector/preload.js index 0687718a..63480548 100644 --- a/injector/preload.js +++ b/injector/preload.js @@ -82,8 +82,9 @@ window.vizality = new Vizality(); * Expose some global objects */ exposeGlobal('vizality', true); -exposeGlobal('$vz', true); exposeGlobal('discord', true); +exposeGlobal('$vz', true); +exposeGlobal('$discord', true); exposeGlobal('require', true); // https://github.com/electron/electron/issues/9047 diff --git a/renderer/src/index.js b/renderer/src/index.js index f37ac31a..8aa3163d 100644 --- a/renderer/src/index.js +++ b/renderer/src/index.js @@ -4,11 +4,13 @@ * @namespace Vizality */ +import { log as _log, warn as _warn, error as _error, deprecate as _deprecate } from '@vizality/util/logger'; import { initialize as initializeWebpackModules, getModule } from '@vizality/webpack'; -import { log as _log, warn as _warn, error as _error } from '@vizality/util/logger'; import { Directories, Developers, Events, Protocols } from '@vizality/constants'; +import { sleep } from '@vizality/util/time'; import { resolveCompiler } from '@vizality/compilers'; import { createElement } from '@vizality/util/dom'; +import { toPlural } from '@vizality/util/string'; import { isArray } from '@vizality/util/array'; import { Updatable } from '@vizality/entities'; import { debounce } from 'lodash'; @@ -86,55 +88,27 @@ export default class Vizality extends Updatable { */ const Flux = await getModule('Store', 'PersistedStore', true); Flux.connectStoresAsync = (stores, fn) => Component => - require('@vizality/components').AsyncComponent.from((async () => { + import('./components').AsyncComponent.from((async () => { const awaitedStores = await Promise.all(stores); - // @todo Remember to add these to @discord/settings module (darkSiderbar, etc.): awaitedStores return Flux.connectStores(awaitedStores, props => fn(awaitedStores, props))(Component); })()); /** * Instantiate the managers. - * @note We're doing this down here, and uglily so we can use webpack modules and - * Vizality's components module inside the managers. + * @note We're doing this down here so that we can utilize webpack modules and + * components inside of the managers. */ this.manager = {}; - const CommunityManager = require('./managers/Community').default; - const BuiltinManager = require('./managers/Builtin').default; - const PluginManager = require('./managers/Plugin').default; - const ThemeManager = require('./managers/Theme').default; - const APIManager = require('./managers/API').default; - this.manager.community = new CommunityManager(); - this.manager.builtins = new BuiltinManager(); - this.manager.plugins = new PluginManager(); - this.manager.themes = new ThemeManager(); - this.manager.apis = new APIManager(); + const managers = [ 'API', 'Builtin', 'Plugin', 'Theme', 'Community' ]; + for (const manager of managers) { + /** + * Make the manager names on the global object plural, except for Community. + */ + const formatted = manager === 'Community' ? manager : toPlural(manager); + this.manager[formatted.toLowerCase()] = new (await import(`./managers/${manager}`))(); + } - /** - * Time to start Vizality. - */ - const before = performance.now(); - const startVizality = () => { - return new Promise(resolve => { - resolve(this.start()); - }); - }; - await startVizality() - .then(() => { - const after = performance.now(); - const time = parseFloat((after - before).toFixed()).toString().replace(/^0+/, '') || 0; - /** - * Let's format the milliseconds to seconds. - */ - let formattedTime = Math.round((time / 1000 + Number.EPSILON) * 100) / 100; - /** - * If it ends up being so fast that it rounds to 0, let's show formatting - * to 3 decimal places, otherwise show 2 decimal places. - */ - if (formattedTime === 0) { - formattedTime = Math.round((time / 1000 + Number.EPSILON) * 1000) / 1000; - } - return this.log(`Vizality loaded. Startup took ${formattedTime} seconds!`); - }); + await this.start(); /** * Get and assign the newly updated git info. @@ -150,6 +124,17 @@ export default class Vizality extends Updatable { setImmediate(() => tokenModule.showToken()); } + /** + * Enables/disables Discord Experiments. + */ + if (this.settings.get('discordExperiments', false)) { + const experimentsModule = getModule(user => user.isDeveloper !== void 0); + Object.defineProperty(experimentsModule, 'isDeveloper', { + get: () => true, + configurable: true + }); + } + /** * Trigger an event indicating that Vizality has been initialized. */ @@ -169,6 +154,33 @@ export default class Vizality extends Updatable { console.clear(); console.log('%c ', `background: url('${Protocols.ASSETS}/images/console-banner.gif') no-repeat center / contain; padding: 110px 350px; font-size: 1px; margin: 10px 0;`); + /** + * Set up the modules for the global vizality object. + */ + this.modules = {}; + const modules = await import('./modules'); + Object.assign(this.modules, modules); + + /** + * Set up a shorthand for Vizality's Discord module. + * Make sure it doesn't exist already, just in case Discord ever uses the same global namespace itself. + */ + if (!window.$discord) { + const discord = await import('./modules/discord'); + window.$discord = Object.assign({}, discord); + window.$discord.constants = Object.assign({}, { ...window.$discord.constants, ...window.$discord.constants.default }); + } + + /** + * Perform some cleanup, removing unnecessary properties. + */ + delete this.modules.discord; + delete window.$discord.constants.default; + delete window.$discord.default; + + this.deprecate('The global namespace object "discord" will be removed on May 24, 2022. Please use "$discord" instead.'); + this.deprecate('The global namespace object "vizality" will be removed on May 24, 2022. Please use "$vz" instead. "$vz" has a slightly different data structure, but provides the same information.'); + /** * Initialize the APIs. */ @@ -176,60 +188,53 @@ export default class Vizality extends Updatable { await this.manager.apis.initialize(); /** - * Set up and initialize Vizality's core settings. + * Set up a shorthand vizality global object with the namespace $vz. */ - this.settings = this.api.settings._buildCategoryObject('settings'); + window.$vz = Object.assign({}, this.manager, this.api, this.modules); /** - * Trigger an event indicating that Vizality's settings are ready. + * Set up and initialize Vizality's core settings. */ - this.emit(Events.VIZALITY_SETTINGS_READY); + this.settings = this.api.settings._buildCategoryObject('settings'); /** * Check if the current user is a Vizality Developer. + * @note This is going before the settings ready event below, because we check this in + * the Icon component after settings ready event has triggered. */ - const currentUserId = (await import('@discord/user')).getCurrentUser()?.id; - if (Developers.some(developer => developer.id === currentUserId)) { - this.settings.set('developer', true); - } else { - this.settings.set('developer', false); - } - /** - * Inject core Vizality styles. + /* + * while (!$discord.users.getCurrentUser()?.id) { + * sleep(1); + * } */ - this._injectCoreStyles(); - /** - * Patch Discord's stylized console logs. - * @note This has to be after settings have been initialized. + this.settings.set('developer', true); + /* + * console.log($discord.users.getCurrentUser()?.id); + * if (Developers.some(developer => developer.id === $discord.users.getCurrentUser()?.id)) { + * console.log('okay'); + * this.settings.set('developer', true); + * } else { + * this.settings.set('developer', false); + * } */ - this._patchDiscordLogs(); /** - * Set up the modules for the global vizality object. + * Trigger an event indicating that Vizality's settings are ready. */ - this.modules = {}; - const modules = await import('@vizality/modules'); - for (const mdl of Object.keys(modules)) { - Object.assign(this.modules, { [mdl]: modules[mdl] }); - } + this.emit(Events.VIZALITY_SETTINGS_READY); /** - * Set up a shorthand vizality global object with the namespace $vz. + * Inject core Vizality styles. */ - window.$vz = Object.assign({}, this.manager, this.api, this.modules); + this._injectCoreStyles(); /** - * Set up a shorthand for Vizality's Discord module. - * Make sure it doesn't exist already, just in case Discord ever uses the same - * global namespace itself. + * Patch Discord's stylized console logs. + * @note This has to be after settings have been initialized. */ - if (!window.discord) { - const discordModule = await import('@discord'); - window.discord = Object.assign({}, discordModule); - delete window.discord.default; - } + this._patchDiscordLogs(); /** * Initialize builtins, plugins, and themes. @@ -476,4 +481,18 @@ export default class Vizality extends Updatable { } return _error({ labels: this._labels, message }); } + + /** + * + * @param {any} message Message to log + * @returns {void} + */ + deprecate (...message) { + // In case the addon wants to provide their own labels + if (isArray(message[0])) { + const _message = message.slice(1); + return _deprecate({ labels: message[0], message: _message }); + } + return _deprecate({ labels: this._labels, message }); + } }