0.26.5 • Published 3 years ago

solid-rx v0.26.5

Weekly downloads
412
License
MIT
Repository
github
Last release
3 years ago

solid-rx(Deprecated)

This package is no longer maintained and may not work properly with future versions of Solid

Build Status NPM Version npm.io npm.io

Functional Reactive Extensions for Solid.js. This package contains a number of operators intended to be use with Solid's createMemo, createComputed, and createEffect to create reactive transformations.

Example:

import { createSignal, createMemo, createEffect } from "solid-js";
import { pipe, tap, map, filter } from "solid-rx";

const doubleEven = pipe(
  tap(console.log),
  filter(t => t % 2 === 0),
  map(t => t * 2)
);

const [number, setNumber] = createSignal(0),
  result = createMemo(doubleEven(number));
// 0

createEffect(() => console.log("transformed", result()));
// transformed 0
setNumber(1);
// 1
setNumber(2);
// 2
// transformed 4
setNumber(3);
// 3

These can also be useful for use with control flow. For example if you wished to delegate applying a class to any row with a model property that matched the current selection (without adding the selected state to the model):

function selectClass(selected, className) {
  return list => {
    createEffect(
      transform(
        list,
        // wrap selection in accessor function and merge since map operators are not tracked
        // find selected element
        mergeMap(list => () => list.find(el => el.model === selected())),
        // group prev value with current
        pairwise(),
        // tap value for side effect of setting `className`
        tap(([prevEl, el]) => {
          prevEl && (prevEl.className = "");
          el && (el.className = className);
        })
      )
    );
    // return the original signal
    return list;
  };
}

const ForWithSelection = props => {
  const applyClass = selectClass(() => props.selected, "active");
  return applyClass(<For each={props.each}>{props.children}</For>);
};

// in a component somewhere:
<ForWithSelection each={state.list} selected={state.selected}>
  {row => <div model={row.id}>{row.description}</div>}
</ForWithSelection>;

Why?

Truthfully nothing in this package is necessary. Solid's auto dependency tracking computations do not need to take a formal functional programming approach to be expressive and succint. If anything this can make simple expressions more complicated. Compare:

// functional
createEffect(transform(
  signal,
  filter(t => t % 2 === 0),
  map(t => t * 2)
  tap(console.log)
));

// imperative
createEffect(() => {
  const s = signal();
  s % 2 === 0 && console.log(s * 2);
})

Obviously map and tap could have been combined in the functional example, but the point still stands. The reason you look at a library like this is that sometimes more complicated problems can easier be modelled as a transformation stream, and that this approach is very composable allowing constructing patterns for code reuse.

Documentation

Utilities

from(setter => dispose) => signal

This operator is useful to create signals from any sort of data structure. You pass in a function that provides a setter. Use the setter to set any value to pass to the signal, from various sources like events, promises, observables, timers etc...

pipe(...operators) => sourceSignal => outSignal

transform(sourceSignal, ...operators) => outSignal

Tbese operators are responsible for chaining together transformations. The only difference is piped if curried and used for composition, whereas transform includes the source as an argument.

observable(signal) => Observable

Connects a signal to a TC39 observable. Whenever the signal is updated the change will be propagated to the observable. This observable can be used with libraries like RxJS which unlock a whole number of operators and functionality. Going the opposite direction is just passing a signal setter to the observable subscribe method.

Operators

All operators support curried and non-curried (signal passed as first argument) forms. Curried form is what is listed here.

delay(timeMs: number)

Delay value propagation by the given time in milliseconds.

defer({ timeoutMs: number })

Defers propagation until CPU is idle unless optional timeout has expired.

map(v => any)

Map value to another value.

mergeMap(v => signal | () => any)

Project inside signal or accessor function to output signal.

tap(v => void)

Does not affect value propagation. Useful for side effects or debugging.

pairwise()

Combines previous value with current value as an array.

scan((accumulator, value) => result, seed)

Accumulators the result of each value propagation and feeds it to the next.

filter(v => boolean)

Propagate value change if condition is true.

Observables

Signals and Observable are similar concepts that can work together but there are a few key differences. Observables are as defined by the TC39 Proposal. These are a standard way of representing streams, and follow a few key conventions. Mostly that they are cold, unicast, and push-based by default. What this means is that they do not do anything until subscribed to at which point they create the source, and do so for each subscription. So if you had an Observable from a DOM Event, subscribing would add an event listener for each function you pass. In so being unicast they aren't managing a list of subscribers. Finally being push you don't ask for the latest value, they tell you.

Observables track next value, errors, and completion. This is very useful for tracking discreet events over time. Signals are much simpler. They are hot and multicast in nature and while capable of pushing values over time aren't aware of it themselves. They are simple and synchronous. They don't complete, they exist or they don't exist.

Observables can work well with Signals as being a source that feeds data into them. Like State, Observables are another tool that allow more control in a specific aspect of your application. Where State is valuable for reconciling multiple Signals together into a serializable structure to keep managing Component or Store code simple, Observables are useful for transforming Async data pipelines like handling Data Communication services.

