diff options
author | 2021-09-26 10:38:53 +0530 | |
---|---|---|
committer | 2021-09-26 12:08:53 +0700 | |
commit | 17d55029b7224038a2daaecf524ce5ec9f5c9ef6 (patch) | |
tree | 0482edd616d4d11663a53aa7a8484eda72108be5 /source/features/index.tsx | |
parent | fdd2e33dbc61675685eaa914f869c955a305764c (diff) | |
download | refined-github-17d55029b7224038a2daaecf524ce5ec9f5c9ef6.tar.gz refined-github-17d55029b7224038a2daaecf524ce5ec9f5c9ef6.tar.zst refined-github-17d55029b7224038a2daaecf524ce5ec9f5c9ef6.zip |
Meta: Add an `asLongAs` option to `features.add()` (#4800)
Co-authored-by: yakov116 <16872793+yakov116@users.noreply.github.com>
Co-authored-by: Federico Brigante <me@fregante.com>
Diffstat (limited to 'source/features/index.tsx')
-rw-r--r-- | source/features/index.tsx | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/source/features/index.tsx b/source/features/index.tsx index 32ab7e3c..e017197c 100644 --- a/source/features/index.tsx +++ b/source/features/index.tsx @@ -8,6 +8,7 @@ import * as pageDetect from 'github-url-detection'; import waitFor from '../helpers/wait-for'; import onNewComments from '../github-events/on-new-comments'; import bisectFeatures from '../helpers/bisect'; +import {shouldFeatureRun} from '../github-helpers'; import optionsStorage, {RGHOptions} from '../options-storage'; import {getLocalHotfixesAsOptions, updateHotfixes} from '../helpers/hotfix'; @@ -34,8 +35,9 @@ interface FeatureLoader extends Partial<InternalRunConfig> { } interface InternalRunConfig { - include: BooleanFunction[]; - exclude: BooleanFunction[]; + asLongAs: BooleanFunction[] | undefined; + include: BooleanFunction[] | undefined; + exclude: BooleanFunction[] | undefined; init: FeatureInit; deinit?: VoidFunction | VoidFunction[]; additionalListeners: CallerFunction[]; @@ -138,10 +140,9 @@ const globalReady: Promise<RGHOptions> = new Promise(async resolve => { }); const setupPageLoad = async (id: FeatureID, config: InternalRunConfig): Promise<void> => { - const {include, exclude, init, deinit, additionalListeners, onlyAdditionalListeners} = config; + const {asLongAs, include, exclude, init, deinit, additionalListeners, onlyAdditionalListeners} = config; - // If every `include` is false and no `exclude` is true, don’t run the feature - if (include.every(c => !c()) || exclude.some(c => c())) { + if (!shouldFeatureRun({asLongAs, include, exclude})) { return; } @@ -192,7 +193,7 @@ function enforceDefaults( additionalListeners: InternalRunConfig['additionalListeners'], ): void { for (const [detection, listener] of defaultPairs) { - if (!include.includes(detection)) { + if (!include?.includes(detection)) { continue; } @@ -218,8 +219,9 @@ const add = async (id: FeatureID, ...loaders: FeatureLoader[]): Promise<void> => // Input defaults and validation const { shortcuts = {}, - include = [() => true], // Default: every page - exclude = [], // Default: nothing + asLongAs, + include, + exclude, init, deinit, awaitDomReady = true, @@ -234,13 +236,13 @@ const add = async (id: FeatureID, ...loaders: FeatureLoader[]): Promise<void> => } // 404 pages should only run 404-only features - if (pageDetect.is404() && !include.includes(pageDetect.is404)) { + if (pageDetect.is404() && !include?.includes(pageDetect.is404) && !asLongAs?.includes(pageDetect.is404)) { continue; } enforceDefaults(id, include, additionalListeners); - const details = {include, exclude, init, deinit, additionalListeners, onlyAdditionalListeners}; + const details = {asLongAs, include, exclude, init, deinit, additionalListeners, onlyAdditionalListeners}; if (awaitDomReady) { (async () => { await domLoaded; @@ -258,7 +260,7 @@ const add = async (id: FeatureID, ...loaders: FeatureLoader[]): Promise<void> => } }; -const addCssFeature = async (id: FeatureID, include: BooleanFunction[], deduplicate?: false | string): Promise<void> => { +const addCssFeature = async (id: FeatureID, include: BooleanFunction[] | undefined, deduplicate?: false | string): Promise<void> => { void add(id, { include, deduplicate, |