mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-09-16 10:06:54 +00:00
Finish the initial conversion of the settings module to TypeScript.
This commit is contained in:
parent
136a2491c8
commit
6c6d4ceb98
5 changed files with 81 additions and 29 deletions
|
@ -190,7 +190,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<rich-feed
|
<rich-feed
|
||||||
url="https://bsky.app/FFZ_SPECIAL_FEED::stendec.dev"
|
url="https://bsky-feed.special.frankerfacez.com/user::stendec.dev"
|
||||||
:context="context"
|
:context="context"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
import {EventEmitter} from 'utilities/events';
|
import {EventEmitter} from 'utilities/events';
|
||||||
import {has, get as getter, array_equals, set_equals, map_equals, deep_equals} from 'utilities/object';
|
import {has, get as getter, array_equals, set_equals, map_equals, deep_equals} from 'utilities/object';
|
||||||
|
|
||||||
import * as DEFINITIONS from './typehandlers';
|
import DEFINITIONS from './typehandlers';
|
||||||
import type { AllSettingsKeys, ContextData, SettingMetadata, SettingType, SettingDefinition, SettingsKeys } from './types';
|
import type { AllSettingsKeys, ContextData, SettingMetadata, SettingType, SettingDefinition, SettingsKeys } from './types';
|
||||||
import type SettingsManager from '.';
|
import type SettingsManager from '.';
|
||||||
import type SettingsProfile from './profile';
|
import type SettingsProfile from './profile';
|
||||||
|
|
|
@ -762,10 +762,13 @@ export default class SettingsManager extends Module<'settings', SettingsEvents>
|
||||||
old_provider.disableEvents();
|
old_provider.disableEvents();
|
||||||
|
|
||||||
// When transfering, we clear all existing settings.
|
// When transfering, we clear all existing settings.
|
||||||
await new_provider.clear();
|
new_provider.clear();
|
||||||
if ( new_provider instanceof AdvancedSettingsProvider && new_provider.supportsBlobs )
|
if ( new_provider instanceof AdvancedSettingsProvider && new_provider.supportsBlobs )
|
||||||
await new_provider.clearBlobs();
|
await new_provider.clearBlobs();
|
||||||
|
|
||||||
|
// Wait for it to do that.
|
||||||
|
await new_provider.flush();
|
||||||
|
|
||||||
for(const [key,val] of old_provider.entries())
|
for(const [key,val] of old_provider.entries())
|
||||||
new_provider.set(key, val);
|
new_provider.set(key, val);
|
||||||
|
|
||||||
|
@ -1128,14 +1131,16 @@ export default class SettingsManager extends Module<'settings', SettingsEvents>
|
||||||
// Context Helpers
|
// Context Helpers
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
|
|
||||||
context(env: ContextData) { return this.main_context.context(env) }
|
context(env: ContextData) {
|
||||||
|
return this.main_context.context(env);
|
||||||
|
}
|
||||||
|
|
||||||
get<
|
get<
|
||||||
K extends AllSettingsKeys,
|
K extends AllSettingsKeys,
|
||||||
TValue = SettingType<K>
|
TValue = SettingType<K>
|
||||||
>(
|
>(key: K): TValue {
|
||||||
key: K
|
return this.main_context.get(key);
|
||||||
): TValue { return this.main_context.get(key); }
|
}
|
||||||
|
|
||||||
getChanges<
|
getChanges<
|
||||||
K extends SettingsKeys,
|
K extends SettingsKeys,
|
||||||
|
|
|
@ -1,32 +1,38 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
import type Logger from "utilities/logging";
|
||||||
|
import type SettingsProfile from "./profile";
|
||||||
|
import type { SettingDefinition, SettingsTypeHandler } from "./types";
|
||||||
|
import type SettingsContext from "./context";
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Settings Types
|
// Settings Types
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
const DEFAULT = Symbol('default');
|
const DEFAULT = Symbol('default');
|
||||||
|
|
||||||
export const basic = {
|
|
||||||
get(key, profiles) {
|
export const basic: SettingsTypeHandler = {
|
||||||
|
get<T>(key: string, profiles: SettingsProfile[]) {
|
||||||
for(const profile of profiles)
|
for(const profile of profiles)
|
||||||
if ( profile.has(key) )
|
if ( profile.has(key) )
|
||||||
return [
|
return [
|
||||||
profile.get(key),
|
profile.get(key) as T,
|
||||||
[profile.id]
|
[profile.id]
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const object_merge = {
|
export const object_merge: SettingsTypeHandler = {
|
||||||
get(key, profiles, log) {
|
get<T>(key: string, profiles: SettingsProfile[], definition: SettingDefinition<any>, log: Logger) {
|
||||||
const values = [],
|
const values: T[] = [],
|
||||||
sources = [];
|
sources: number[] = [];
|
||||||
|
|
||||||
for(const profile of profiles)
|
for(const profile of profiles)
|
||||||
if ( profile.has(key) ) {
|
if ( profile.has(key) ) {
|
||||||
const val = profile.get(key);
|
const val = profile.get<T>(key);
|
||||||
if ( typeof val !== 'object' ) {
|
if ( ! val || typeof val !== 'object' ) {
|
||||||
log.warn(`Profile #${profile.id} has an invalid value for "${key}" of type ${typeof val}. Skipping.`);
|
log.warn(`Profile #${profile.id} has an invalid value for "${key}" of type ${typeof val}. Skipping.`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -44,14 +50,16 @@ export const object_merge = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const basic_array_merge = {
|
type UnwrapArray<T> = T extends Array<infer U> ? U : T;
|
||||||
get(key, profiles, log) {
|
|
||||||
const values = [],
|
export const basic_array_merge: SettingsTypeHandler = {
|
||||||
sources = [];
|
get<T>(key: string, profiles: SettingsProfile[], definition: SettingDefinition<any>, log: Logger) {
|
||||||
|
const values: UnwrapArray<T>[] = [],
|
||||||
|
sources: number[] = [];
|
||||||
|
|
||||||
for(const profile of profiles)
|
for(const profile of profiles)
|
||||||
if ( profile.has(key) ) {
|
if ( profile.has(key) ) {
|
||||||
const val = profile.get(key);
|
const val = profile.get<UnwrapArray<T>>(key);
|
||||||
if ( ! Array.isArray(val) ) {
|
if ( ! Array.isArray(val) ) {
|
||||||
log.warn(`Profile #${profile.id} has an invalid value for "${key}"`);
|
log.warn(`Profile #${profile.id} has an invalid value for "${key}"`);
|
||||||
continue;
|
continue;
|
||||||
|
@ -71,7 +79,7 @@ export const basic_array_merge = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const array_merge = {
|
export const array_merge: SettingsTypeHandler = {
|
||||||
default(val) {
|
default(val) {
|
||||||
const values = [];
|
const values = [];
|
||||||
for(const v of val)
|
for(const v of val)
|
||||||
|
@ -81,13 +89,20 @@ export const array_merge = {
|
||||||
return values;
|
return values;
|
||||||
},
|
},
|
||||||
|
|
||||||
get(key, profiles, definition, log, ctx) {
|
get<T>(
|
||||||
const values = [],
|
key: string,
|
||||||
sources = [];
|
profiles: SettingsProfile[],
|
||||||
let trailing = [];
|
definition: SettingDefinition<any>,
|
||||||
|
log: Logger,
|
||||||
|
ctx: SettingsContext
|
||||||
|
) {
|
||||||
|
|
||||||
|
const values: UnwrapArray<T>[] = [],
|
||||||
|
sources: number[] = [];
|
||||||
|
let trailing: UnwrapArray<T>[] = [];
|
||||||
let had_value = false;
|
let had_value = false;
|
||||||
|
|
||||||
let profs = profiles;
|
let profs: (SettingsProfile | typeof DEFAULT)[] = profiles;
|
||||||
if ( definition.inherit_default )
|
if ( definition.inherit_default )
|
||||||
profs = [...profiles, DEFAULT];
|
profs = [...profiles, DEFAULT];
|
||||||
|
|
||||||
|
@ -109,7 +124,7 @@ export const array_merge = {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const trail = [];
|
const trail: UnwrapArray<T>[] = [];
|
||||||
|
|
||||||
if ( profile !== DEFAULT )
|
if ( profile !== DEFAULT )
|
||||||
sources.push(profile.id);
|
sources.push(profile.id);
|
||||||
|
@ -141,3 +156,12 @@ export const array_merge = {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export default {
|
||||||
|
basic,
|
||||||
|
object_merge,
|
||||||
|
basic_array_merge,
|
||||||
|
array_merge
|
||||||
|
} as Record<string, SettingsTypeHandler>;
|
|
@ -1,8 +1,10 @@
|
||||||
import type SettingsManager from ".";
|
import type SettingsManager from ".";
|
||||||
import type { FilterData } from "../utilities/filtering";
|
import type { FilterData } from "../utilities/filtering";
|
||||||
|
import type Logger from "../utilities/logging";
|
||||||
import type { PathNode } from "../utilities/path-parser";
|
import type { PathNode } from "../utilities/path-parser";
|
||||||
import type { ExtractSegments, ExtractType, JoinKeyPaths, ObjectKeyPaths, OptionalPromise, OptionallyCallable, RecursivePartial, SettingsTypeMap } from "../utilities/types";
|
import type { ExtractSegments, ExtractType, JoinKeyPaths, ObjectKeyPaths, OptionalPromise, OptionallyCallable, RecursivePartial, SettingsTypeMap } from "../utilities/types";
|
||||||
import type SettingsContext from "./context";
|
import type SettingsContext from "./context";
|
||||||
|
import type SettingsProfile from "./profile";
|
||||||
import type { SettingsProvider } from "./providers";
|
import type { SettingsProvider } from "./providers";
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,7 +105,7 @@ export type SettingMetadata = {
|
||||||
|
|
||||||
export type SettingDefinition<T> = {
|
export type SettingDefinition<T> = {
|
||||||
|
|
||||||
default: T,
|
default: ((ctx: SettingsContext) => T) | T,
|
||||||
type?: string;
|
type?: string;
|
||||||
|
|
||||||
equals?: 'requirements' | ((new_value: T, old_value: T | undefined, cache: Map<SettingsKeys, unknown>, old_cache: Map<SettingsKeys, unknown>) => boolean);
|
equals?: 'requirements' | ((new_value: T, old_value: T | undefined, cache: Map<SettingsKeys, unknown>, old_cache: Map<SettingsKeys, unknown>) => boolean);
|
||||||
|
@ -114,6 +116,9 @@ export type SettingDefinition<T> = {
|
||||||
required_by?: string[];
|
required_by?: string[];
|
||||||
requires?: string[];
|
requires?: string[];
|
||||||
|
|
||||||
|
always_inherit?: boolean;
|
||||||
|
inherit_default?: boolean;
|
||||||
|
|
||||||
// Tracking
|
// Tracking
|
||||||
__source?: string | null;
|
__source?: string | null;
|
||||||
|
|
||||||
|
@ -205,6 +210,24 @@ export type SettingsProfileMetadata = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Type Handlers
|
||||||
|
|
||||||
|
export type SettingsTypeHandler = {
|
||||||
|
|
||||||
|
default?(input: any, definition: SettingDefinition<any>, log: Logger): any;
|
||||||
|
|
||||||
|
get(
|
||||||
|
key: string,
|
||||||
|
profiles: SettingsProfile[],
|
||||||
|
definition: SettingDefinition<any>,
|
||||||
|
log: Logger,
|
||||||
|
ctx: SettingsContext
|
||||||
|
): [unknown, number[]] | null | undefined;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Processors
|
// Processors
|
||||||
|
|
||||||
export type SettingProcessor<T> = (
|
export type SettingProcessor<T> = (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue