Skip to content

Commit

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

This reverts commit c0db182.
  • Loading branch information
widoz committed Feb 14, 2024
1 parent c0db182 commit ac90d94
Show file tree
Hide file tree
Showing 29 changed files with 203 additions and 201 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 new Set(entities);
return Set.new(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: new Set(),
selectedEntitiesOptions: Set.new(),
});
return;
}
Expand All @@ -45,8 +45,8 @@ export function CompositeEntitiesByKind<E, K>(
}),
])
.then((result) => {
const currentEntitiesOptions = result[0] ?? new Set();
const selectedEntitiesOptions = result[1] ?? new Set();
const currentEntitiesOptions = result[0] ?? Set.new();
const selectedEntitiesOptions = result[1] ?? Set.new();

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 : new Set([kind]);
const emptySet = new Set<any>();
const _kind = kind instanceof Set ? kind : Set.new([kind]);
const emptySet = Set.new<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(new Set());
props.onChange(Set.new());
}

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] ?? new Set();
const selectedEntitiesOptions = result[1] ?? new Set();
const currentEntitiesOptions = result[0] ?? Set.new();
const selectedEntitiesOptions = result[1] ?? Set.new();

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: () => new Set(entities.records ?? []),
records: () => Set.new(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 = new Set<EntitiesSearch.ControlOption<E>>();
const emptySet = Set.new<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: new Set<E>([]),
kind: new Set<K>([]),
contextualEntitiesOptions: new Set<Options<E>>(),
currentEntitiesOptions: new Set<Options<E>>(),
selectedEntitiesOptions: new Set<Options<E>>(),
entities: Set.new<E>([]),
kind: Set.new<K>([]),
contextualEntitiesOptions: Set.new<Options<E>>(),
currentEntitiesOptions: Set.new<Options<E>>(),
selectedEntitiesOptions: Set.new<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: new Set(),
contextualEntitiesOptions: new Set(),
currentEntitiesOptions: new Set(),
selectedEntitiesOptions: Set.new(),
contextualEntitiesOptions: Set.new(),
currentEntitiesOptions: Set.new(),
};

case 'UPDATE_ENTITIES_OPTIONS_FOR_NEW_KIND':
return {
...state,
contextualEntitiesOptions: action.entitiesOptions,
currentEntitiesOptions: action.entitiesOptions,
selectedEntitiesOptions: new Set(),
entities: new Set(),
selectedEntitiesOptions: Set.new(),
entities: Set.new(),
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 = new Set<EntitiesSearch.ControlOption<V>>();
let _options = new Set<EntitiesSearch.ControlOption<V>>();
let _collection = Set.new<EntitiesSearch.ControlOption<V>>();
let _options = Set.new<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 = new Set<EntitiesSearch.ControlOption<V>>();
let uniqueOptions = Set.new<EntitiesSearch.ControlOption<V>>();
const temp: Array<EntitiesSearch.ControlOption<V>['value']> = [];

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

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

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

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

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

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

return new Set(this.#data.filter((item) => !this.isEqual(item, value)));
return Set.new(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 new Set(this.#data.map(fn));
return Set.new(this.#data.map(fn));
}

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

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

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

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

public copy(start: number, end: number): Set<T> {
return new Set(this.#data.slice(start, end));
return Set.new(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: new Set(props.attributes.posts),
value: Set.new(props.attributes.posts),
onChange: (posts) =>
props.setAttributes({ posts: posts.toArray() }),
},
kind: {
value: new Set(props.attributes.postType),
value: Set.new(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: new Set(props.attributes.terms),
value: Set.new(props.attributes.terms),
onChange: (terms) =>
props.setAttributes({ terms: terms?.toArray() }),
},
kind: {
value: new Set(props.attributes.taxonomy),
value: Set.new(props.attributes.taxonomy),
options: convertEntitiesToControlOptions(
taxonomiesEntities.records(),
'name',
Expand Down
Loading

0 comments on commit ac90d94

Please sign in to comment.