Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move createActions to core #5

Open
krispya opened this issue Oct 21, 2024 · 0 comments
Open

Move createActions to core #5

krispya opened this issue Oct 21, 2024 · 0 comments

Comments

@krispya
Copy link
Member

krispya commented Oct 21, 2024

Right now createActions lives in the React package and gets used as a hook.

import { createActions } from 'koota/react';

const useMyActions = createActions((world) => ({
    spawnShip: (position) => world.spawn(Position(position), Velocity),
    destroyAllShips: (world) => {
        world.query(Position, Velocity).forEach((entity) => {
            entity.destroy();
        });
    },
}));

function Component() {
    const { spawnShip, destroyAllShips } = useMyActions();
}

And then can be used in vanilla with an additional API call that binds world to the actions manually.

    const { spawnShip, destroyAllShips } = useMyActions.get(world)

This was inspired by the Zustand API but it turns out actions are really useful for vanilla making me think it is better to flip this around and make it a core export that always takes a world to bind.

import { createActions } from 'koota';

const useMyActions = createActions((world) => ({
    spawnShip: (position) => world.spawn(Position(position), Velocity),
    destroyAllShips: (world) => {
        world.query(Position, Velocity).forEach((entity) => {
            entity.destroy();
        });
    },
}));

function Component() {
    const world = useWorld()
    // Bind world manually. Can choose the world instead of getting it from context.
    const { spawnShip, destroyAllShips } = useMyActions(world);
}

// This is the exact same API for vanilla.
const { spawnShip, destroyAllShips } = useMyActions(world);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant