mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-06-27 21:05:53 +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>
|
||||
|
||||
<rich-feed
|
||||
url="https://bsky.app/FFZ_SPECIAL_FEED::stendec.dev"
|
||||
url="https://bsky-feed.special.frankerfacez.com/user::stendec.dev"
|
||||
:context="context"
|
||||
/>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
import {EventEmitter} from 'utilities/events';
|
||||
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 SettingsManager from '.';
|
||||
import type SettingsProfile from './profile';
|
||||
|
|
|
@ -762,10 +762,13 @@ export default class SettingsManager extends Module<'settings', SettingsEvents>
|
|||
old_provider.disableEvents();
|
||||
|
||||
// When transfering, we clear all existing settings.
|
||||
await new_provider.clear();
|
||||
new_provider.clear();
|
||||
if ( new_provider instanceof AdvancedSettingsProvider && new_provider.supportsBlobs )
|
||||
await new_provider.clearBlobs();
|
||||
|
||||
// Wait for it to do that.
|
||||
await new_provider.flush();
|
||||
|
||||
for(const [key,val] of old_provider.entries())
|
||||
new_provider.set(key, val);
|
||||
|
||||
|
@ -1128,14 +1131,16 @@ export default class SettingsManager extends Module<'settings', SettingsEvents>
|
|||
// Context Helpers
|
||||
// ========================================================================
|
||||
|
||||
context(env: ContextData) { return this.main_context.context(env) }
|
||||
context(env: ContextData) {
|
||||
return this.main_context.context(env);
|
||||
}
|
||||
|
||||
get<
|
||||
K extends AllSettingsKeys,
|
||||
TValue = SettingType<K>
|
||||
>(
|
||||
key: K
|
||||
): TValue { return this.main_context.get(key); }
|
||||
>(key: K): TValue {
|
||||
return this.main_context.get(key);
|
||||
}
|
||||
|
||||
getChanges<
|
||||
K extends SettingsKeys,
|
||||
|
|
|
@ -1,32 +1,38 @@
|
|||
'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
|
||||
// ============================================================================
|
||||
|
||||
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)
|
||||
if ( profile.has(key) )
|
||||
return [
|
||||
profile.get(key),
|
||||
profile.get(key) as T,
|
||||
[profile.id]
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const object_merge = {
|
||||
get(key, profiles, log) {
|
||||
const values = [],
|
||||
sources = [];
|
||||
export const object_merge: SettingsTypeHandler = {
|
||||
get<T>(key: string, profiles: SettingsProfile[], definition: SettingDefinition<any>, log: Logger) {
|
||||
const values: T[] = [],
|
||||
sources: number[] = [];
|
||||
|
||||
for(const profile of profiles)
|
||||
if ( profile.has(key) ) {
|
||||
const val = profile.get(key);
|
||||
if ( typeof val !== 'object' ) {
|
||||
const val = profile.get<T>(key);
|
||||
if ( ! val || typeof val !== 'object' ) {
|
||||
log.warn(`Profile #${profile.id} has an invalid value for "${key}" of type ${typeof val}. Skipping.`);
|
||||
continue;
|
||||
}
|
||||
|
@ -44,14 +50,16 @@ export const object_merge = {
|
|||
}
|
||||
|
||||
|
||||
export const basic_array_merge = {
|
||||
get(key, profiles, log) {
|
||||
const values = [],
|
||||
sources = [];
|
||||
type UnwrapArray<T> = T extends Array<infer U> ? U : T;
|
||||
|
||||
export const basic_array_merge: SettingsTypeHandler = {
|
||||
get<T>(key: string, profiles: SettingsProfile[], definition: SettingDefinition<any>, log: Logger) {
|
||||
const values: UnwrapArray<T>[] = [],
|
||||
sources: number[] = [];
|
||||
|
||||
for(const profile of profiles)
|
||||
if ( profile.has(key) ) {
|
||||
const val = profile.get(key);
|
||||
const val = profile.get<UnwrapArray<T>>(key);
|
||||
if ( ! Array.isArray(val) ) {
|
||||
log.warn(`Profile #${profile.id} has an invalid value for "${key}"`);
|
||||
continue;
|
||||
|
@ -71,7 +79,7 @@ export const basic_array_merge = {
|
|||
}
|
||||
|
||||
|
||||
export const array_merge = {
|
||||
export const array_merge: SettingsTypeHandler = {
|
||||
default(val) {
|
||||
const values = [];
|
||||
for(const v of val)
|
||||
|
@ -81,13 +89,20 @@ export const array_merge = {
|
|||
return values;
|
||||
},
|
||||
|
||||
get(key, profiles, definition, log, ctx) {
|
||||
const values = [],
|
||||
sources = [];
|
||||
let trailing = [];
|
||||
get<T>(
|
||||
key: string,
|
||||
profiles: SettingsProfile[],
|
||||
definition: SettingDefinition<any>,
|
||||
log: Logger,
|
||||
ctx: SettingsContext
|
||||
) {
|
||||
|
||||
const values: UnwrapArray<T>[] = [],
|
||||
sources: number[] = [];
|
||||
let trailing: UnwrapArray<T>[] = [];
|
||||
let had_value = false;
|
||||
|
||||
let profs = profiles;
|
||||
let profs: (SettingsProfile | typeof DEFAULT)[] = profiles;
|
||||
if ( definition.inherit_default )
|
||||
profs = [...profiles, DEFAULT];
|
||||
|
||||
|
@ -109,7 +124,7 @@ export const array_merge = {
|
|||
continue;
|
||||
}
|
||||
|
||||
const trail = [];
|
||||
const trail: UnwrapArray<T>[] = [];
|
||||
|
||||
if ( profile !== DEFAULT )
|
||||
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 { FilterData } from "../utilities/filtering";
|
||||
import type Logger from "../utilities/logging";
|
||||
import type { PathNode } from "../utilities/path-parser";
|
||||
import type { ExtractSegments, ExtractType, JoinKeyPaths, ObjectKeyPaths, OptionalPromise, OptionallyCallable, RecursivePartial, SettingsTypeMap } from "../utilities/types";
|
||||
import type SettingsContext from "./context";
|
||||
import type SettingsProfile from "./profile";
|
||||
import type { SettingsProvider } from "./providers";
|
||||
|
||||
|
||||
|
@ -103,7 +105,7 @@ export type SettingMetadata = {
|
|||
|
||||
export type SettingDefinition<T> = {
|
||||
|
||||
default: T,
|
||||
default: ((ctx: SettingsContext) => T) | T,
|
||||
type?: string;
|
||||
|
||||
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[];
|
||||
requires?: string[];
|
||||
|
||||
always_inherit?: boolean;
|
||||
inherit_default?: boolean;
|
||||
|
||||
// Tracking
|
||||
__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
|
||||
|
||||
export type SettingProcessor<T> = (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue