javascript 重构可怕光以采取可选的LIFX选择器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 重构可怕光以采取可选的LIFX选择器相关的知识,希望对你有一定的参考价值。

const config = require('$config')
const errorWrapper = require('./errorWrapper')
const lifxApi = require('./lifxApi')

const headers = {
  Authorization: `Bearer ${config.getApiToken()}`,
  'Content-Type': 'application/json',
}

const getCycles = () => (
  Math
  .ceil(
    Math
    .random() * 3
  )
)

const getPeriod = () => 1

const createScaryLightFlasher = (
  ajaxFetcher,
) => (
  lifxSelector,
) => (
  colorSet = {},
) => (
  errorWrapper(
    (
      !lifxSelector
      || typeof lifxSelector !== 'string'
    ),
    "`lifxSelector` needs to be a valid LIFX selector string in `createScaryLightFlasher`."
  )(
    ajaxFetcher(
      (
        lifxApi
        .concat('/v1')
        .concat('/lights')
        .concat(`/${lifxSelector}`)
        .concat(':random/effects/breathe')
      ),
      {
        body: (
          JSON.stringify({
            ...colorSet,
            cycles: getCycles(),
            period: getPeriod(),
          })
        ),
        headers,
        method: 'POST',
      }
    )
  )
)

module.exports = createScaryLightFlasher
const { map, switchMap } = require('rxjs/operators')
const { pipe } = require('rxjs')

const doScaryLightFlash = require('./doScaryLightFlash')
const getColorSetAtIndex = require('./getColorSetAtIndex')
const getDataFromPromise = require('./getDataFromPromise')
const getRandomColorSetIndex = require('./getRandomColorSetIndex')

const flashRandomLight = (
  lifxSelector,
) => (
  pipe(
    map(getRandomColorSetIndex),
    map(getColorSetAtIndex),
    switchMap(
      doScaryLightFlash(
        lifxSelector,
      )
    ),
    map(getDataFromPromise),
  )
)

module.exports = flashRandomLight
const { interval } = require('rxjs')
const { filter, map, tap } = require('rxjs/operators')

const config = require('$config')

const {
  flashRandomLight,
  isDuringHalloweenNight,
} = require('./')

interval(10000)
.pipe(
  map(isDuringHalloweenNight),
  tap(isDuringHalloweenNight => (
    console
    .info(
      'isDuringHalloweenNight:',
      isDuringHalloweenNight,
    )
  )),
  filter(Boolean),
  flashRandomLight(
    config.getLifxSelector()
  ),
)
.subscribe(
  console.log,
  console.error,
)

以上是关于javascript 重构可怕光以采取可选的LIFX选择器的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 可选的函数参数

ActionScript 3 导航到URL。调用可选的javascript弹出窗口

导航到URL。调用可选的javascript弹出窗口

javascript 当前页面的降价链接的书签(包括可选的选定文本作为报价)

javascript中创建日期对象

深入typescript之‘可选的’和‘必要的’