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

react-mobx-observed

v1.0.6

Published

`@observed` decorator for MobX and React/React-Native projects.

Downloads

16

Readme

react-mobx-observed

@observed decorator for MobX and React/React-Native projects.

This package adds @observed decorator to your environment. Utilizes mobx, rxjs and react packages.

@observed decorator is used for properties to automatically update them according to provided observable. Additionally, you can pass the source as observable returning function, which will be called with related React.Component class' instance as this. The returned observable will be used as the source for the decorated property.

There is also a setting for marking the source observable returning function as computed, hence whenever the dependent value changes, the observable is re-subscribed and the emitted values are assigned to the property.

Apart from the source function, you can specify another computed function which will cause the re-subscription to the source observable.

You can also list several properties your component class has and whenever they change, the source observable will be re-subscribed.

Likewise, you can also select when to assign a value when it is emitted from the source observable.

There is also a function to determine whether the property should be updated or not. This will have arguments given by shouldComponentUpdate function of React.Component and will be bound to the instance. Hence, you can reach class properties as this..

Lastly, there is side-effect property of this decorator which allows you to update several other properties of the class while it is still updating the decorated properties.

This decorator is also adds a function to the class called loadData. This is for manual calls.

This library uses WeakMaps to prevent modifications on the instance.

If your component has functions setError(error) and clearError() for error reporting, and startLoading(), stopLoading() for loading state changes, they will be called automatically. So you can update your views accordingly.

Examples

@observer
class VideoCommentsView extends React.PureComponent<{ video: IVideo }> {
  @observed({
    // Whenever the property `video` changes
    onPropsChange: 'video',
    // Or whenever any change happens on `comments` property of the `video`
    computedBy(this: VideoCommentsView) { return this.props.video.comments },
    // Load the comments of the video (returns an Observable)
    source(this: VideoCommentsView) { return loadCommentsOf$(this.props.video) }
  })
  readonly comments!: IVideoComment[] // Readonly because the property will be updated automatically.

  @observed({ source: likes$ })
  readonly likes!: IVideoLike[]

  @observed({
    // Source is computed, which means that whenever the viewState.currentVideo (observable) changes, the source will be subscribed again.
    isSourceComputed: true,
    source() { return getOwner$(viewState.currentVideo) }
  })
  readonly owner!: IVideoOwner

  @observed({
    // Whenever the `video` or `videoSources` change
    onPropsChange: [ 'video', 'videoSources' ],
    source(this: VideoCommentsView) {
      // Assume this observable emits value as
      // { name: string, value: any }
      // where name is the event name and the value is event value
      return getSourceInfo$(this.props.video)
    },
    select(data) {
      // Select (to assign to the property) when data.name is source
      // and the value to assign will be the `data.value`
      // If `data.name` is not source, then skip this
      return { select: data.name === 'source', value: data.value }
    },
    makeSideEffects(data) {
      // While the source is extracting, it may send information
      // such as the thumbnail, and assume we also want to update
      // the thumbnail information.

      // If the emitted event name is not `thumbnail`, skip.
      if (data.name !== 'thumbnail') { return }

      // Otherwise, assign the `thumbnail` property of this class, as `data.value`
      return { thumbnail: data.value }
    }
    readonly source!: string

    // This will be updated whenever source is being updated as a side-effect of updating source.
    @observable readonly thumbnail!: string
  })

  render() {
    return (
      <View>
        {this.comments.map(comment => <VideoComment model={comment}/>)}
      </View>
    )
  }
}