@ -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 us e webpack modules and
* Vizality' s components module inside the managers .
* @ note We ' re doing this down here so that we can utiliz e 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 ) {
/ * *
* Time to start Vizal ity.
* Make the manager names on the global object plural , except for Community .
* /
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 ;
const formatted = manager === 'Community' ? manager : toPlural ( manager ) ;
this . manager [ formatted . toLowerCase ( ) ] = new ( await import ( ` ./managers/ ${ manager } ` ) ) ( ) ;
}
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 a nd 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 } ) ;
}
}