Skip to content

z81/lather

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pull based typesafe stream library

[chain type DI] [safe(Result | Fail) or unsafe(exception | data)] npm version Build Status Coverage Status

Installation

yarn add @lather/core or npm i @lather/core

Docs

https://z81.github.io/lather/

Full examples

src/examples/

Short examples

console.log('Sync task:', Task.succeed(1).map(n => n * 2).runUnsafe())
// Sync task: 4
console.log('Async task:', await Task.succeed(1).map(n => n * 2).delay(1000).runUnsafe())
// Async task: 4     after 1000ms
console.log('Sync safe task:', Task.succeed(1).map(n => n * 2).run())
// Sync safe task: Result { value: 4 }
try {
  console.log('Sync unsafe task:', Task.succeed(1).map(n => {throw new Error()}).runUnsafe())
} catch(e) {
  console.error("error", e)
}
// error Error
console.log('Sync safe task:', Task.succeed(1).map(n => {throw new Error()}).run()))
// Sync safe task: Fail {error: Error}
let i = 0;
const r = Task.succeed(1).map(n => {
  if (++i < 3) {
    throw "Error i < 3!!!"
  }
  return i;
}).retryWhile(() => true).run() // or .retryWhile(Conditions.always)
// r = 4
let i = 0;
const r = Task.succeed(1).map(n => {
  if (++i < 3) {
    throw "Error i < 3!!!"
  }
  return i;
}).retryWhile(Conditions.times(7)).run()
// r = 4
Task.succeed(1).access<{logger: (msg: string) => void}>().run()
//                                          type error----^^^^^
/*                                 ... {
                                    logger: {
                                        Error: "Incorrect dependencies";
                                        Field: "logger";
                                        RequiredValue: (msg: string) => void;
                                        ExceptedValue: undefined;
                                    }
                                  }...
*/
const serviceWithLogging = Task.succeed(1)
  .access<{ logger: (msg: string) => void }>()
  .map(({ logger }) => logger("Hi !"));

serviceWithLogging.provide({ logger: console.log }).run();
// Hi!
const serviceWithLogging = Task.succeed(1)
  .access<{ logger: (msg: string) => void }>()
  .map(({ logger }) => logger("Hi !"));

const app = serviceWithLogging
  .provide({ logger: console.log })

// override for test
app.provide({ logger: (msg: string) => mock("Log", msg) })
  .run();
// mock called with ["Log", "Hi!"]
let i = 0;
const r = Task.succeed(1).map(n => {
  if (++i < 3) {
    throw "Error i < 3!!!"
  }
  return i;
}).mapError(e => `Omg! ${e}`).run()
// r = Fail {value: "Omg! Error i < 3!!!"}
const openDb = () => console.log("open db");
const closeDb = () => console.log("close db");
const writeDb = () => console.log("write db");

Task.succeed(1)
  .map(openDb)
  .ensure(closeDb) // run after end
  .map(writeDb)
  .map((n) => {
    throw "Error ";
  })
  .run();
// open db
// write db
// close db
const t = Task.sequenceFrom([1, 2, 3]) // any iterable value
  .reduce((a, b) => a + b, 0)
  .runUnsafe();
console.log(t);
const t = Task.sequenceFrom("test")
  .reduce((a, b) => `${b}/${a}`, "")
  .runUnsafe();
console.log(t);
//  /t/e/s/t
const t = Task.sequenceFrom(new Set([1, 2, 3]))
  .reduce((a, b) => a + b, 0)
  .runUnsafe();
console.log(t); // 6
const t = Task.sequenceGen(function* () {
  yield 1;
  yield 2;
  yield 3;
})
  .reduce((a, b) => a + b, 0)
  .runUnsafe();
console.log(t); // 6

map mapError mapTo tap chain repeatWhile repeat sequenceGen sequenceFromIterable sequenceFromObject reduce retryWhile timeout access provide collectWhen collectAll ensureAll ensure delay

About

Pull based typesafe stream library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published