Skip to content

Commit

Permalink
Revert "Revert "Revert "Refactor Set introducing a new static factory…
Browse files Browse the repository at this point in the history
… and put the main constructor private"""

This reverts commit ac90d94.
  • Loading branch information
widoz committed Feb 14, 2024
1 parent ac90d94 commit ea6874a
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 203 deletions.
2 changes: 1 addition & 1 deletion sources/client/src/api/search-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function searchEntities<E>(
throw error;
});

return Set.new(entities);
return new Set(entities);
}

function serializeFields(fields: EntitiesSearch.SearchQueryFields): string {
Expand Down
10 changes: 5 additions & 5 deletions sources/client/src/components/composite-entities-by-kind.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function CompositeEntitiesByKind<E, K>(
if (entities.length() <= 0) {
dispatch({
type: 'UPDATE_SELECTED_ENTITIES_OPTIONS',
selectedEntitiesOptions: Set.new(),
selectedEntitiesOptions: new Set(),
});
return;
}
Expand All @@ -45,8 +45,8 @@ export function CompositeEntitiesByKind<E, K>(
}),
])
.then((result) => {
const currentEntitiesOptions = result[0] ?? Set.new();
const selectedEntitiesOptions = result[1] ?? Set.new();
const currentEntitiesOptions = result[0] ?? new Set();
const selectedEntitiesOptions = result[1] ?? new Set();

dispatch({
type: 'UPDATE_SELECTED_ENTITIES_OPTIONS',
Expand All @@ -63,8 +63,8 @@ export function CompositeEntitiesByKind<E, K>(
};

const onChangeKind = (kind: EntitiesSearch.Kind<K>) => {
const _kind = kind instanceof Set ? kind : Set.new([kind]);
const emptySet = Set.new<any>();
const _kind = kind instanceof Set ? kind : new Set([kind]);
const emptySet = new Set<any>();

props.kind.onChange(_kind);
props.entities.onChange(emptySet);
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/components/plural-select-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function PluralSelectControl(

const onChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
if (event.target.selectedOptions.length <= 0) {
props.onChange(Set.new());
props.onChange(new Set());
}

const selectedOptions = Array.from(event.target.selectedOptions).map(
Expand Down
4 changes: 2 additions & 2 deletions sources/client/src/hooks/use-entities-options-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function useEntitiesOptionsStorage<E, K>(
: undefined,
])
.then((result) => {
const currentEntitiesOptions = result[0] ?? Set.new();
const selectedEntitiesOptions = result[1] ?? Set.new();
const currentEntitiesOptions = result[0] ?? new Set();
const selectedEntitiesOptions = result[1] ?? new Set();

dispatch({
type: 'UPDATE_SELECTED_ENTITIES_OPTIONS',
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-entity-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useEntityRecords<Entity>(
const status = entities.status as any as ResolveStatus;

return Object.freeze({
records: () => Set.new(entities.records ?? []),
records: () => new Set(entities.records ?? []),
isResolving: () =>
entities.isResolving &&
!entities.hasResolved &&
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useSearch<E, K>(
)
.catch((error) => {
doAction('wp-entities-search.on-search.error', error);
const emptySet = Set.new<EntitiesSearch.ControlOption<E>>();
const emptySet = new Set<EntitiesSearch.ControlOption<E>>();
dispatch({
type: 'UPDATE_CURRENT_ENTITIES_OPTIONS',
currentEntitiesOptions: emptySet,
Expand Down
10 changes: 5 additions & 5 deletions sources/client/src/storage/entities/initial-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export function makeInitialState<E, K>(
initialState: Partial<EntitiesSearch.EntitiesState<E, K>>
): EntitiesSearch.EntitiesState<E, K> {
return {
entities: Set.new<E>([]),
kind: Set.new<K>([]),
contextualEntitiesOptions: Set.new<Options<E>>(),
currentEntitiesOptions: Set.new<Options<E>>(),
selectedEntitiesOptions: Set.new<Options<E>>(),
entities: new Set<E>([]),
kind: new Set<K>([]),
contextualEntitiesOptions: new Set<Options<E>>(),
currentEntitiesOptions: new Set<Options<E>>(),
selectedEntitiesOptions: new Set<Options<E>>(),
searchPhrase: '',
...initialState,
};
Expand Down
10 changes: 5 additions & 5 deletions sources/client/src/storage/entities/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ export function reducer<E, K>(
case 'CLEAN_ENTITIES_OPTIONS':
return {
...state,
selectedEntitiesOptions: Set.new(),
contextualEntitiesOptions: Set.new(),
currentEntitiesOptions: Set.new(),
selectedEntitiesOptions: new Set(),
contextualEntitiesOptions: new Set(),
currentEntitiesOptions: new Set(),
};

case 'UPDATE_ENTITIES_OPTIONS_FOR_NEW_KIND':
return {
...state,
contextualEntitiesOptions: action.entitiesOptions,
currentEntitiesOptions: action.entitiesOptions,
selectedEntitiesOptions: Set.new(),
entities: Set.new(),
selectedEntitiesOptions: new Set(),
entities: new Set(),
kind: action.kind,
};

Expand Down
4 changes: 2 additions & 2 deletions sources/client/src/utils/order-selected-options-at-the-top.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function orderSelectedOptionsAtTheTop<V>(
return options;
}

let _collection = Set.new<EntitiesSearch.ControlOption<V>>();
let _options = Set.new<EntitiesSearch.ControlOption<V>>();
let _collection = new Set<EntitiesSearch.ControlOption<V>>();
let _options = new Set<EntitiesSearch.ControlOption<V>>();

options.forEach((option) => {
if (collection.has(option.value)) {
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/utils/unique-control-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Set } from '../vo/set';
export function uniqueControlOptions<V>(
set: Set<EntitiesSearch.ControlOption<V>>
): Set<EntitiesSearch.ControlOption<V>> {
let uniqueOptions = Set.new<EntitiesSearch.ControlOption<V>>();
let uniqueOptions = new Set<EntitiesSearch.ControlOption<V>>();
const temp: Array<EntitiesSearch.ControlOption<V>['value']> = [];

for (const option of set) {
Expand Down
18 changes: 7 additions & 11 deletions sources/client/src/vo/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { isEqual as _isEqual } from 'lodash';
export class Set<T> {
readonly #data: ReadonlyArray<T>;

public static new<T>(data: ReadonlyArray<T> = []): Set<T> {
return new Set(data);
}

private constructor(data: ReadonlyArray<T> = []) {
public constructor(data: ReadonlyArray<T> = []) {
this.#data = data;
}

Expand All @@ -16,23 +12,23 @@ export class Set<T> {
return this;
}

return Set.new([...this.#data, value]);
return new Set([...this.#data, value]);
}

public delete(value: T): Set<T> {
if (!this.has(value)) {
return this;
}

return Set.new(this.#data.filter((item) => !this.isEqual(item, value)));
return new Set(this.#data.filter((item) => !this.isEqual(item, value)));
}

public has(value: T): boolean {
return this.#data.some((current) => this.isEqual(current, value));
}

public map<R = T>(fn: (value: T) => R): Set<R> {
return Set.new(this.#data.map(fn));
return new Set(this.#data.map(fn));
}

public toArray(): ReadonlyArray<T> {
Expand All @@ -48,11 +44,11 @@ export class Set<T> {
}

public concat(set: Set<T>): Set<T> {
return Set.new([...this.#data, ...set.toArray()]);
return new Set([...this.#data, ...set.toArray()]);
}

public filter(fn: (value: T) => boolean): Set<T> {
return Set.new(this.#data.filter(fn));
return new Set(this.#data.filter(fn));
}

public find(fn: (value: T) => boolean): T | undefined {
Expand All @@ -68,7 +64,7 @@ export class Set<T> {
}

public copy(start: number, end: number): Set<T> {
return Set.new(this.#data.slice(start, end));
return new Set(this.#data.slice(start, end));
}

public equals(set: Set<T>): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ document.addEventListener('DOMContentLoaded', () => {
);
},
entities: {
value: Set.new(props.attributes.posts),
value: new Set(props.attributes.posts),
onChange: (posts) =>
props.setAttributes({ posts: posts.toArray() }),
},
kind: {
value: Set.new(props.attributes.postType),
value: new Set(props.attributes.postType),
options: convertEntitiesToControlOptions(
postTypesEntities
.records()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ document.addEventListener('DOMContentLoaded', () => {
);
},
entities: {
value: Set.new(props.attributes.terms),
value: new Set(props.attributes.terms),
onChange: (terms) =>
props.setAttributes({ terms: terms?.toArray() }),
},
kind: {
value: Set.new(props.attributes.taxonomy),
value: new Set(props.attributes.taxonomy),
options: convertEntitiesToControlOptions(
taxonomiesEntities.records(),
'name',
Expand Down
Loading

0 comments on commit ea6874a

Please sign in to comment.