[Javascript] Broadcaster + Operator + Listener pattern -- 18. Create a Win Condition with a mapDone Operator

Many streams of events end when a certain condition is met. When we run into that condition, we'll want to pass down our own custom values. This lessons walks through adding a done condition to hangman and how we can use new operators to control what happens when it's done.

 

Continue with previous post.

 

1. Set a win logic:

复制代码
let hangman = pipe(map(hangmanLogic), applyOperator(word), stringConcat);
hangman(inputValue)(log);

// to

let hangman = pipe(map(hangmanLogic), applyOperator(word), stringConcat);
let play = hangman(inputValue);
let winPipe = pipe(filter((str) => !str.includes("*")));
let win = winPipe(play);

play(console.log);
win(() => {
  console.log("you win");
});
复制代码

When we enter 'honeycomb' in the input, it will log out 'you win', but as you continue typing, 'you win' continue pop up:

/*
honeycomb 
you win 
honeycombb
you win 
honeycombbbb 
you win 
*/

 

 

2. doneCondition operator:

复制代码
let hangman = pipe(map(hangmanLogic), applyOperator(word), stringConcat);

let doneCondition = (condition) => (broadcaster) => (listener) => {
  let cancel = filter(condition)(broadcaster)((value) => {
    listener(done);
    cancel();
  });

  return () => {
    cancel();
  };
};

let winPipe = pipe(doneCondition((str) => !str.includes("*")));
let play = hangman(inputValue);
let win = winPipe(play);

play(console.log);
win(() => {
  console.log("you win");
});
复制代码

Now, 'you win' only log out once.

 

3. Pass 'you win' from a operator:

复制代码
let hangman = pipe(map(hangmanLogic), applyOperator(word), stringConcat);

let doneCondition = (condition) => (broadcaster) => (listener) => {
  let cancel = filter(condition)(broadcaster)((value) => {
    listener(done);
    cancel();
  });

  return () => {
    cancel();
  };
};

let mapDone = (doneValue) => (broadcaster) => (listener) => {
  broadcaster((value) => {
    if (value === done) {
      listener(doneValue);
    }
  });
};

let winPipe = pipe(
  doneCondition((str) => !str.includes("*")),
  mapDone("you win!")
);
let play = hangman(inputValue);
let win = winPipe(play);

play(console.log);
win(console.log);
复制代码

 

4. Cancel 'play' when 'win':

复制代码
let hangman = pipe(map(hangmanLogic), applyOperator(word), stringConcat);

let doneCondition = (condition) => (broadcaster) => (listener) => {
  let cancel = filter(condition)(broadcaster)((value) => {
    listener(done);
    cancel();
  });

  return () => {
    cancel();
  };
};

let mapDone = (doneValue) => (broadcaster) => (listener) => {
  broadcaster((value) => {
    if (value === done) {
      listener(doneValue);
    }
  });
};
let cancelWhen = (cancelBroadcaster) => (broadcaster) => (listener) => {
  let cancel = broadcaster(listener);

  let cancel2 = cancelBroadcaster(() => {
    cancel();
  });

  return () => {
    cancel();
    cancel2();
  };
};

let winPipe = pipe(
  doneCondition((str) => !str.includes("*")),
  mapDone("you win!")
);
let play = hangman(inputValue);
let win = winPipe(play);

let rules = pipe(cancelWhen(win));
let playWithWin = rules(play);
playWithWin(console.log);
win(console.log);
复制代码

 

posted @   Zhentiw  阅读(141)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-11-17 [ES6] Symbol
2016-11-17 [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
2016-11-17 [Angular2 Router] Resolving route data in Angular 2
2014-11-17 [ES6] 02. Traceur compiler and Grunt
2014-11-17 [Node.js] Level 6. Socket.io
2014-11-17 [Grunt] Cleaning your build folder with grunt-contrib-clean
2014-11-17 [Grunt] Minifying your output with grunt-uglify
点击右上角即可分享
微信分享提示