Refactor
This commit is contained in:
parent
a20c1c8547
commit
cd2c23e390
|
@ -7,7 +7,14 @@ type IResult = {
|
|||
object: IObject;
|
||||
};
|
||||
|
||||
async function resolveUnrequestedOne(this: Resolver, value) {
|
||||
export default class Resolver {
|
||||
private requesting: Set<string>;
|
||||
|
||||
constructor(iterable?: Iterable<string>) {
|
||||
this.requesting = new Set(iterable);
|
||||
}
|
||||
|
||||
private async resolveUnrequestedOne(value) {
|
||||
if (typeof value !== 'string') {
|
||||
return { resolver: this, object: value };
|
||||
}
|
||||
|
@ -33,15 +40,15 @@ async function resolveUnrequestedOne(this: Resolver, value) {
|
|||
}
|
||||
|
||||
return { resolver, object };
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveCollection(this: Resolver, value) {
|
||||
private async resolveCollection(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const resolved = typeof value === 'string' ?
|
||||
await resolveUnrequestedOne.call(this, value) :
|
||||
await this.resolveUnrequestedOne(value) :
|
||||
value;
|
||||
|
||||
switch (resolved.type) {
|
||||
|
@ -54,21 +61,14 @@ async function resolveCollection(this: Resolver, value) {
|
|||
default:
|
||||
return [resolved];
|
||||
}
|
||||
}
|
||||
|
||||
export default class Resolver {
|
||||
private requesting: Set<string>;
|
||||
|
||||
constructor(iterable?: Iterable<string>) {
|
||||
this.requesting = new Set(iterable);
|
||||
}
|
||||
|
||||
public async resolve(value): Promise<Array<Promise<IResult>>> {
|
||||
const collection = await resolveCollection.call(this, value);
|
||||
const collection = await this.resolveCollection(value);
|
||||
|
||||
return collection
|
||||
.filter(element => !this.requesting.has(element))
|
||||
.map(resolveUnrequestedOne.bind(this));
|
||||
.map(this.resolveUnrequestedOne.bind(this));
|
||||
}
|
||||
|
||||
public resolveOne(value) {
|
||||
|
@ -76,11 +76,11 @@ export default class Resolver {
|
|||
throw new Error();
|
||||
}
|
||||
|
||||
return resolveUnrequestedOne.call(this, value);
|
||||
return this.resolveUnrequestedOne(value);
|
||||
}
|
||||
|
||||
public async resolveRemoteUserObjects(value) {
|
||||
const collection = await resolveCollection.call(this, value);
|
||||
const collection = await this.resolveCollection(value);
|
||||
|
||||
return collection.filter(element => !this.requesting.has(element)).map(element => {
|
||||
if (typeof element === 'string') {
|
||||
|
@ -91,7 +91,7 @@ export default class Resolver {
|
|||
}
|
||||
}
|
||||
|
||||
return resolveUnrequestedOne.call(this, element);
|
||||
return this.resolveUnrequestedOne(element);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue