Skip to content

Commit

Permalink
feature: Backported helpers from 1.0.0 to 0.2.0, to help with migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan authored and ijlee2 committed Nov 11, 2024
1 parent 71f650b commit 9f14a8f
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 206 deletions.
28 changes: 28 additions & 0 deletions addon/helpers/did-insert-helper.ts
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);
}
}
42 changes: 20 additions & 22 deletions addon/helpers/did-insert.ts
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);
}
}
37 changes: 37 additions & 0 deletions addon/helpers/did-update-helper.ts
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);
}
}
51 changes: 20 additions & 31 deletions addon/helpers/did-update.ts
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);
}
}
38 changes: 38 additions & 0 deletions addon/helpers/will-destroy-helper.ts
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();
}
}
52 changes: 20 additions & 32 deletions addon/helpers/will-destroy.ts
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();
}
}
1 change: 1 addition & 0 deletions app/helpers/did-insert-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-render-helpers/helpers/did-insert-helper';
1 change: 1 addition & 0 deletions app/helpers/did-update-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-render-helpers/helpers/did-update-helper';
1 change: 1 addition & 0 deletions app/helpers/will-destroy-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-render-helpers/helpers/will-destroy-helper';
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-render-helpers",
"version": "0.2.0",
"version": "0.2.1",
"description": "Complimentary render template helpers to the render modifiers",
"keywords": [
"ember-addon",
Expand Down Expand Up @@ -78,5 +78,8 @@
},
"ember-addon": {
"configPath": "tests/dummy/config"
},
"volta": {
"node": "16.20.2"
}
}
27 changes: 27 additions & 0 deletions tests/integration/helpers/did-insert-helper-test.ts
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.');
});
});
5 changes: 2 additions & 3 deletions tests/integration/helpers/did-insert-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { module, test } from 'qunit';
import { getProperties, set } from '@ember/object';
import { run } from '@ember/runloop';

import { hbs } from 'ember-cli-htmlbars';
import {
PositionalParameters,
NamedParameters
} from 'ember-render-helpers/types';

import hbs from 'htmlbars-inline-precompile';

import { TestContext } from 'dummy/tests/helpers/test-context';
import type { TestContext } from 'dummy/tests/helpers/test-context';

module('Integration | Helper | did-insert', function (hooks) {
setupRenderingTest(hooks);
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/helpers/did-update-helper-test.ts
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.');
});
});
5 changes: 2 additions & 3 deletions tests/integration/helpers/did-update-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { module, test } from 'qunit';
import { getProperties, set } from '@ember/object';
import { run } from '@ember/runloop';

import { hbs } from 'ember-cli-htmlbars';
import {
PositionalParameters,
NamedParameters
} from 'ember-render-helpers/types';

import hbs from 'htmlbars-inline-precompile';

import { TestContext } from 'dummy/tests/helpers/test-context';
import type { TestContext } from 'dummy/tests/helpers/test-context';

module('Integration | Helper | did-update', function (hooks) {
setupRenderingTest(hooks);
Expand Down
Loading

0 comments on commit 9f14a8f

Please sign in to comment.