npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@penumbra-zone/getters

v3.0.1

Published

Convenience getters for the deeply nested optionals of Penumbra's protobuf types

Downloads

346

Readme

Getters

Getters were designed to solve a common pain point when working with deserialized Protobuf messages: accessing deeply nested, often optional properties.

For example, let's say you have an AddressView, and you want to render an AddressIndex. You'd need render it conditionally, like so:

<div>
  {addressView.addressView.case === 'decoded' && addressView.addressView.value.index?.account && (
    <span>{addressView.addressView.value.index.account}</span>
  )}
</div>

Not very readable, and pretty annoying to type. But it can get even worse! Imagine you now have a MemoView, which optionally contains an AddressView. You still want to render an AddressIndex. So you'd render it with even more conditions, like so:

<div>
  {memoView.memoView.case === 'visible' &&
    memoView.memoView.value.plaintext?.returnAddress.addressView.case === 'decoded' &&
    memoView.memoView.value.plaintext.returnAddress.addressView.value.index?.account && (
      <span>{addressView.addressView.value.index.account}</span>
    )}
</div>

This quickly gets pretty cumbersome. You could, of course, throw a bunch of type guards at the top of your component to simplify your markup:

if (memoView.memoView.case !== 'visible') throw new Error()
if (memoView.memoView.value.plaintext?.returnAddress.addressView.case !== 'decoded') throw new Error()
if (typeof memoView.memoView.value.plaintext.returnAddress.addressView.value.index?.account === 'undefined') throw new Error()

<div>
  <span>{addressView.addressView.value.index.account}</span>
</div>

This works, but you still have a bunch of boilerplate code crowding your component.

Getters solve all that. Getters are tiny, composable functions, inspired by the functional programming lens pattern, that allow you to get at deeply nested, often optional properties without all the boilerplate.

Let's solve the above problem with getters. First, let's create a getter that, when given a MemoView, returns its AddressView:

const getAddressView = createGetter((memoView?: MemoView) =>
  memoView.memoView.case === 'visible' ? memoView.memoView.plaintext?.returnAddress : undefined,
);

getAddressView() is now a simple function that can be called with a MemoView, like this: getAddressView(memoView). It will then return the AddressView, or throw if it's undefined.

OK, next, let's create another getter that, when given an AddressView, returns its AddressIndex:

const getAddressIndex = createGetter((addressView?: AddressView) =>
  addressView?.addressView.case === 'decoded' ? addressView.addressView.value.index : undefined,
);

Again, getAddressIndex() is a simple function that can be called with an AddressView, like this: getAddressIndex(addressView). It will then return the AddressIndex, or throw if it's undefined.

Since we defined these two functions with createGetter(), though, they have a pipe method that let us chain them together. That way, we can easily create a getter that, when given a MemoView, will return an AddressIndex:

const getAddressIndexFromMemoView = getAddressView.pipe(getAddressIndex);

OK, now we can quickly clean up our component code:

<div>
  <span>{getAddressIndexFromMemoView(memoView)}</span>
</div>

Way better!

At this point, it's worth mentioning that getters are required by default. If any step along the getter chain above returns undefined, they will throw a GetterMissingValueError.

(It might seem unintuitive that getter functions are required, but are defined with an optional argument -- e.g., createGetter((addressView?: AddressView) => ... ). Without that optionality, you'd get a TypeScript complaint like Type 'undefined' is not assignable to type 'AddressView'. Getters assume that they can be passed undefined; otherwise, TypeScript would make pipe()ing impossible, since deeply nested properties are often optional. Don't worry, though: createGetter ensures that your getters still throw if they get undefined, which is what guarantees type safety.)

What if the value you're getting is optional, though? What if you don't want your getter to throw if either the value it's passed, or the value it returns, is undefined? That's what the .optional() property on the getter is for:

const addressView = getAddressView.optional()(memoView)

<div>
  {addressView && <AddressViewComponent addressView={addressView} />}
</div>

Or, if you want to chain multiple getters together and make the whole chain optional, call .optional() on the first getter in the chain (which will then mark the rest of the chain as optional, too):

const getAddressIndexFromMemoView = getAddressView.optional().pipe(getAddressIndex);
const addressIndex = getAddressIndexFromMemoView(memoView)

<div>
  {addressIndex && <AddressIndex addressIndex={addressIndex} />}
</div>