spierala / mini-rx-store

MiniRx - The reactive state management platform
https://mini-rx.io
Other
170 stars 9 forks source link

feature(mini-rx-store): lazy feature store initialization #149

Closed spierala closed 1 year ago

spierala commented 1 year ago

Sometimes it can be useful to provide initialState later, using setInitialState. Especially when managing local component state: Imagine the initial state depends on component Inputs / props.

const fs = new FeatureStore<TodosState>('test', undefined); // Set undefined explicitly as initial state
const fsState$ = fs.select();
fsState$.subscribe((v) => console.log('# test', v));

// Test lazy initialization
setTimeout(() => {
    fs.setInitialState(initialState); // Provide initial state later
}, 5000);

setTimeout(() => {
    // Use setState as usual
    fs.setState({
        todos: [{ id: 123, title: 'test', isDone: false }],
    });
}, 6000);