Skip to content

Commit

Permalink
feat: update getters (entities, entity, activeId, activeEntity) to si…
Browse files Browse the repository at this point in the history
…gnals #TINFR-417
  • Loading branch information
why520crazy committed Aug 20, 2024
1 parent 42734b2 commit ec38717
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 55 deletions.
51 changes: 12 additions & 39 deletions packages/store/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,28 @@ export class EntityStore<TState extends EntityState<TEntity, TReferences>, TEnti

private isSingleEntity: boolean;

/**
* @deprecated 请使用 snapshot.entities
*/
get entities() {
return this.snapshot.entities;
}

/**
* @deprecated 请使用 snapshot.entity
*/
get entity() {
return this.snapshot.entity;
}

entities$ = this.select$((state) => {
return state.entities;
});

// 临时添加,后期使用 entities 替代你
private entitiesSignal: Signal<TEntity[]>;
entities: Signal<TEntity[]>;

entity$ = this.select$((state) => {
return state.entity;
});

// 临时添加,后期使用 entity 替代你
private entitySignal: Signal<TEntity>;

/**
* @deprecated 请使用 snapshot.activeId
*/
get activeId(): Id | null {
return this.snapshot.activeId || null;
}
entity: Signal<TEntity>;

activeId$: Observable<Id | null> = this.select$((state) => {
return state.activeId || null;
});

/**
* @deprecated 请使用 snapshot
*/
activeId: Signal<Id | null>;

get activeEntity(): TEntity | null {
return this.snapshot.activeId ? this.getEntityById(this.snapshot.activeId) : null;
}
activeEntity = computed(() => {
const activeId = this.activeId();
return activeId ? this.getEntityById(activeId) : null;
});

