Skip to content

Commit d243d9d

Browse files
committed
Code cleanup
1 parent d2b0e19 commit d243d9d

File tree

5 files changed

+513
-492
lines changed

5 files changed

+513
-492
lines changed

package.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@
5050
"@babel/plugin-proposal-class-properties": "^7.18.6",
5151
"@babel/preset-env": "^7.20.2",
5252
"@babel/preset-react": "^7.18.6",
53-
"@storybook/addon-actions": "^7.0.0-beta.25",
54-
"@storybook/addon-essentials": "^7.0.0-beta.25",
55-
"@storybook/addon-interactions": "^7.0.0-beta.25",
56-
"@storybook/addon-links": "^7.0.0-beta.25",
57-
"@storybook/react": "^7.0.0-beta.25",
58-
"@storybook/react-webpack5": "^7.0.0-beta.25",
53+
"@storybook/addon-actions": "^7.0.0-beta.28",
54+
"@storybook/addon-essentials": "^7.0.0-beta.28",
55+
"@storybook/addon-interactions": "^7.0.0-beta.28",
56+
"@storybook/addon-links": "^7.0.0-beta.28",
57+
"@storybook/react": "^7.0.0-beta.28",
58+
"@storybook/react-webpack5": "^7.0.0-beta.28",
5959
"@storybook/testing-library": "^0.0.14-next.1",
60+
"@types/react-reconciler": "^0.28.2",
6061
"alea": "^1.0.1",
6162
"babel-loader": "^9.1.2",
6263
"d3-scale": "^4.0.2",
@@ -71,21 +72,22 @@
7172
"eslint-plugin-storybook": "^0.6.10",
7273
"lodash.range": "^3.2.0",
7374
"prettier": "^2.8.2",
74-
"react": "^18.2.0",
75-
"react-dom": "^18.2.0",
75+
"react": "==18.2.0",
76+
"react-dom": "==18.2.0",
7677
"rimraf": "^3.0.2",
77-
"storybook": "^7.0.0-beta.25"
78+
"storybook": "^7.0.0-beta.28"
7879
},
7980
"peerDependencies": {
80-
"react": "^18.2.0",
81-
"react-dom": "^18.2.0"
81+
"react": "==18.2.0",
82+
"react-dom": "==18.2.0"
8283
},
8384
"dependencies": {
8485
"css-layout": "^1.1.1",
8586
"css-line-break": "^2.1.0",
8687
"invariant": "^2.2.4",
8788
"multi-key-cache": "^1.0.2",
8889
"prop-types": "^15.8.1",
89-
"react-reconciler": "^0.29.0"
90+
"react-reconciler": "==0.28.0",
91+
"scheduler": "==0.22.0"
9092
}
9193
}

src/Canvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Object.assign(Canvas.prototype, {
2222
},
2323

2424
getContext() {
25-
return this._canvas?.getContext('2d')
25+
return this._canvas ? this._canvas.getContext('2d') : undefined
2626
}
2727
})
2828

src/CanvasRenderer.js

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import invariant from 'invariant'
33
import ReactFiberReconciler from 'react-reconciler'
44
import { DefaultEventPriority } from 'react-reconciler/constants'
5+
import { unstable_now as now } from 'scheduler'
56
import { emptyObject } from './utils'
67
import Gradient from './Gradient'
78
import Text from './Text'
@@ -50,38 +51,12 @@ const freeComponentAndChildren = (c) => {
5051
freeComponentToPool(c)
5152
}
5253

