0.8.3 • Published 8 months ago
@tinyslice/core v0.8.3
tinyslice/core 🍕
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 });
0.8.3
8 months ago
0.8.2
1 year ago
0.8.1
1 year ago
0.8.0
2 years ago
0.6.6
3 years ago
0.3.0
3 years ago
0.6.3
3 years ago
0.4.5
3 years ago
0.2.7
3 years ago
0.6.2
3 years ago
0.4.4
3 years ago
0.2.6
3 years ago
0.6.5
3 years ago
0.6.4
3 years ago
0.5.0
3 years ago
0.4.1
3 years ago
0.3.2
3 years ago
0.2.3
3 years ago
0.4.0
3 years ago
0.3.1
3 years ago
0.7.0
3 years ago
0.6.1
3 years ago
0.4.3
3 years ago
0.3.4
3 years ago
0.2.5
3 years ago
0.6.0
3 years ago
0.5.1
3 years ago
0.4.2
3 years ago
0.3.3
3 years ago
0.2.4
3 years ago
0.2.2
3 years ago
0.2.1
3 years ago
0.2.0
3 years ago
0.1.3
3 years ago
0.1.2
3 years ago
0.1.1
3 years ago