Skip to content

Commit

Permalink
types: add generics to Transaction (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
kepta committed Oct 7, 2023
1 parent e3f55d7 commit 840fe30
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/base-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { StoreState } from './store-state';
import type { Transaction } from './transaction';

export type Dispatch = (
txn: Transaction | Operation,
txn: Transaction<any, any> | Operation,
opts?: {
debugInfo?: string;
},
Expand All @@ -16,5 +16,5 @@ export abstract class BaseStore {
// @internal
abstract _rootStore: Store<any>;

abstract dispatch(txn: Transaction | Operation): void;
abstract dispatch(txn: Transaction<any, any> | Operation): void;
}
2 changes: 1 addition & 1 deletion packages/core/src/effect/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class EffectStore<TSliceName extends string = any> extends BaseStore {
return this._rootStore.state;
}

dispatch(txn: Transaction | Operation) {
dispatch(txn: Transaction<any, any> | Operation) {
this._rootStore.dispatch(txn);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/effect/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class OperationStore extends BaseStore {
return this.rootStore.state;
}

dispatch(txn: Transaction | Operation) {
dispatch(txn: Transaction<any, any> | Operation) {
this.rootStore.dispatch(txn);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/slice/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class StateField<
._getFieldStateVal(this) as TVal;
}

update(val: TVal | ((val: TVal) => TVal)): Transaction {
update(val: TVal | ((val: TVal) => TVal)): Transaction<TName, TDep> {
const txn = this.key.transaction();

return txn.step((state: StoreState<any>) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/slice/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Key<TName extends string, TDepName extends string> {
actions,
}: {
fields: TFieldsSpec;
actions?: (...args: any) => Transaction;
actions?: (...args: any) => Transaction<TName, TDepName>;
}): Slice<TFieldsSpec, TName, TDepName> {
if (this._slice) {
throwValidationError(
Expand All @@ -80,7 +80,7 @@ export class Key<TName extends string, TDepName extends string> {
* Creates a new transaction object which is used to update the slice state.
*/
transaction() {
return new Transaction();
return new Transaction<TName, TDepName>();
}

// @internal
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/store-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class StoreState<TSliceName extends string> {
private config: StoreStateConfig,
) {}

apply(transaction: Transaction): StoreState<TSliceName> {
// TODO: add strict type checking to Txn.
apply(transaction: Transaction<any, any>): StoreState<TSliceName> {
if (transaction._isDestroyed()) {
throwValidationError(
`Transaction "${transaction.id}" has already been applied.`,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const DEFAULT_DISPATCH_TRANSACTION: DispatchTransaction<any> = (
type DispatchTransaction<TSliceName extends string> = (
store: Store<TSliceName>,
updateState: (state: StoreState<TSliceName>) => void,
tx: Transaction,
tx: Transaction<any, any>,
) => void;

export function createStore<TSliceName extends string>(
Expand Down Expand Up @@ -112,7 +112,7 @@ export class Store<TSliceName extends string> extends BaseStore {
});
}

dispatch(transaction: Transaction | Operation): void {
dispatch(transaction: Transaction<any, any> | Operation): void {
if (this.destroyed) {
return;
}
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { genTransactionID } from './helpers/id-generation';
import type { StoreState } from './store-state';

type Step = { stepper: (storeState: StoreState<any>) => StoreState<any> };
type Step<TName extends string = any> = {
stepper: (storeState: StoreState<TName>) => StoreState<TName>;
};

export const META_DISPATCHER = 'DEBUG__DISPATCHER';
export const TX_META_STORE_NAME = 'store-name';

export class Transaction {
export class Transaction<TName extends string, TDepName extends string> {
readonly id = genTransactionID();
readonly metadata = new Metadata();

Expand All @@ -25,7 +27,11 @@ export class Transaction {
return this.steps;
}

step(stepper: Step['stepper']): Transaction {
step(
// merge both TName and TDepName into one type, so its easier to use
// with StoreState
stepper: Step<TName | TDepName>['stepper'],
): Transaction<TName, TDepName> {
this.steps.push({ stepper });
return this;
}
Expand Down

0 comments on commit 840fe30

Please sign in to comment.