Giter Club home page Giter Club logo

callbags's Introduction

@cycle/callbags

Build codecov code style: prettier

This is the set of callbags that will most likely be used in the next version of Cycle.js. They are written in TypeScript and export ES modules to allow for tree shaking. Also, the implementation is specifically tailored to the Cycle.js use case, so all of them are push-only. Using them with sink or operators that try to pull the sources here will most likely lead to bugs und weird behavior.

Building

This project uses pnpm, other package managers might work, but only pnpm has a lockfile that pins dependencies. To build run:

pnpm install
pnpm run build

Implemented operators

Currently, the following set of operators is implemented, others might follow. Note that this repo is not designed to become the "official" home of callbags, so after a basic set is implemented it is very unlikely that further operators will be added. This is no problem in practise as all of the callbags here adhere to the spec, so you can easily mix and match them with any callbag operator out there.

Factories

Operators

Sinks

Helpers

callbags's People

Contributors

andarist avatar jvanbruegge avatar staltz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

callbags's Issues

Non-blocking callbag question

So in trying to understand callbags, especially in native typescript better I started implementing a non-blocking version of interval which didn't work but the blocking version did. Obviously don't want to lock up other code waiting on an interval. What am I missing in the non-blocking version?

import { Producer, END } from './types';

export function intervalNonblocking(ms: number): Producer<number> {
  return (_, sink) => {
    let id: NodeJS.Timeout;
    let i = 0;

    id = setInterval(() => {
      sink(1, i);
      i++;
    }, ms);

    sink(0, (_: END) => {
      clearTimeout(id);
    });
  };
}

export function intervalBlocking(ms: number): Producer<number> {
  return (_, sink) => {
    let ended = false;

    sink(0, (_: END) => {
      ended = true;
    });

    let lastTime = Date.now();
    let i = 0;
    while (!ended) {
      const now = Date.now();
      const delta = now - lastTime;
      if (delta >= ms) {
        sink(1, i++);
        lastTime = now;
      }
    }
  };
}

[Question]: Any reason for not to use enum to represent code type?

callbags/src/types.ts

Lines 1 to 5 in c024712

export type START = 0;
export type DATA = 1;
export type END = 2;
export type ALL = START | DATA | END;

Currently, we're using couple of plain types to represent code type.
IMO, It would be better if we use enum to do so for these reasons:

  1. Typings became more meaningful
enum Code {
  START,
  DATA,
  END,
}

is better than just spreading plain types.

  1. Increase code readability in term level

Instead of using numeric constants(whose shape is very odd) like 0, 1 to represent code,
we can use constants like Code.START when we write code.
And this will make our code more readable.

Is there any reason not to use enum to represent code type?
Please let me know if it exist. :)

Callbag type and Subject type

Currently

type Callbag<T> = (t: ALL, d?: Talkback | T | any) => void;

the d: Talkback means that if we call this callbag with (0, talkback) then the given talkback can never be called with 0 or 1, but that happens in src/subject.ts:

import { Callbag, ALL } from './types';

export function makeSubject<T>(): Callbag<T> {
  let sinks: any[] = [];

  return (type: ALL, data: any) => {
    if (type === 0) {
      const sink = data; // this sink *would* be typed Talkback and 
                         // thus we shouldn't call `sink(0, ...)`          
      sinks.push(data);
      sink(0, () => {
        const i = sinks.indexOf(sink);
        sinks[i] = void 0;
      });
    } else {
      let hasDeleted = false;

      for (let i = 0; i < sinks.length; i++) {
        if (sinks[i]) sinks[i](type, data);
        else hasDeleted = true;
      }

      if (hasDeleted) {
        sinks = sinks.filter(x => x !== undefined);
      }
    }
  };
}

This is kind of a non-problem, though, because the any dominates in d?: Talkback | T | any. But it doesn't make semantic sense.

But we don't use Callbag for anything else than typing the Subject, so how about we just rename it to Subject? And because of that we can type the Subject type correctly so that it can:

  • Receive sinks (not talkbacks) when t=0
  • Receive data when t=1
  • Receive t=2

The (outward-facing) Subject type might be very soon important for implementation of the new Cycle run.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.