Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

[bug] wired observe behavior #215

Open
Brooooooklyn opened this issue Jul 24, 2017 · 2 comments
Open

[bug] wired observe behavior #215

Brooooooklyn opened this issue Jul 24, 2017 · 2 comments

Comments

@Brooooooklyn
Copy link

Brooooooklyn commented Jul 24, 2017

Code to reproduce:

var schemaBuilder = lf.schema.create('todo', 1);

schemaBuilder.createTable('Item').
    addColumn('id', lf.Type.INTEGER).
    addColumn('name', lf.Type.STRING).
    addPrimaryKey(['id']);
    
var db;
var item;
schemaBuilder.connect().then(function(database) {
  db = database;
  item = db.getSchema().table('Item');
  var rows = [
    item.createRow({'id': 1, 'name': 'foo'}),
    item.createRow({'id': 2, 'name': 'bar'}),
  ];
  
  return db.insertOrReplace().into(item).values(rows).exec();
}).then(function() {
  // case one
  // observe a query, observe callback would not be fired
  var selectQuery = db.select().from(item);
  db.observe(selectQuery, function(changes) {
    selectQuery.exec().then(values => {
      console.log('just observe: ', values);
    });
  });
}).then(function() {
  // case two
  // observe a query and exec the query
  // observe callback would be fired
  var selectQuery = db.select().from(item);
  db.observe(selectQuery, function(changes) {
     selectQuery.exec().then(values => {
       console.log('empty results: ', values);
     });
  });
  // would triggle changes
  return selectQuery.exec();
}).then(function() {
  // case three
  // observe a query and exec the query
  // but the results of the query is an empty array
  // observe callback would not be fired
   var selectQuery = db.select().from(item).where(item.id.gt(2));
   db.observe(selectQuery, function(changes) {
     selectQuery.exec().then(values => {
       console.log('empty results: ', values);
     });
   });
   // would not triggle changes
   return selectQuery.exec().then(results => {
     console.log('empty: ', results)
   })
})

see: https://jsfiddle.net/6t3fofwm/11/

I'm confused which case is working as expected.

@freshp86
Copy link
Contributor

case 1 and case 2 seeems like a duplicate of #209?

@freshp86 freshp86 marked this as a duplicate of #209 Jul 24, 2017
@Brooooooklyn
Copy link
Author

in #209 , observe callback is fired after update or delete query.
my problem is, in the case2 :

db.observe(query, callback)
query.exec()

observe callback is fired by query.exec(), is this a expected behavior?
the difference between #209 and this case is, nothing updated after the query was observed, but the callback is fired.

chuan6 pushed a commit to ReactiveDB/core that referenced this issue Jul 26, 2017
...results from observed query is not empty.

resolve #10
related google/lovefield#215

refactor(Selector): move change$ to getter setter

this._change$ 是用来保存外部 set 的 change$, 因为 getter setter 只能通
过一个其它变量来保存值。比如:

get xxx () { return 1 }
set xxx (val) { this.xxx = val  } // stackoverflow

所以在 getter 里面计算出来的值不需要赋值给 this._change$, 直接 return
就好了。

refactor(Selector): 尝试减少 change$ 生成中的重复代码

...并将针对 Lovefield issue 做“修复”的代码提出到 lfIssueFix 函数中。
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants