1.0.16 • Published 2 years ago

@lather/core v1.0.16

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Pull based typesafe stream library

chain type DI 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

1.0.2

2 years ago

1.0.1

2 years ago

1.0.16

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.17

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.1

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.4.0

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago