From 8959b574104e87cdcd2bee5905fb643176f08969 Mon Sep 17 00:00:00 2001 From: stanislavsky Date: Mon, 8 Apr 2024 15:00:12 +0300 Subject: [PATCH] added state storage for raw counter options Module entrypoint and debugger module wip 1ea26edcee7f10c91f3d15c3f93c8fcd4c7fedb4 --- .mapping.json | 1 + src/index.ts | 17 +++++++++- .../__tests__/consoleRenderer.spec.ts | 13 -------- .../consoleRenderer/consoleRenderer.ts | 4 --- .../destruct/__tests__/destruct.spec.ts | 24 ++++++++++---- src/providers/destruct/destruct.ts | 3 +- src/types.ts | 2 +- .../__tests__/counterOptions.spec.ts | 4 +-- src/utils/counterOptions/counterOptions.ts | 33 +++++++++++++------ .../counterOptions/originalOptionsState.ts | 29 ++++++++++++++++ 10 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 src/utils/counterOptions/originalOptionsState.ts diff --git a/.mapping.json b/.mapping.json index ccf67d9..9257c04 100644 --- a/.mapping.json +++ b/.mapping.json @@ -384,6 +384,7 @@ "src/utils/counterOptions/counterOptions.ts":"metrika/frontend/watch/public/src/utils/counterOptions/counterOptions.ts", "src/utils/counterOptions/getCounterKey.ts":"metrika/frontend/watch/public/src/utils/counterOptions/getCounterKey.ts", "src/utils/counterOptions/index.ts":"metrika/frontend/watch/public/src/utils/counterOptions/index.ts", + "src/utils/counterOptions/originalOptionsState.ts":"metrika/frontend/watch/public/src/utils/counterOptions/originalOptionsState.ts", "src/utils/counterOptions/types.ts":"metrika/frontend/watch/public/src/utils/counterOptions/types.ts", "src/utils/counterSettings/const.ts":"metrika/frontend/watch/public/src/utils/counterSettings/const.ts", "src/utils/counterSettings/counterSettings.ts":"metrika/frontend/watch/public/src/utils/counterSettings/counterSettings.ts", diff --git a/src/index.ts b/src/index.ts index aaaae1c..6488a2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,11 @@ import { ProviderResultPromised, MetrikaCounterConstructor, } from 'src/types'; -import { normalizeOptions, getCounterKey } from 'src/utils/counterOptions'; +import { + normalizeOptions, + getCounterKey, + normalizeOriginalOptions, +} from 'src/utils/counterOptions'; import { bindArgs, firstArg, @@ -55,6 +59,7 @@ import { throwFunction } from './utils/errorLogger/throwFunction'; import { yaNamespace, ASYNC_PROVIDERS_MAX_EXEC_TIME } from './const'; import { stackProxy } from './providers/stackProxy/stackProxy'; import { DUPLICATE_COUNTERS_CONSOLE_MESSAGE } from './providers/consoleRenderer/dictionary'; +import { saveOriginalOptions } from './utils/counterOptions/originalOptionsState'; type CounterMethod = keyof CounterObject; const globalConfig = getGlobalStorage(window); @@ -181,6 +186,16 @@ const MetrikaCounter: MetrikaCounterConstructor = function MetrikaCounter( return counters[counterKey]; } + saveOriginalOptions( + ctx, + counterKey, + normalizeOriginalOptions( + counterId, + counterParams, + counterType, + counterDefer, + ), + ); counters[counterKey] = this; globalConfig.setVal(COUNTERS_GLOBAL_KEY, counters); globalConfig.setSafe('counter', this); diff --git a/src/providers/consoleRenderer/__tests__/consoleRenderer.spec.ts b/src/providers/consoleRenderer/__tests__/consoleRenderer.spec.ts index 05a06fc..2d8b003 100644 --- a/src/providers/consoleRenderer/__tests__/consoleRenderer.spec.ts +++ b/src/providers/consoleRenderer/__tests__/consoleRenderer.spec.ts @@ -9,7 +9,6 @@ import { EMPTY_LINK_CONSOLE_MESSAGE, PAGE_VIEW_CONSOLE_MESSAGE, } from '../dictionary'; -import * as enabledUtils from '../../debugConsole/debugEnabled'; import { DebuggerEvent } from '../../debugEvents/types'; describe('consoleRenderer', () => { @@ -30,14 +29,10 @@ describe('consoleRenderer', () => { Parameters, ReturnType >; - let isDebugEnabled: sinon.SinonStub<[ctx: Window], boolean>; beforeEach(() => { sandbox.stub(consoleUtils, 'getConsole').returns(fakeConsole); sandbox.stub(eventsUtils, 'getEvents').returns(fakeEvents); - isDebugEnabled = sandbox - .stub(enabledUtils, 'debugEnabled') - .returns(true); getObserver = sandbox.stub(observerUtils, 'dataLayerObserver'); }); @@ -49,14 +44,6 @@ describe('consoleRenderer', () => { fakeConsole.error.resetHistory(); }); - it('does nothing if debug is disabled', () => { - const win = {} as Window; - isDebugEnabled.returns(false); - useConsoleRendererRaw(win); - - sinon.assert.notCalled(getObserver); - }); - it('subscribes and renders console messages', () => { const win = {} as Window; useConsoleRendererRaw(win); diff --git a/src/providers/consoleRenderer/consoleRenderer.ts b/src/providers/consoleRenderer/consoleRenderer.ts index dfdfcfd..8a8a902 100644 --- a/src/providers/consoleRenderer/consoleRenderer.ts +++ b/src/providers/consoleRenderer/consoleRenderer.ts @@ -5,7 +5,6 @@ import { cMap } from 'src/utils/array'; import { has } from 'src/utils/object'; import { isString } from 'src/utils/string'; import { globalMemoWin } from 'src/utils/function'; -import { debugEnabled } from '../debugConsole/debugEnabled'; import { getEvents } from '../debugEvents'; import { CONSOLE_DICTIONARY, variableRegex } from './dictionary'; @@ -44,9 +43,6 @@ export const getMessage = ( }; export const useConsoleRendererRaw = (ctx: Window) => { - if (!debugEnabled(ctx)) { - return; - } const debuggerEvents = getEvents(ctx); const console = getConsole(ctx); dataLayerObserver(ctx, debuggerEvents, (observer) => { diff --git a/src/providers/destruct/__tests__/destruct.spec.ts b/src/providers/destruct/__tests__/destruct.spec.ts index 489ec94..f323cf9 100644 --- a/src/providers/destruct/__tests__/destruct.spec.ts +++ b/src/providers/destruct/__tests__/destruct.spec.ts @@ -2,22 +2,29 @@ import * as chai from 'chai'; import * as sinon from 'sinon'; import * as globalStorage from 'src/storage/global'; import { CounterOptions } from 'src/utils/counterOptions'; +import * as optionsUtils from 'src/utils/counterOptions/originalOptionsState'; import { destruct } from '../destruct'; describe('destruct provider', () => { - let globalStorageMock: any; + const sandbox = sinon.createSandbox(); + let deleteOriginalOptions: sinon.SinonStub< + Parameters, + ReturnType + >; const gs = { - getVal: sinon.stub(), + getVal: sandbox.stub(), }; beforeEach(() => { - globalStorageMock = sinon - .stub(globalStorage, 'getGlobalStorage') - .returns(gs as any); + deleteOriginalOptions = sandbox.stub( + optionsUtils, + 'deleteOriginalOptions', + ); + sandbox.stub(globalStorage, 'getGlobalStorage').returns(gs as any); }); afterEach(() => { - globalStorageMock.restore(); + sandbox.restore(); gs.getVal.resetHistory(); }); @@ -43,6 +50,11 @@ describe('destruct provider', () => { const destructor = destruct(windowStub, counterOptions, callbacks); destructor(); + sinon.assert.calledOnceWithExactly( + deleteOriginalOptions, + windowStub, + counterKey, + ); chai.expect(cb1.calledOnce).to.be.true; chai.expect(cb2.calledOnce).to.be.true; chai.expect(counters['100:0']).to.not.exist; diff --git a/src/providers/destruct/destruct.ts b/src/providers/destruct/destruct.ts index 499bc96..762f439 100644 --- a/src/providers/destruct/destruct.ts +++ b/src/providers/destruct/destruct.ts @@ -3,6 +3,7 @@ import { ctxErrorLogger, errorLogger } from 'src/utils/errorLogger'; import { CounterOptions, getCounterKey } from 'src/utils/counterOptions'; import { cForEach } from 'src/utils/array'; import { isFunction } from 'src/utils/object'; +import { deleteOriginalOptions } from 'src/utils/counterOptions/originalOptionsState'; import { DestructHandler } from './const'; type UnsubscribeCallbacks = (Function | undefined)[]; @@ -30,7 +31,7 @@ export const destruct = ctxErrorLogger( errorLogger(ctx, `dest.fr.${index}`, cb)(), unsubscribeMethods, ); - + deleteOriginalOptions(ctx, getCounterKey(counterOptions)); delete globalConfig.getVal<{ [key: string]: object }>('counters')[ getCounterKey(counterOptions) ]; diff --git a/src/types.ts b/src/types.ts index a488c6d..f1019a8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,7 +15,7 @@ export type MetrikaCounterConstructor = ( export interface MetrikaCounter extends MetrikaCounterConstructor {} export type CounterMethod = keyof CounterObject; -type ProviderResultObject = Record & { +export type ProviderResultObject = Record & { [UNSUBSCRIBE_PROPERTY]?: () => void; }; export type ProviderResult = diff --git a/src/utils/counterOptions/__tests__/counterOptions.spec.ts b/src/utils/counterOptions/__tests__/counterOptions.spec.ts index bb9619d..c411d42 100644 --- a/src/utils/counterOptions/__tests__/counterOptions.spec.ts +++ b/src/utils/counterOptions/__tests__/counterOptions.spec.ts @@ -25,10 +25,10 @@ describe('Counter Options', () => { optKey: 'params', }, counterType: { - optKey: 'counterType', + optKey: 'type', }, counterDefer: { - optKey: 'counterDefer', + optKey: 'defer', }, }; }); diff --git a/src/utils/counterOptions/counterOptions.ts b/src/utils/counterOptions/counterOptions.ts index 027b74e..5086ce4 100644 --- a/src/utils/counterOptions/counterOptions.ts +++ b/src/utils/counterOptions/counterOptions.ts @@ -4,7 +4,7 @@ import { RSYA_COUNTER_TYPE } from 'src/providers/counterOptions/const'; import { OptionsKeysMaps } from 'src/providers/counterOptions/types'; import { cReduce } from 'src/utils/array'; import { equal } from 'src/utils/function'; -import { entries } from 'src/utils/object'; +import { entries, isObject } from 'src/utils/object'; import { setTurboInfo } from 'src/utils/turboParams'; import { CounterOptions, CounterTypeInterface } from './types'; @@ -12,6 +12,22 @@ import { CounterOptions, CounterTypeInterface } from './types'; export const isRsyaCounter = equal(RSYA_COUNTER_TYPE); +export const normalizeOriginalOptions = ( + counterId: Record | number, + counterParams?: Record, + counterType?: number, + counterDefer?: boolean, +): Record => { + return isObject(counterId) + ? counterId + : { + ['id']: counterId, + ['type']: counterType, + ['defer']: counterDefer, + ['params']: counterParams, + }; +}; + export const normalizeOptions = ( counterId: Record | number, optionsKeysMap: OptionsKeysMaps, @@ -19,15 +35,12 @@ export const normalizeOptions = ( counterType?: number, counterDefer?: boolean, ): CounterOptions => { - const counterData: Record = - typeof counterId === 'object' - ? counterId - : { - id: counterId, - counterType, - counterDefer, - params: counterParams, - }; + const counterData: Record = normalizeOriginalOptions( + counterId, + counterParams, + counterType, + counterDefer, + ); const options = cReduce( ( diff --git a/src/utils/counterOptions/originalOptionsState.ts b/src/utils/counterOptions/originalOptionsState.ts new file mode 100644 index 0000000..852ff09 --- /dev/null +++ b/src/utils/counterOptions/originalOptionsState.ts @@ -0,0 +1,29 @@ +import { getGlobalStorage } from 'src/storage/global'; + +const COUNTER_OPTIONS_GLOBAL_STORAGE_KEY = 'cok'; + +export const saveOriginalOptions = ( + ctx: Window, + counterKey: string, + rawOptions: Record, +) => { + const globalStorage = getGlobalStorage(ctx); + const optionsState: Record> = + globalStorage.getVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, {}); + optionsState[counterKey] = rawOptions; + globalStorage.setVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, optionsState); +}; + +export const deleteOriginalOptions = (ctx: Window, counterKey: string) => { + const globalStorage = getGlobalStorage(ctx); + const optionsState: Record> = + globalStorage.getVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, {}); + delete optionsState[counterKey]; +}; + +export const loadOriginalOptions = (ctx: Window, counterKey: string) => { + const globalStorage = getGlobalStorage(ctx); + const optionsState: Record> = + globalStorage.getVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, {}); + return optionsState[counterKey]; +};