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-blocbuilder

v1.0.0

Published

BlocBuilder is a component which is like Flutter StreamBuilder. This component can help you to use BLoC pattern in react.

Downloads

5

Readme

react-blocbuilder

BlocBuilder is a component which is like Flutter StreamBuilder. This component can help you to use BLoC pattern in react.

NPM JavaScript Style Guide

Install

npm install react-blocbuilder

or

yarn add react-blocbuilder

Usage

  • BLoC Class
  import { Subscription, BehaviorSubject, Observable } from  'rxjs';
  // 引入RxJS庫
  // Subscription是用來管理資料流
  // BehaviorSubject是一個廣播資料流,可以有預設數值,通常用來儲存狀態
  // Observable,可以利用create創建一個流,通常可以將API包在裡面

  export class DataBloc {
	// 用來管理資料流
	_disposables = new  Subscription();

	// 資料儲存在這
	list = new  BehaviorSubject([]);

	// 當頁面關閉會呼叫,銷毀所有的訂閱
	dispose() {
	  this._disposables.unsubscribe();
	}

	getData() {
	  // 模擬一支API用RxJS包裝,通常將同一類型的API寫在另一個js或ts裡
	  const  observable =
		Observable.create(async  subscriber  => {
		  try {
			await  new  Promise((resolve, reject) => {
			  setTimeout(() =>  resolve(), 2000);
			});
			subscriber.next(['data_01', 'data_02']);
			subscriber.complete();
		  }
		  catch (e) {
			subscriber.error(e);
		  }
		});

	  // 呼叫API,並訂閱資料流
	  const  it = observable.subscribe({
		next:  data  => {
		  // 當API成功後會傳回data,更新list這個BehaviorSubject,
		  // 有訂閱這個流的會收到通知,也可以透過getValue()獲取
		  // 最新的狀態
		  this.list.next(data);
		},
		error:  err  => {
		  // 當API失敗,可以做例外處理
		},
	  });
	  // 將這個訂閱加到disposables中,集中管理
	  this._disposables.add(it);
    }
  }
  • React Component

  import  React, { useRef, useEffect } from  'react'
  import { DataBloc } from  './data_bloc';
  import { BlocBuilder } from  'react-blocbuilder'

  const  App = () => {
	const  bloc = useRef(new  DataBloc());

	useEffect(() => {
	  const  _bloc = bloc.current;
	  return () => {
		_bloc.dispose();
	  };
	}, []);

	return (
	  <div  style={{ margin:  '20px 20px' }}>
		<h1>BlocBuilder Demo</h1>
		<BlocBuilder
		  // 
		  initialValue={bloc.current.list.getValue()}
		  subject={bloc.current.list}
		  builder={snapshot  => {
			return (
			  <p>{JSON.stringify(snapshot.data)}</p>
			);
		  }}
		/>
		<button  onClick={() => bloc.current.getData()}>Get Data</button>
		<button  onClick={() =>  bloc.current.list.next([])}>Clear Data</button>
	  </div>
	);
  }
  
  export  default  App

License

MIT License

Copyright (c) 2020 JimmyTai.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.