@tinyslice/core
TypeScript icon, indicating that this package has built-in type declarations

0.8.1 • Public • Published

tinyslice/core 🍕

Latest NPM Version CI Codacy Badge codecov

Reactive state management

Check out the example projects in this repository and my Minesweeper clone build using this.

import { Scope } from '@tinyslice/core';
import { TinySliceDevtoolPlugin } from '@tinyslice/devtools-plugin';
import { TinySliceHydrationPlugin } from '@tinyslice/hydration-plugin';

const scope = new Scope();

interface RootSlice {
  count: number;
}
const rootSlice$ = scope.createRootSlice({ count: 1, pies: {} } as RootSlice, {
  plugins: [
    new TinySliceDevtoolPlugin({
      name: 'myExampleApp',
    }),
  ],
});

const increment = scope.createAction('increment');
const countSlice$ = rootSlice$.slice('count', {
  reducers: [increment.reduce((state) => state + 1)],
});

countSlice$.subscribe((count) => console.log('count', count));
increment.next(); // Use custom action to trigger reducer
countSlice$.set(10); // Use premade actions and reducers

// "Entity" pattern.
export interface PieState {
  sauce: number;
  cheese: number;
}

// This creates 2 layes at once, the first one is a Record<number, PieState>
const pieDicer = rootSlice$.addDicedSlice(
  'pies',
  { cheese: 0, sauce: 0 } as PieState,
  {
    defineInternals: (slice) => {
      const cheese$ = slice.slice('cheese');
      const sauce$ = slice.slice('sauce');
      return { cheese$, sauce$ };
    },
    // Plugins can be anywhere, save this slice to localstorage and read as initialised!
    plugins: [new TinySliceHydrationPlugin<PieState>('pies')],
  },
);

// To get a specific entity slice
const firstPie = pieDicer.get(1);

firstPie.internals.cheese$.subscribe((cheese) => console.log('cheese', cheese));
firstPie.internals.cheese$.set(2);

pieDicer.set(2, { cheese: 12, sauce: 13 });

Package Sidebar

Install

npm i @tinyslice/core

Weekly Downloads

4

Version

0.8.1

License

MIT

Unpacked Size

122 kB

Total Files

59

Last publish

Collaborators

  • alexaegis