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