Neolithic Psychedelic Mushrooms

    ember-weak-ref

    1.0.0 • Public • Published

    Ember-weak-ref

    npm Version Build Status

    THIS PROJECT IS CURRENTLY EXPERIMENTAL. PLEASE SUBMIT FEEDBACK.


    WeakRef implementation for ember objects. Allowing a mixture of long-running and short running tasks to exist, without strongly retaining all parties.

    Installation

    • ember install ember-weak-ref

    Examples

    note: with real ES6 proxies, the api becomes nicer

    basic example

    import WeakMixin from 'ember-weak-ref/mixin';
     
    const Foo = Ember.Object.extend(WeakMixin);
     
    let foo = Foo.create();
     
    const weak = foo.weakRef();
     
    someAsync(function() {
      weak.invoke('toString')
      // => undefined, or the value of foo.toString(), depending on if foo is gone
      or not.
    });

    more advanced example

    It is common, to want to create a weak reference to this, unfortunately this requires a second scope. To make this easier, obj.weak(fn) yields the WeakReference as its argument.

    note: be sure not to close over anything you don't want to retain (other weak refs are ok)

    export default Component.extend({
      actions: {
        save() {
          this.set('saving', true);
     
          this.weak(async (component) => {
            try {
              let model = await component.invoke('saveModel');
            } finally {
              // ignore these operations, if
              // * the this.weak('save') is invoked again, resulting in a new operation id
              // * the component is destroyed
              // * if the compnoent has been released
              component.set('saving',  false);
            }
          });
        }
      }
    })
     

    Keywords

    Install

    npm i ember-weak-ref

    DownloadsWeekly Downloads

    6

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • jasonmit
    • stefanpenner