Skip to content

Commit

Permalink
added state storage for raw counter options
Browse files Browse the repository at this point in the history
Module entrypoint and debugger module

wip
1ea26edcee7f10c91f3d15c3f93c8fcd4c7fedb4
  • Loading branch information
Stanislavsky34200 committed Apr 8, 2024
1 parent 6421b70 commit 8959b57
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 38 deletions.
1 change: 1 addition & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions src/providers/consoleRenderer/__tests__/consoleRenderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -30,14 +29,10 @@ describe('consoleRenderer', () => {
Parameters<typeof observerUtils.dataLayerObserver>,
ReturnType<typeof observerUtils.dataLayerObserver>
>;
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');
});

Expand All @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/providers/consoleRenderer/consoleRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) => {
Expand Down
24 changes: 18 additions & 6 deletions src/providers/destruct/__tests__/destruct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof optionsUtils.deleteOriginalOptions>,
ReturnType<typeof optionsUtils.deleteOriginalOptions>
>;
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();
});

Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/providers/destruct/destruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[];
Expand Down Expand Up @@ -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)
];
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type MetrikaCounterConstructor = (
export interface MetrikaCounter extends MetrikaCounterConstructor {}

export type CounterMethod = keyof CounterObject;
type ProviderResultObject = Record<string, Function> & {
export type ProviderResultObject = Record<string, Function> & {
[UNSUBSCRIBE_PROPERTY]?: () => void;
};
export type ProviderResult =
Expand Down
4 changes: 2 additions & 2 deletions src/utils/counterOptions/__tests__/counterOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ describe('Counter Options', () => {
optKey: 'params',
},
counterType: {
optKey: 'counterType',
optKey: 'type',
},
counterDefer: {
optKey: 'counterDefer',
optKey: 'defer',
},
};
});
Expand Down
33 changes: 23 additions & 10 deletions src/utils/counterOptions/counterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,43 @@ 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';

// NOTE: Extend the type in order to be able to check all string inputs.
export const isRsyaCounter =
equal<CounterTypeInterface | string>(RSYA_COUNTER_TYPE);

export const normalizeOriginalOptions = (
counterId: Record<string, unknown> | number,
counterParams?: Record<string, unknown>,
counterType?: number,
counterDefer?: boolean,
): Record<string, unknown> => {
return isObject(counterId)
? counterId
: {
['id']: counterId,
['type']: counterType,
['defer']: counterDefer,
['params']: counterParams,
};
};

export const normalizeOptions = (
counterId: Record<string, unknown> | number,
optionsKeysMap: OptionsKeysMaps,
counterParams?: Record<string, unknown>,
counterType?: number,
counterDefer?: boolean,
): CounterOptions => {
const counterData: Record<string, unknown> =
typeof counterId === 'object'
? counterId
: {
id: counterId,
counterType,
counterDefer,
params: counterParams,
};
const counterData: Record<string, unknown> = normalizeOriginalOptions(
counterId,
counterParams,
counterType,
counterDefer,
);

const options = cReduce(
(
Expand Down
29 changes: 29 additions & 0 deletions src/utils/counterOptions/originalOptionsState.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>,
) => {
const globalStorage = getGlobalStorage(ctx);
const optionsState: Record<string, Record<string, unknown>> =
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<string, Record<string, unknown>> =
globalStorage.getVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, {});
delete optionsState[counterKey];
};

export const loadOriginalOptions = (ctx: Window, counterKey: string) => {
const globalStorage = getGlobalStorage(ctx);
const optionsState: Record<string, Record<string, unknown>> =
globalStorage.getVal(COUNTER_OPTIONS_GLOBAL_STORAGE_KEY, {});
return optionsState[counterKey];
};

0 comments on commit 8959b57

Please sign in to comment.