Skip to content

Instantly share code, notes, and snippets.

@matthewp
Last active July 3, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewp/676eec2c20c395c6fc7e0ce75866e77f to your computer and use it in GitHub Desktop.
Save matthewp/676eec2c20c395c6fc7e0ce75866e77f to your computer and use it in GitHub Desktop.
huntry types

In the past CanJS typed objects such as CanMap, DefineMap, and DefineList would always eagerly convert objects into observables. For example when you do:

let map = new DefineMap({a: {b: 'c'}});

Then map.a is also a DefineMap, and is therefore observable.

This changes with DefineObject, DefineArray and anything else that uses their mixins, like StacheDefineElement. These objects do not convert inner values. So:

let props = {a: {b: 'c'}};
let obj = new DefineObject(props);

obj.a === props.a;

This is good in a lot of ways; it helps with performance and prevents attempting to convert objects that you want left alone.

However there are still cases where you would want this sort of recursive observable conversion. So we've created a new type that can be used like so:

import { DefineObject, type, DeepObservableObjectOrArray } from "can";

class Favorites extends DefineObject {
  static define = {
    options: type.convert(DeepObservableObjectOrArray)
  }
}

new DefineObject({ options: {a: {b: 'c'}} });

What to name it

We need help because we're unsure of what to call this. Some options for consideration with brief explanations:

  • Eager: It eagerly converts objects to observables.
  • Greedy: Like a greedy regex.
  • RecursiveObservable: It recursively converts to observables.
  • ToDefineObjectOrArray: This is more direct in what it does, which is convert to DefineObjects, DefineArrays, or leaves alone if primitive types.
  • BuiltInOrObservable: Also direct but less wordy.
  • Hungry: A type that is hungry, it eats objects and digests them into observables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment