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

v0.0.26

Published

BLoC (Business Logic of Component) Pattern for React.

Downloads

37

Readme

BLoC (Business Logic of Component) pattern for React.

BLoC 全称: Business Logic of Component, 业务逻辑组件.

BLoC 架构图:

architecture

Bloc

Bloc 主要做一些业务逻辑, 事件绑定等功能. 其内部注入了一个 Repository 作为数据源.

/**
 * Created by MeePwn
 * https://github.com/maybewaityou
 *
 * description:
 *
 */
import { EnhanceSubject } from 'react-bloc';
import { injectable, inject } from 'inversify';

import { Bloc } from '@arch';
import { BLOC_TYPES, REPOSITORY_TYPES } from './ioc/types';
import ITestDataSource from './data/source/ITestDataSource';

@injectable()
export default class TestBloc extends Bloc {
  @inject(REPOSITORY_TYPES.TestRepository)
  private repository: ITestDataSource;

  private _result = '';
  public result$ = new EnhanceSubject(this._result);

  public handleLocalPress = async () => {
    this._result = await this.repository.fetchData({ isDirty: false });
    this.result$.add(this._result);
  };

  public handleRemotePress = async () => {
    this._result = await this.repository.fetchData({ isDirty: true });
    this.result$.add(this._result);
  };

  public token() {
    return BLOC_TYPES.TestBloc;
  }
}

Repository

Repository 作为 Bloc 唯一的数据源. 内部注入了 LocalDataSource 和 RemoteDataSource 两个数据源, 在其内部进行逻辑判断, 为 Bloc 提供唯一数据.

/**
 * Created by MeePwn
 * https://github.com/maybewaityou
 *
 * description:
 *
 */
import { IRepository } from 'react-bloc';
import { injectable, inject } from 'inversify';

import { StrategyProcessor } from '@arch';
import { DATA_SOURCE_TYPES } from '../../ioc/types';
import ITestDataSource from './ITestDataSource';
import { ITestLocalDataSource } from '../source/local/TestLocalDataSource';
import { ITestRemoteDataSource } from '../source/remote/TestRemoteDataSource';

@injectable()
export default class TestRepository implements IRepository, ITestDataSource {
  @inject(DATA_SOURCE_TYPES.TestRemoteDataSource)
  private remoteDataSource: ITestRemoteDataSource;
  @inject(DATA_SOURCE_TYPES.TestLocalDataSource)
  private localDataSource: ITestLocalDataSource;

  public fetchData(params: any) {
    if (params.isDirty) {
      return this.remoteDataSource.fetchData(params);
    } else {
      return this.localDataSource.fetchData(params);
    }
  }
}

DataSource

DataSource 主要分为两种:

  1. LocalDataSource: 内部注入了 DBService, 使用 DBService 获取本地数据.
/**
 * Created by MeePwn
 * https://github.com/maybewaityou
 *
 * description:
 *
 */
import { ILocalDataSource } from 'react-bloc';
import { injectable, inject } from 'inversify';

import { PROCESSOR } from '@arch';
import { DBService } from '@service';
import ITestDataSource from '../ITestDataSource';

export interface ITestLocalDataSource extends ILocalDataSource, ITestDataSource {}

@injectable()
export class TestLocalDataSource implements ITestLocalDataSource {
  @inject(PROCESSOR.DBService)
  private dbService: DBService;

  public fetchData(params: any) {
    return Promise.resolve('local');
  }
}
  1. RemoteDataSource: 内部注入了 WebService, 使用 WebService 调用接口, 获取远程数据.
/**
 * Created by MeePwn
 * https://github.com/maybewaityou
 *
 * description:
 *
 */
import { IRemoteDataSource } from 'react-bloc';
import { injectable, inject } from 'inversify';

import { PROCESSOR } from '@arch';
import { WebService } from '@service';
import ITestDataSource from '../ITestDataSource';

export interface ITestRemoteDataSource extends IRemoteDataSource, ITestDataSource {}

@injectable()
export class TestRemoteDataSource implements ITestRemoteDataSource {
  @inject(PROCESSOR.WebService)
  private webService: WebService;

  public fetchData(params: any) {
    return Promise.resolve('remote');
  }
}

BlocProvider (视图标签)

BlocProvider: 使用 BlocProvider 将 Bloc 与 View 进行关联, 使在 View 中, 可以使用 Bloc.

StreamBuilder (视图标签)

StreamBuilder: 响应式视图. 当 Bloc 中数据改变时, 会重构 StreamBuilder.

StreamBuilder 架构图:

StreamBuilder

/**
 * Created by MeePwn
 * https://github.com/maybewaityou
 *
 * description:
 *
 */
import React from 'react';
import { StyleSheet, Button, Image, ImageStyle, Text, TextStyle, View, ViewStyle } from 'react-native';
import { BlocProvider, IBloc, StreamBuilder, ISnapshot } from 'react-bloc';

import { container } from '@arch';
import { TestBloc, BLOC_TYPES } from '../bloc/index';

interface IStyle {
  container: ViewStyle;
}

export default () => {
  const _testBloc = container.get<IBloc>(BLOC_TYPES.TestBloc);
  return <BlocProvider bloc={_testBloc} child={_viewBuilder} />;
};

function _viewBuilder() {
  const _testBloc = BlocProvider.of<TestBloc>(BLOC_TYPES.TestBloc);
  return (
    <View style={styles.container}>
      <StreamBuilder stream={_testBloc.result$} builder={_contentBuilder} />
    </View>
  );
}

function _contentBuilder(snapshot: ISnapshot<string>) {
  return <Text>{snapshot.data}</Text>;
}

const styles = StyleSheet.create<IStyle>({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});