generated from garronej/ts-ci
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
createUseClassNamesFactory_optimized.ts
159 lines (115 loc) · 4.47 KB
/
createUseClassNamesFactory_optimized.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import "./tools/Object.fromEntries";
import { assert } from "evt/tools/typeSafety/assert";
import { typeGuard } from "evt/tools/typeSafety/typeGuard";
import { getRefFromDepsFactory } from "./tools/getRefFromDeps";
import { areSameSet } from "./tools/areSameSet";
import { Polyfill as WeakMap } from "minimal-polyfills/WeakMap";
import { css } from "@emotion/css";
import type { CSSObject } from "@emotion/css";
import { objectKeys } from "./tools/objectKeys";
/**
* !EXPERIMENTAL!
*
* Functionally equivalent from the exported but with optimization
* mechanism implemented.
*
* Implementation of JSS createStyles() based on @emotion/react
*
* const { useCssRecord } = createUseClassNames<Props & { color: "red" | "blue" }>()({
* (theme, { color })=> ({
* "root": { color }
* })
* });
*
*
* function MyComponent(props: Props){
*
* const [ color, setColor ]= useState<"red" | "blue">("red");
*
* const { classNames }=useClassName({...props, color });
*
* return <span className={classNames.root}>hello world</span>;
*
* }
*
*/
export function createUseClassNamesFactory<Theme extends Object = {}>(
params: {
useTheme(): Theme;
}
) {
const { useTheme } = params;
function createUseClassNames<Params extends Record<string, unknown>>() {
return function <Key extends string>(
getCssObjects: (theme: Theme, params: Params) => Record<Key, CSSObject>
) {
const getClassNamesAndAccessedParamsKeys = (theme: Theme, params: Params) => {
const accessedParamsKeys = new Set<Key>();
const cssObjects = getCssObjects(
theme,
new Proxy(
params,
{
"get": (...args) => {
const [, prop] = args;
assert(
typeof prop === "string" &&
typeGuard<Key>(prop)
);
accessedParamsKeys.add(prop);
return Reflect.get(...args);
}
}
)
);
return {
"classNames": Object.fromEntries(
objectKeys(cssObjects)
.map(key => [key, css(cssObjects[key])])
) as Record<Key, string>,
accessedParamsKeys
};
};
const arrayOfAccessedParamsKeys: Set<Key>[] = [];
const classNamesByRef = new WeakMap<Object, Record<Key, string>>();
const { getRef } = (() => {
const getDeps = (accessedParamsKeys: Set<Key>, params: Params) =>
[...accessedParamsKeys].map(key => params[key])
const { getRefFromDeps } = getRefFromDepsFactory({ "max": 50 });
function getRef(theme: Theme, accessedParamsKeys: Set<Key>, params: Params) {
return getRefFromDeps([theme, ...getDeps(accessedParamsKeys, params)])
}
return { getRef };
})();
function useClassNames(params: Params) {
const theme = useTheme();
for (const accessedParamsKeys of arrayOfAccessedParamsKeys) {
const classNames = classNamesByRef.get(
getRef(theme, accessedParamsKeys, params)
);
if (classNames !== undefined) {
return { classNames };
}
}
const { classNames, accessedParamsKeys } = getClassNamesAndAccessedParamsKeys(theme, params);
if (
arrayOfAccessedParamsKeys.every(
accessedParamsKeys_i => !areSameSet(
accessedParamsKeys_i,
accessedParamsKeys
)
)
) {
arrayOfAccessedParamsKeys.push(accessedParamsKeys);
}
classNamesByRef.set(
getRef(theme, accessedParamsKeys, params),
classNames
);
return { classNames };
}
return { useClassNames };
}
}
return { createUseClassNames };
}