Define settings using blueprints.

This commit is contained in:
flash 2024-04-17 17:26:33 +00:00
parent cf71bab92d
commit 58d4bfb948
3 changed files with 122 additions and 63 deletions

View file

@ -82,54 +82,54 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } };
const settings = new MamiSettings('umi-', ctx.events.scopeTo('settings')); const settings = new MamiSettings('umi-', ctx.events.scopeTo('settings'));
ctx.settings = settings; ctx.settings = settings;
settings.define('style', 'string', 'dark'); settings.define('style').default('dark').create();
settings.define('compactView', 'boolean', false); settings.define('compactView').default(false).create();
settings.define('autoScroll', 'boolean', true); settings.define('autoScroll').default(true).create();
settings.define('closeTabConfirm', 'boolean', false); settings.define('closeTabConfirm').default(false).create();
settings.define('showChannelList', 'boolean', false); settings.define('showChannelList').default(false).create();
settings.define('fancyInfo', 'boolean', true); settings.define('fancyInfo').default(true).create();
settings.define('autoCloseUserContext', 'boolean', true); settings.define('autoCloseUserContext').default(true).create();
settings.define('enableParser', 'boolean', true); settings.define('enableParser').default(true).create();
settings.define('enableEmoticons', 'boolean', true); settings.define('enableEmoticons').default(true).create();
settings.define('autoParseUrls', 'boolean', true); settings.define('autoParseUrls').default(true).create();
settings.define('preventOverflow', 'boolean', false); settings.define('preventOverflow').default(false).create();
settings.define('expandTextBox', 'boolean', false); settings.define('expandTextBox').default(false).create();
settings.define('eepromAutoInsert', 'boolean', true); settings.define('eepromAutoInsert').default(true).create();
settings.define('autoEmbedV1', 'boolean', false); settings.define('autoEmbedV1').default(false).create();
settings.define('soundEnable', 'boolean', true, false, true); settings.define('soundEnable').default(true).critical().create();
settings.define('soundPack', 'string', 'ajax-chat'); settings.define('soundPack').default('ajax-chat').create();
settings.define('soundVolume', 'number', 80); settings.define('soundVolume').default(80).create();
settings.define('soundEnableJoin', 'boolean', true); settings.define('soundEnableJoin').default(true).create();
settings.define('soundEnableLeave', 'boolean', true); settings.define('soundEnableLeave').default(true).create();
settings.define('soundEnableError', 'boolean', true); settings.define('soundEnableError').default(true).create();
settings.define('soundEnableServer', 'boolean', true); settings.define('soundEnableServer').default(true).create();
settings.define('soundEnableIncoming', 'boolean', true); settings.define('soundEnableIncoming').default(true).create();
settings.define('onlySoundOnMention', 'boolean', false); settings.define('onlySoundOnMention').default(false).create();
settings.define('soundEnableOutgoing', 'boolean', true); settings.define('soundEnableOutgoing').default(true).create();
settings.define('soundEnablePrivate', 'boolean', true); settings.define('soundEnablePrivate').default(true).create();
settings.define('soundEnableForceLeave', 'boolean', true); settings.define('soundEnableForceLeave').default(true).create();
settings.define('minecraft', ['no', 'yes', 'old'], 'no'); settings.define('minecraft').type(['no', 'yes', 'old']).default('no').create();
settings.define('windowsLiveMessenger', 'boolean', false); settings.define('windowsLiveMessenger').default(false).create();
settings.define('seinfeld', 'boolean', false); settings.define('seinfeld').default(false).create();
settings.define('flashTitle', 'boolean', true); settings.define('flashTitle').default(true).create();
settings.define('showServerMsgInTitle', 'boolean', true); settings.define('showServerMsgInTitle').default(true).create();
settings.define('onlyConnectWhenVisible', 'boolean', true); settings.define('onlyConnectWhenVisible').default(true).create();
settings.define('playJokeSounds', 'boolean', true); settings.define('playJokeSounds').default(true).create();
settings.define('weeaboo', 'boolean', false); settings.define('weeaboo').default(false).create();
settings.define('motivationalImages', 'boolean', false); settings.define('motivationalImages').default(false).create();
settings.define('motivationalVideos', 'boolean', false); settings.define('motivationalVideos').default(false).create();
settings.define('osuKeys', 'boolean', false); settings.define('osuKeys').default(false).create();
settings.define('osuKeysV2', ['no', 'yes', 'rng'], 'no'); settings.define('osuKeysV2').type(['no', 'yes', 'rng']).default('no').create();
settings.define('explosionRadius', 'number', 20); settings.define('explosionRadius').default(20).create();
settings.define('dumpPackets', 'boolean', FUTAMI_DEBUG); settings.define('dumpPackets').default(FUTAMI_DEBUG).create();
settings.define('dumpEvents', 'boolean', FUTAMI_DEBUG); settings.define('dumpEvents').default(FUTAMI_DEBUG).create();
settings.define('marqueeAllNames', 'boolean', false); settings.define('marqueeAllNames').default(false).create();
settings.define('tmpDisableOldThemeSys', 'boolean', false, false, true); settings.define('tmpDisableOldThemeSys').default(false).critical().create();
const noNotifSupport = !('Notification' in window); const noNotifSupport = !('Notification' in window);
settings.define('enableNotifications', 'boolean', false, noNotifSupport, true); settings.define('enableNotifications').default(false).immutable(noNotifSupport).critical().create();
settings.define('notificationShowMessage', 'boolean', false, noNotifSupport); settings.define('notificationShowMessage').default(false).immutable(noNotifSupport).create();
settings.define('notificationTriggers', 'string', '', noNotifSupport); settings.define('notificationTriggers').default('').immutable(noNotifSupport).create();
loadingOverlay.setMessage('Loading sounds...'); loadingOverlay.setMessage('Loading sounds...');

