Skip to content

Commit

Permalink
feat(ref): export createRef
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 10, 2024
1 parent 1c83169 commit e3e3b6b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './components';
export { createElement, jsx } from './element';
export { createRef } from './ref';
export { render } from './render';
1 change: 1 addition & 0 deletions src/ref/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ref';
12 changes: 12 additions & 0 deletions src/ref/ref.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createRef } from './ref';

it('creates ref', () => {
expect(createRef()).toEqual({ current: null });
});

it('sets ref', () => {
type Type = 'value';
const ref = createRef<Type>();
ref.current = 'value';
expect(ref).toEqual({ current: 'value' });
});
12 changes: 12 additions & 0 deletions src/ref/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Ref } from '../types';

/**
* Creates a ref object which can contain arbitrary value.
*
* @returns - Creates an object with a single property `current` (initially set to `null`).
*/
export function createRef<Type>(): Ref<Type> {
return {
current: null,
};
}
2 changes: 1 addition & 1 deletion src/render/gameobject.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Phaser from 'phaser';
import type { JSX } from 'react';

import { Container } from '..';
import { Container, createRef } from '..';
import { createGameObject } from './gameobject';

jest.mock('phaser', () => {
Expand Down
21 changes: 14 additions & 7 deletions src/render/ref.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import type Phaser from 'phaser';

import { createRef } from '..';
import { attachRef } from './ref';

const gameObject = {} as Phaser.GameObjects.GameObject;

it('attaches ref', () => {
const mock = jest.fn();
expect(attachRef(gameObject, mock));
expect(mock).toBeCalledTimes(1);
expect(mock).toBeCalledWith(gameObject);
});

it('does not attach ref', () => {
expect(
attachRef(gameObject, null as unknown as Parameters<typeof attachRef>[1]),
);
});

it('attaches ref callback', () => {
const callback = jest.fn();
expect(attachRef(gameObject, callback));
expect(callback).toBeCalledTimes(1);
expect(callback).toBeCalledWith(gameObject);
});

it('attaches ref object', () => {
const ref = createRef<Phaser.GameObjects.GameObject>();
expect(attachRef(gameObject, ref));
expect(ref).toEqual({ current: gameObject });
});
2 changes: 2 additions & 0 deletions src/render/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export function attachRef(
) {
if (typeof ref === 'function') {
ref(gameObject);
} else if (ref && typeof ref === 'object') {
ref.current = gameObject;
}
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './events';
export * from './phaser';
export * from './props';
export * from './ref';
export * from './utils';
6 changes: 4 additions & 2 deletions src/types/props.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { JSX } from 'react';

import type { Events, GameObject, RecursivePartial } from '.';
import type { Events, GameObject, RecursivePartial, Ref } from '.';

type RefCallback<Type> = (gameObject: Type) => void;

interface ObjectProps<Type> extends Partial<Events> {
children?: JSX.Element | JSX.Element[] | null;
ref?: (gameObject: Type) => void;
ref?: RefCallback<Type> | Ref<Type>;
}

export type GameObjectProps<Type = GameObject> = ObjectProps<Type> &
Expand Down
3 changes: 3 additions & 0 deletions src/types/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Ref<Type> {
current: Type | null;
}

0 comments on commit e3e3b6b

Please sign in to comment.