• 简体中文
  • Mapper 插件

    Mapper 接收响应式的 HAST 内容块,返回另一份响应式内容块列表。它不只执行一次树转换,还能在两次文本到达之间继续更新输出;平滑输出就是其中一种用途。

    声明 mapper

    下面只保留前几个内容块,用于限制预览长度:

    import type { MapperInputs, MapperPluggable } from '@fluxdown/core';
    
    import { once, useMap } from 'stative';
    
    type PreviewOptions = { count: number };
    type PreviewInputs = MapperInputs<PreviewOptions>;
    
    const Preview = once(function Preview({ source, count }: PreviewInputs) {
      return useMap(source, (blocks) => blocks.slice(0, count));
    });
    
    const mappers: MapperPluggable<PreviewOptions>[] = [[Preview, { count: 3 }]];

    在快速开始的 Core 输入中加入 mappers 即可。返回的列表会跟随源列表变化,每个内容块仍保留独立的内容订阅。这个 mapper 的 count 是普通的构造选项。

    once 只构建一次状态流,useMap 描述后续如何更新。mapper 可以返回可读闭包,也可以返回 Stative 描述符。

    不要销毁或直接修改上游内容块。如果创建了派生内容块或订阅,应交给 Stative 的生命周期管理,在 mapper 销毁时一并清理。

    配置平滑输出

    预设 Smooth 接收响应式选项。把外部状态转成可读闭包,再放进插件选项:

    import type { MapperPluggable } from '@fluxdown/core';
    import type { SmoothBaseInputs } from '@fluxdown/core-presets/mapper';
    
    import { Smooth } from '@fluxdown/core-presets/mapper';
    import { MutableState, toClosure } from 'stative';
    
    const enabled = MutableState.of(true);
    const active = toClosure(enabled);
    
    const mappers: MapperPluggable<SmoothBaseInputs>[] = [
      [Smooth, { enabled: active }],
    ];

    将 mappers 传给 Core 后,调用 enabled.next(false) 可以立即显示剩余内容。结束使用时,先销毁 Core,再销毁 active 和 enabled。

    内置插件

    Core 默认包含以下插件,无需手动注册。

    PRESET_MAPPER_PLUGINS,从 @fluxdown/core-presets/mapper 导入。

    插件作用
    Smooth将新内容按时间逐步释放,让成批到达的文本平滑显示。
    Shad为刚出现的末尾文字生成渐变标记,由渲染器呈现。

    Core 中这两项默认关闭,自定义渲染器的启用方式见状态与更新。