Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
hoaxvo16 committed May 18, 2024
1 parent 460648f commit e80833b
Show file tree
Hide file tree
Showing 6 changed files with 6,003 additions and 77 deletions.
6 changes: 3 additions & 3 deletions example/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ secondStore.applyMiddleware(
}
);

store.add(keys.inputValue, '');
store.add(keys.count, 0);
secondStore.add(keys.todoList, []);
store.create(keys.inputValue, '');
store.create(keys.count, 0);
secondStore.create(keys.todoList, []);
5,947 changes: 5,947 additions & 0 deletions example/yarn.lock

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions src/core/injector.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import {FunctionComponent, useCallback, useEffect, useState} from 'react';
import {Store} from './store/store';
import { FunctionComponent, useCallback, useEffect, useState } from 'react';
import { Store } from './store/store';

export function StoreInjector(
store: Store | Array<Store>,
Component: FunctionComponent
store: Store | Array<Store>,
component: FunctionComponent
) {
return function (props: any) {
const [, setTrigger] = useState(false);
return function(props: any) {
const [, setTrigger] = useState(false);

const triggerFunction = useCallback(() => {
setTrigger((prev: boolean) => !prev);
}, []);
const triggerFunction = useCallback(() => {
setTrigger((prev: boolean) => !prev);
}, []);

useEffect(() => {
if (Array.isArray(store)) {
store.forEach(s => s.injectComponent(triggerFunction));
} else {
store.injectComponent(triggerFunction);
}
useEffect(() => {
if (Array.isArray(store)) {
store.forEach(s => s.connect(triggerFunction));
} else {
store.connect(triggerFunction);
}

return () => {
if (Array.isArray(store)) {
store.forEach(s => s.disconnect(triggerFunction));
} else {
store.disconnect(triggerFunction);
}
};
}, [store, triggerFunction]);
return Component(props);
};
return () => {
if (Array.isArray(store)) {
store.forEach(s => s.disconnect(triggerFunction));
} else {
store.disconnect(triggerFunction);
}
};
}, [store, triggerFunction]);
return component(props);
};
}
6 changes: 3 additions & 3 deletions src/core/store/base-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class BaseStore {
}

protected notify() {
this.subscribeComponents.forEach((triggerFunction) => triggerFunction());
this.subscribeComponents.forEach(triggerFunction => triggerFunction());
}

protected checkExist(key: string) {
return this.dataMap.has(key);
public get(key: string) {
return this.dataMap.get(key);
}
}
54 changes: 17 additions & 37 deletions src/core/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DispatchAction, MiddlewareCallback} from '../../types';
import {BaseStore} from './base-store';
import { DispatchAction, MiddlewareCallback } from '../../types';
import { BaseStore } from './base-store';

export class Store extends BaseStore {
constructor() {
Expand All @@ -12,77 +12,57 @@ export class Store extends BaseStore {
type: DispatchAction
) {
if (this.middlewareHandler) {
return this.middlewareHandler(
key,
current,
payload,
type
);
return this.middlewareHandler(key, current, payload, type);
}
return true;
}

private handleDispatchAction(
type: DispatchAction,
key: string,
payload?: any
newPayload?: any
) {
const currentPayload = this.get(key);
switch (type) {
case DispatchAction.CREATE:
if (!this.runMiddleware(key, null, payload, type)) {
this.dataMap.set(key, payload);
if (!this.runMiddleware(key, currentPayload, newPayload, type)) {
this.dataMap.set(key, newPayload);
}
break;
case DispatchAction.UPDATE:
if (!this.runMiddleware(key, this.dataMap.get(key), payload, type)) {
this.dataMap.set(key, payload);
if (!this.runMiddleware(key, currentPayload, newPayload, type)) {
this.dataMap.set(key, newPayload);
this.notify();
}
break;
case DispatchAction.DELETE:
if (!this.runMiddleware(key, this.dataMap.get(key), null, type)) {
if (!this.runMiddleware(key, currentPayload, newPayload, type)) {
this.dataMap.delete(key);
this.notify();
}
break;
}
}

add(key: string, value: any) {
if (this.checkExist(key)) {
this.throwExist(key);
} else {
this.handleDispatchAction(DispatchAction.CREATE, key, value);
}
create(key: string, value: any) {
this.handleDispatchAction(DispatchAction.CREATE, key, value);
}

update(key: string, value: any) {
if (!this.checkExist(key)) {
this.throwNotFound(key);
} else {
this.handleDispatchAction(DispatchAction.UPDATE, key, value);
}
}

remove(key: string) {
if (!this.checkExist(key)) {
this.throwNotFound(key);
} else {
this.handleDispatchAction(DispatchAction.DELETE, key);
}
this.handleDispatchAction(DispatchAction.UPDATE, key, value);
}

get(key: string) {
return this.dataMap.get(key);
delete(key: string) {
this.handleDispatchAction(DispatchAction.DELETE, key);
}

injectComponent(triggerFunction: VoidFunction) {
connect(triggerFunction: VoidFunction) {
this.subscribeComponents.push(triggerFunction);
}

disconnect(triggerFunction: VoidFunction) {
this.subscribeComponents = this.subscribeComponents.filter(
com => com !== triggerFunction
componentTrigger => componentTrigger !== triggerFunction
);
}

Expand Down
17 changes: 8 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
export type MiddlewareCallback = (
key: string,
current: any,
payload: any,
type: DispatchAction
key: string,
currentPayload: any,
newPayload: any,
type: DispatchAction
) => void | boolean;


export enum DispatchAction {
UPDATE,
CREATE,
DELETE
UPDATE,
CREATE,
DELETE,
}

export type NullData = null;
export type NullData = null;

0 comments on commit e80833b

Please sign in to comment.