-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Backported helpers from 1.0.0 to 0.2.0, to help with migration
- Loading branch information
Showing
17 changed files
with
377 additions
and
206 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from '../types'; | ||
|
||
/** | ||
* This helper is activated only when it is rendered for the first time | ||
* (inserted in the DOM). It does not run during or after it is un-rendered | ||
* (removed from the DOM), or when its arguments are updated. | ||
*/ | ||
export default class DidInsertHelper extends Helper { | ||
didRun = false; | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-insert fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
); | ||
if (this.didRun) return; | ||
this.didRun = true; | ||
fn(positional.slice(1), named); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
import { deprecate } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from 'ember-render-helpers/types'; | ||
import DidInsertHelper from './did-insert-helper'; | ||
|
||
/** | ||
* This helper is activated only when it is rendered for the first time | ||
* (inserted in the DOM). It does not run during or after it is un-rendered | ||
* (removed from the DOM), or when its arguments are updated. | ||
*/ | ||
export default class DidInsertHelper extends Helper { | ||
didRun = false; | ||
export default class DeprecatedDidInsertHelper extends DidInsertHelper { | ||
/* eslint-disable-next-line @typescript-eslint/ban-types */ | ||
constructor(properties?: object) { | ||
super(properties); | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-insert fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
deprecate( | ||
'The {{did-insert}} helper has been renamed to {{did-insert-helper}}.', | ||
false, | ||
{ | ||
id: 'new-helper-names', | ||
until: '1.0.0', | ||
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */ | ||
// @ts-ignore: Outdated types do not know the for property yet but cannot be upgraded. | ||
for: 'ember-render-helpers', | ||
since: { | ||
available: '0.2.1', | ||
enabled: '0.2.1' | ||
} | ||
} | ||
); | ||
if (this.didRun) return; | ||
this.didRun = true; | ||
fn(positional.slice(1), named); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from '../types'; | ||
|
||
/** | ||
* This helper is activated only on _updates_ to it's arguments (both positional | ||
* and named). It does not run during or after initial render, or before it is | ||
* un-rendered (removed from the DOM). | ||
*/ | ||
export default class DidUpdateHelper extends Helper { | ||
didRun = false; | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-update fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
); | ||
if (!this.didRun) { | ||
this.didRun = true; | ||
|
||
// Consume individual properties to entangle tracking. | ||
// https://github.com/emberjs/ember.js/issues/19277 | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
positional.forEach(() => {}); | ||
Object.values(named); | ||
|
||
return; | ||
} | ||
fn(positional.slice(1), named); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,26 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
import { deprecate } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from 'ember-render-helpers/types'; | ||
import DidUpdateHelper from './did-update-helper'; | ||
|
||
/** | ||
* This helper is activated only on _updates_ to it's arguments (both positional | ||
* and named). It does not run during or after initial render, or before it is | ||
* un-rendered (removed from the DOM). | ||
*/ | ||
export default class DidUpdateHelper extends Helper { | ||
didRun = false; | ||
export default class DeprecatedDidUpdateHelper extends DidUpdateHelper { | ||
/* eslint-disable-next-line @typescript-eslint/ban-types */ | ||
constructor(properties?: object) { | ||
super(properties); | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-update fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
deprecate( | ||
'The {{did-update}} helper has been renamed to {{did-update-helper}}.', | ||
false, | ||
{ | ||
id: 'new-helper-names', | ||
until: '1.0.0', | ||
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */ | ||
// @ts-ignore: Outdated types do not know the for property yet but cannot be upgraded. | ||
for: 'ember-render-helpers', | ||
since: { | ||
available: '0.2.1', | ||
enabled: '0.2.1' | ||
} | ||
} | ||
); | ||
if (!this.didRun) { | ||
this.didRun = true; | ||
|
||
// Consume individual properties to entangle tracking. | ||
// https://github.com/emberjs/ember.js/issues/19277 | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
positional.forEach(() => {}); | ||
Object.values(named); | ||
|
||
return; | ||
} | ||
fn(positional.slice(1), named); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from '../types'; | ||
|
||
/** | ||
* This helper is activated immediately before the helper is un-rendered | ||
* (removed from the DOM). It does not run during or after initial render, or | ||
* when its arguments are updated. | ||
*/ | ||
export default class WillDestroyHelper extends Helper { | ||
fn?: (positional: PositionalParameters, named: NamedParameters) => void; | ||
positional?: PositionalParameters; | ||
named?: NamedParameters; | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-insert fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
); | ||
this.fn = fn; | ||
this.positional = positional.slice(1); | ||
this.named = named; | ||
} | ||
|
||
willDestroy() { | ||
if (this.fn && this.positional && this.named) { | ||
const { fn } = this; | ||
fn(this.positional, this.named); | ||
} | ||
super.willDestroy(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,26 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { assert } from '@ember/debug'; | ||
import { deprecate } from '@ember/debug'; | ||
|
||
import { | ||
PositionalParameters, | ||
NamedParameters, | ||
HelperCallback | ||
} from 'ember-render-helpers/types'; | ||
import WillDestroyHelper from './will-destroy-helper'; | ||
|
||
/** | ||
* This helper is activated immediately before the helper is un-rendered | ||
* (removed from the DOM). It does not run during or after initial render, or | ||
* when its arguments are updated. | ||
*/ | ||
export default class WillDestroyHelper extends Helper { | ||
fn?: (positional: PositionalParameters, named: NamedParameters) => void; | ||
positional?: PositionalParameters; | ||
named?: NamedParameters; | ||
export default class DeprecatedWillDestroyHelper extends WillDestroyHelper { | ||
/* eslint-disable-next-line @typescript-eslint/ban-types */ | ||
constructor(properties?: object) { | ||
super(properties); | ||
|
||
compute(positional: PositionalParameters, named: NamedParameters): void { | ||
const fn = positional[0] as HelperCallback; | ||
assert( | ||
`\`{{did-insert fn}}\` expects a function as the first parameter. You provided: ${fn}`, | ||
typeof fn === 'function' | ||
deprecate( | ||
'The {{will-destroy}} helper has been renamed to {{will-destroy-helper}}.', | ||
false, | ||
{ | ||
id: 'new-helper-names', | ||
until: '1.0.0', | ||
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */ | ||
// @ts-ignore: Outdated types do not know the for property yet but cannot be upgraded. | ||
for: 'ember-render-helpers', | ||
since: { | ||
available: '0.2.1', | ||
enabled: '0.2.1' | ||
} | ||
} | ||
); | ||
this.fn = fn; | ||
this.positional = positional.slice(1); | ||
this.named = named; | ||
} | ||
|
||
willDestroy() { | ||
if (this.fn && this.positional && this.named) { | ||
const { fn } = this; | ||
fn(this.positional, this.named); | ||
} | ||
super.willDestroy(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-render-helpers/helpers/did-insert-helper'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-render-helpers/helpers/did-update-helper'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-render-helpers/helpers/will-destroy-helper'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { render } from '@ember/test-helpers'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
import type { TestContext } from 'dummy/tests/helpers/test-context'; | ||
|
||
module('Integration | Helper | did-insert-helper', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders and calls the callback', async function (this: TestContext, assert) { | ||
this.callback = () => { | ||
assert.step('callback called'); | ||
}; | ||
|
||
await render(hbs`{{did-insert-helper this.callback}}`); | ||
|
||
assert.strictEqual( | ||
this.element.textContent, | ||
'', | ||
'It does not produce any DOM nodes.' | ||
); | ||
|
||
assert.verifySteps(['callback called'], 'It calls the callback.'); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { render } from '@ember/test-helpers'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
import type { TestContext } from 'dummy/tests/helpers/test-context'; | ||
|
||
module('Integration | Helper | did-update-helper', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders and calls the callback', async function (this: TestContext, assert) { | ||
this.callback = () => { | ||
assert.step('callback called'); | ||
}; | ||
this.pos1 = 'first positional argument'; | ||
|
||
await render(hbs`{{did-update-helper this.callback this.pos1}}`); | ||
|
||
assert.strictEqual( | ||
this.element.textContent, | ||
'', | ||
'It does not produce any DOM nodes.' | ||
); | ||
|
||
assert.verifySteps( | ||
[], | ||
'It does not call the callback before value changes' | ||
); | ||
|
||
this.set('pos1', 'something else'); | ||
|
||
assert.verifySteps(['callback called'], 'It calls the callback.'); | ||
}); | ||
}); |
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
Oops, something went wrong.