├── app └── .gitkeep ├── addon ├── .gitkeep ├── once-observer.js ├── in-dom-observer.js └── lib │ └── once-helper.js ├── vendor └── .gitkeep ├── tests ├── unit │ ├── .gitkeep │ ├── once-observer-test.js │ └── in-dom-observer-test.js ├── integration │ └── .gitkeep ├── dummy │ ├── app │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── models │ │ │ └── .gitkeep │ │ ├── routes │ │ │ └── .gitkeep │ │ ├── styles │ │ │ └── app.css │ │ ├── components │ │ │ └── .gitkeep │ │ ├── controllers │ │ │ └── .gitkeep │ │ ├── templates │ │ │ └── components │ │ │ │ └── .gitkeep │ │ ├── resolver.js │ │ ├── router.js │ │ ├── app.js │ │ └── index.html │ ├── public │ │ ├── robots.txt │ │ └── crossdomain.xml │ └── config │ │ └── environment.js ├── test-helper.js ├── helpers │ ├── destroy-app.js │ ├── resolver.js │ ├── start-app.js │ └── module-for-acceptance.js ├── .jshintrc └── index.html ├── .watchmanconfig ├── .bowerrc ├── index.js ├── config ├── environment.js └── ember-try.js ├── bower.json ├── .npmignore ├── testem.js ├── .ember-cli ├── .gitignore ├── ember-cli-build.js ├── .jshintrc ├── .editorconfig ├── .travis.yml ├── LICENSE.md ├── package.json └── README.md /app/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /addon/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/unit/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/integration/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower_components", 3 | "analytics": false 4 | } 5 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 'use strict'; 3 | 4 | module.exports = { 5 | name: 'ember-runloop-utils' 6 | }; 7 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | 'use strict'; 3 | 4 | module.exports = function(/* environment, appConfig */) { 5 | return { }; 6 | }; 7 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import resolver from './helpers/resolver'; 2 | import { 3 | setResolver 4 | } from 'ember-qunit'; 5 | 6 | setResolver(resolver); 7 | -------------------------------------------------------------------------------- /tests/helpers/destroy-app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | 3 | export default function destroyApp(application) { 4 | Ember.run(application, 'destroy'); 5 | } 6 | -------------------------------------------------------------------------------- /addon/once-observer.js: -------------------------------------------------------------------------------- 1 | import onceHelper from './lib/once-helper'; 2 | 3 | export default onceHelper('onceObserver', function(target) { 4 | return target.isDestroying || target.isDestroyed; 5 | }); 6 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-runloop-utils", 3 | "dependencies": { 4 | "ember": "~2.6.0-beta.2", 5 | "ember-cli-shims": "0.1.1", 6 | "ember-cli-test-loader": "0.2.2", 7 | "ember-qunit-notifications": "0.1.0" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import config from './config/environment'; 3 | 4 | const Router = Ember.Router.extend({ 5 | location: config.locationType 6 | }); 7 | 8 | Router.map(function() { 9 | }); 10 | 11 | export default Router; 12 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components 2 | /config/ember-try.js 3 | /dist 4 | /tests 5 | /tmp 6 | **/.gitkeep 7 | .bowerrc 8 | .editorconfig 9 | .ember-cli 10 | .gitignore 11 | .jshintrc 12 | .watchmanconfig 13 | .travis.yml 14 | bower.json 15 | ember-cli-build.js 16 | testem.js 17 | -------------------------------------------------------------------------------- /addon/in-dom-observer.js: -------------------------------------------------------------------------------- 1 | import onceHelper from './lib/once-helper'; 2 | 3 | export default onceHelper('inDOMObserver', function(target) { 4 | return target.isDestroyed || 5 | target.isDestroying || 6 | ((target._state || target.state)) !== 'inDOM'; 7 | }) 8 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | module.exports = { 3 | "framework": "qunit", 4 | "test_page": "tests/index.html?hidepassed", 5 | "disable_watching": true, 6 | "launch_in_ci": [ 7 | "PhantomJS" 8 | ], 9 | "launch_in_dev": [ 10 | "PhantomJS", 11 | "Chrome" 12 | ] 13 | }; 14 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /tests/helpers/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from '../../resolver'; 2 | import config from '../../config/environment'; 3 | 4 | const resolver = Resolver.create(); 5 | 6 | resolver.namespace = { 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix 9 | }; 10 | 11 | export default resolver; 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log 17 | testem.log 18 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | let App; 7 | 8 | Ember.MODEL_FACTORY_INJECTIONS = true; 9 | 10 | App = Ember.Application.extend({ 11 | modulePrefix: config.modulePrefix, 12 | podModulePrefix: config.podModulePrefix, 13 | Resolver 14 | }); 15 | 16 | loadInitializers(App, config.modulePrefix); 17 | 18 | export default App; 19 | -------------------------------------------------------------------------------- /tests/helpers/start-app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import Application from '../../app'; 3 | import config from '../../config/environment'; 4 | 5 | export default function startApp(attrs) { 6 | let application; 7 | 8 | let attributes = Ember.merge({}, config.APP); 9 | attributes = Ember.merge(attributes, attrs); // use defaults, but you can override; 10 | 11 | Ember.run(() => { 12 | application = Application.create(attributes); 13 | application.setupForTesting(); 14 | application.injectTestHelpers(); 15 | }); 16 | 17 | return application; 18 | } 19 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | /* global require, module */ 3 | var EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 4 | 5 | module.exports = function(defaults) { 6 | var app = new EmberAddon(defaults, { 7 | // Add options here 8 | }); 9 | 10 | /* 11 | This build file specifies the options for the dummy test app of this 12 | addon, located in `/tests/dummy` 13 | This build file does *not* influence how the addon or the app using it 14 | behave. You most likely want to be modifying `./index.js` or app's build file 15 | */ 16 | 17 | return app.toTree(); 18 | }; 19 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "-Promise" 6 | ], 7 | "browser": true, 8 | "boss": true, 9 | "curly": true, 10 | "debug": false, 11 | "devel": true, 12 | "eqeqeq": true, 13 | "evil": true, 14 | "forin": false, 15 | "immed": false, 16 | "laxbreak": false, 17 | "newcap": true, 18 | "noarg": true, 19 | "noempty": false, 20 | "nonew": false, 21 | "nomen": false, 22 | "onevar": false, 23 | "plusplus": false, 24 | "regexp": false, 25 | "undef": true, 26 | "sub": true, 27 | "strict": false, 28 | "white": false, 29 | "eqnull": true, 30 | "esnext": true, 31 | "unused": true 32 | } 33 | -------------------------------------------------------------------------------- /tests/dummy/public/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.js] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [*.hbs] 21 | insert_final_newline = false 22 | indent_style = space 23 | indent_size = 2 24 | 25 | [*.css] 26 | indent_style = space 27 | indent_size = 2 28 | 29 | [*.html] 30 | indent_style = space 31 | indent_size = 2 32 | 33 | [*.{diff,md}] 34 | trim_trailing_whitespace = false 35 | -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/helpers/module-for-acceptance.js: -------------------------------------------------------------------------------- 1 | import { module } from 'qunit'; 2 | import Ember from 'ember'; 3 | import startApp from '../helpers/start-app'; 4 | import destroyApp from '../helpers/destroy-app'; 5 | 6 | const { RSVP: { Promise } } = Ember; 7 | 8 | export default function(name, options = {}) { 9 | module(name, { 10 | beforeEach() { 11 | this.application = startApp(); 12 | 13 | if (options.beforeEach) { 14 | return options.beforeEach.apply(this, arguments); 15 | } 16 | }, 17 | 18 | afterEach() { 19 | let afterEach = options.afterEach && options.afterEach.apply(this, arguments); 20 | return Promise.resolve(afterEach).then(() => destroyApp(this.application)); 21 | } 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | - "4" 5 | 6 | sudo: false 7 | 8 | cache: 9 | directories: 10 | - node_modules 11 | 12 | env: 13 | - EMBER_TRY_SCENARIO=default 14 | - EMBER_TRY_SCENARIO=ember-1.13 15 | - EMBER_TRY_SCENARIO=ember-release 16 | - EMBER_TRY_SCENARIO=ember-beta 17 | - EMBER_TRY_SCENARIO=ember-canary 18 | 19 | matrix: 20 | fast_finish: true 21 | allow_failures: 22 | - env: EMBER_TRY_SCENARIO=ember-canary 23 | 24 | before_install: 25 | - npm config set spin false 26 | - npm install -g bower 27 | - npm install phantomjs-prebuilt 28 | 29 | install: 30 | - npm install 31 | - bower install 32 | 33 | script: 34 | # Usually, it's ok to finish the test scenario without reverting 35 | # to the addon's original dependency state, skipping "cleanup". 36 | - ember try $EMBER_TRY_SCENARIO test --skip-cleanup 37 | -------------------------------------------------------------------------------- /tests/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "location", 6 | "setTimeout", 7 | "$", 8 | "-Promise", 9 | "define", 10 | "console", 11 | "visit", 12 | "exists", 13 | "fillIn", 14 | "click", 15 | "keyEvent", 16 | "triggerEvent", 17 | "find", 18 | "findWithAssert", 19 | "wait", 20 | "DS", 21 | "andThen", 22 | "currentURL", 23 | "currentPath", 24 | "currentRouteName" 25 | ], 26 | "node": false, 27 | "browser": false, 28 | "boss": true, 29 | "curly": true, 30 | "debug": false, 31 | "devel": false, 32 | "eqeqeq": true, 33 | "evil": true, 34 | "forin": false, 35 | "immed": false, 36 | "laxbreak": false, 37 | "newcap": true, 38 | "noarg": true, 39 | "noempty": false, 40 | "nonew": false, 41 | "nomen": false, 42 | "onevar": false, 43 | "plusplus": false, 44 | "regexp": false, 45 | "undef": true, 46 | "sub": true, 47 | "strict": false, 48 | "white": false, 49 | "eqnull": true, 50 | "esnext": true, 51 | "unused": true 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | {{content-for "body-footer"}} 32 | {{content-for "test-body-footer"}} 33 | 34 | 35 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | module.exports = { 3 | scenarios: [ 4 | { 5 | name: 'default', 6 | bower: { 7 | dependencies: { } 8 | } 9 | }, 10 | { 11 | name: 'ember-1.13', 12 | bower: { 13 | dependencies: { 14 | 'ember': '~1.13.0' 15 | }, 16 | resolutions: { 17 | 'ember': '~1.13.0' 18 | } 19 | } 20 | }, 21 | { 22 | name: 'ember-release', 23 | bower: { 24 | dependencies: { 25 | 'ember': 'components/ember#release' 26 | }, 27 | resolutions: { 28 | 'ember': 'release' 29 | } 30 | } 31 | }, 32 | { 33 | name: 'ember-beta', 34 | bower: { 35 | dependencies: { 36 | 'ember': 'components/ember#beta' 37 | }, 38 | resolutions: { 39 | 'ember': 'beta' 40 | } 41 | } 42 | }, 43 | { 44 | name: 'ember-canary', 45 | bower: { 46 | dependencies: { 47 | 'ember': 'components/ember#canary' 48 | }, 49 | resolutions: { 50 | 'ember': 'canary' 51 | } 52 | } 53 | } 54 | ] 55 | }; 56 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 3 | module.exports = function(environment) { 4 | var ENV = { 5 | modulePrefix: 'dummy', 6 | environment: environment, 7 | baseURL: '/', 8 | locationType: 'auto', 9 | EmberENV: { 10 | FEATURES: { 11 | // Here you can enable experimental features on an ember canary build 12 | // e.g. 'with-controller': true 13 | } 14 | }, 15 | 16 | APP: { 17 | // Here you can pass flags/options to your application instance 18 | // when it is created 19 | } 20 | }; 21 | 22 | if (environment === 'development') { 23 | // ENV.APP.LOG_RESOLVER = true; 24 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 25 | // ENV.APP.LOG_TRANSITIONS = true; 26 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 27 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 28 | } 29 | 30 | if (environment === 'test') { 31 | // Testem prefers this... 32 | ENV.baseURL = '/'; 33 | ENV.locationType = 'none'; 34 | 35 | // keep test console output quieter 36 | ENV.APP.LOG_ACTIVE_GENERATION = false; 37 | ENV.APP.LOG_VIEW_LOOKUPS = false; 38 | 39 | ENV.APP.rootElement = '#ember-testing'; 40 | } 41 | 42 | if (environment === 'production') { 43 | 44 | } 45 | 46 | return ENV; 47 | }; 48 | -------------------------------------------------------------------------------- /addon/lib/once-helper.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import WeakMap from 'ember-weakmap'; 3 | 4 | const { run, observer } = Ember; 5 | const oncePending = new WeakMap(); 6 | 7 | export default function(name, shortCircuitFn) { 8 | return function(...args) { 9 | let fn = args.pop(); 10 | 11 | if (typeof fn !== 'function') { 12 | throw new TypeError(`last argument to ${name} must be a function`); 13 | } 14 | 15 | return observer(...args, function onceFn() { 16 | // grab the map of oncePending functions for this instance 17 | let pendingMap = oncePending.get(this); 18 | 19 | if (pendingMap && pendingMap.get(onceFn)) { 20 | // we found a map, and the current function is already schedule so we 21 | // have no work todo, skipping... 22 | return; 23 | } else if (!pendingMap) { 24 | // no map found, create a new one 25 | pendingMap = new Ember.Map(); 26 | oncePending.set(this, pendingMap); 27 | } else { 28 | // map found, but not for the giving function 29 | } 30 | 31 | // set the current function as pending 32 | pendingMap.set(onceFn, true); 33 | 34 | run.schedule('actions', () => { 35 | // purge the current function from pending 36 | oncePending.get(this).delete(onceFn); 37 | 38 | if (shortCircuitFn(this)) { return; } 39 | 40 | fn.call(this); 41 | }); 42 | }); 43 | }; 44 | } 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-runloop-utils", 3 | "version": "0.0.4", 4 | "description": "The default blueprint for ember-cli addons.", 5 | "directories": { 6 | "doc": "doc", 7 | "test": "tests" 8 | }, 9 | "scripts": { 10 | "build": "ember build", 11 | "start": "ember server", 12 | "test": "ember try:each" 13 | }, 14 | "repository": "https://github.com/stefanpenner/ember-runloop-utils", 15 | "engines": { 16 | "node": ">= 0.10.0" 17 | }, 18 | "author": "", 19 | "license": "MIT", 20 | "devDependencies": { 21 | "broccoli-asset-rev": "^2.4.2", 22 | "ember-ajax": "^2.0.1", 23 | "ember-cli": "2.6.0-beta.2", 24 | "ember-cli-app-version": "^1.0.0", 25 | "ember-cli-dependency-checker": "^1.2.0", 26 | "ember-cli-htmlbars": "^1.0.3", 27 | "ember-cli-htmlbars-inline-precompile": "^0.3.1", 28 | "ember-cli-inject-live-reload": "^1.4.0", 29 | "ember-cli-jshint": "^1.0.0", 30 | "ember-cli-qunit": "^1.4.0", 31 | "ember-cli-release": "0.2.8", 32 | "ember-cli-sri": "^2.1.0", 33 | "ember-cli-uglify": "^1.2.0", 34 | "ember-data": "^2.6.0-beta.1", 35 | "ember-disable-prototype-extensions": "^1.1.0", 36 | "ember-export-application-global": "^1.0.5", 37 | "ember-load-initializers": "^0.5.1", 38 | "ember-resolver": "^2.0.3", 39 | "ember-welcome-page": "^1.0.1", 40 | "loader.js": "^4.0.1" 41 | }, 42 | "keywords": [ 43 | "ember-addon" 44 | ], 45 | "dependencies": { 46 | "ember-cli-babel": "^5.1.6", 47 | "ember-weakmap": "1.0.0" 48 | }, 49 | "ember-addon": { 50 | "configPath": "tests/dummy/config" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ember-runloop-utils [![Build Status](https://travis-ci.org/stefanpenner/ember-runloop-utils.svg?branch=master)](https://travis-ci.org/stefanpenner/ember-runloop-utils) 2 | 3 | Observers are hard to use, when possible we should avoid them see: [this video](https://www.youtube.com/watch?v=vvZEddrClAQ) if you are curious how. 4 | 5 | #### Now sometimes, observers are required (but rarely): 6 | 7 | * pushing change information away from ember (to d3, or jQuery or similar) 8 | * thats more or less it 9 | 10 | Unfortunately, even in this scenarios it can be quite tricky to write them correctly. 11 | 12 | #### Common Requirements (tricky to implement): 13 | 14 | * schedule on the run-loop 15 | * should fire once per run-loop queue flush 16 | * should not fire if the object is `isDestroyed` or `isDestroying` 17 | * sometimes should not fire if the object is no longer `inDOM` 18 | 19 | #### Solution: 20 | 21 | This library provides several helpers which aim to address the above issue. The goal is to have a shared, well tested implemention of `onceObserver` and `inDomObserver` macros. 22 | 23 | 24 | ```js 25 | // For the DOM observer use-case 26 | import inDOMObserver from 'ember-runloop-utils/in-dom-observer' 27 | import Component from 'ember/component' 28 | 29 | export default Component.extend({ 30 | dataDidChange: inDOMObserver('data.[]', function() { 31 | // safely and efficiently invoke your wonderful D3 code 32 | }) 33 | }) 34 | ``` 35 | 36 | ```js 37 | // For crappy reasons non inDOM use-cases 38 | import onceObserver from 'ember-runloop-utils/once-observer' 39 | import Component from 'ember/component' 40 | 41 | export default Service.extend({ 42 | dataDidChange: onceObserver('data.[]', function() { 43 | // for some unknown and most likely poor reason create an observer that only flushes once per run-loop flush 44 | // in well factored code, this should essentially never happen instead: 45 | // * use actions + invoke explicit methods 46 | // * use computed properties 47 | // Why is stef so emo about this? go watch -> https://www.youtube.com/watch?v=vvZEddrClAQ 48 | }) 49 | }); 50 | ``` 51 | 52 | 53 | 54 | 55 | ## Installation 56 | 57 | * `git clone` this repository 58 | * `npm install` 59 | * `bower install` 60 | 61 | ## Running 62 | 63 | * `ember server` 64 | * Visit your app at http://localhost:4200. 65 | 66 | ## Running Tests 67 | 68 | * `npm test` (Runs `ember try:testall` to test your addon against multiple Ember versions) 69 | * `ember test` 70 | * `ember test --server` 71 | 72 | ## Building 73 | 74 | * `ember build` 75 | 76 | For more information on using ember-cli, visit [http://ember-cli.com/](http://ember-cli.com/). 77 | -------------------------------------------------------------------------------- /tests/unit/once-observer-test.js: -------------------------------------------------------------------------------- 1 | import onceObserver from 'ember-runloop-utils/once-observer'; 2 | import { module, test } from 'qunit'; 3 | import Ember from 'ember'; 4 | 5 | module('once-observer'); 6 | 7 | test('validates input', function(assert) { 8 | assert.throws(function() { 9 | onceObserver(); 10 | }, /last argument to onceObserver must be a function/); 11 | }); 12 | 13 | test('basic usage', function(assert) { 14 | let invoked = 0; 15 | let Parent = Ember.Object.extend({ 16 | nameDidChange: onceObserver('name', function() { 17 | invoked++; 18 | }) 19 | }); 20 | 21 | var p = Parent.create(); 22 | 23 | assert.equal(invoked, 0, 'expected no invocations'); 24 | 25 | Ember.run(() => { 26 | p.set('name', 'Stef'); 27 | assert.equal(invoked, 0, 'expected no invocations'); 28 | }); 29 | 30 | assert.equal(invoked, 1, 'expected one invocation'); 31 | }); 32 | 33 | test('coalescing (same property)', function(assert) { 34 | let invoked = 0; 35 | let Parent = Ember.Object.extend({ 36 | nameDidChange: onceObserver('name', function() { 37 | invoked++; 38 | }) 39 | }); 40 | 41 | var p = Parent.create(); 42 | 43 | assert.equal(invoked, 0, 'expected no invocations'); 44 | 45 | Ember.run(() => { 46 | p.set('name', 'Stef'); 47 | p.set('name', 'Bob'); 48 | assert.equal(invoked, 0, 'expected no invocations'); 49 | }); 50 | 51 | assert.equal(invoked, 1, 'expected one invocation'); 52 | }); 53 | 54 | test('coalescing (different property)', function(assert) { 55 | let invoked = 0; 56 | let Parent = Ember.Object.extend({ 57 | nameDidChange: onceObserver('name', 'age', function() { 58 | invoked++; 59 | }) 60 | }); 61 | 62 | var p = Parent.create(); 63 | 64 | assert.equal(invoked, 0, 'expected no invocations'); 65 | 66 | Ember.run(() => { 67 | p.set('name', 'Stef'); 68 | p.set('age', 12); 69 | assert.equal(invoked, 0, 'expected no invocations'); 70 | }); 71 | 72 | assert.equal(invoked, 1, 'expected one invocation'); 73 | }); 74 | 75 | test('isDestroying', function(assert) { 76 | let invoked = 0; 77 | let Parent = Ember.Object.extend({ 78 | nameDidChange: onceObserver('name', function() { 79 | invoked++; 80 | }) 81 | }); 82 | 83 | var p = Parent.create(); 84 | 85 | assert.equal(invoked, 0, 'expected no invocations'); 86 | 87 | Ember.run(() => { 88 | p.set('name', 'Stef'); 89 | p.isDestroying = true; 90 | assert.equal(invoked, 0, 'expected no invocations'); 91 | }); 92 | 93 | assert.equal(invoked, 0, 'expected one invocation'); 94 | }); 95 | 96 | test('isDestroyed', function(assert) { 97 | let invoked = 0; 98 | let Parent = Ember.Object.extend({ 99 | nameDidChange: onceObserver('name', function() { 100 | invoked++; 101 | }) 102 | }); 103 | 104 | var p = Parent.create(); 105 | 106 | assert.equal(invoked, 0, 'expected no invocations'); 107 | 108 | Ember.run(() => { 109 | p.set('name', 'Stef'); 110 | p.isDestroyed = true; 111 | assert.equal(invoked, 0, 'expected no invocations'); 112 | }); 113 | 114 | assert.equal(invoked, 0, 'expected one invocation'); 115 | }); 116 | -------------------------------------------------------------------------------- /tests/unit/in-dom-observer-test.js: -------------------------------------------------------------------------------- 1 | import inDOMObserver from 'ember-runloop-utils/in-dom-observer'; 2 | import { module, test } from 'qunit'; 3 | import Ember from 'ember'; 4 | 5 | module('in-dom-observer'); 6 | 7 | test('input is validated (last arg is a function)', function(assert) { 8 | assert.throws(function() { 9 | inDOMObserver(); 10 | }, /last argument to inDOMObserver must be a function/); 11 | }); 12 | 13 | test('works with both `state` and `_state`', function(assert) { 14 | let invoked = 0; 15 | let Parent = Ember.Object.extend({ 16 | nameDidChange: inDOMObserver('name', function() { 17 | invoked++; 18 | }) 19 | }); 20 | 21 | let a = Parent.create({ state: 'inDOM' }); 22 | let b = Parent.create({ _state: 'inDOM' }); 23 | 24 | assert.equal(invoked, 0, 'expected no invocations'); 25 | 26 | Ember.run(() => { 27 | a.set('name', 'foo'); 28 | b.set('name', 'foo'); 29 | }); 30 | 31 | assert.equal(invoked, 2, 'expected TWO invocations'); 32 | 33 | a.state = 'preRender'; 34 | 35 | Ember.run(() => { 36 | a.set('name', 'bar'); 37 | b.set('name', 'bar'); 38 | }); 39 | 40 | assert.equal(invoked, 3, 'expected THREE invocations'); 41 | 42 | b._state = 'preRender'; 43 | 44 | Ember.run(() => { 45 | a.set('name', 'baz'); 46 | b.set('name', 'baz'); 47 | }); 48 | 49 | assert.equal(invoked, 3, 'expected THREE invocations'); 50 | 51 | a.state = 'inDOM'; 52 | b._state = 'inDOM'; 53 | 54 | Ember.run(() => { 55 | a.set('name', 'quz'); 56 | b.set('name', 'quz'); 57 | }); 58 | 59 | assert.equal(invoked, 5, 'expected FIVE invocations'); 60 | }); 61 | 62 | test('basic usage', function(assert) { 63 | let invoked = 0; 64 | let Parent = Ember.Object.extend({ 65 | nameDidChange: inDOMObserver('name', function() { 66 | invoked++; 67 | }) 68 | }); 69 | 70 | var p = Parent.create({ 71 | _state: 'inDOM', 72 | }); 73 | 74 | assert.equal(invoked, 0, 'expected no invocations'); 75 | 76 | Ember.run(() => { 77 | p.set('name', 'Stef'); 78 | assert.equal(invoked, 0, 'expected no invocations'); 79 | }); 80 | 81 | assert.equal(invoked, 1, 'expected one invocation'); 82 | }); 83 | 84 | test('state pre-render + change', function(assert) { 85 | let invoked = 0; 86 | let Parent = Ember.Object.extend({ 87 | nameDidChange: inDOMObserver('name', function() { 88 | invoked++; 89 | }) 90 | }); 91 | 92 | var p = Parent.create({ 93 | _state: 'prerender', 94 | }); 95 | 96 | assert.equal(invoked, 0, 'expected no invocations'); 97 | 98 | Ember.run(() => { 99 | p.set('name', 'Stef'); 100 | assert.equal(invoked, 0, 'expected no invocations'); 101 | }); 102 | 103 | assert.equal(invoked, 0, 'expected no invocations'); 104 | }); 105 | 106 | test('pre-render -> inDOM', function(assert) { 107 | let invoked = 0; 108 | let Parent = Ember.Object.extend({ 109 | nameDidChange: inDOMObserver('name', function() { 110 | invoked++; 111 | }) 112 | }); 113 | 114 | var p = Parent.create({ 115 | _state: 'prerender', 116 | }); 117 | 118 | assert.equal(invoked, 0, 'expected no invocations'); 119 | 120 | Ember.run(() => { 121 | p.set('name', 'Stef'); 122 | assert.equal(invoked, 0, 'expected no invocations'); 123 | }); 124 | 125 | Ember.run(() => p.set('_state', 'inDOM')); 126 | 127 | assert.equal(invoked, 0, 'expected no invocations'); 128 | 129 | Ember.run(() => { 130 | p.set('name', 'Desmond'); 131 | assert.equal(invoked, 0, 'expected no invocations'); 132 | }); 133 | 134 | assert.equal(invoked, 1, 'expected one invocation'); 135 | }); 136 | 137 | test('pre-render -> inDOM but after change', function(assert) { 138 | let invoked = 0; 139 | let Parent = Ember.Object.extend({ 140 | nameDidChange: inDOMObserver('name', function() { 141 | invoked++; 142 | }) 143 | }); 144 | 145 | var p = Parent.create({ 146 | _state: 'prerender', 147 | }); 148 | 149 | assert.equal(invoked, 0, 'expected no invocations'); 150 | 151 | Ember.run(() => { 152 | p.set('name', 'Desmond'); 153 | p.set('_state', 'inDOM'); 154 | assert.equal(invoked, 0, 'expected no invocations'); 155 | }); 156 | 157 | assert.equal(invoked, 1, 'expected one invocation'); 158 | }); 159 | 160 | test('isDestroyed', function(assert) { 161 | let invoked = 0; 162 | let Parent = Ember.Object.extend({ 163 | nameDidChange: inDOMObserver('name', function() { 164 | invoked++; 165 | }) 166 | }); 167 | 168 | var p = Parent.create({ 169 | _state: 'inDOM', 170 | }); 171 | 172 | assert.equal(invoked, 0, 'expected no invocations'); 173 | 174 | Ember.run(() => { 175 | p.set('name', 'Desmond'); 176 | p.isDestroyed = true; 177 | assert.equal(invoked, 0, 'expected no invocations'); 178 | }); 179 | 180 | assert.equal(invoked, 0, 'expected no invocations'); 181 | }); 182 | 183 | test('isDestroying', function(assert) { 184 | let invoked = 0; 185 | let Parent = Ember.Object.extend({ 186 | nameDidChange: inDOMObserver('name', function() { 187 | invoked++; 188 | }) 189 | }); 190 | 191 | var p = Parent.create({ 192 | _state: 'inDOM', 193 | }); 194 | 195 | assert.equal(invoked, 0, 'expected no invocations'); 196 | 197 | Ember.run(() => { 198 | p.set('name', 'Desmond'); 199 | p.isDestroying = true; 200 | assert.equal(invoked, 0, 'expected no invocations'); 201 | }); 202 | 203 | assert.equal(invoked, 0, 'expected no invocations'); 204 | }); 205 | 206 | test('same instance 2 inDOMObservers', function(assert) { 207 | let invokedName = 0; 208 | let invokedAge = 0; 209 | 210 | let Parent = Ember.Object.extend({ 211 | nameDidChange: inDOMObserver('name', function() { 212 | invokedName++; 213 | }), 214 | ageDidChange: inDOMObserver('age', function() { 215 | invokedAge++; 216 | }) 217 | }); 218 | 219 | var p = Parent.create({ 220 | _state: 'inDOM', 221 | }); 222 | 223 | assert.equal(invokedName, 0, 'expected no invocations'); 224 | assert.equal(invokedAge, 0, 'expected no invocations'); 225 | 226 | Ember.run(() => { 227 | p.set('name', 'Desmond'); 228 | assert.equal(invokedName, 0, 'expected no invocations'); 229 | assert.equal(invokedAge, 0, 'expected no invocations'); 230 | }); 231 | 232 | assert.equal(invokedName, 1, 'expected no invocations'); 233 | assert.equal(invokedAge, 0, 'expected no invocations'); 234 | 235 | Ember.run(() => { 236 | p.set('age', '30'); 237 | assert.equal(invokedName, 1, 'expected ONE invocations'); 238 | assert.equal(invokedAge, 0, 'expected no invocations'); 239 | }); 240 | 241 | assert.equal(invokedName, 1, 'expected ONE invocations'); 242 | assert.equal(invokedAge, 1, 'expected ONE invocations'); 243 | }); 244 | 245 | test('same instance 1 inDOMObserver 2 keys', function(assert) { 246 | let invoked = 0; 247 | 248 | let Parent = Ember.Object.extend({ 249 | nameDidChange: inDOMObserver('name', 'age', function() { 250 | invoked++; 251 | }), 252 | }); 253 | 254 | var p = Parent.create({ 255 | _state: 'inDOM', 256 | }); 257 | 258 | assert.equal(invoked, 0, 'expected no invocations'); 259 | 260 | Ember.run(() => { 261 | p.set('name', 'Desmond'); 262 | assert.equal(invoked, 0, 'expected no invocations'); 263 | }); 264 | 265 | assert.equal(invoked, 1, 'expected no invocations'); 266 | 267 | Ember.run(() => { 268 | p.set('age', '30'); 269 | assert.equal(invoked, 1, 'expected ONE invocations'); 270 | }); 271 | 272 | assert.equal(invoked, 2, 'expected TWO invocations'); 273 | 274 | Ember.run(() => { 275 | p.set('age', '99'); 276 | p.set('name', 'Santa'); 277 | assert.equal(invoked, 2, 'expected no new invocations'); 278 | }); 279 | 280 | assert.equal(invoked, 3, 'expected TREE (one more) invocations'); 281 | }); 282 | 283 | test('same inDOMObservers 2 instances', function(assert) { 284 | let invokedName = 0; 285 | 286 | let nameDidChange = inDOMObserver('name', function() { 287 | invokedName++; 288 | }); 289 | 290 | let Parent = Ember.Object.extend({ 291 | nameDidChange, 292 | }); 293 | 294 | let a = Parent.create({ _state: 'inDOM' }); 295 | let b = Parent.create({ _state: 'inDOM' }); 296 | 297 | Ember.run(() => a.set('name', 'foo')); 298 | 299 | assert.equal(invokedName, 1, 'expected ONE invocation'); 300 | 301 | Ember.run(() => b.set('name', 'foo')); 302 | 303 | assert.equal(invokedName, 2, 'expected TWO invocations'); 304 | 305 | Ember.run(() => { 306 | b.set('name', 'bar'); 307 | b.isDestroyed = true; 308 | }); 309 | 310 | assert.equal(invokedName, 2, 'expected TWO invocations'); 311 | 312 | Ember.run(() => a.set('name', 'bar')); 313 | 314 | assert.equal(invokedName, 3, 'expected THREE invocations'); 315 | }); 316 | 317 | test('once-ness', function(assert) { 318 | let invokedName = 0; 319 | let value; 320 | let nameDidChange = inDOMObserver('name', function() { 321 | invokedName++; 322 | value = this.get('name'); 323 | }); 324 | 325 | let Parent = Ember.Object.extend({ 326 | _state: 'inDOM', 327 | nameDidChange, 328 | }); 329 | 330 | let a = Parent.create(); 331 | 332 | assert.equal(invokedName, 0, 'expected no invocations'); 333 | 334 | Ember.run(() => { 335 | a.set('name', 'stef'); 336 | a.set('name', 'desmond'); 337 | 338 | assert.equal(invokedName, 0, 'expected no invocations'); 339 | }); 340 | 341 | assert.equal(invokedName, 1, 'expected ONE invocation'); 342 | assert.equal(value, 'desmond'); 343 | }); 344 | --------------------------------------------------------------------------------