Neutrino Packing Machine

    @thi.ng/vclock
    TypeScript icon, indicating that this package has built-in type declarations

    0.3.7 • Public • Published

    vclock

    npm version npm downloads Twitter Follow

    This project is part of the @thi.ng/umbrella monorepo.

    About

    Vector clock functions for synchronizing distributed states & processes.

    Vector clock algorithm:

    • Initially all clocks are zero.
    • Each time a process experiences an internal event, it increments its own logical clock in the vector by one (inc()).
    • Each time a process sends a message, it increments its own logical clock in the vector by one and then sends a copy of its own vector.
    • Each time a process receives a message, it increments its own logical clock in the vector by one and updates each element in its vector by taking the maximum of the value in its own vector clock and the value in the vector in the received message (merge()).

    The package provides the following immutable vector clock operations, where vector clocks are plain JS objects:

    • inc(clock, id) - increment (or create) clock component
    • remove(clock, id) - remove clock component
    • merge(a, b) - merge two vector clocks
    • signedSkew(a, b) - max signed difference between vector clocks
    • absSkew(a, b) - max unsigned difference between vector clocks
    • compare(a, b) - comparator for logically ordering vector clocks
    • isBefore(a, b) - true if a < b
    • isAfter(a, b) - true if a > b
    • isConcurrent(a, b) - if both clocks represent concurrent updates
    • equiv(a, b) - equality predicate
    • orderAsc(a, b) - alias for compare()
    • orderDesc(a, b) - reverse order to orderAsc()

    References:

    Status

    BETA - possibly breaking changes forthcoming

    Search or submit any issues for this package

    Installation

    yarn add @thi.ng/vclock

    ES module import:

    <script type="module" src="https://cdn.skypack.dev/@thi.ng/vclock"></script>

    Skypack documentation

    For Node.js REPL:

    # with flag only for < v16
    node --experimental-repl-await
    
    > const vclock = await import("@thi.ng/vclock");
    

    Package sizes (gzipped, pre-treeshake): ESM: 464 bytes

    Dependencies

    API

    Generated API docs

    TODO

    Authors

    Karsten Schmidt

    If this project contributes to an academic publication, please cite it as:

    @misc{thing-vclock,
      title = "@thi.ng/vclock",
      author = "Karsten Schmidt",
      note = "https://thi.ng/vclock",
      year = 2018
    }

    License

    © 2018 - 2022 Karsten Schmidt // Apache Software License 2.0

    Install

    npm i @thi.ng/vclock

    DownloadsWeekly Downloads

    10

    Version

    0.3.7

    License

    Apache-2.0

    Unpacked Size

    27.6 kB

    Total Files

    6

    Last publish

    Collaborators

    • thi.ng