New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run a flush cycle before calling model promise callbacks. #50

Merged
merged 3 commits into from Jan 10, 2018

Conversation

Projects
None yet
4 participants
@burrows
Copy link
Member

burrows commented Jan 8, 2018

This ensures that any cached props that are invalidated by a mapper response
will be updated before the promise callback is called.

const Transis = require('../dist');

const PersonMapper = {
  get: function(id) {
    return new Promise(function(resolve, reject) {
      setTimeout(function() {
        resolve({id: id, firstName: 'Homer', lastName: 'Simpson'});
      }, 5);
    });
  }
};

const Person = Transis.Model.extend('Person', function() {
  this.mapper = PersonMapper;

  this.attr('firstName', 'string');
  this.attr('lastName', 'string');

  this.prop('fullName', {
    cache: true,
    on: ['firstName', 'lastName'],
    get: function(firstName, lastName) {
      return firstName + ' ' + lastName;
    }
  });
});

let p = Person.get(1);
console.log('get:', p.fullName);

p.then(function() {
  console.log('then callback:', p.fullName);
});

Previously this logged:

get: undefined undefined
then callback: undefined undefined

But now it logs:

get: undefined undefined
then callback: Homer Simpson
Run a flush cycle before calling model promise callbacks. This ensures
that any cached props that have be invalidated by a mapper response
have been updated before the callback is called.
@@ -2,7 +2,7 @@ import "es6-shim";
import * as util from "../util";
import TransisObject from "../object";

function delay(f) { setTimeout(f, 5); }
function delay(f) { setTimeout(f, 10); }

This comment has been minimized.

@burrows

burrows Jan 8, 2018

Member

This was needed to ensure that specs pass reliably on phantomjs.

@burrows burrows requested a review from stallings Jan 8, 2018

@burrows burrows self-assigned this Jan 8, 2018

Commit compiled files to repo so that the module can be installed from a
github url. We're currently limited to installing from NPM or a locaal
file, which complicates testing in a CI environment.
@burrows

This comment has been minimized.

Copy link
Member

burrows commented Jan 10, 2018

Note to reviewers: I committed the compiled files in the dist/ directory on this branch in order to allow for installing the module from a github url. Without those files committed you get a broken install otherwise. This allows us to test Transis changes in CI without having to release a new npm module.

So you can skip reviewing any files in dist/.

@alexraginskiy

This comment has been minimized.

Copy link
Contributor

alexraginskiy commented Jan 10, 2018

@burrows is there a reason there are so many files in the dist? I believe the package consumes index.js, not the individual source files.

@burrows

This comment has been minimized.

Copy link
Member

burrows commented Jan 10, 2018

@alexraginskiy The index.js requires the other files. Are you thinking of the webpack package? I just added that as well, but we don't actually use that file.

@burrows burrows merged commit d6d5466 into master Jan 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment