Skip to content

Commit

Permalink
auto start effects by default (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
kepta authored Nov 13, 2023
1 parent 655396b commit 4ea3ba3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
9 changes: 9 additions & 0 deletions documentation/pages/docs/api/store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ Example:
store.dispatch(slice.someAction());
```

### `.config`

Get the store configuration passed during the creation of the store.

Signature:

```ts
store.config: Record<string, any>;
```

### `.destroy()`

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/base-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export abstract class BaseStore {
abstract _rootStore: Store<any>;

abstract dispatch(txn: Transaction<any, any> | Operation): void;

abstract readonly config: Record<string, any>;
}
4 changes: 4 additions & 0 deletions packages/core/src/effect/effect-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class EffectStore<TSliceName extends string = any> extends BaseStore {
return this._rootStore.state;
}

get config(): Record<string, any> {
return this._rootStore.config;
}

dispatch(txn: Transaction<any, any>) {
// TODO consider freeze, where we prevent dispatching txns post effect cleanup
// if user wants that behaviour
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/effect/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export class OperationStore extends BaseStore {
private readonly _cleanupCallbacks: Set<EffectCleanupCallback> = new Set();

_rootStore: Store<any>;

get config(): Record<string, any> {
return this._rootStore.config;
}

constructor(
private rootStore: Store<any>,
public readonly name: string,
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Operation } from './effect/operation';
import type { Slice } from './slice/slice';
import type { SliceId } from './types';
import { Transaction } from './transaction';
import { genEffectId, genStoreId } from './helpers/id-generation';
import { genStoreId } from './helpers/id-generation';
import { onAbortOnce } from './effect/on-abort';
import { calcReverseDependencies } from './helpers/dependency-helpers';
import type {
Expand All @@ -21,7 +21,16 @@ export interface StoreOptions<TSliceName extends string> {
name?: string;
slices: Slice<any, TSliceName, any>[];
debug?: DebugLogger;
/**
* If true, effects will be started automatically when the store is created.
* Defaults to true.
*/
autoStartEffects?: boolean | undefined;
/**
* config can be used to store any information about the store.
* This can come handy to pass config information to effects and slices
*/
config?: Record<string, any>;
overrides?: {
stateOverride?: Record<SliceId, Record<string, unknown>>;
/**
Expand All @@ -47,10 +56,15 @@ type DispatchTransaction<TSliceName extends string> = (
tx: Transaction<any, any>,
) => void;

const defaultOptions: StoreOptions<any> = {
autoStartEffects: true,
slices: [],
};

export function createStore<TSliceName extends string>(
config: StoreOptions<TSliceName>,
): Store<TSliceName> {
return new Store<TSliceName>(config);
return new Store<TSliceName>({ ...defaultOptions, ...config });
}

type StoreComputed = {
Expand Down Expand Up @@ -102,6 +116,7 @@ export class Store<TSliceName extends string = any> extends BaseStore {
public get destroySignal() {
return this.destroyController.signal;
}
public readonly config: Record<string, any>;

// @internal
readonly _computed: StoreComputed;
Expand All @@ -117,6 +132,7 @@ export class Store<TSliceName extends string = any> extends BaseStore {
constructor(public readonly options: StoreOptions<TSliceName>) {
super();
this.uid = genStoreId.generate(options.name || 'unnamed-store');
this.config = options.config || {};

this._state = StoreState.create({
slices: options.slices,
Expand Down

0 comments on commit 4ea3ba3

Please sign in to comment.