Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fluids

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fluids

Glue layer for reactivity

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

fluids

This library is a tiny glue layer for observable events.

  • Create a tree of observable values
  • Let child nodes send arbitrary events to parents (for maximum flexibility)
  • Be as tiny as possible

API

  • hasFluidValue(any): Returns true if the given value is a fluid object
  • getFluidValue(any): Returns the current value of the fluid object (if possible), otherwise the argument is passed thru
  • getFluidConfig(any): Returns the FluidConfig object that allows for observing the argument
  • setFluidConfig(object, FluidConfig): Defines the hidden property that holds the FluidConfig object. This can only be called once per fluid object.
  • addFluidObserver(object, FluidObserver): Attach an observer to a fluid object, and get an unsubscribe function back. Returns undefined if the first argument is not a fluid object.

FluidConfig example

This example adds observability to a ref object (like in React: { value }).

Any object can conform to the FluidConfig interface without needing to change its public API.

import { setFluidConfig, FluidObserver, FluidEvent } from 'fluids'

/** Create a `{ value }` object that can be observed */
function createRef(value) {
  const ref = {}

  // Observer tracking
  const children = new Set<FluidObserver>()
  const emit = (event: FluidEvent) =>
    children.forEach(child => child.onParentChange(event))

  // Change tracking
  const get = () => value
  Object.defineProperty(ref, 'value', {
    enumerable: true,
    get,
    set: newValue => {
      if (value !== newValue) {
        value = newValue
        emit({
          type: 'change',
          parent: ref,
          value,
        })
      }
    }
  })

  // Observer API
  setFluidConfig(ref, {
    get,
    addChild: child => children.add(child),
    removeChild: child => children.delete(child),
  })

  return ref
}

FluidObserver example

This example shows how to observe a fluid object.

import { addFluidObserver } from 'fluids'

const ref = createRef(0)
const stop = addFluidObserver(ref, {
  onParentChange(event) {
    if (event.type === 'change') {
      console.log(event.value, event.parent)
    }
  }
})

ref.value++
stop()
ref.value++

Keywords

FAQs

Package last updated on 08 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc