-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
233 additions
and
152 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ComponentProps, memo, NamedExoticComponent, ReactElement } from 'react'; | ||
import { Computed } from './Computed'; | ||
import { isEmpty, isFunction } from '@legendapp/state'; | ||
import { createElement, FC, forwardRef } from 'react'; | ||
import { ReactiveFnBinders, ReactiveFns } from './configureReactive'; | ||
import { reactive } from './reactive-observer'; | ||
import { IReactive } from '@legendapp/state/react'; | ||
|
||
type ComputedWithMemo = (params: { | ||
children: ComponentProps<typeof Computed>['children']; | ||
scoped?: boolean; | ||
}) => ReactElement; | ||
|
||
const Memo = memo(Computed as ComputedWithMemo, (prev, next) => | ||
next.scoped ? prev.children === next.children : true, | ||
) as NamedExoticComponent<{ | ||
children: any; | ||
scoped?: boolean; | ||
}>; | ||
|
||
type ReactiveProxy = typeof Memo & IReactive; | ||
|
||
const setReactProps = new Set([ | ||
'$$typeof', | ||
'defaultProps', | ||
'propTypes', | ||
'tag', | ||
'PropTypes', | ||
'displayName', | ||
'getDefaultProps', | ||
'type', | ||
'compare', | ||
]); | ||
|
||
const reactives: Record<string, FC> = {}; | ||
|
||
export const $: ReactiveProxy = new Proxy(Memo as any, { | ||
get(target: Record<string, FC>, p: string) { | ||
if (Object.hasOwn(target, p) || setReactProps.has(p)) { | ||
return target[p]; | ||
} | ||
if (!reactives[p]) { | ||
const Component = ReactiveFns.get(p) || p; | ||
|
||
// Create a wrapper around createElement with the string so we can proxy it | ||
// eslint-disable-next-line react/display-name | ||
const render = forwardRef((props, ref) => { | ||
const propsOut = { ...props } as any; | ||
if (ref && (isFunction(ref) || !isEmpty(ref))) { | ||
propsOut.ref = ref; | ||
} | ||
return createElement(Component, propsOut); | ||
}); | ||
|
||
reactives[p] = reactive(render, [], ReactiveFnBinders.get(p)); | ||
} | ||
return reactives[p]; | ||
}, | ||
}) as unknown as ReactiveProxy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
import { memo } from 'react'; | ||
import { memo, ReactElement, NamedExoticComponent, ComponentProps } from 'react'; | ||
import { Computed } from './Computed'; | ||
|
||
export const Memo = memo(Computed, () => true); | ||
type ComputedWithMemo = (params: { | ||
children: ComponentProps<typeof Computed>['children']; | ||
scoped?: boolean; | ||
}) => ReactElement; | ||
|
||
export const Memo = memo(Computed as ComputedWithMemo, (prev, next) => | ||
next.scoped ? prev.children === next.children : true, | ||
) as NamedExoticComponent<{ | ||
children: any; | ||
scoped?: boolean; | ||
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.