Skip to content

Instantly share code, notes, and snippets.

@Supamiu
Created October 10, 2018 20:07
Show Gist options
  • Save Supamiu/63851079d48f1d64f72be3a6dd79cdb2 to your computer and use it in GitHub Desktop.
Save Supamiu/63851079d48f1d64f72be3a6dd79cdb2 to your computer and use it in GitHub Desktop.
import { map, mergeMap, shareReplay, switchMap, tap } from 'rxjs/operators';
import { concat, merge, Observable, of, race, Subject } from 'rxjs';
class Foo {
public addLevesToList(leves: Levequest[]): void {
const omniRecipes = leves.filter(leve => leve.recipes.length > 1).map(leve => {
return leve.recipes.map(recipe => recipe.job);
});
let job$: Observable<number>;
if (omniRecipes.length > 0) {
let sharedJobs = omniRecipes[0];
omniRecipes.slice(1).forEach(jobs => {
sharedJobs = jobs.filter(x => sharedJobs.indexOf(x) > -1);
});
const recipes = sharedJobs.map(job => ({ job: job }));
job$ = this.jobPicker.pickJob(recipes).pipe(shareReplay(1));
} else {
// Idk where you get default job if there's no job to pick :)
job$ = of(job);
}
this.listPicker.pickList().pipe(
mergeMap(list => {
const operation$ = concat(
...leves.map(leve => {
return job$.pipe(
map(job => leve.recipes[0]/*Return recipe based on job*/),
switchMap(recipe => {
return this.listManager.addToList(leve.itemId, list, recipe.recipeId, this.craftAmount(leve, recipe));
})
);
})
);
return this.progressService.showProgress(operation$,
leves.length,
'Adding_recipes',
{ amount: leves.length, listname: list.name });
}),
tap(list => list.$key ? this.listsFacade.updateList(list) : this.listsFacade.addList(list)),
mergeMap(list => {
// We want to get the list created before calling it a success, let's be pessimistic !
return this.listsFacade.myLists$.pipe(
map(lists => lists.find(l => l.createdAt === list.createdAt && l.$key !== undefined)),
filter(l => l !== undefined),
first()
);
})
).subscribe((list) => {
this.itemsAdded = leves.length;
this.modifiedList = list;
this.notificationService.template(this.notification);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment