什么是反应性?

Ember Octane中出现了许多新功能,但是对我来说,没有什么比自动跟踪(自动跟踪)更令人兴奋了自动跟踪是Ember的新反应系统,它使Ember可以知道状态值(例如标记为的属性@tracked何时已更改。这是一个重大的更新,包括在新内核之上完全重写了一些最古老的Ember抽象。


译者:Chris Garrett-在LinkedIn上工作,是Ember js框架的核心贡献者之一。他积极参与了该框架的新版本-E​​mber Octane的开发。尽管他的系列是为Ember开发人员编写的,但它涉及了对所有Web程序员都有用的概念。

乍一看,自动跟踪与反应性模型非常相似Ember Classic(基于计算出的属性,观察者和Ember.set())。如果将它们并排比较,则主要区别在于注释的放置位置。在Octane中,您注释依赖的属性,而在Classic中,您注释getter和setter。


class OctaneGreeter {
  @tracked name = 'Liz';

  get greeting() {
    return `Hi, ${this.name}!`;
  }
}

class ClassicGreeter {
  name = 'Liz';

  @computed('name')
  get greeting() {
    return `Hi, ${this.name}!`;
  }
}

但是,如果深入研究,您会发现自动跟踪更加灵活和强大。例如,您可以自动跟踪功能结果,这在Ember Classic中是不可能的。以一个简单的2D视频游戏的模型为例:


class Tile {
  @tracked type;

  constructor(type) {
    this.type = type;
  }
}

class Character {
  @tracked x = 0;
  @tracked y = 0;
}

class WorldMap {
  @tracked character = new Character();

  tiles = [
    [new Tile('hills'), new Tile('beach'), new Tile('ocean')],
    [new Tile('hills'), new Tile('beach'), new Tile('ocean')]
    [new Tile('beach'), new Tile('ocean'), new Tile('reef')],
  ];

  getType(x, y) {
    return this.tiles[x][y].type;
  }

  get isSwimming() {
    let { x, y } = this.character;

    return this.getType(x, y) === 'ocean';
  }
}

, isSwimming , character ( x / y). , , getType, . CP (. .: Computed Properties — Ember Classics), - . .


, Ember Classic. ! , , Ember , Octane.


-, , . , . , . , , , , .


7 . , « », , :


  1. ? ←
  2. ?
  3. — TrackedMap
  4. — @localCopy
  5. — RemoteData
  6. — effect()

, , Octane. -, , , ! Discord.


: «»?



, « », , . . , .


: .

«» , « » « ». , « Ember» « Vue», , . , .


, ! .

( (streams))


, , , , : «» (declarative) «» (state). « » , «» «» ? "" (state)? , , .


(state)


«» «». , , ; , "". JavaScript :


  • , let const
  • , Map Set

, (root state), , . , , , . :


let a = 1;
let b = 2;

function aPlusB() {
  return a + b;
}

aPlusB() , a b. , . , , a b, aPlusB(). , a b , aPlusB() , .



, «», «» «», , , . , :


, , , .
, - .

«» , (). , — « »? , , , , , , , ( , ).


aPlusB. , , :


  1. ( ).
  2. a + b.

let a = 1;
let b = 2;

let aPlusB = a + b;

, , . , a b. aPlusB , :


let a = 1;
let b = 2;

let aPlusB = a + b;

//  `a`
a = 4;
aPlusB = a + b;

//  `b`
b = 7;
aPlusB = a + b;

, . , a, aPlusB. API , , :


export default class Counter extends Component {
 count = 0;

  @action
    incrementCount() {
    this.count++;
    this.rerender();
  }
}

, rerender() , , . , , - , , .


, «» . aPlusB().


let a = 1;
let b = 2;

function aPlusB() {
  return a + b;
}

, , , . , «» , . , aPlusB() , . , , , .


, , Ember, Vue Svelte, (, ) JSX , , JSX . HTML — , , , . HTML, , . «» , , , DOM, , DOM , .


FP (Functional Programming)?


. «» «» «». , . , HTML, , - , .


( FP), . «» — .


let a = 1;
let b = 2;

function plus(first, second) {
  return first + second;
}

//  a + b;
plus(a, b);

. , , .



, ( ). , FP , , , . , , , .


, . , , , .


, , . , «» FP, - (, , . « », !). , .



, :


  1. «» « ». , , , .
  2. «» , . ( , ):
    • , , , .
    • — , , , .
  3. « » , , , . HTML , — , . , , .

. , , , - , - . , , «» .


: , . , , )


All Articles