Skip to content

Commit e02e7ef

Browse files
author
Viktor
committed
feat: 2.3.0
1 parent 82389d7 commit e02e7ef

4 files changed

Lines changed: 49 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 2.3.0
6+
7+
- Style: simplified logger output
8+
- Feat: added `slotName` to `meta` of custom logger
9+
510
## 2.2.0
611

712
- Feat: add `attachLogger`.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ Accepts object:
214214

215215
```typescript
216216
{
217-
meta: { action: 'set' | 'remove' | 'hide' | 'show', slotId: SlotId };
217+
meta: {
218+
action: 'set' | 'remove' | 'hide' | 'show',
219+
slotId: SlotId,
220+
slotName: string;
221+
};
218222
message: string;
219223
}
220224
```

src/__tests__/attachLogger.spec.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { SLOTS } from './stub';
22

33
import { createSlotFactory, LOG_TITLE } from '../index';
44

5+
type SlotsKeys = keyof typeof SLOTS;
6+
7+
const SLOTS_NAMES = Object.entries(SLOTS).reduce(
8+
(acc, [key, value]) => ({ ...acc, [value]: key }),
9+
{} as Record<typeof SLOTS[SlotsKeys], SlotsKeys>,
10+
);
11+
512
describe('AttachLogger', () => {
613
test('Basic', () => {
714
const logger = jest.fn();
@@ -16,32 +23,32 @@ describe('AttachLogger', () => {
1623

1724
api.set({ id: SLOTS.AWESOME, component: () => null });
1825
expect(logger).toBeCalledWith({
19-
message: `${LOG_TITLE} ${SLOTS.AWESOME} slot content was set`,
20-
meta: { action: 'set', slotId: SLOTS.AWESOME },
26+
message: `${LOG_TITLE} ${SLOTS_NAMES.awesome} -> set // ${SLOTS.AWESOME}`,
27+
meta: { action: 'set', slotId: SLOTS.AWESOME, slotName: SLOTS_NAMES.awesome },
2128
});
2229

2330
api.hide({ id: SLOTS.AWESOME });
2431
expect(logger).toBeCalledWith({
25-
message: `${LOG_TITLE} ${SLOTS.AWESOME} slot content was hidden`,
26-
meta: { action: 'hide', slotId: SLOTS.AWESOME },
32+
message: `${LOG_TITLE} ${SLOTS_NAMES.awesome} -> hide // ${SLOTS.AWESOME}`,
33+
meta: { action: 'hide', slotId: SLOTS.AWESOME, slotName: SLOTS_NAMES.awesome },
2734
});
2835

2936
api.show({ id: SLOTS.AWESOME });
3037
expect(logger).toBeCalledWith({
31-
message: `${LOG_TITLE} ${SLOTS.AWESOME} slot content was shown`,
32-
meta: { action: 'show', slotId: SLOTS.AWESOME },
38+
message: `${LOG_TITLE} ${SLOTS_NAMES.awesome} -> show // ${SLOTS.AWESOME}`,
39+
meta: { action: 'show', slotId: SLOTS.AWESOME, slotName: SLOTS_NAMES.awesome },
3340
});
3441

3542
api.remove({ id: SLOTS.AWESOME });
3643
expect(logger).toBeCalledWith({
37-
message: `${LOG_TITLE} ${SLOTS.AWESOME} slot content was removed`,
38-
meta: { action: 'remove', slotId: SLOTS.AWESOME },
44+
message: `${LOG_TITLE} ${SLOTS_NAMES.awesome} -> remove // ${SLOTS.AWESOME}`,
45+
meta: { action: 'remove', slotId: SLOTS.AWESOME, slotName: SLOTS_NAMES.awesome },
3946
});
4047

4148
api.hide({ id: SLOTS.MAIN });
4249
expect(logger).toBeCalledWith({
43-
message: `${LOG_TITLE} ${SLOTS.MAIN} slot content was hidden`,
44-
meta: { action: 'hide', slotId: SLOTS.MAIN },
50+
message: `${LOG_TITLE} ${SLOTS_NAMES.main} -> hide // ${SLOTS.MAIN}`,
51+
meta: { action: 'hide', slotId: SLOTS.MAIN, slotName: SLOTS_NAMES.main },
4552
});
4653

4754
expect(logger).toBeCalledTimes(5);

src/index.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,32 @@ const ACTIONS = {
1515
ATTACH_LOGGER: 'attachLogger',
1616
} as const;
1717

18-
type Logger<S, P> = (
18+
type SlotName = string;
19+
type Logger<S, P, T> = (
1920
_: Readonly<{
2021
message: string;
2122
meta: {
2223
action: P;
2324
slotId: S;
25+
slotName: T;
2426
};
2527
}>,
2628
) => unknown;
27-
type GetLogText<S, P> = (_: { action: S; slotId: P }) => string;
29+
type GetLogText<S, P, T> = (_: { action: S; slotId: P; slotName: T }) => string;
2830
type Component<S> = (props: S) => ReactElement | null;
2931
type SlotStore<S> = Readonly<{
3032
component: Component<S>;
3133
isVisible: boolean;
3234
}>;
3335

34-
export const createSlotFactory = <Id extends string>(slots: Record<string, Id>) => {
36+
export const createSlotFactory = <Id extends string>(slots: Record<SlotName, Id>) => {
37+
const slotNames = new Map(Object.entries(slots).map(([key, value]) => [value, key]));
38+
3539
const attachLogger = createEvent<Readonly<{
36-
fn?: Logger<Id, Exclude<typeof ACTIONS[keyof typeof ACTIONS], 'attachLogger'>>;
40+
fn?: Logger<Id, Exclude<typeof ACTIONS[keyof typeof ACTIONS], 'attachLogger'>, SlotName>;
3741
watchList?: Id[];
3842
}> | void>();
43+
3944
const $logger = createStore<
4045
Readonly<{
4146
shouldLog: boolean;
@@ -55,19 +60,12 @@ export const createSlotFactory = <Id extends string>(slots: Record<string, Id>)
5560
[ACTIONS.SHOW]: createEvent<Readonly<{ id: Id }>>(),
5661
[ACTIONS.ATTACH_LOGGER]: attachLogger,
5762
};
58-
const getLogText: GetLogText<Exclude<keyof typeof api, 'attachLogger'>, Id> = ({ action, slotId }) => {
59-
if (action === ACTIONS.HIDE) {
60-
return `${LOG_TITLE} ${slotId} slot content was hidden`;
61-
}
6263

63-
if (action === ACTIONS.REMOVE) {
64-
return `${LOG_TITLE} ${slotId} slot content was removed`;
65-
}
66-
67-
return action === ACTIONS.SET
68-
? `${LOG_TITLE} ${slotId} slot content was set`
69-
: `${LOG_TITLE} ${slotId} slot content was shown`;
70-
};
64+
const getLogText: GetLogText<Exclude<keyof typeof api, 'attachLogger'>, Id, SlotName> = ({
65+
action,
66+
slotId,
67+
slotName,
68+
}) => `${LOG_TITLE} ${slotName} -> ${action} // ${slotId}`;
7169

7270
type LogParameters = Parameters<typeof getLogText>[0];
7371

@@ -87,20 +85,24 @@ export const createSlotFactory = <Id extends string>(slots: Record<string, Id>)
8785
clock: sample({
8886
clock: [
8987
api.hide.map(({ id }) => ({
90-
slotId: id,
9188
action: ACTIONS.HIDE,
89+
slotId: id,
90+
slotName: slotNames.get(id) as SlotName,
9291
})),
9392
api.remove.map(({ id }) => ({
94-
slotId: id,
9593
action: ACTIONS.REMOVE,
94+
slotId: id,
95+
slotName: slotNames.get(id) as SlotName,
9696
})),
9797
api.set.map(({ id }) => ({
98-
slotId: id,
9998
action: ACTIONS.SET,
99+
slotId: id,
100+
slotName: slotNames.get(id) as SlotName,
100101
})),
101102
api.show.map(({ id }) => ({
102-
slotId: id,
103103
action: ACTIONS.SHOW,
104+
slotId: id,
105+
slotName: slotNames.get(id) as SlotName,
104106
})),
105107
],
106108
source: $logger,

0 commit comments

Comments
 (0)