Skip to content

Commit 7a5b897

Browse files
authored
Merge pull request #26 from alevnyacow/docs/tsdoc-and-rerenders-list-refactor
Addad TSDoc and made some refactoring in RerendersList class
2 parents c3253f7 + 5353b8a commit 7a5b897

File tree

5 files changed

+87
-16
lines changed

5 files changed

+87
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alevnyacow/shared-react-variables",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Easy and comfortable React state manager",
55
"main": "transpiled/index.js",
66
"scripts": {

sources/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ type ReactVariableRewriteHook<T> = () => (
99
generator: (oldState: T) => T
1010
) => void;
1111

12+
/**
13+
* Generates a tuple of two hooks: first hook is a React variable generator,
14+
* second hook can be used to rewrite a whole variable (you don't need this most likely).
15+
*
16+
* @param initialState Initial state of shared React variable.
17+
*/
1218
function createUseSharedVariable<T extends object>(initialState: T) {
1319
const variableIdentifier = v4();
1420
let sharedVariable = deepProxy(
1521
[rerenderersList.fire],
1622
variableIdentifier
1723
)(initialState);
1824

25+
/**
26+
* React hook returning shared React variable.
27+
*
28+
* @param rerenderOnChange Flag can be used to prevent a rerender on React variable changes, when set to true.
29+
*/
1930
const useSharedVariable: ReactVariableHook<T> = (
2031
rerenderOnChange = true
2132
) => {
@@ -39,6 +50,9 @@ function createUseSharedVariable<T extends object>(initialState: T) {
3950
return sharedVariable;
4051
};
4152

53+
/**
54+
* React hook returning function can be used to rewrite whole associated variable.
55+
*/
4256
const useSharedVariableRewrite: ReactVariableRewriteHook<T> =
4357
() => (generator: (oldState: T) => T) => {
4458
sharedVariable = deepProxy(

sources/rerenderers-list.ts

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
1+
import { Rerenderer } from "./use-rerenderer";
2+
3+
type RerenderersData = {
4+
[variableId: string]: { [rerendererId: string]: Rerenderer };
5+
};
6+
7+
/**
8+
* Describes a list of rerenders can be used to register/delete/fire rerenderers.
9+
*/
110
class RerenderersList {
2-
private _rerenderersData: Record<string, Record<string, () => void>>;
11+
private _rerenderersData: RerenderersData;
312

413
constructor() {
514
this._rerenderersData = {};
615
}
716

8-
private addForVariable = (variableId: string) => {
9-
this._rerenderersData[variableId] =
10-
this._rerenderersData[variableId] || {};
11-
};
12-
17+
/**
18+
* Checks if rerenderers data has a section associated with given variable.
19+
*
20+
* @param variableId Variable identifier.
21+
*/
1322
private existsForVariable = (variableId: string) => {
1423
return this._rerenderersData[variableId] !== undefined;
1524
};
1625

26+
/**
27+
* Adds a section for a given variable.
28+
*
29+
* @param variableId Variable identifier.
30+
*
31+
* @throws {@link Error} When tried to add a section for a variable that already exists.
32+
*/
33+
private addForVariable = (variableId: string) => {
34+
if (this.existsForVariable(variableId)) {
35+
throw new Error(
36+
"Tried to add a section for a variable that already exists."
37+
);
38+
}
39+
40+
this._rerenderersData[variableId] = {};
41+
};
42+
43+
/**
44+
* Registers new rerenderer for a given variable.
45+
*
46+
* @param variableId Variable identifier.
47+
* @param rerendererId Rerenderer identifier.
48+
* @param rerenderer Rerenderer logic.
49+
*/
1750
public add = (
1851
variableId: string,
1952
rerendererId: string,
20-
rerenderer: () => void
53+
rerenderer: Rerenderer
2154
) => {
2255
if (!this.existsForVariable(variableId)) {
2356
this.addForVariable(variableId);
@@ -26,19 +59,37 @@ class RerenderersList {
2659
this._rerenderersData[variableId][rerendererId] = rerenderer;
2760
};
2861

62+
/**
63+
* Unregisters a rerender from variable by an identifier.
64+
*
65+
* @param variableId Variable identifier.
66+
* @param rerendererId Rerenderer identifier.
67+
*
68+
* @throws {@link Error} When tried to remove variable data that does not exist.
69+
*/
2970
public remove = (variableId: string, rerendererId: string) => {
30-
const variableRerendersData = this._rerenderersData[variableId];
31-
32-
if (!variableRerendersData) {
33-
return;
71+
if (!this.existsForVariable(variableId)) {
72+
throw new Error(
73+
"Tried to remove variable data that does not exist."
74+
);
3475
}
3576

77+
const variableRerendersData = this._rerenderersData[variableId];
3678
delete variableRerendersData[rerendererId];
3779
};
3880

81+
/**
82+
* Fires every rerenderer associated with a given variable.
83+
*
84+
* @param variableId Variable identifier.
85+
*
86+
* @throws {@link Error} When tried to fire rerenders for a variable that does not exist.
87+
*/
3988
public fire = (variableId: string) => {
4089
if (!this.existsForVariable(variableId)) {
41-
return;
90+
throw new Error(
91+
"Tried to fire rerenders for a variable that does not exist."
92+
);
4293
}
4394

4495
Object.values(this._rerenderersData[variableId]).forEach((rerender) => {

sources/use-rerenderer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useState } from "react";
22

3-
export function useRerenderer() {
3+
type Rerenderer = () => void;
4+
5+
/**
6+
* React hook returning a function that causes a rerender when called.
7+
*/
8+
function useRerenderer() {
49
const [, setNewDummyObject] = useState({});
5-
return () => setNewDummyObject({});
10+
return (() => setNewDummyObject({})) as Rerenderer;
611
}
12+
export { useRerenderer, Rerenderer };

tests/test-application/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@alevnyacow/shared-react-variables": "file:../../alevnyacow-shared-react-variables-2.0.2.tgz",
6+
"@alevnyacow/shared-react-variables": "file:../../alevnyacow-shared-react-variables-2.0.3.tgz",
77
"@testing-library/jest-dom": "^5.16.2",
88
"@testing-library/react": "^12.1.4",
99
"@testing-library/user-event": "^13.5.0",

0 commit comments

Comments
 (0)