Giter Club home page Giter Club logo

cuekit's Introduction

Tests

CueKit ๐Ÿš‚

Interruptible queuing system for closures

on cue - a signal for action

Description ๐Ÿท

A way to run tasks one after the other. (with support for canceling tasks)

Problem ๐Ÿคฏ

  • Hard to queue up tasks that needs to be preformed one after the other
  • Hard to unit-test network code
  • Async network code all over the place is hard to reason about and hard to make bug free
  • Features such as timeout and retry are hard to build into DispatchWorkItem or NSOperationQueue

Solution: ๐Ÿ’Š

  • Ability to add and remove items to the queue
  • The Queue should be able to be run from start to finish
  • Has tasks that can .start() and .stop() and has .onComplete() callback
  • Tasks should have the ability to support timeout and retry
  • The Queue should be able to paused/stopped, resumed/started, reset/canceled (โš ๏ธ๏ธ resume is not supported, only cancel โš ๏ธ๏ธ)
  • Queues should be able to be nested? Queues within queues? (maybe) (reach-goal, โš ๏ธ๏ธ not currently supported โš ๏ธ๏ธ) ๐Ÿค”

Example:

let networkTasks: [NetworkTask] = (0..<5).map { _ in // Create 5 network tasks
   .init { (_ value: Any?) in
      guard let strValue: String = (value as? Data)?.stringUTF8 else { XCTFail(); return }
      Swift.print("strValue:  \(strValue) โœจ")
   }
} // For loop network task
let queue: Queue = .init()
queue.onComplete = {
   Swift.print("queue test completed โœ…")
   expectation.fulfill() // If this line is called, then test passed
}
queue.enqueue(networkTasks) // Add tasks and begin executing

Todo: ๐Ÿ“

  • Look at pagination service project for mock-api inspo โœ…
  • Take a look at TestRunner (it has FIFO style architecture) โœ…
  • Add timeout callback? โœ… (would need to separate network and db transaction in one task, in order to cancel transaction if network task is not complete etc) (can use Dispatchueue with timeout ?)
  • Do research on other cancelable closures in swift. โœ… There should be another one in your dispatch research etc (DispatchWorkItem)
  • Do tests with timers and background calls etc โœ…
  • Test with run, add and cancel buttons ๐Ÿ‘ˆ
  • Explain some of the use cases
  • Maybe subclass NSOperation (investigation needed) see issues โœจ
  • Add support for result type? instead of just Any?
  • Add hasNext() like TestRunner? ๐Ÿšซ

cuekit's People

Contributors

eonist avatar

Stargazers

 avatar

cuekit's Issues

Support nested queues

  • requires isSuspended
  • requires dealing with tasks that are in progress etc

For now use one queue for all tasks. (simpler to reason about, and simpler code)

Add ability to resume queues after app quits

  • Would need support for queues to resume
  • Also for tasks and their content to be Codable and storable in plist or json etc

(Maybe not needed, might be too complex and unececary for the intent of this framework)

Make persistent queue system ala getLotus

from getlotus:

Under the hood
Lotus needs to be in sync with GitHub to ensure the state of notifications stays the same between the two. To do that, Lotus sends API requests to GitHub to mark notifications as read or unread.

Previously, I triggered those API requests immediately after you clicked a "mark as read" button (for example). However, requests can fail, take longer than expected or be triggered in parallel with other conflicting requests (e.g. mark as unread). Also, all in-flight requests can be canceled and lost if user quits the app.

To solve that, I implemented a persistent queue in Lotus. As soon as you mark notification as read, a new job is added to a queue. Then queue executes pending jobs one by one. While that is happening, queue's state is updated in the database to make sure no jobs are lost. If queue is currently processing a job and user quits the app, that same job will be restarted after app is launched again.

Here are the modules I've used to build it:

yocto-queue - Queue data structure.
p-timeout - Handles job timeouts.
p-retry - Retries failed jobs.
This feature was a much needed improvement and now I can sleep well at night, knowing that notifications are safely synced between Lotus and GitHub.

https://github.com/sindresorhus/yocto-queue

https://github.com/sindresorhus/p-timeout

https://github.com/sindresorhus/p-retry

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.