项目作者: theKashey

项目描述 :
⛓ Declarative task scheduler
高级语言: TypeScript
项目地址: git://github.com/theKashey/react-queue.git
创建时间: 2018-07-08T10:56:59Z
项目社区:https://github.com/theKashey/react-queue

开源协议:MIT License

下载



React ⏳ Queue




A declarative scheduler

















To schedule events one-after another. To play lazy animations in order, correlated with their position on the page.

API

Scheduler

  • Scheduler - task scheduler. Collect tasks and execute them with stepDelay between in the priority order.
    • stepDelay - delay between two events
    • [reverse] - reverses the queue
    • [source] - priority calculation function
    • [withSideEffect] - indicates that Scheduler has side effects, and enabled auto update(re-render) on task execution. Affects performance.
    • [observe] - cache buster property. Scheduler sorts queue only on element change, in case of using source you might need “inform”
    • [noInitialDelay] - remove delay from the first task.
    • [disabled] - disables ticking
      it to resort queue.
  1. import {Scheduler} from 'react-queue';
  2. <Scheduler stepDelay={1000} >
  3. {channel => .... }
  4. </Scheduler>
  5. // use source to create priority based on element position on the page
  6. <Scheduler stepDelay={1000} source={ ({ref}) => ref.getBoundingClientRect().top} />

channel also provides channel.reset() function, to clear all executed bits, and start everything from the scratch.

Queue

  • Queue - queued event. It just got executed, nothing more. “When”, in “when order” - that is the question.
    • channel - channel acquired from Scheduler
    • callback - callback to execute. In case if callback will return a number, or a promise resolving to number, it would be used to shift delay to the next step.
    • priority - pririty in queue, where 0-s should be executed before 1-s.
    • [shift] - sub priority change. shift={-1} will swap this task with previous sibling.
    • [disabled] - holds queue execution (sets priority to Infitity).
      next tick will be moved by {number}ms. In case of just Promise - next tick will wait to for promise to be resolved.
    • [children] - any DOM node, Queue will pass as ref into scheduler’s source
  1. import {Scheduler, Queue} from 'react-queue';
  2. <Scheduler stepDelay={1000} >
  3. {channel =>
  4. <Queue channel={channel} priority={1} callback={doSomething} ></Queue>
  5. // this one will report `ref` to the scheduler
  6. <Queue channel={channel} callback={doSomething}>
  7. <div>42</div>
  8. </Queue>
  9. <Queue channel={channel} callback={() => this.setState({x: 1})}>
  10. <div style={{position: 'absolute', top: 50}}> 1 {x == 1 && "selected!!"}</div>
  11. </Queue>
  12. <Queue channel={channel} callback={() => this.setState({x: 2})}>
  13. <div style={{position: 'absolute', top: 10}}> 2 {x == 2 && "selected!!"}</div>
  14. </Queue>
  15. <Queue channel={channel} callback={() => this.setState({x: 3})}>
  16. <div style={{position: 'absolute', top: 100}}> 3 {x == 3 && "selected!!"}</div>
  17. </Queue>
  18. }
  19. </Scheduler>

FlattenPriorityGroup

  • FlattenPriorityGroup - “flattens” all priority changes inside. Could help manage nested tasks.
    • channel - channel acquired from Scheduler
    • [children] - render function
    • [priority] - task priority. Would be set for all nested tasks.
    • [shift] - sub priority change. shift={-1} will swap this task with previous sibling.
    • [disabled] - holds queue execution (sets priority to Infitity).

In the next example executing order would be - 2, 1, 4, 3.

  1. <Scheduler stepDelay={1000} >
  2. {channel => (
  3. <React.Fragment>
  4. <FlattenPriorityGroup channel={channel}>
  5. { pchannel => [
  6. <Promised priority={1}>1</Promised>,
  7. <Promised priority={0}>2</Promised>
  8. ]}
  9. </FlattenPriorityGroup>
  10. <FlattenPriorityGroup channel={channel}>
  11. { pchannel => [
  12. <Promised priority={1}>3</Promised>,
  13. <Promised priority={0}>4</Promised>
  14. ]}
  15. </FlattenPriorityGroup>
  16. </React.Fragment>
  17. )}
  18. </Scheduler>

Promised

  • Promised - promised event. Once it started it should all done when it’s done. This is a more complex form of queue, with much stronger feedback.
    • channel - channel acquired from Scheduler
    • [children] - render function
    • [autoexecuted] - auto “done” the promised. boolean or number. If number - would be used to shift next step.
    • [priority] - task priority. Lower goes first
    • [shift] - sub priority change. shift={-1} will swap this task with previous sibling.
    • [disabled] - holds queue execution (sets priority to Infitity).
      ```js
      import {Scheduler, Promised} from ‘react-queue’;
      import {Trigger} from ‘recondition’;

{channel =>

{({executed, active, done, forwardRed}) => (

{executed && “task is done”}
{active && “task is running”}
// don’t call anything in render
done(42/ make next step by 42ms later/)}/>

)

}

// this code has the same behavior


{channel =>

{({executed, active, done, forwardRed}) => (


{executed && “task is done”}
{active && “task is running”}

)

}

  1. For example - animation - it will execute one `Promised` after another, and triggering waterfall animation.
  2. ```js
  3. import {Scheduler, Promised} from 'react-queue';
  4. import {Trigger} from 'recondition';
  5. <Scheduler stepDelay={300} >
  6. {channel =>
  7. <Promised channel={channel} autoexecuted>
  8. {({executed, active, fired}) => (<div style={styles[executed||active ? styleA : styleB}>Line1</div>)}
  9. </Promised>
  10. <Promised channel={channel} autoexecuted>
  11. {({executed, active, fired}) => (<div style={styles[executed||active ? styleA : styleB}>Line2</div>)}
  12. </Promised>
  13. <Promised channel={channel} autoexecuted>
  14. {({executed, active, fired}) => (<div style={styles[executed||active ? styleA : styleB}>Line3</div>)}
  15. </Promised>
  16. <Promised channel={channel} autoexecuted>
  17. {({executed, active, fired}) => (<div style={styles[executed||active ? styleA : styleB}>Line4</div>)}
  18. </Promised>
  19. }
  20. </Scheduler>

Examples

react-remock + react-queue - simple and complex example - “jquery like” image lazy loading with queued execution.
react-visibility-sensor + react-queue - animate element appearance based on visibility check.

Licence

MIT