View file

@ -10,7 +10,7 @@ const MamiSettingsScoped = function(settings, prefix) {
prefix += ':'; prefix += ':';
return { return {
define: (name, type, fallback, immutable, critical) => settings.define(prefix + name, type, fallback, immutable, critical), define: name => settings.define(prefix + name),
info: name => settings.info(prefix + name), info: name => settings.info(prefix + name),
names: () => { names: () => {
const filtered = []; const filtered = [];

View file

@ -108,26 +108,85 @@ const MamiSettings = function(storageOrPrefix, eventTarget) {
} }
}; };
const pub = { const settingBlueprint = function(name) {
define: (name, type, fallback, immutable, critical, virtual) => {
if(typeof name !== 'string') if(typeof name !== 'string')
throw 'setting name must be a string'; throw 'setting name must be a string';
if(typeof type !== 'string' && !Array.isArray(type))
throw 'type must be a javascript type or array of valid string values.'; const checkDefined = () => {
if(settings.has(name)) if(settings.has(name))
throw `setting ${name} has already been defined`; throw `setting ${name} has already been defined`;
};
checkDefined();
let created = false;
let type = undefined;
let fallback = null;
let immutable = false;
let critical = false;
let virtual = false;
const checkCreated = () => {
if(created)
throw 'setting has already been created';
};
const pub = {
type: value => {
if(typeof value !== 'string' && !Array.isArray(value))
throw 'type must be a javascript type or array of valid string values.';
checkCreated();
type = value;
return pub;
},
default: value => {
checkCreated();
fallback = value === undefined ? null : value;
if(type === undefined)
type = typeof fallback;
return pub;
},
immutable: value => {
checkCreated();
immutable = value === undefined || value === true;
return pub;
},
critical: value => {
checkCreated();
critical = value === undefined || value === true;
return pub;
},
virtual: value => {
checkCreated();
virtual = value === undefined || value === true;
return pub;
},
create: () => {
checkCreated();
checkDefined();
settings.set(name, Object.freeze({ settings.set(name, Object.freeze({
name: name, name: name,
type: type === null ? undefined : type, type: type,
fallback: fallback === undefined ? null : fallback, fallback: fallback,
immutable: immutable === true, immutable: immutable,
critical: critical === true, critical: critical,
})); }));
if(virtual === true) if(virtual)
storage.virtualise(name); storage.virtualise(name);
}, },
};
return pub;
};
const pub = {
define: name => new settingBlueprint(name),
info: name => getSetting(name), info: name => getSetting(name),
names: () => Array.from(settings.keys()), names: () => Array.from(settings.keys()),
has: name => { has: name => {