Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyshulga1cs committed Feb 26, 2024
1 parent 596ca4e commit 9e0d21a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions .stylelintcache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\chat\\styles.scss":"1"},{"size":13372,"mtime":1708907856866,"hashOfConfig":"2"},"1v6j01t"]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"vite": "^5.1.0"
},
"dependencies": {
"@types/uuid": "^9.0.8",
"eslint-config-airbnb": "^19.0.4",
"express": "^4.18.2",
"uuid": "^9.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/chat/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ section#chat {
top: 2px;
width: calc(100% + 20px);
height: calc(100% - 4px);
background-color: $lightprimary-color;
background-color: $light-primary-color;
}

&:hover {
Expand Down Expand Up @@ -336,7 +336,7 @@ section#chat {
}

&.current-user {
background-color: $lightprimary-color;
background-color: $light-primary-color;
padding-right: 60px;
margin-left: auto;

Expand Down
28 changes: 15 additions & 13 deletions src/services/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import EventBus from "./EventBus";

type CreatedElement = HTMLElement | HTMLTemplateElement | DocumentFragment;

type ObjectT = Record<string | symbol, any>;

export default class Block {
static EVENTS = {
INIT: "init",
Expand All @@ -17,14 +19,14 @@ export default class Block {
tagName?: keyof HTMLElementTagNameMap;
};

props: Record<string, Block>;
props: ObjectT;
children: Record<string, Block>;
lists: Record<string, Block[]>;

eventBus: () => EventBus;
_element: CreatedElement;
_id: string;
events?: Record<string, unknown>;
events?: ObjectT;

/** JSDoc
* @param {string} tagName
Expand Down Expand Up @@ -59,17 +61,17 @@ export default class Block {
return this._element;
}

_registerEvents(eventBus) {
_registerEvents(eventBus: EventBus) {
eventBus.on(Block.EVENTS.INIT, this.init.bind(this));
eventBus.on(Block.EVENTS.FLOW_CDM, this._componentDidMount.bind(this));
eventBus.on(Block.EVENTS.FLOW_RENDER, this._render.bind(this));
eventBus.on(Block.EVENTS.FLOW_CDU, this._componentDidUpdate.bind(this));
}

_getChildren(propsAndChildren) {
const props = {};
const children = {};
const lists = {};
_getChildren(propsAndChildren: ObjectT) {
const props: ObjectT = {};
const children: ObjectT = {};
const lists: ObjectT = {};

Object.entries(propsAndChildren).forEach(([key, value]) => {
if (value instanceof Block) {
Expand All @@ -84,7 +86,7 @@ export default class Block {
return { props, children, lists };
}

compile(template, props): DocumentFragment {
compile(template: string, props: ObjectT): DocumentFragment {
const propsAndStubs = { ...props };

Object.entries(this.children).forEach(
Expand Down Expand Up @@ -154,13 +156,13 @@ export default class Block {
this.eventBus().emit(Block.EVENTS.FLOW_CDM);
}

_componentDidUpdate(oldProps, newProps) {
_componentDidUpdate(oldProps: unknown, newProps: unknown) {
const response = this.componentDidUpdate(oldProps, newProps);
return response;
}

// Может переопределять пользователь, необязательно трогать
componentDidUpdate(oldProps, newProps) {
componentDidUpdate(oldProps: unknown, newProps: unknown) {
if (oldProps !== newProps) {
// this._render();
this.eventBus().emit(Block.EVENTS.FLOW_RENDER);
Expand All @@ -170,7 +172,7 @@ export default class Block {
return false;
}

setProps = (newProps) => {
setProps = (newProps: ObjectT) => {
if (!newProps) {
return;
}
Expand Down Expand Up @@ -229,8 +231,8 @@ export default class Block {
});
}

_makeProxy(props) {
const handleEventBus = (key, value) => {
_makeProxy(props: ObjectT) {
const handleEventBus = (key: string | symbol, value: any) => {
this.eventBus().emit(Block.EVENTS.FLOW_CDU, this, {
...this,
[key]: value,
Expand Down
10 changes: 5 additions & 5 deletions src/services/EventBus.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class EventBus {
listeners: object;
listeners: Record<string, Function[]>;

constructor() {
this.listeners = {};
}

on(event, callback) {
on(event: string, callback: Function) {
//Код здесь
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(callback);
}

off(event, callback) {
off(event: string, callback: Function) {
//Код здесь
if (!this.listeners[event]) {
throw new Error(`Нет события: ${event}`);
Expand All @@ -23,12 +23,12 @@ class EventBus {
);
}

emit(event, ...args) {
emit(event: string, ...args: any) {
if (!this.listeners[event]) {
throw new Error(`Нет события: ${event}`);
}
let response;
this.listeners[event].forEach((listener) => {
this.listeners[event].forEach((listener: Function) => {
// listener(...args);
response = listener(...args);
});
Expand Down

0 comments on commit 9e0d21a

Please sign in to comment.