GithubHelp home page GithubHelp logo

jatins / icaro Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gianlucaguarini/icaro

0.0 2.0 0.0 96 KB

Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)

License: MIT License

JavaScript 100.00%

icaro's Introduction

icaro

Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)

Build Status NPM version NPM downloads MIT License

Installation

Via npm

$ npm i icaro -S

Script import

Via <script>

<script src='path/to/icaro.js'></script>

Via ES2015 modules

import icaro from 'icaro'

Via commonjs

const icaro = require('icaro')

Demos

Performance

icaro is really fast compared to the other reactive libs because it throttles smartly all the state changes

Usage

icaro will let you listen all the changes happening in a javascript object or array grouping them efficiently optimizing the performances of your listeners

const obj = icaro({})

// the variable "changes" here is a Map and the function is async
obj.listen(function(changes) {
  console.log(changes.get('foo')) // 'hi'
  console.log(changes.get('bar')) // 'there'
  console.log(changes.get('baz')) // 'dude'

  // kill all the listeners
  obj.unlisten()
})

obj.foo = 'hi'
obj.bar = 'dear'
obj.baz = 'dude'

icaro will let you also listen nested object, all the non primitive properties added to an icaro object will be automatically converted into icaro observable objects

const obj = icaro({})

// listen only the changes happening on the root object
obj.listen(function(changes) {
})

obj.nested = {

}

obj.nested.listen(function(changes) {
  // listen only the changes of obj.nested
})

obj.nested.someVal = 'hello'

icaro is able also to listen changes in arrays

// Here a bit of hardcore async stuff

const arr = icaro([])

// here you will get the name of the event that changed your array
arr.listen(function(changes) {
  console.log(changes.get('push')) // ['foo', 'bar']
  // kill all the listeners this included
  arr.unlisten()

  // add a brand new listener recursively.. why not?
  arr.listen(function(changes) {
    console.log(changes.get('reverse')) // ['bar', 'foo']
  })

  // dispatch with "reverse"
  arr.reverse()
})

// initial dispatch
arr.push('foo')
arr.push('bar')

You can also avoid to unsubscribe your callbacks because icaro will automatically unlisten them if the object bound to them will be garbage collected

API

Any icaro call will return a Proxy with the following api methods

icaro.listen(callback)

Listen any object or array calling the callback function asynchronously grouping all the contiguous changes via setImmediate

@returns self

icaro.unlisten(callback|null)

Unsubscribing a callback previously subscribed to the object, if no callback is provided all the previous subscriptions will be cleared

@returns self

icaro.toJSON()

Return all data contained in an icaro Proxy as JSON object

@returns Object

Support

icaro uses advanced es6 fetatures like Proxies, WeakMaps, Maps and Symbols and it targets only modern browsers

All major evergreen browsers (Edge, Chrome, Safari, Firefox) should be supported

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.