GithubHelp home page GithubHelp logo

duart38 / observe Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 0.0 251 KB

Type-safe observable values (made with Deno)

License: MIT License

TypeScript 83.38% JavaScript 16.62%
observable observer event livedata-databinding binding deno denoland typescript javascript javascript-library

observe's Introduction

alt text

Observe

Simply observe any variable

Test module
nest badge

Example (Deno)

let obs = new Observe("initial value"); // new observable with type String

obs.bind((data) => { // bind to receive updates on value change
    // Do some stuff here (data here is of type string)
});

obs.setValue("another value"); // sets the value
obs.setValue("lorem", "ipsum", "dolor"); // sets the last value as active and emitting the other ones
console.log(obs.setValue("another value"))

Also check the examples folder for more examples

Example (Basic html + js)

Download Observe.js from the releases tab

index.html

<html>
    <head></head>
    <body>
        <h1>Observable value test</h1>
        <script type="module" src="Observe.bundle.js"></script> <!-- Needs to come from a server as type module does not support local files-->
        <script src="./index.js"></script>
    </body>
</html>

index.js

var observable = new Observe("s")
observable.bind((d)=>{
    console.log(d)
})
observable.setValue("new val")

Methods


Name Description
getValue() Returns the current value. To be used outside of callbacks
getHistory() Gets the change history. Every time a new value is set it is pushed to a history array
bind() used to listen to changes. Takes a callback method that is called with the new data when the observe instance changes. Returns the function used for the event listener.. To be used with the unBind method (see below)
unBind() Unbinds a previously bound EventListener or EventListenerObject. The callback returned by bind() should be provided. Returns this
setValue() Updates the observed value.. all bound will be notified. Setting a value equals to the last set value will do nothing. Returns the value that was passed in it (last value in case multiple)
stop() Prevents event from reaching any registered event listeners after the current one. Returns this
reset() Restore the state to the original provided method. Returns this
unBindAll() Unbinds ALL previously bound EventListener or EventListenerObject.
maxHistorySize Limit the history array size (can be lowered to save some precious ram)
getGlobalObservable If opted in, receives the instance created that was attached to the JS window

The history length is limited to 1000 values.. after this the first value (excluding the original) will be removed on each push.. to increase or decrease this value change the "maxHistorySize" variable

See JSdoc for more information

It is recommended to lower the value of maxHistorySize if you are not going to use the history.

Testing

deno test

observe's People

Contributors

duart38 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

observe's Issues

[BUG] Table in README broken on nest.land

Describe the bug
MarkDown table is broken on nest.land

To Reproduce
Steps to reproduce the behavior:

  1. Go to Observe on nest.land
  2. Scroll down to the table section

Expected behavior
image

Screenshots
If applicable, add screenshots to help explain your problem.
image

Desktop (please complete the following information):

  • OS: [MacOS]
  • Browser [chrome, safari]
  • Version [14.0]

Additional context
I had to use a table generator to get the table working on deno.land.. this seems to work fine on github either.
However it breaks on nest.land

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.