politie / sherlock

A reactive programming library for JavaScript applications, built with TypeScript.
Apache License 2.0
39 stars 12 forks source link

Pluggable plucker #26

Closed pavadeli closed 6 years ago

pavadeli commented 6 years ago

Sherlock provides the pluck method on derivations:

const a$ = atom({ property: 'value' });

const plucked$ = a$.pluck('property');

// now you can do:
plucked$.get();            // returns 'value';
plucked$.set('new value'); // a$ becomes: { property: 'new value' }

// In this case the pluck command is shorthand for the following:
const plucked$ = a$.lens({
    get: obj => obj.property,
    set: (newValue, obj) => { ...obj, property: newValue },
});

This can be used to create a cursor to a specific subtree of a more complex state. It works with ordinary JavaScript objects, but also with objects from Immutable.js and JavaScript Maps.

Just like with pluggable equals functions (#14), this pluck mechanism should also be pluggable to support other object types.