@ringozz/react-godot 1.0.0-4 → 1.0.0-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.
- package/README.md +268 -280
- package/package.json +6 -2
- package/src/index.ts +58 -58
- package/src/react-fiber.ts +312 -307
- package/src/react-hooks.ts +25 -22
- package/src/react-jsx.ts +45 -41
- package/src/react-types.ts +39 -36
package/src/react-hooks.ts
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
/**********************************************************************
|
|
2
|
-
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
-
***********************************************************************/
|
|
4
|
-
|
|
5
|
-
import type { Signal } from '@ringozz/godot';
|
|
6
|
-
import type React from 'react';
|
|
7
|
-
import { useEffect, useRef } from 'react';
|
|
8
|
-
|
|
9
|
-
export function useMutableCallback<T>(fn: T): React.RefObject<T> {
|
|
10
|
-
const ref = useRef<T>(fn);
|
|
11
|
-
useEffect(() => void (ref.current = fn), [fn]);
|
|
12
|
-
return ref;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function useSignal<T extends (...args: any[]) => any>(signal: Signal<T>, handler: T): void {
|
|
16
|
-
const ref = useMutableCallback(handler);
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
const fn = ((...args: any[]) => ref.current(...args)) as T;
|
|
19
|
-
signal.connect(fn);
|
|
20
|
-
return () => signal.disconnect(fn);
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
/**********************************************************************
|
|
2
|
+
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
+
***********************************************************************/
|
|
4
|
+
|
|
5
|
+
import type { Signal } from '@ringozz/godot';
|
|
6
|
+
import type React from 'react';
|
|
7
|
+
import { useEffect, useRef } from 'react';
|
|
8
|
+
|
|
9
|
+
export function useMutableCallback<T>(fn: T): React.RefObject<T> {
|
|
10
|
+
const ref = useRef<T>(fn);
|
|
11
|
+
useEffect(() => void (ref.current = fn), [fn]);
|
|
12
|
+
return ref;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useSignal<T extends (...args: any[]) => any>(signal: Signal<T>, handler: T): void {
|
|
16
|
+
const ref = useMutableCallback(handler);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const fn = ((...args: any[]) => ref.current(...args)) as T;
|
|
19
|
+
signal.connect(fn);
|
|
20
|
+
return () => signal.disconnect(fn);
|
|
21
|
+
// do not specify 'signal' dependency, because it changes often.
|
|
22
|
+
// specify invariant contents instead.
|
|
23
|
+
/* oxlint-disable-next-line react-hooks/exhaustive-deps */
|
|
24
|
+
}, [signal.getObjectId(), signal.getName()]);
|
|
25
|
+
}
|
package/src/react-jsx.ts
CHANGED
|
@@ -1,41 +1,45 @@
|
|
|
1
|
-
/**********************************************************************
|
|
2
|
-
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
-
***********************************************************************/
|
|
4
|
-
|
|
5
|
-
import { jsxDEV as reactJsxDEV } from 'react/jsx-dev-runtime';
|
|
6
|
-
import type * as ReactJSX from 'react/jsx-runtime';
|
|
7
|
-
import { Fragment, jsx as reactJsx, jsxs as reactJsxs } from 'react/jsx-runtime';
|
|
8
|
-
import type { ComponentProps,
|
|
9
|
-
import { GodotVar } from '@ringozz/godot/runtime';
|
|
10
|
-
|
|
11
|
-
export function jsx(type: any, props?: any, key?: any) {
|
|
12
|
-
return reactJsx(GodotVar.isPrototypeOf(type) ? type.name : type, props, key);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function jsxs(type: any, props?: any, key?: any) {
|
|
16
|
-
return reactJsxs(GodotVar.isPrototypeOf(type) ? type.name : type, props, key);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function jsxDEV(type: any, props?: any, key?: any, isStatic?: any, source?: any, self?: any) {
|
|
20
|
-
return reactJsxDEV(GodotVar.isPrototypeOf(type) ? type.name : type, props, key, isStatic, source, self);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { Fragment };
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
/**********************************************************************
|
|
2
|
+
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
+
***********************************************************************/
|
|
4
|
+
|
|
5
|
+
import { jsxDEV as reactJsxDEV } from 'react/jsx-dev-runtime';
|
|
6
|
+
import type * as ReactJSX from 'react/jsx-runtime';
|
|
7
|
+
import { Fragment, jsx as reactJsx, jsxs as reactJsxs } from 'react/jsx-runtime';
|
|
8
|
+
import type { ComponentProps, GodotConstructor } from './react-types.ts';
|
|
9
|
+
import { GodotVar } from '@ringozz/godot/runtime';
|
|
10
|
+
|
|
11
|
+
export function jsx(type: any, props?: any, key?: any) {
|
|
12
|
+
return reactJsx(GodotVar.isPrototypeOf(type) ? type.name : type, props, key);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function jsxs(type: any, props?: any, key?: any) {
|
|
16
|
+
return reactJsxs(GodotVar.isPrototypeOf(type) ? type.name : type, props, key);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function jsxDEV(type: any, props?: any, key?: any, isStatic?: any, source?: any, self?: any) {
|
|
20
|
+
return reactJsxDEV(GodotVar.isPrototypeOf(type) ? type.name : type, props, key, isStatic, source, self);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { Fragment };
|
|
24
|
+
|
|
25
|
+
// JSX namespace augmentation: these types are the public JSX contract for the
|
|
26
|
+
// `@ringozz/react-godot/jsx-runtime` import source, consumed by TypeScript in
|
|
27
|
+
// consumer files. They are intentionally not referenced within this module.
|
|
28
|
+
/* oxlint-disable no-unused-vars */
|
|
29
|
+
declare module '@ringozz/react-godot/jsx-runtime' {
|
|
30
|
+
export namespace JSX {
|
|
31
|
+
interface IntrinsicAttributes extends ReactJSX.JSX.IntrinsicAttributes { }
|
|
32
|
+
interface IntrinsicElements extends ReactJSX.JSX.IntrinsicElements { }
|
|
33
|
+
interface Element extends ReactJSX.JSX.Element { }
|
|
34
|
+
|
|
35
|
+
type ElementType =
|
|
36
|
+
| ReactJSX.JSX.ElementType
|
|
37
|
+
| GodotConstructor;
|
|
38
|
+
|
|
39
|
+
type LibraryManagedAttributes<C, P> =
|
|
40
|
+
C extends GodotConstructor
|
|
41
|
+
? ComponentProps<C>
|
|
42
|
+
: ReactJSX.JSX.LibraryManagedAttributes<C, P>;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/* oxlint-enable no-unused-vars */
|
package/src/react-types.ts
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
|
-
/**********************************************************************
|
|
2
|
-
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
-
***********************************************************************/
|
|
4
|
-
|
|
5
|
-
import type React from 'react';
|
|
6
|
-
import type { Object } from '@ringozz/godot/Object';
|
|
7
|
-
import type { ValueTypes } from '@ringozz/godot';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
/**********************************************************************
|
|
2
|
+
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
+
***********************************************************************/
|
|
4
|
+
|
|
5
|
+
import type React from 'react';
|
|
6
|
+
import type { Object } from '@ringozz/godot/Object';
|
|
7
|
+
import type { ValueTypes } from '@ringozz/godot';
|
|
8
|
+
import type { PackedScene } from '@ringozz/godot/PackedScene';
|
|
9
|
+
|
|
10
|
+
type FunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T];
|
|
11
|
+
type Properties<T> = Omit<T, FunctionKeys<T>>;
|
|
12
|
+
type Overwrite<P, O> = Properties<P> & O;
|
|
13
|
+
type Mutable<P> = { [K in keyof P]: P[K] | Readonly<P[K]> };
|
|
14
|
+
type ConstructorRepresentation<T = any> = new (...args: any[]) => T;
|
|
15
|
+
|
|
16
|
+
export type Instance = Object;
|
|
17
|
+
|
|
18
|
+
export type GodotConstructor<T extends Instance = Instance> = ConstructorRepresentation<T> & Function;
|
|
19
|
+
|
|
20
|
+
export type InstanceProps<T extends Instance = Instance> = {
|
|
21
|
+
/** An existing instance to render instead of creating a new one. */
|
|
22
|
+
object?: T | PackedScene;
|
|
23
|
+
/** Attaches the element to a named property of the parent instead of adding it as a child. */
|
|
24
|
+
attach?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type ReactProps<P> = React.PropsWithChildren<React.RefAttributes<P>>;
|
|
28
|
+
|
|
29
|
+
type WidenVT<T> = {
|
|
30
|
+
[K in keyof T]: T[K] extends ValueTypes ? T[K] | number[] : T[K];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type ElementProps<T extends GodotConstructor, P = InstanceType<T>> = Partial<
|
|
34
|
+
Overwrite<WidenVT<P>, ReactProps<P>>
|
|
35
|
+
>;
|
|
36
|
+
|
|
37
|
+
export type ComponentProps<T extends GodotConstructor> = Mutable<
|
|
38
|
+
Overwrite<ElementProps<T>, InstanceProps<InstanceType<T>>>
|
|
39
|
+
>;
|