Last active
November 8, 2017 23:14
-
-
Save jsturgis/8113ebc10c2940b050fc7db003aae62c to your computer and use it in GitHub Desktop.
New Twiddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
//import environment from 'ember-stdlib/utils/environment'; | |
const environment = {isBrowser() { return true; }}; | |
const BOTH = 'both'; | |
const INITIAL = 'initial'; | |
const SUBSEQUENT = 'subsequent'; | |
const CONTROL = 'control'; | |
const LIX_DELIMITER = '-'; | |
const DELAY_DELIMITER = '_'; | |
const DELAY_LIX_KEY = 'site_speed_v_engagement_trt'; | |
const DELAY_COUNTRY_LIX_KEY = 'site_speed_v_engagement_rep'; | |
function _delayPromise(delay, runTask) { | |
return function inner(data) { | |
return new Ember.RSVP.Promise((resolve) => { | |
// This delay is not being invoked for any Ember object, so this.runTask doesn't | |
// make sense here (Ember.run.later used instead). Naively passing through the | |
// calling route's this.runTask will not work here. | |
// eslint-disable-next-line ember-best-practices/require-ember-lifeline | |
console.log('schedule timer'); | |
runTask(() => { | |
console.log('timer called'); | |
resolve(data); | |
}, delay); | |
}); | |
}; | |
} | |
function addDelayToChain(transition, lix, promise, runTask) { | |
const isSubsequent = transition.router.currentHandlerInfos; | |
const delayLix = lix.getTreatment(DELAY_LIX_KEY); | |
// This lix fetch is for tracking only and the return value is not meant to be used | |
lix.getTreatment(DELAY_COUNTRY_LIX_KEY); | |
// don't delay for BPR (running in node) | |
if (!environment.isBrowser()) { | |
return promise; | |
} | |
// don't delay for control group | |
if (delayLix === undefined || delayLix === null || delayLix === CONTROL) { | |
return promise; | |
} | |
// Extract delay type and duration | |
const [delayType, rawDelayDuration] = delayLix.split(LIX_DELIMITER); | |
// don't delay for the wrong load type | |
if ((isSubsequent && delayType === INITIAL) || | |
(!isSubsequent && delayType === SUBSEQUENT)) { | |
return promise; | |
} | |
let delayDuration = 0; | |
// if delay type was both, then there are two delay values in the lix, and we need to use the right one | |
if (delayType === BOTH) { | |
const [initialDelayDuration, subsequentDelayDuration] = rawDelayDuration.split(DELAY_DELIMITER); | |
delayDuration = isSubsequent ? subsequentDelayDuration : initialDelayDuration; | |
} else if (delayType === INITIAL || delayType === SUBSEQUENT) { | |
delayDuration = rawDelayDuration; | |
} else { | |
// invalid delay type, abort delay attempt | |
return promise; | |
} | |
// Don't delay if the delay duration is not valid | |
if (isNaN(delayDuration) || Number(delayDuration) < 1) { | |
return promise; | |
} | |
const delayedPromise = promise.then(_delayPromise(delayDuration, runTask)); | |
return delayedPromise; | |
} | |
export default { | |
addDelayToChain, | |
_delayPromise, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import delay from '../delay'; | |
export default Ember.Route.extend({ | |
runTask() { | |
console.log('runTask called'); | |
}, | |
model(_, transition) { | |
this._super(...arguments); | |
return delay.addDelayToChain(transition, {getTreatment(){ return 'both-400'}}, Ember.RSVP.resolve(), Ember.run.bind(this, 'runTask')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { test } from 'qunit'; | |
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; | |
moduleForAcceptance('delay test'); | |
//test('visiting /', function(assert) { | |
// visit('/'); | |
// andThen(function() { | |
// assert.equal(currentURL(), '/'); | |
// }); | |
//}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default function destroyApp(application) { | |
Ember.run(application, 'destroy'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { module } from 'qunit'; | |
import Ember from 'ember'; | |
import startApp from '../helpers/start-app'; | |
import destroyApp from '../helpers/destroy-app'; | |
const { RSVP: { Promise } } = Ember; | |
export default function(name, options = {}) { | |
module(name, { | |
beforeEach() { | |
this.application = startApp(); | |
if (options.beforeEach) { | |
return options.beforeEach.apply(this, arguments); | |
} | |
}, | |
afterEach() { | |
let afterEach = options.afterEach && options.afterEach.apply(this, arguments); | |
return Promise.resolve(afterEach).then(() => destroyApp(this.application)); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Resolver from '../../resolver'; | |
import config from '../../config/environment'; | |
const resolver = Resolver.create(); | |
resolver.namespace = { | |
modulePrefix: config.modulePrefix, | |
podModulePrefix: config.podModulePrefix | |
}; | |
export default resolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import Application from '../../app'; | |
import config from '../../config/environment'; | |
import QUnit from 'qunit'; | |
const { run } = Ember; | |
const assign = Ember.assign || Ember.merge; | |
QUnit.testDone(({ module, name }) => { | |
console.log('tests done'); | |
if (Ember.run.hasScheduledTimers()) { | |
console.error('YOU ARE LEAKING!'); | |
Ember.run.cancelTimers(); | |
} | |
}); | |
export default function startApp(attrs) { | |
let application; | |
let attributes = assign({rootElement: "#test-root"}, config.APP); | |
attributes = assign(attributes, attrs); // use defaults, but you can override; | |
run(() => { | |
application = Application.create(attributes); | |
application.setupForTesting(); | |
application.injectTestHelpers(); | |
}); | |
return application; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import resolver from './helpers/resolver'; | |
import { | |
setResolver | |
} from 'ember-qunit'; | |
setResolver(resolver); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { moduleFor, test } from 'ember-qunit'; | |
moduleFor('route:application', 'application route delay unit test', { | |
// Specify the other units that are required for this test. | |
// needs: ['controller:foo'] | |
}); | |
test('delay is cleaned up', function(assert) { | |
let route = this.subject(); | |
route.model({}, {resolveIndex: 0, state: {handlerInfos: []}, router: {}}); | |
assert.ok(route); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": true | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment