我有一个flatMap,目的是发出两个动作:
import java.util.Scanner;
import java.io.*;
public class GameOfLife
{
//variables used, 24 and 80 are the default size.
private int rows = 24;
private int cols = 80;
private String [][] current = new String[rows][cols];
private String [][] next = new String[rows][cols];
// primary constructor that takes in 2 integers and sets the rows and cols to them
public GameOfLife(int r, int c)
{
rows = r;
cols = c;
if(r<0){
rows = rows * -1;
}
if(c<0){
cols = cols * -1;
}
if (r==0||c==0){
System.err.print("Incorrect parameters, setting to default");
rows = 24;
cols = 80;
}
for (int x = 0; x <= rows - 1; x++){
for (int y = 0; y <= cols - 1; y++){
int q = (int)(Math.random()*100);
if(q%2 == 0){
current[x][y]="dead";
}
else{
current[x][y]="alive";}
}
}
}
// secondary constructor that takes in no parameters
public GameOfLife()
{
for (int x = 0; x <= rows - 1; x++){
for (int y = 0; y <= cols - 1; y++){
int r = (int)(Math.random()*100);
if(r%2 == 0){
current[x][y]="dead";
}
else{
current[x][y]="alive";}
}
}
}
//constructor that takes in a string double array in the graphical style as the parameter to make as the first generation
public GameOfLife(String[][] s){
rows = s.length;
cols = s[0].length;
for (int x = 0; x <= rows - 1; x++){
for (int y = 0; y <= cols - 1; y++){
if (s[x][y].equals("*")){
current[x][y]="alive";}
else {current[x][y]="dead";}
}
}
}
//uses the game rules to run the game and progress to the next generation
public void runLife(){
//loops and checks neighbors to abide by the rules of the game
for (int x = 0; x <= rows - 1; x++){
for (int y = 0; y <= cols - 1; y++){
//checks the alive rules for each one
int p=countNeighbors(x,y);
if(p<=1){next[x][y]="dead";}
if(p==2){
if(current[x][y].equals("alive")){
next[x][y]="alive";}
else{next[x][y]="dead";}}
if(p==3){next[x][y]="alive";}
if(p>3){next[x][y]="dead";}
}
}
current = next;
}
//counts the neighbors of a specified location
public int countNeighbors(int row, int col){
int q=0;
for ( int x = row -1; x <= row + 1 ; x++)
{
for (int y = col -1 ; y <= col + 1 ; y++)
{
boolean go=true;
if((x < 0 || y < 0)){go=false;}
if(x>=rows){go=false;}
if(y>=cols){go=false;}
if ((x == row) && (y==col)){ go=false;}
if(go==true){if(current[x][y].equals("alive")){q++;}}
}
}
return q;
}
//makes the array into a string of graphics that is a good visualization of the code in work
public String toGraphic(){
String s = "";
for(int x = 0; x <= rows - 1; x++){
for(int y = 0; y <= cols - 1; y++){
if(current[x][y].equals("alive")){
s=s+"*";
}
else{
s=s+" ";}
}
s= s+"\n";
}
return s;
}
//continuously prints out the toGraphic option to visualise what happens every pass
public void animate(int t, int o){
//checking for incorrect parameters
if(t<0){
t = t * -1;
}
if(o<0){
o = o * -1;
}
if(o==0){
System.err.print("You have selected no amount");
}
//looping the toGraphic method a set amount of times
int q=0;
while(q<=t){
long start = System.currentTimeMillis();
long end = 0;
System.out.println(toGraphic());
while ((end-start)<=o){
end = System.currentTimeMillis();
}
q++;
runLife();
}
}
}
然而,export default function searchForPartner(action$, store) {
return action$.ofType(statusActions.SEARCH_FOR_PARTNER)
.filter(() => store.getState().user.signedIn)
.delay(2000)
.flatMap(() => [
carouselActions.slideTo(config.CAROUSEL_SLIDES.CONFIRM_CHAT),
statusActions.foundPerson(),
])
.takeUntil(action$.ofType(carouselActions.TAP_CANCEL))
};
处于错误的位置。当takeUntil
被发射时,它正在取消整个史诗。相反,我只希望它取消TAP_CANCEL
内部动作的发出。我该怎么做?
=== UPDATE ===
我骗了它,这似乎有用吗?但我不喜欢有2个计时器:
flatMap
答案 0 :(得分:4)
您希望将delay
和takeUntil
隔离到内部Observable中,最重要的是您启动delay
计时器,然后{{1}它,如果有人取消它,你肯定会倾听 - 如果你的takeUntil
在外面,你就不会听取取消行动了!这很微妙,但要理解为什么很重要。
delay
您可能还想考虑使用export default function searchForPartner(action$, store) {
return action$.ofType(statusActions.SEARCH_FOR_PARTNER)
.filter(() => store.getState().user.signedIn)
.flatMap(() =>
Observable.of(
carouselActions.slideTo(config.CAROUSEL_SLIDES.CONFIRM_CHAT),
statusActions.foundPerson()
)
.delay(2000)
.takeUntil(action$.ofType(carouselActions.TAP_CANCEL))
);
}
代替switchMap
(又名flatMap
),因为如果另一个mergeMap
进入,而前一个仍然在等待,那么您就是&#39 ;他们排成一排排队。可能不想要你,但你必须打电话。