activeEntity$: Observable<TEntity | null> = this.select$((state) => {
return state.entities.find((entity) => entity[this.options.idKey] === state.activeId);
Expand All @@ -115,7 +91,7 @@ export class EntityStore<TState extends EntityState<TEntity, TReferences>, TEnti
);

entitiesWithRefs = computed(() => {
const entities = this.entitiesSignal();
const entities = this.entities();
if (!entities) {
return entities;
}
Expand All @@ -133,7 +109,7 @@ export class EntityStore<TState extends EntityState<TEntity, TReferences>, TEnti
);

entityWithRefs = computed(() => {
const entity = this.entitySignal();
const entity = this.entity();
if (!entity) {
return entity;
}
Expand Down Expand Up @@ -193,12 +169,9 @@ export class EntityStore<TState extends EntityState<TEntity, TReferences>, TEnti
throw new Error(`idKey is required in EntityStore`);
}
this.buildReferencesIdMap();
this.entitiesSignal = toSignal(this.entities$);
this.entitySignal = toSignal(this.entity$);
// this.entities = toSignal(this.entities$);
// this.entity = toSignal(this.entity$);
// this.activeId = toSignal(this.activeId$);
// this.activeEntity = toSignal(this.activeEntity$);
this.entities = toSignal(this.entities$);
this.entity = toSignal(this.entity$);
this.activeId = toSignal(this.activeId$);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class DetailStore
@Action()
updateState() {
this.updateWithReferences(
this.entity._id,
this.entity()._id,
{ state_id: 'success-state-id' },
{
states: [
Expand All @@ -77,6 +77,6 @@ export class DetailStore

@Action()
updateTitle() {
this.update(this.entity._id, { title: '新的title' });
this.update(this.entity()._id, { title: '新的title' });
}
}
4 changes: 2 additions & 2 deletions packages/store/store/examples/todos/todos.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class TodosStore extends EntityStore<TodosState, Todo> {
}

private getWithCompleted(completed: Boolean) {
return this.entities.filter((todo: Todo) => todo.completed === completed);
return this.entities().filter((todo: Todo) => todo.completed === completed);
}

getCompleted() {
Expand Down Expand Up @@ -111,7 +111,7 @@ export class TodosStore extends EntityStore<TodosState, Todo> {
@Action()
setAllTo(completed: boolean) {
this.update(
this.entities.map((todo) => {
this.entities().map((todo) => {
return todo.id;
}),
{
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/entity-store-refs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ describe('Store: EntityStore with refs', () => {
name: 'group-4 name'
};
taskDetailStore.updateWithReferences(
taskDetailStore.entity._id,
taskDetailStore.entity()._id,
{
title: 'new 1 task',
group_id: 'group-4'
Expand Down
27 changes: 16 additions & 11 deletions packages/store/test/entity-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('Store: EntityStore', () => {
const state = taskEntityStore.snapshot;
expect(state.pagination).toEqual(undefined);
expect(state.entities).toEqual([]);
expect(taskEntityStore.entities()).toEqual([]);

expect(taskEntityStore.trackBy(0, { _id: '111', name: 'name1' })).toEqual('111');
});
Expand Down Expand Up @@ -513,9 +514,10 @@ describe('Store: EntityStore', () => {
const tasksEntityStore = injectStoreForTest(TasksEntityStore, {
entities: [...initialTasks]
});
expect(tasksEntityStore.activeId).toEqual(null);
expect(tasksEntityStore.activeId()).toEqual(null);
tasksEntityStore.setActive('1');
expect(tasksEntityStore.activeId).toEqual('1');
expect(tasksEntityStore.snapshot.activeId).toEqual('1');
expect(tasksEntityStore.activeId()).toEqual('1');
tasksEntityStore.activeId$.subscribe((id) => {
expect(id).toEqual('1');
});
Expand Down Expand Up @@ -565,11 +567,11 @@ describe('Store: EntityStore', () => {
});
tasksEntityStore.setActive('1');
tasksEntityStore.setActive(null);
expect(tasksEntityStore.activeId).toEqual(null);
expect(tasksEntityStore.activeId()).toEqual(null);
tasksEntityStore.activeId$.subscribe((id) => {
expect(id).toEqual(null);
});
expect(tasksEntityStore.activeEntity).toEqual(null);
expect(tasksEntityStore.activeEntity()).toEqual(null);
tasksEntityStore.activeEntity$.subscribe((entity) => {
expect(entity).toEqual(null);
});
Expand All @@ -583,20 +585,21 @@ describe('Store: EntityStore', () => {
taskDetailStore.initialize(initialTaskDetail);
const state = taskDetailStore.snapshot;
expect(state.entity).toEqual({ _id: '1', name: 'task 1' });
expect(taskDetailStore.entity()).toEqual({ _id: '1', name: 'task 1' });
});

it('should update by id', () => {
const taskDetailStore = injectStoreForTest(TaskDetailStore);
taskDetailStore.initialize(initialTaskDetail);
taskDetailStore.update(taskDetailStore.entity._id, { name: 'task 1 new' });
expect(taskDetailStore.entity).toEqual({ _id: '1', name: 'task 1 new' });
taskDetailStore.update(taskDetailStore.entity()._id, { name: 'task 1 new' });
expect(taskDetailStore.entity()).toEqual({ _id: '1', name: 'task 1 new' });
});

it('should update whole entity by predicate state', () => {
const taskDetailStore = injectStoreForTest(TaskDetailStore);
taskDetailStore.initialize(initialTaskDetail);
taskDetailStore.update({ entity: { _id: '1', name: 'task 1 new' } });
expect(taskDetailStore.entity).toEqual({ _id: '1', name: 'task 1 new' });
expect(taskDetailStore.entity()).toEqual({ _id: '1', name: 'task 1 new' });
});

it('should update whole entity by predicate function', () => {
Expand All @@ -607,7 +610,8 @@ describe('Store: EntityStore', () => {
entity: { ...state.entity, name: 'task 1 new' }
};
});
expect(taskDetailStore.entity).toEqual({ _id: '1', name: 'task 1 new' });
expect(taskDetailStore.snapshot.entity).toEqual({ _id: '1', name: 'task 1 new' });
expect(taskDetailStore.entity()).toEqual({ _id: '1', name: 'task 1 new' });
});

it('should effect taskNameState when update entity name', () => {
Expand All @@ -628,7 +632,7 @@ describe('Store: EntityStore', () => {
const component = fixture.componentInstance;
fixture.detectChanges();
const newTaskName = 'task 1 new';
taskDetailStore.update(taskDetailStore.entity._id, { name: newTaskName });
taskDetailStore.update(taskDetailStore.entity()._id, { name: newTaskName });
fixture.detectChanges();

expect(component.taskNameStateSpy).toHaveBeenCalled();
Expand All @@ -652,9 +656,10 @@ describe('Store: EntityStore', () => {
it('should set active and get id', () => {
const taskDetailStore = injectStoreForTest(TaskDetailStore);
taskDetailStore.initialize(initialTaskDetail);
expect(taskDetailStore.activeId).toEqual(null);
expect(taskDetailStore.activeId()).toEqual(null);
taskDetailStore.setActive('1');
expect(taskDetailStore.activeId).toEqual('1');
expect(taskDetailStore.snapshot.activeId).toEqual('1');
expect(taskDetailStore.activeId()).toEqual('1');
taskDetailStore.activeId$.subscribe((id) => {
expect(id).toEqual('1');
});
Expand Down

0 comments on commit ec38717

Please sign in to comment.