0.26.5-beta.0

3 years ago

0.26.3

3 years ago

0.26.2

3 years ago

0.26.5

3 years ago

0.26.4

3 years ago

0.26.1

3 years ago

0.26.0

3 years ago

0.25.1

3 years ago

0.25.0

3 years ago

0.24.16

3 years ago

0.24.15

3 years ago

0.24.14

3 years ago

0.24.13

3 years ago

0.24.10

3 years ago

0.24.12

3 years ago

0.24.11

3 years ago

0.24.9

3 years ago

0.24.7

3 years ago

0.24.6

3 years ago

0.24.6-beta.0

3 years ago

0.24.5

3 years ago

0.24.4

3 years ago

0.24.3

3 years ago

0.24.2

3 years ago

0.24.1

3 years ago

0.24.0-beta.4

3 years ago

0.24.0

3 years ago

0.24.0-beta.2

3 years ago

0.24.0-beta.1

3 years ago

0.24.0-beta.0

3 years ago

0.23.11

3 years ago

0.23.10

3 years ago

0.23.9

3 years ago

0.23.8

3 years ago

0.23.7

3 years ago

0.23.6

3 years ago

0.23.5

3 years ago

0.23.4

3 years ago

0.23.3

3 years ago

0.23.2

3 years ago

0.23.1

3 years ago

0.23.0

3 years ago

0.23.0-beta.0

3 years ago

0.22.10

3 years ago

0.22.9

3 years ago

0.22.8

3 years ago

0.22.7

3 years ago

0.22.6

3 years ago

0.22.5

3 years ago

0.22.4

3 years ago

0.22.3

3 years ago

0.22.2

3 years ago

0.22.0

3 years ago

0.22.0-beta.4

3 years ago

0.22.0-beta.3

3 years ago

0.22.0-beta.2

3 years ago

0.21.6

4 years ago

0.22.0-beta.1

4 years ago

0.22.0-beta.0

4 years ago

0.21.5

4 years ago

0.21.4

4 years ago

0.21.3

4 years ago

0.21.2

4 years ago

0.21.1

4 years ago

0.21.0

4 years ago

0.21.0-beta.0

4 years ago

0.20.3

4 years ago

0.20.2

4 years ago

0.20.1

4 years ago

0.20.0

4 years ago

0.20.0-beta.10

4 years ago

0.20.0-beta.9

4 years ago

0.20.0-beta.8

4 years ago

0.20.0-beta.7

4 years ago

0.20.0-beta.6

4 years ago

0.20.0-beta.5

4 years ago

0.20.0-beta.4

4 years ago

0.20.0-beta.3

4 years ago

0.20.0-beta.2

4 years ago

0.20.0-beta.1

4 years ago

0.20.0-beta.0

4 years ago

0.19.3

4 years ago

0.19.2

4 years ago

0.19.1

4 years ago

0.19.0

4 years ago

0.19.0-beta.5

4 years ago

0.19.0-beta.3

4 years ago

0.19.0-beta.2

4 years ago

0.19.0-beta.1

4 years ago

0.19.0-beta.0

4 years ago

0.18.13

4 years ago

0.18.14

4 years ago

0.18.12

4 years ago

0.18.11

4 years ago

0.18.10

4 years ago

0.18.9

4 years ago

0.18.8

4 years ago

0.18.7

4 years ago

0.18.5

4 years ago

0.18.4

4 years ago

0.18.3

4 years ago

0.18.2

4 years ago

0.18.1

4 years ago

0.18.0

4 years ago

0.18.0-beta.2

4 years ago

0.18.0-beta.3

4 years ago

0.18.0-beta.0

4 years ago

0.18.0-beta.1

4 years ago

0.17.7

4 years ago

0.17.5

4 years ago

0.17.2

4 years ago

0.17.1

4 years ago

0.17.0

4 years ago

0.17.0-beta.3

4 years ago

0.17.0-beta.1

4 years ago

0.17.0-beta.2

4 years ago

0.17.0-beta.0

4 years ago

0.16.14

4 years ago

0.16.12

4 years ago

0.16.11

4 years ago

0.16.10

4 years ago

0.16.9

4 years ago

0.16.8

4 years ago

0.16.7

4 years ago

0.16.6

4 years ago

0.16.5

4 years ago

0.16.4

4 years ago

0.16.2-beta.4

4 years ago

0.16.2-beta.3

4 years ago

0.16.2

4 years ago

0.16.2-beta.2

4 years ago

0.16.2-beta.1

4 years ago

0.16.2-beta.0

4 years ago

0.16.1

4 years ago

0.16.0-beta.7

4 years ago

0.16.0

4 years ago

0.16.0-beta.6

4 years ago

0.16.0-beta.5

4 years ago

0.16.0-beta.4

4 years ago

0.16.0-beta.2

4 years ago

0.16.0-beta.1

4 years ago

0.16.0-beta.0

4 years ago

0.15.5

4 years ago

0.15.4

4 years ago