Skip to content

Commit

Permalink
fix(akita): support TS strict mode
Browse files Browse the repository at this point in the history
This is a quick spike created to find out a way to fix akita for Angular 15+ in strict mode

Refs: salesforce#870
  • Loading branch information
Den-dp committed Feb 5, 2023
1 parent e8b7d6a commit 682f66e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/akita/src/lib/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ export class EntityStore<S extends EntityState = any, EntityType = getEntityType
}

// @internal
export class EntityUIStore<UIState, DEPRECATED = any> extends EntityStore<UIState> {
export class EntityUIStore<UIState extends EntityState, DEPRECATED = any> extends EntityStore<UIState> {
_akitaCreateEntityFn: EntityUICreateFn;

constructor(initialState = {}, storeConfig: Partial<StoreConfigOptions> = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isUndefined } from '../../isUndefined';
import { Query } from '../../query';
import { QueryEntity } from '../../queryEntity';
import { AkitaPlugin, Queries } from '../plugin';
import { EntityState } from '../../types';

type Head<State = any> = State | Partial<State>;

Expand All @@ -29,7 +30,7 @@ export type DirtyCheckResetParams<StoreState = any> = {
updateFn?: StoreState | ((head: StoreState, current: StoreState) => any);
};

export class DirtyCheckPlugin<State = any> extends AkitaPlugin<State> {
export class DirtyCheckPlugin<State extends EntityState = any> extends AkitaPlugin<State> {
private head: Head<State>;
private dirty = new BehaviorSubject(false);
private subscription: Subscription;
Expand Down
5 changes: 3 additions & 2 deletions packages/akita/src/lib/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { toBoolean } from '../toBoolean';
import { getAkitaConfig } from '../config';
import { getValue } from '../getValueByString';
import { setValue } from '../setValueByString';
import { EntityState } from '../types';

export type Queries<State> = Query<State> | QueryEntity<State>;
export type Queries<State extends EntityState> = Query<State> | QueryEntity<State>;

export abstract class AkitaPlugin<State = any> {
export abstract class AkitaPlugin<State extends EntityState = any> {
protected constructor(protected query: Queries<State>, config?: { resetFn?: Function }) {
if (config && config.resetFn) {
if (getAkitaConfig().resettable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BehaviorSubject, distinctUntilChanged, Observable, pairwise } from 'rxj
import { logAction } from '../../actions';
import { isFunction } from '../../isFunction';
import { AkitaPlugin, Queries } from '../plugin';
import { EntityState } from '../../types';

export interface StateHistoryParams {
maxAge?: number;
Expand All @@ -15,7 +16,7 @@ export type History<State> = {
future: State[];
};

export class StateHistoryPlugin<State = any> extends AkitaPlugin<State> {
export class StateHistoryPlugin<State extends EntityState = any> extends AkitaPlugin<State> {
/** Allow skipping an update from outside */
private skip = false;

Expand Down
2 changes: 1 addition & 1 deletion packages/akita/src/lib/queryEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export class QueryEntity<S extends EntityState, EntityType = getEntityType<S>, I
}

// @internal
export class EntityUIQuery<UIState, DEPRECATED = any> extends QueryEntity<UIState> {
export class EntityUIQuery<UIState extends EntityState, DEPRECATED = any> extends QueryEntity<UIState> {
constructor(store) {
super(store);
}
Expand Down
20 changes: 10 additions & 10 deletions packages/akita/src/lib/runStoreAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isNil } from './isNil';
import { Store } from './store';
import { configKey } from './storeConfig';
import { __stores__ } from './stores';
import { Constructor } from './types';
import { Constructor, EntityState } from './types';

export enum StoreAction {
Update = 'UPDATE',
Expand Down Expand Up @@ -60,15 +60,15 @@ export function getStoreByName<TStore extends Store<S>, S = TStore extends Store
* Get a {@link EntityStore} from the global store registry.
* @param storeClass The {@link EntityStore} class of the instance to be returned.
*/
export function getEntityStore<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : never>(storeClass: Constructor<TEntityStore>): TEntityStore {
export function getEntityStore<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : never>(storeClass: Constructor<TEntityStore>): TEntityStore {
return getStore(storeClass as Constructor<Store<S>>) as TEntityStore;
}

/**
* Get a {@link EntityStore} from the global store registry.
* @param storeName The {@link EntityStore} name of the instance to be returned.
*/
export function getEntityStoreByName<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : never>(storeName: string): TEntityStore {
export function getEntityStoreByName<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : never>(storeName: string): TEntityStore {
return getStoreByName<TEntityStore, S>(storeName) as TEntityStore;
}

Expand Down Expand Up @@ -106,7 +106,7 @@ export function runStoreAction<TStore extends Store<S>, S = TStore extends Store
* runEntityStoreAction(BooksStore, EntityStoreAction.SetEntities, set => set([{ id: 1 }, { id: 2 }]));
*
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.SetEntities,
operation: (operator: TEntityStore['set']) => void
Expand All @@ -121,7 +121,7 @@ export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TE
* runEntityStoreAction(BooksStore, EntityStoreAction.AddEntities, add => add({ id: 1 }));
*
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.AddEntities,
operation: (operator: TEntityStore['add']) => void
Expand All @@ -136,7 +136,7 @@ export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TE
* runEntityStoreAction(BooksStore, EntityStoreAction.UpdateEntities, update => update(2, { title: 'New title' }));
*
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.UpdateEntities,
operation: (operator: TEntityStore['update']) => void
Expand All @@ -151,7 +151,7 @@ export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TE
* runEntityStoreAction(BooksStore, EntityStoreAction.RemoveEntities, remove => remove(2));
*
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.RemoveEntities,
operation: (operator: TEntityStore['remove']) => void
Expand All @@ -166,7 +166,7 @@ export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TE
* runEntityStoreAction(BooksStore, EntityStoreAction.UpsertEntities, upsert => upsert([2, 3], { title: 'New Title' }, (id, newState) => ({ id, ...newState, price: 0 })));
*
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.UpsertEntities,
operation: (operator: TEntityStore['upsert']) => void
Expand All @@ -183,12 +183,12 @@ export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TE
* { id: 4, title: 'Another title', price: 0 },
* ));
*/
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction.UpsertManyEntities,
operation: (operator: TEntityStore['upsertMany']) => void
);
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S = TEntityStore extends EntityStore<infer T> ? T : any>(
export function runEntityStoreAction<TEntityStore extends EntityStore<S>, S extends EntityState = TEntityStore extends EntityStore<infer T> ? T : any>(
storeClassOrName: Constructor<TEntityStore> | string,
action: EntityStoreAction,
operation: (operator: TEntityStore[keyof TEntityStore] & Function) => void
Expand Down

0 comments on commit 682f66e

Please sign in to comment.