rn-css 1.6.4 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,12 +25,19 @@ function buildCSSString(chunks, functs, props, shared) {
25
25
  computedString += props.rnCSS.replace(/=/gm, ':') + ';';
26
26
  return computedString;
27
27
  }
28
- function initFromCSS(css) {
29
- const hash = (0, generateHash_1.default)(css);
30
- const rns = (0, cssToRN_1.default)(css);
28
+ function convertCSS(css, init) {
31
29
  (0, cssToRN_1.default)(css);
32
- styleMap[hash] = { style: rns, usages: 0 };
33
- return rns;
30
+ const hash = (0, generateHash_1.default)(css);
31
+ const style = styleMap[hash];
32
+ if (style) {
33
+ style.usages++;
34
+ return style;
35
+ }
36
+ else {
37
+ const rns = (0, cssToRN_1.default)(css);
38
+ styleMap[hash] = { style: rns, usages: init ? 0 : 1, hash };
39
+ return styleMap[hash];
40
+ }
34
41
  }
35
42
  const styled = (Component) => {
36
43
  const styledComponent = (chunks, ...functs) => {
@@ -40,28 +47,18 @@ const styled = (Component) => {
40
47
  // Build the css string with the context
41
48
  const css = react_1.default.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared]);
42
49
  // Store the style in RN format
43
- const [rnStyle, setRNStyle] = react_1.default.useState(() => initFromCSS(css));
50
+ const [rnStyle, setRNStyle] = react_1.default.useState(() => convertCSS(css, true).style);
44
51
  react_1.default.useEffect(() => {
45
52
  // Try to load an existing style from the style map or save it for next time
46
- const hash = (0, generateHash_1.default)(css);
47
- const style = styleMap[hash];
48
- if (style) {
49
- setRNStyle(style.style);
50
- style.usages++;
51
- }
52
- else {
53
- const rns = (0, cssToRN_1.default)(css);
54
- setRNStyle(rns);
55
- styleMap[hash] = { style: rns, usages: 1 };
56
- }
53
+ const style = convertCSS(css, false);
54
+ setRNStyle(style.style);
57
55
  // When the style is not used anymore, we destroy it
58
56
  return () => {
59
- const style = styleMap[hash];
60
57
  setTimeout(() => {
61
58
  style.usages--;
62
59
  if (style.usages <= 0)
63
- delete styleMap[hash];
64
- }, 3000);
60
+ delete styleMap[style.hash];
61
+ }, 300);
65
62
  };
66
63
  }, [css]);
67
64
  const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
package/dist/types.d.ts CHANGED
@@ -83,5 +83,6 @@ export declare type StyleMap = {
83
83
  [key: string]: {
84
84
  usages: number;
85
85
  style: Style;
86
+ hash: string;
86
87
  };
87
88
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-css",
3
- "version": "1.6.4",
3
+ "version": "1.6.5",
4
4
  "scripts": {
5
5
  "test": "jest",
6
6
  "prepare": "tsc",
@@ -33,12 +33,19 @@ function buildCSSString<T extends { rnCSS?: string }> (chunks: TemplateStringsAr
33
33
  if (props.rnCSS) computedString += props.rnCSS.replace(/=/gm, ':') + ';'
34
34
  return computedString
35
35
  }
36
- function initFromCSS (css: string) {
37
- const hash = calculHash(css)
38
- const rns = cssToStyle(css)
36
+ function convertCSS (css: string, init?: boolean) {
39
37
  cssToStyle(css)
40
- styleMap[hash] = { style: rns, usages: 0 }
41
- return rns
38
+ const hash = calculHash(css)
39
+ const style = styleMap[hash]
40
+ if (style) {
41
+ style.usages++
42
+ return style
43
+ }
44
+ else {
45
+ const rns = cssToStyle(css)
46
+ styleMap[hash] = { style: rns, usages: init ? 0 : 1, hash }
47
+ return styleMap[hash]
48
+ }
42
49
  }
43
50
  const styled = <Props, >(Component: React.ComponentType<Props>) => {
44
51
  const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
@@ -48,27 +55,17 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
48
55
  // Build the css string with the context
49
56
  const css = React.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared])
50
57
  // Store the style in RN format
51
- const [rnStyle, setRNStyle] = React.useState<Style>(() => initFromCSS(css))
58
+ const [rnStyle, setRNStyle] = React.useState<Style>(() => convertCSS(css, true).style)
52
59
  React.useEffect(() => {
53
60
  // Try to load an existing style from the style map or save it for next time
54
- const hash = calculHash(css)
55
- const style = styleMap[hash]
56
- if (style) {
57
- setRNStyle(style.style)
58
- style.usages++
59
- }
60
- else {
61
- const rns = cssToStyle(css)
62
- setRNStyle(rns)
63
- styleMap[hash] = { style: rns, usages: 1 }
64
- }
61
+ const style = convertCSS(css, false)
62
+ setRNStyle(style.style)
65
63
  // When the style is not used anymore, we destroy it
66
64
  return () => {
67
- const style = styleMap[hash]
68
65
  setTimeout(() => {
69
66
  style.usages--
70
- if (style.usages <= 0) delete styleMap[hash]
71
- }, 3000)
67
+ if (style.usages <= 0) delete styleMap[style.hash]
68
+ }, 300)
72
69
  }
73
70
  }, [css])
74
71
 
package/src/types.ts CHANGED
@@ -90,5 +90,6 @@ export type StyleMap = {
90
90
  [key: string]: {
91
91
  usages: number;
92
92
  style: Style;
93
+ hash: string;
93
94
  };
94
95
  }