How to use ferp - 10 common examples

To help you get started, we’ve selected a few ferp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ferp-js / ferp / examples / cli / file-reader-node.js View on Github external
const { updateLogger } = require('./updateLogger.js');

const { batch, defer, none } = ferp.effects;

const readFile = (file, errorMessage, successMessage) => defer(new Promise((done) => {
  fs.readFile(file, { encoding: 'utf-8' }, (err, data) => {
    if (err) {
      done({ type: errorMessage, file, err });
    } else {
      done({ type: successMessage, file, data });
    }
  });
}));

ferp.app({
  init: [
    null,
    batch([
      readFile(path.resolve(__dirname, './hello-world.txt'), 'READ_ERR', 'READ_OK'),
      readFile(path.resolve(__dirname, './hello-world.txt.garbage'), 'READ_ERR', 'READ_OK'),
    ]),
  ],

  update: updateLogger((message, state) => {
    switch (message.type) {
      case 'READ_OK':
        // Can do something with message.data
        return [state, none()];

      case 'READ_ERR':
        // Can do something with message.err
github ferp-js / ferp / examples / http-server / src / main.js View on Github external
const http = require('http');
const ferp = require('ferp');

const { router } = require('./router.js');
const { serverSubscription } = require('./subscription.js');

const { defer, none } = ferp.effects;

const responseEffect = ({ response }, status, json) => defer(new Promise((done) => {
  response.writeHead(status, { 'Content-Type': 'application/json' });
  response.end(JSON.stringify(json), () => done(none()));
}));

const showState = (message, state) => [state, responseEffect(message, 200, state.logs)];
const fourOhFour = (message, state) => [state, responseEffect(message, 404, { error: 'not found' })];

const main = () => ferp.app({
  init: [{ logs: [] }, none()],

  update: router({
    'GET /': showState,
    'GET /not-found': fourOhFour,
  }),
github ferp-js / ferp / examples / cli / timer-with-effects / main.js View on Github external
const ferp = require('ferp');

const { updateLogger } = require('../common/updateLogger.js');

const { delay, none } = ferp.effects;

const update = (_, state) => {
  const nextState = state + 1;
  return [
    nextState,
    nextState < 5 ? delay(null, 1000) : none(),
  ];
};

const main = () => ferp.app({
  init: [
    0,
    delay(null, 1000),
  ],

  update: updateLogger(update),
github ferp-js / ferp / examples / cli / timer-with-subscription.js View on Github external
const ferp = require('ferp');

const { updateLogger } = require('./updateLogger.js');

const { every } = ferp.subscriptions;

const { none } = ferp.effects;

ferp.app({
  init: [
    0,
    none(),
  ],

  update: updateLogger((_, state) => {
    const nextState = state + 1;
    return [
      nextState,
      none(),
    ];
  }),

  subscribe: state => [
github ferp-js / ferp / examples / with-superfine / src / main.js View on Github external
const ferp = require('ferp');
const { h, patch } = require('superfine');

const { superfineEffect } = require('./superfineEffect.js');

const { none } = ferp.effects;

const view = (state, dispatch) => (
  h('div', null, [
    h(
      'div',
      {
        style: {
          display: 'flex',
          flexDirection: 'row',
          alignItems: 'center',
          justifyContent: 'center',
        },
      },
      [
        h('button', { type: 'button', onclick: () => dispatch({ type: 'SET', value: state.value - 1 }) }, '-'),
        h('h1', { style: { width: '200px', textAlign: 'center' } }, state.value),
github ferp-js / ferp / examples / game-input / main.js View on Github external
update: (message, state) => {
    const result = appReducer(view)(message, state);
    switch (message.type) {
      case 'KEY_DOWN':
        return withMoreEffects(result)([
          effectForKeyEvent(true, message.key, state.players),
        ]);

      case 'KEY_UP':
        return withMoreEffects(result)([
          effectForKeyEvent(false, message.key, state.players),
        ]);

      case 'TICK':
        return withMoreEffects(result)([
          ferp.effects.raf({ type: 'TICK' }, message.timestamp),
          gamePadEffect(state.gamePads),
        ]);

      case 'RENDER':
        return result;

      default:
        return withMoreEffects(result)([
          { type: 'RENDER', view },
        ]);
    }
  },
github ferp-js / ferp / examples / game-input / reducers / vdomReducer.js View on Github external
const { patch } = require('superfine');

const { defer, none } = require('ferp').effects;

const vdomReducer = view => players => (message, state) => {
  switch (message.type) {
    case 'RENDER':
      return (() => {
        let dispatch = () => {};
        const eventPromise = new Promise((resolve) => {
          dispatch = resolve;
        });
        return [
          Object.assign({}, state, {
            node: patch(
              state.node,
              view(
                players,
                (playerId, sourceType) => {
github ferp-js / ferp / examples / game-input / main.js View on Github external
const ferp = require('ferp');
const { appReducer, initialState } = require('./reducers/appReducer.js');
const { keyboardSubscription } = require('./subscriptions/keyboardSubscription.js'); const { inputEffect } = require('./effects/inputEffect.js');
const { gamePadEffect } = require('./effects/gamePadEffect.js');
const { view } = require('./view.js');

const { batch, defer, none } = ferp.effects;

const effectForKeyEvent = (isKeyDown, key, players) => {
  const mapping = {
    wasd: {
      w: 'up',
      s: 'down',
      a: 'left',
      d: 'right',
    },
    arrows: {
      ArrowUp: 'up',
      ArrowDown: 'down',
      ArrowLeft: 'left',
      ArrowRight: 'right',
    },
  };
github ferp-js / ferp / examples / game-input / reducers / playerReducer.js View on Github external
const ferp = require('ferp');

const { none } = ferp.effects;

const playerReducer = id => (message, state) => {
  if (message.playerId !== id) return [state, none()];
  switch (message.type) {
    case 'SOURCE_CHANGE':
      return [
        Object.assign({}, state, {
          sourceType: message.sourceType,
          gamePadIndex: null,
          up: false,
          down: false,
          left: false,
          right: false,
        }),
        none(),
      ];
github ferp-js / ferp / examples / game-input / reducers / playersReducer.js View on Github external
const ferp = require('ferp');
const { playerReducer } = require('./playerReducer.js');
const { updatesCollectionToStateEffects } = require('./helper.js');

const { none } = ferp.effects;

const clamp = (max, value) => Math.min(Math.max(0, value), max);

const integrate = (player, delta) => {
  if (!delta) return player;
  const speed = 9;
  const xDiff = (player.left ? -speed : 0) + (player.right ? speed : 0);
  const yDiff = (player.up ? -speed : 0) + (player.down ? speed : 0);

  return Object.assign({}, player, {
    x: clamp(800, player.x + (xDiff * delta / 100)),
    y: clamp(300, player.y + (yDiff * delta / 100)),
  });
};

const playersReducer = (message, state) => {

ferp

Build functional and pure applications in NodeJS and the Browser!

MIT
Latest version published 3 years ago

Package Health Score

42 / 100
Full package analysis