canjs / can-observable-mixin

Define properties on JavaScript classes
https://canjs.com
MIT License
2 stars 1 forks source link

Length event not triggered when calling update with empty array #142

Closed m-mujica closed 5 years ago

m-mujica commented 5 years ago
import { ObservableArray, ObservableObject } from "//unpkg.com/can@6/core.mjs";

class MyObs extends ObservableObject {
  static props = {
    availableKeys: {
      get default() {
        return new ObservableArray();
      }
    },

    selectedKey: {
      type: String,

      value({ resolve, listenTo, lastSet }) {
        // all key to be set
        listenTo(lastSet, setVal => {
          resolve(setVal);
        });

        listenTo(this.availableKeys, "length", (ev, newLength) => {
          if (newLength) {
            resolve(this.availableKeys[0]);
          } else {
            resolve(undefined);
          }
        });
      }
    }
  };
}

const obj = new MyObs();
obj.on("selectedKey", () => {});

obj.availableKeys.update(["one", "two", "three"]);
console.log("First: ", obj.selectedKey);  // one

obj.availableKeys.push([]);
console.log("Last: ", obj.selectedKey);  // should be undefined but prints "one"
m-mujica commented 5 years ago

maybe related:

import { ObservableArray, ObservableObject } from "//unpkg.com/can@6/core.mjs";

class MyObs extends ObservableObject {
  static props = {
    breakpoints: {
      get default() {
        return new ObservableArray();
      }
    },

    newBreakpointKey: {
      default: "",
      value({ listenTo, resolve, lastSet }) {
        resolve(lastSet.get());

        listenTo(lastSet, resolve);

        listenTo("breakpoints", () => {
          resolve("");
        });
      }
    }
  };

const obj = new MyObs();
obj.on("newBreakpointKey", () => {});

obj.breakpoints = [ "todos.length" ];
assert.equal(vm.newBreakpointKey, ""); // FAILS
m-mujica commented 5 years ago

@phillipskevin any pointers regarding where to start looking? Where should the event be triggered?

m-mujica commented 5 years ago

seems this might be a good start https://github.com/canjs/can-observable-array/blob/cee10e6856606a5b9121b625162e1e3f2c00d423/src/proxy-array.js#L197

m-mujica commented 5 years ago

I found this issue working on StacheElement components... I had to call .initialize otherwise the events are not fired for performance reasons. (Note: .render also works)