npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inking

v0.0.2

Published

Light-weight reactive date management library.

Downloads

7

Readme

Install

$ yarn add inking

Motivation

Proxy is an awesome feature feature of ES2015. Base on it, we can do meta-programming and hijack object's native operations easier and more seamless. Inking is a state manage library based on Proxy and inspired by awesome MobX.

Concept

  • Just Like MobX, the object Inking return is not a plain object, but an Observable or Computed object which is hijacked by Proxy. All get and set operations are hijacked, which makes it possible to collect dependencies on trigger reactions.
  • Although some test cases have been added, Inking is still in a prototype phase and needs inking-react and devtools.
  • Feel free to leave any thing in the issue ❤️.

Usage

Making things observable

API:

observable(object)

EXAMPLE:

import { observable, autorun } from 'inking'

const counter = observable({ num: 0 })
const countLogger = observe(() => console.log(counter.num))

counter.num++
// $ 1

API:

@observable
class Model {
    ...
}

EXAMPLE:

import { observable } from 'inking'

@observable
class Model {
  count = 0
}

const m = new Model()
autorun(() => {
  console.log(m.count)
})

m.count++
// $ 1

Any plain object passed into observable will turn to be a observable value.

EXAMPLE:

import { observable } from 'inking'

const person = observable({
  // observable properties:
  name: 'John',
  age: 25,
  showAge: false,

  // computed property:
  get labelText() {
    return `${this.name} (age: ${this.age})`
  },

  setAge(age) {
    this.age = age
  }
})

autorun(() => console.log(person.labelText))

person.name = 'David'
// $: David (age: 25)
person.setAge(26)
// $: David (age: 26)

Any array passed into observable will turn to be a observable array, even nested.

EXAMPLE:

const todos = observable([{ title: 'a', completed: true }, { title: 'b', completed: false }])

autorun(() => {
  console.log(
    todos
      .filter(todo => !todo.completed)
      .map(todo => todo.title)
      .join('_')
  )
})

todos[0].completed = false
// $ a_b
todos[1].completed = true
// $ a
todos.push({ title: 'c', completed: false })
// $ a_c
todos.pop()
// $ a
todos.shift()
// $
  • [ ] maps
  • [ ] boxed values
  • [ ] decorators

Reacting to observables

Computed values are values that can be derived from the existing state or other computed values.

EXAMPLE:

import { observable, computed } from 'inking'

const obj = observable(['eat', 'sleep'])

const c1 = computed(() => {
  return obj.skills.join('_').toLowerCase()
})

autorun(() => {
  console.log(c1.get())
})

obj.skills.push('code')
// $ eat_sleep_code
obj.skills[2] = 'newCode'
// $ eat_sleep_newcode
obj.skills[2] = 'NEWCODE'
// will not print

Any getter property of in Class will turn to be a computed value automatically.

EXAMPLE:

import { observable, computed } from 'inking'

@observable
class Person {
  public firstName = 'a'
  public lastName = 'b'
  public arr: any[] = [1, 2, 3]
  public get fullName() {
    return `${this.firstName}_${this.lastName}`.toUpperCase()
  }
}

const p = new Person()

autorun(() => {
  console.log(p.fullName)
})

p.firstName = 'A'
// will not print
p.firstName = 'a'
// will not print
p.firstName = 'newA'
// $ NEWA_B
p.firstName = 'NEWA'
// will not print
  • [x] @computed

autorun can be used in those cases where you want to create a reactive function that will never have observers itself.

EXAMPLE:

import { autorun } from 'inking'

// ⚠️ disposer is not implemented so far
const disposer = autorun(reaction => {
  /* do some stuff */
})
disposer()

// or

autorun(reaction => {
  /* do some stuff */
  reaction.dispose()
})

when observes & runs the given predicate until it returns true. Once that happens, the given effect is executed and the autorunner is disposed. The function returns a disposer to cancel the autorunner prematurely.

EXAMPLE:

import { observable, when } from 'inking'

const skills = observable(['eat', 'sleep'])

when(
  () => skills.length >= 3,
  () => {
    console.log(skills[skills.length - 1])
  }
)

skills.push('code1')
// $ code1
skills.unshift('code2')
// $ code1
skills.pop()
// $ sleep
skills.shift()
// $ will not print
skills[0] = 'EAT'
// $ will not print

A variation on autorun that gives more fine grained control on which observables will be tracked.

EXAMPLE:

import { observable, reaction } from 'inking'

const skills = observable(['eat', 'sleep'])

reaction(
  () => obj.skills.length,
  () => {
    console.log(obj.skills[obj.skills.length - 1])
  }
)

skills.push('code1')
// $ code1
skills.unshift('code2')
// $ code1
skills.pop()
// $ sleep
skills.shift()
// $ sleep
skills[0] = 'EAT'
// $ will not print
  • [x] @observer

Changing observables

Any application has actions. Actions are anything that modify the state. With MobX you can make it explicit in your code where your actions live by marking them. Actions help you to structure your code better.

EXAMPLE:

import { observable, action } from 'inking'

const skills = observable(['eat', 'sleep'])

autorun(() => {
  console.log(skills.[1])
})

const act = action(() => {
  obj.skills.unshift('i1')
  obj.skills.unshift('i2')
  obj.skills.pop()
  obj.skills.splice(0, 2, 'i3')
  obj.skills.shift()
})

act()
// $ undefined
  • [ ] async actions & flows
  • [ ] Object api

Utility functions

Return raw value from observable value.

EXAMPLE:

// a test case of Jest
test('basic toJS', () => {
  const obj = observable(getPlainObj())
  const skills = obj.skills
  expect(toJS(obj)).toEqual(getPlainObj())
  expect(toJS(skills)).toEqual(getPlainObj().skills)
})
  • [ ] extendObservable
  • [ ] createAtom
  • [ ] intercept & observe

Others

  • [ ] Devtools

Platform support

  • Node: 6+
  • Chrome: 49+
  • Firefox: 38+
  • Safari: 10+
  • Edge: 12+
  • Opera: 36+
  • IE: NEVER SUPPORTED