|
1 | 1 | // @flow |
2 | | -// Development-related helper functions |
3 | | -// Note: none of these rely on DataStore.* functions etc., _except_ for the logging functions. However, the DataStore.settings object _is_ available in React windows/components, through @DBW's wizardry. |
| 2 | +/** |
| 3 | + * Development-related helper functions. |
| 4 | + * |
| 5 | + * Logging vs sync/async `DataStore.settings` (plugin JS vs HTML/React WebView): |
| 6 | + * - In the main plugin context, `DataStore.settings` is typically a plain object, so reading `_logLevel` for |
| 7 | + * `shouldOutputForLogLevel` is synchronous and immediate. |
| 8 | + * - In HTML/React windows, `DataStore.settings` may be thenable (Promise-like). Synchronous code cannot read |
| 9 | + * `_logLevel` until it resolves. This file uses an in-memory cache filled by a one-shot background `await` |
| 10 | + * (`getPluginSettingsForLogging`, `primePluginSettingsCacheViaAwait`). Until the cache is populated, |
| 11 | + * log gating uses the default threshold (DEBUG) so early lines are not silenced. After settings resolve, |
| 12 | + * `_logLevel` from the real object applies. If the first logs look noisier than afterward or the level |
| 13 | + * seems to "kick in" shortly after load, that is this bootstrap window - not necessarily a wrong user setting. |
| 14 | + * |
| 15 | + * Aside from logging helpers, most functions here intentionally avoid `DataStore.*`. |
| 16 | + */ |
4 | 17 |
|
5 | 18 | import isEqual from 'lodash-es/isEqual' |
6 | 19 | import isObject from 'lodash-es/isObject' |
@@ -611,8 +624,7 @@ let cachedPluginSettingsForLog: any = null |
611 | 624 | let pluginSettingsAwaitPrimeStartedForLog: boolean = false |
612 | 625 |
|
613 | 626 | /** Trace settings resolution via console.log. Enable with env NP_INSTRUMENT_PLUGIN_SETTINGS=1 (Node/Jest only). */ |
614 | | -const ENABLE_GET_PLUGIN_SETTINGS_INSTRUMENTATION: boolean = |
615 | | - typeof process !== 'undefined' && process.env && process.env.NP_INSTRUMENT_PLUGIN_SETTINGS === '1' |
| 627 | +const ENABLE_GET_PLUGIN_SETTINGS_INSTRUMENTATION: boolean = typeof process !== 'undefined' && process.env && process.env.NP_INSTRUMENT_PLUGIN_SETTINGS === '1' |
616 | 628 | let getPluginSettingsInstrumentationResolutionLogged: boolean = false |
617 | 629 |
|
618 | 630 | /** |
@@ -670,7 +682,7 @@ function primePluginSettingsCacheViaAwait(): void { |
670 | 682 | /** |
671 | 683 | * Return plugin settings for log-level checks. |
672 | 684 | * - If `DataStore.settings` is already a plain object (not a thenable), use it synchronously. |
673 | | - * - Otherwise schedule one background await and return null until cache fills (same default log level as “no settings”). |
| 685 | + * - Otherwise schedule one background await and return null until cache fills (shouldOutputForLogLevel then defaults to DEBUG until settings apply). |
674 | 686 | * |
675 | 687 | * @returns {?Object} |
676 | 688 | */ |
@@ -707,7 +719,8 @@ function getPluginSettingsForLogging(): any { |
707 | 719 | * @returns {boolean} |
708 | 720 | */ |
709 | 721 | export const shouldOutputForLogLevel = (logType: string): boolean => { |
710 | | - let userLogLevel = 1 |
| 722 | + // Default DEBUG so early logs are not dropped while DataStore.settings is still unresolved or _logLevel is unset. |
| 723 | + let userLogLevel = 0 |
711 | 724 | const thisMessageLevel = LOG_LEVELS.indexOf(logType.toUpperCase()) |
712 | 725 | const pluginSettings = getPluginSettingsForLogging() |
713 | 726 | // Note: Performing a null change against a value that is `undefined` will be true |
|
0 commit comments