@beenotung/s-object
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

SObject

Convenient object signal for S.js.

npm Package Version

Example

import S, { DataSignal } from 's-js';
import SObject, { lift } from "@beenotung/s-object";

// sample data
var product = {
  title: 'Alice',
  price: 20,
}
var titleInput = document.querySelector('input[name=title]') as HTMLInputElement
var priceInput = document.querySelector('input[name=price]') as HTMLInputElement

// detect changes without SObject
var signals: Record<
  keyof typeof product,
  DataSignal<(typeof product)[keyof typeof product]>
> = Object.entries(product)
  .map(([key,value]) =>
    S.value((product as any)[key])) as any
S(() => titleInput.value = signals.title())
S(() => priceInput.value = signals.price())

// detect changes with SObject
var signal = SObject(product)
// using typed `get(key)`
S(() => titleInput.value = signal.get('title'))
// or using typed proxy
S(() => priceInput.value = signal.proxy.price)

// mutations without SObject
signals.title('Bob')

// mutations with SObject
// using typed `set(key, value)
signal.set("title", 'Bob')
// using typed proxy
signal.proxy.title = 'Bob'

// Any object-carrying signal can be 'lifted' to create an SObject
var sValue = S.data(product),
    sObject = lift(sValue);
S(() => titleInput.value = sObject.proxy.title); // etc

// When a field is deleted, a "undefined" signal will be emitted
signal.proxy.price = 20
S(() => console.log('price:', signal.proxy.price))
delete signal.proxy.price
signal.proxy.price = 21
// printed: 20, undefined, 21

For a full list of methods and thier signatures, consult index.ts.

Installation

Install from npm

npm install @beenotung/s-object

Install from git

mkdir -p lib
cd lib
git submodule add https://github.com/beenotung/s-object.git

License

BSD 2-Clause License

Readme

Keywords

Package Sidebar

Install

npm i @beenotung/s-object

Weekly Downloads

5

Version

1.0.1

License

BSD-2-Clause

Unpacked Size

11 kB

Total Files

6

Last publish

Collaborators

  • beenotung