From 5b80856eda59abfb81d3daf3be2d5d07b36e1b9a Mon Sep 17 00:00:00 2001 From: Riccardo Perra Date: Sat, 28 Oct 2023 11:22:57 +0200 Subject: [PATCH] allow to inject states from parent context --- examples/counter/src/components/Counter.tsx | 33 ++++++++++- packages/state/src/container.ts | 63 ++++++++++----------- packages/state/src/index.ts | 7 ++- packages/state/src/root.ts | 36 ++++++++++++ packages/state/src/solid/index.ts | 10 +++- packages/state/src/solid/provider.ts | 31 +++++----- packages/state/src/solid/resource.ts | 23 +++++--- packages/state/test/resource.test.ts | 11 ++-- 8 files changed, 147 insertions(+), 67 deletions(-) create mode 100644 packages/state/src/root.ts diff --git a/examples/counter/src/components/Counter.tsx b/examples/counter/src/components/Counter.tsx index 98bc8fc..5cc7e45 100644 --- a/examples/counter/src/components/Counter.tsx +++ b/examples/counter/src/components/Counter.tsx @@ -1,5 +1,10 @@ import './Counter.css'; -import { defineStore, InjectFlags, provideState } from 'statebuilder'; +import { + defineSignal, + defineStore, + provideState, + StateProvider, +} from 'statebuilder'; import { withProxyCommands } from 'statebuilder/commands'; import { withReduxDevtools } from 'statebuilder/devtools'; import { withAsyncAction } from 'statebuilder/asyncAction'; @@ -26,6 +31,11 @@ function appReducer(state: AppState, action: AppActions) { } } +const GlobalCount = defineSignal(() => 1).extend((_, context) => { + context.hooks.onInit(() => console.log('init count2')); + context.hooks.onDestroy(() => console.log('destroy count2')); +}); + const CountStore = defineStore(() => ({ count: 0, })) @@ -54,11 +64,19 @@ const CountStore = defineStore(() => ({ }; }); -export default function Counter() { - const store = provideState(CountStore, InjectFlags.global); +function Counter() { + const store = provideState(CountStore); + const globalCount = provideState(GlobalCount); return ( <> + +