zoukankan      html  css  js  c++  java
  • [Javascript] Broadcaster + Operator + Listener pattern -- 27. combine multi broadcaster and compose operator to implement game logic

    New broadcaster: compose:

    export let combine = (broadcaster1, broadcaster2) => listener => {
      let value1;
      let value2;
    
      let cancel1 = broadcaster1(value => {
        value1 = value;
        listener([
          value1,
          value2
        ])
      })
    
      let cancel2 = broadcaster2(value => {
        value2 = value;
        listener([
          value1,
          value2
        ])
      })
    
      return () => {
        cancel1();
        cancel2();
      }
    }

    Game logic:

    import React from "react"
    import { render } from "react-dom"
    
    import {
      useBroadcaster,
      getUrl,
      useListener,
      combine,
    } from "./broadcasters"
    import { map, share, targetValue, filter} from "./operators"
    import {pipe, every, isString} from "lodash/fp"
    import { head  } from "lodash"
    
    let getWord = pipe(
        map(head),
        share()
      )(getUrl('https://random-word-api.herokuapp.com/word'))
    
    // gameLgoic take broadcaster will receive values: [guess, word] let gameLogic
    = pipe( filter(every(isString)), map(([guess, word]) => { return Array.from(word).map(c => guess.includes(c) ? c: "*").join("") }) ) let App = () => { let onInput = useListener(); let gameBroadcaster = gameLogic(combine(targetValue(onInput), getWord)) let word = useBroadcaster(getWord) let game = useBroadcaster(gameBroadcaster, "") return ( <div> <input type="text" onInput={onInput} /> <p> {word} </p> <p>{game}</p> </div> ) } render(<App></App>, document.querySelector("#root"))

  • 相关阅读:
    MySQL表的四种分区类型
    微信开发配置(Yii框架下的开发)
    一道编程题—输出字符串内重复的数字
    无序数组内查找指定值(快速查找)
    指针
    chmod
    cookie和session的区别
    使用keytool生成证书
    人大金仓修改最大连接数
    数据库链接地址
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14150076.html
Copyright © 2011-2022 走看看