2.0.0-alpha.19 • Published 3 years ago

@joist/control v2.0.0-alpha.19

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Observable

Observable in Joist means something slightly different then in something like RxJs. Decorating a class with @observable means that instances of that class will BE observable. This means you can watch for changes on select properties.s

Installation:

npm i @joist/observable@alpha

Example:

import { observable, observer, OnChange, Changes } from '@joist/observable';

@observable
class State implements OnChange {
  // Changes to these will trigger callback
  @observe todos: string[] = [];
  @observe userName?: string;

  // changes to this will not
  someValue: boolean = false;

  onChange(changes: Changes) {
    console.log(changes);
    // { todos: { value: ['Build Shit'], previousValue: [] }, userName: { value: 'Danny Blue', previousValue: undefined } }
  }
}

const state = new State();

state.todos = [...state.todos, 'Build Shit'];
state.userName = 'Danny Blue'

Event target example:

If you want to externally monitor your class for changes you can extend event target and dispatch events. (available in both node and the browser)

import { observable, observer, OnChange, Changes } from '@joist/observable';

class StateChangeEvent extends Event {
  consetructor(public changes: Changes) {
    super('statechange')
  }
}

@observable
class State extends EventTarget implements OnChange {
  // Changes to these will trigger callback
  @observe todos: string[] = [];
  @observe userName?: string;

  // changes to this will not
  someValue: boolean = false;

  onChange(changes: Changes) {
    this.dispatchEvent(new StateChangeEvent());
  }
}

const state = new State();

state.addEventListener('statechange', (e) => {
  console.log(e.changes);
});

state.todos = [...state.todos, 'Build Shit'];
state.userName = 'Danny Blue'
2.0.0-alpha.20

3 years ago

2.0.0-alpha.11

3 years ago

2.0.0-alpha.19

3 years ago

2.0.0-alpha.18

3 years ago

2.0.0-alpha.17

3 years ago

2.0.0-alpha.16

3 years ago

2.0.0-alpha.15

3 years ago

2.0.0-alpha.14

3 years ago

2.0.0-alpha.13

3 years ago

2.0.0-alpha.12

3 years ago

2.0.0-y.0

3 years ago

2.0.0-beta.4

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

2.0.0-beta.3

3 years ago

2.0.0-beta.0

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.4

4 years ago