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

taro-list-data-manager

v0.3.2

Published

taro-list 虚拟列表数据管理

Downloads

51

Readme

VirtualListDataManager

Build StatusCoverage Status

虚拟列表数据管理类, 启用虚拟滚动时必传

由于小程序不能动态渲染视图, 通过暴露一系列方法操作数据和更新配置, 然后再通过函数回调的方式把数据传给上层, 然后进行渲染。

更新数据的时候, 首先同步触发 stateChange, 得到当前总高度; 通过设置好的回调,在下一个事件循环中, 通知虚拟列表返回当前位置的数据。

更新 itemSize, column 等数据也会触发上一步骤。

使用方式

const dataManager = new VirtualListDataManager({
  itemSize: 120,
  onChange: data => {
    this.setState({
      list: data
    });
  }
});

API

class VirtualListDataManager<T = any> {
  // 初始化参数
  constructor(options: VirtualListDataManagerOptions<T>);
  // 更新配置 参数等同 options
  updateConfig: (config: Partial<VirtualListDataManagerOptions<T>>) => void;
  // 清空数据
  clear: () => void;
  // push 数据 同 Array.prototype.push
  push: (...value: T[]) => number;
  // 设置数据
  set: (...value: T[]) => void;
  // 删除或者增加数据, 同 Array.prototype.splice
  splice: (start: number, deleteCount: number, ...items: T[]) => T[];
  // 获取整个数据
  get: () => T[];
  // 同 Array.prototype.pop
  pop: () => T | undefined;
  // 获取当前数据行数
  getItemCount: () => number;
  // 更新 Virtual List
  forceUpdate: () => void;
  // 设置加载状态 具体使用可参考 demo https://github.com/raohong/taro-list/blob/master/src/pages/list/index.tsx
  setLoadStatus: (
    customData?: Record<string | number, any>,
    itemSizeOfLoadStatus?: MiniItemSizeValue
  ) => ILoadStatusResult<T>;
  // 清空所有加载状态 (传入 ID 时清空该 ID)
  clearAllLoadStatus: (id?: string) => void;
}

// 初始化参数
interface VirtualListDataManagerOptions<T> {
  // 虚拟列表项目每个大小, 支持 number / string / number[] / string[] / () => string[] | number[] , 默认 50
  itemSize?: MiniItemSize;
  // 每行的列数 具体使用可参考 demo https://github.com/raohong/taro-list/blob/master/src/pages/list/index.tsx
  column?: number;
  // 项目估算大小 默认 60
  estimatedSize?: number;
  // sticky 数组, 通过 sticky 定位实现
  stickyIndices?: number[];
  // 提前渲染项目数量, 增大可避免快速滚动白屏, 默认 5
  overscan?: number;
  // 必传, 将当前要渲染的数据更新
  onChange: VirtualListDataManagerChangeHandler<T>;
}

type MiniItemSizeValue = string | number;
type MiniItemSize =
  | MiniItemSizeValue
  | MiniItemSizeValue[]
  | ((index: number) => MiniItemSizeValue);

interface SizeAndPositionOfItemData {
  index: number;
  style: ItemStyle;
}

interface VirtualListItemData<T = any> extends SizeAndPositionOfItemData {
  item: T;
}

type VirtualListDataManagerChangeHandler<T> = (
  items: VirtualListItemData<T>[]
) => void;

type VirtualListDataManagerUpdater<T> = (data: T[]) => VirtualListItemData<T>[];