54+
/** @type {ReactFiberReconciler.HostConfig} */
5355
const CanvasHostConfig = {
54-
supportsHydration: false,
55-
supportsPersistence: true,
56-
cancelTimeout: clearTimeout,
57-
scheduleTimeout: setTimeout,
58-
noTimeout: -1,
59-
detachDeletedInstance() {},
60-
getInstanceFromScope() {
61-
return null
62-
},
63-
preparePortalMount() {},
64-
afterActiveInstanceBlur() {},
65-
beforeActiveInstanceBlur() {},
66-
getCurrentEventPriority() {
67-
return DefaultEventPriority
68-
},
69-
getInstanceFromNode() {
70-
return undefined
71-
},
72-
prepareScopeUpdate() {},
73-
clearContainer() {},
74-
appendInitialChild(parentInstance, child) {
75-
if (typeof child === 'string') {
76-
// Noop for string children of Text (eg <Text>{'foo'}{'bar'}</Text>)
77-
invariant(false, 'Text children should already be flattened.')
78-
return
79-
}
80-
81-
child.getLayer().inject(parentInstance.getLayer())
82-
},
56+
supportsMutation: true,
57+
supportsPersistence: false,
8358

84-
createInstance(type, props /* , internalInstanceHandle */) {
59+
createInstance(type, props) {
8560
let instance
8661

8762
const pool = componentPool[type]
@@ -100,34 +75,32 @@ const CanvasHostConfig = {
10075
return instance
10176
},
10277

103-
createTextInstance(
104-
text /* , rootContainerInstance, internalInstanceHandle */
105-
) {
78+
createTextInstance(text) {
10679
return text
10780
},
10881

109-
finalizeInitialChildren(/* domElement, type, props */) {
110-
return false
111-
},
82+
appendInitialChild(parentInstance, child) {
83+
if (typeof child === 'string') {
84+
// Noop for string children of Text (eg <Text>{'foo'}{'bar'}</Text>)
85+
invariant(false, 'Text children should already be flattened.')
86+
return
87+
}
11288

113-
getPublicInstance(instance) {
114-
return instance
89+
child.getLayer().inject(parentInstance.getLayer())
11590
},
11691

117-
prepareForCommit() {
118-
return null
92+
finalizeInitialChildren() {
93+
return false
11994
},
12095

121-
prepareUpdate(/* domElement, type, oldProps, newProps */) {
96+
prepareUpdate() {
12297
return UPDATE_SIGNAL
12398
},
12499

125-
resetAfterCommit() {
126-
// Noop
127-
},
128-
129-
shouldDeprioritizeSubtree(/* type, props */) {
130-
return false
100+
shouldSetTextContent(type, props) {
101+
return (
102+
typeof props.children === 'string' || typeof props.children === 'number'
103+
)
131104
},
132105

133106
getRootHostContext() {
@@ -138,15 +111,31 @@ const CanvasHostConfig = {
138111
return emptyObject
139112
},
140113

141-
shouldSetTextContent(type, props) {
142-
return (
143-
typeof props.children === 'string' || typeof props.children === 'number'
144-
)
114+
getPublicInstance(instance) {
115+
return instance
145116
},
146117

147-
isPrimaryRenderer: false,
118+
prepareForCommit() {
119+
return null
120+
},
148121

149-
supportsMutation: true,
122+
resetAfterCommit() {
123+
// Noop
124+
},
125+
126+
preparePortalMount() {},
127+
128+
now,
129+
130+
scheduleTimeout: setTimeout,
131+
132+
cancelTimeout: clearTimeout,
133+
134+
noTimeout: -1,
135+
136+
queueMicrotask,
137+
138+
isPrimaryRenderer: false,
150139

151140
appendChild(parentInstance, child) {
152141
const childLayer = child.getLayer()
@@ -161,6 +150,26 @@ const CanvasHostConfig = {
161150
parentLayer.invalidateLayout()
162151
},
163152

153+
getCurrentEventPriority() {
154+
return DefaultEventPriority
155+
},
156+
157+
getInstanceFromNode() {
158+
return undefined
159+
},
160+
161+
beforeActiveInstanceBlur() {},
162+
163+
afterActiveInstanceBlur() {},
164+
165+
prepareScopeUpdate() {},
166+
167+
getInstanceFromScope() {
168+
return null
169+
},
170+
171+
detachDeletedInstance() {},
172+
164173
appendChildToContainer(parentInstance, child) {
165174
const childLayer = child.getLayer()
166175
const parentLayer = parentInstance.getLayer()
@@ -205,7 +214,11 @@ const CanvasHostConfig = {
205214
instance.applyLayerProps(oldProps, newProps)
206215
instance.getLayer().invalidateLayout()
207216
}
208-
}
217+
},
218+
219+
clearContainer() {},
220+
221+
supportsHydration: false
209222
}
210223

211224
const CanvasRenderer = ReactFiberReconciler(CanvasHostConfig)

src/Surface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Surface extends React.Component {
7575
// =======
7676
getLayer = () => this.node
7777

78-
getContext = () => this.canvas?.getContext('2d')
78+
getContext = () => (this.canvas ? this.canvas.getContext('2d') : undefined)
7979

8080
scale = () => {
8181
const ctx = this.getContext()

0 commit comments

Comments
 (0)