Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Offline update different than online update. #60

Open
tja4472 opened this issue Aug 16, 2017 · 3 comments
Open

Offline update different than online update. #60

tja4472 opened this issue Aug 16, 2017 · 3 comments

Comments

@tja4472
Copy link

tja4472 commented Aug 16, 2017

Hello again

Online update allows for the update of individual properties.

this.fb_CurrentTodos.update(itemsToSave[x].$key, { index: x });

Offline: Just the updated property is returned.

todo.data.service.ts:24 >>>>>>>>>>AngularFireOfflineDatabase.list> (2) [{…}, {…}]
todo.data.service.ts:87 fromFirebaseTodo> {index: 0, $key: "-KXpuvXAo3jYqlp5s8OH", $exists: ƒ}
todo.data.service.ts:87 fromFirebaseTodo> {index: 1, $key: "-KrVLgUO1C-nNeE4fK98", $exists: ƒ}

Proposed fix

emulate-list.ts - processEmulation

 } else if (method === 'update') {
      let found = false;
      let itemToUpdateIndex = this.observableValue.findIndex(item => item.$key == key);

      if (itemToUpdateIndex > -1) {
        found = true;

        const cc_newValue = Object.assign(this.observableValue[itemToUpdateIndex], value );
        this.observableValue[itemToUpdateIndex] = cc_newValue;
        console.log('cc_newValue>', cc_newValue);
/*
        const aa_newValue = { ...this.observableValue[itemToUpdateIndex], ...value };
        console.log('aa_newValue>', aa_newValue);

        const bb_newValue = unwrap(key, aa_newValue, () => aa_newValue !== null);
        console.log('bb_newValue>', bb_newValue);

        this.observableValue[itemToUpdateIndex] = bb_newValue;
*/        
      }

      /*
            this.observableValue.forEach((item, index) => {
              if (item.$key === key) {
                found = true;
                this.observableValue[index] = newValue;
              }
            });
      */
      if (!found) {
        this.observableValue.push(newValue);
      }
    } else { // `remove` is the only remaining option

internal-list-observable.ts

  uniqueNext(newValue) {
    // Sort
    console.log('##uniqueNext');
    console.log('newValue>', newValue);
    console.log('previousValue>', this.previousValue);

    if (this.previousValue) { this.previousValue.sort((a, b) => a.$key - b.$key); }
    if (newValue) { newValue.sort((a, b) => a.$key - b.$key); }

    if (this.updated > 1 || (stringify(this.previousValue) !== stringify(newValue))) {
      // this.previousValue = Object.assign([], newValue);
      let deepClone = Object.assign([], newValue);
      deepClone = deepClone.map(x => {
        let b = Object.assign({}, x);
        unwrap(b.$key, b, () => b !== null);
        return b;
      });
      this.previousValue = deepClone;
      this.next(newValue);
      this.updated++;
    }
  }

hth
Tim

@tja4472
Copy link
Author

tja4472 commented Aug 16, 2017

On reflection it would be best to leave internal-list-observable.ts alone.

emulate-list.ts - processEmulation will then be

        const aa_newValue = { ...this.observableValue[itemToUpdateIndex], ...value };
        console.log('aa_newValue>', aa_newValue);

        const bb_newValue = unwrap(key, aa_newValue, () => aa_newValue !== null);
        console.log('bb_newValue>', bb_newValue);

        this.observableValue[itemToUpdateIndex] = bb_newValue;

@Pl4yeR
Copy link

Pl4yeR commented Sep 5, 2017

I'm facing an issue:
I have a list of elements, where each element is like:
{ property1: "this is one", property2: "this is two" }
When I do
this.db.list('/elements').update(elementId, {property1: "this is NEW one"});
property1 is updated, but property2 is DELETED, just like a SET instead of an UPDATE.

Is this the problem you are describing above?

@tja4472
Copy link
Author

tja4472 commented Sep 5, 2017

Yes, I'm afraid so.

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