Skip to content

Commit 6a85006

Browse files
committed
refactor(flash object): convert to native class
1 parent 9881f9c commit 6a85006

File tree

6 files changed

+43
-95
lines changed

6 files changed

+43
-95
lines changed

ember-cli-flash/src/flash/object.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
import EmberObject, { set } from '@ember/object';
21
import { cancel, later } from '@ember/runloop';
3-
import { guidFor } from '../utils/computed';
42
import { isTesting, macroCondition } from '@embroider/macros';
3+
import { tracked } from '@glimmer/tracking';
4+
import { guidFor } from '@ember/object/internals';
55

66
// Disable timeout by default when running tests
77
const defaultDisableTimeout = macroCondition(isTesting()) ? true : false;
88

9-
// Note:
10-
// To avoid https://github.com/adopted-ember-addons/ember-cli-flash/issues/341 from happening, this class can't simply be called Object
11-
export default class FlashObject extends EmberObject {
9+
export default class FlashObject {
1210
exitTimer = null;
13-
exiting = false;
11+
@tracked exiting = false;
1412
isExitable = true;
1513
initializedTime = null;
14+
isDestroyed = false;
1615

1716
// testHelperDisableTimeout – Set by `disableTimeout` and `enableTimeout` in test-support.js
1817

1918
get disableTimeout() {
2019
return this.testHelperDisableTimeout ?? defaultDisableTimeout;
2120
}
2221

23-
@(guidFor('message').readOnly())
24-
_guid;
22+
get _guid() {
23+
return guidFor(this.message?.toString());
24+
}
2525

26-
// eslint-disable-next-line ember/classic-decorator-hooks
27-
init() {
28-
super.init(...arguments);
26+
constructor(messageOptions) {
27+
for (const [key, value] of Object.entries(messageOptions)) {
28+
this[key] = value;
29+
}
2930

3031
if (this.disableTimeout || this.sticky) {
3132
return;
@@ -49,24 +50,25 @@ export default class FlashObject extends EmberObject {
4950
return;
5051
}
5152
this.exitTimerTask();
53+
this.onDidExitMessage?.();
5254
}
5355

54-
willDestroy() {
56+
destroy() {
5557
if (this.onDestroy) {
5658
this.onDestroy();
5759
}
5860

5961
this._cancelTimer();
6062
this._cancelTimer('exitTaskInstance');
61-
super.willDestroy(...arguments);
63+
this.isDestroyed = true;
6264
}
6365

6466
preventExit() {
65-
set(this, 'isExitable', false);
67+
this.isExitable = false;
6668
}
6769

6870
allowExit() {
69-
set(this, 'isExitable', true);
71+
this.isExitable = true;
7072
this._checkIfShouldExit();
7173
}
7274

@@ -77,27 +79,27 @@ export default class FlashObject extends EmberObject {
7779
const timerTaskInstance = later(() => {
7880
this.exitMessage();
7981
}, this.timeout);
80-
set(this, 'timerTaskInstance', timerTaskInstance);
82+
this.timerTaskInstance = timerTaskInstance;
8183
}
8284

8385
exitTimerTask() {
8486
if (this.isDestroyed) {
8587
return;
8688
}
87-
set(this, 'exiting', true);
89+
this.exiting = true;
8890
if (this.extendedTimeout) {
8991
let exitTaskInstance = later(() => {
9092
this._teardown();
9193
}, this.extendedTimeout);
92-
set(this, 'exitTaskInstance', exitTaskInstance);
94+
this.exitTaskInstance = exitTaskInstance;
9395
} else {
9496
this._teardown();
9597
}
9698
}
9799

98100
_setInitializedTime() {
99101
let currentTime = new Date().getTime();
100-
set(this, 'initializedTime', currentTime);
102+
this.initializedTime = currentTime;
101103

102104
return this.initializedTime;
103105
}

ember-cli-flash/src/services/flash-messages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default class FlashMessagesService extends Service {
103103
set(flashMessageOptions, key, option);
104104
}
105105

106-
return FlashMessage.create(flashMessageOptions);
106+
return new FlashMessage(flashMessageOptions);
107107
}
108108

109109
_getOptionOrDefault(key, value) {

ember-cli-flash/src/utils/computed.js

-17
This file was deleted.

test-app/tests/integration/components/flash-message-test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module('Integration | Component | flash message', function (hooks) {
1818
setupRenderingTest(hooks);
1919

2020
test('it renders a flash message', async function (assert) {
21-
this.set('flash', FlashMessage.create({ message: 'hi', sticky: true }));
21+
this.set('flash', new FlashMessage({ message: 'hi', sticky: true }));
2222

2323
await render(hbs`
2424
<FlashMessage @flash={{this.flash}} as |component flash|>
@@ -34,7 +34,7 @@ module('Integration | Component | flash message', function (hooks) {
3434

3535
this.set(
3636
'flash',
37-
FlashMessage.create({
37+
new FlashMessage({
3838
message: 'test',
3939
type: 'test',
4040
timeout: TIMEOUT,
@@ -68,7 +68,7 @@ module('Integration | Component | flash message', function (hooks) {
6868
});
6969

7070
test('it does not error when quickly removed from the DOM', async function (assert) {
71-
this.set('flash', FlashMessage.create({ message: 'hi', sticky: true }));
71+
this.set('flash', new FlashMessage({ message: 'hi', sticky: true }));
7272
this.set('flag', true);
7373

7474
await render(hbs`
@@ -90,7 +90,7 @@ module('Integration | Component | flash message', function (hooks) {
9090

9191
this.set(
9292
'flash',
93-
FlashMessage.create({
93+
new FlashMessage({
9494
message: 'hi',
9595
sticky: false,
9696
timeout: timeoutDefault,
@@ -123,7 +123,7 @@ module('Integration | Component | flash message', function (hooks) {
123123
test('flash message is removed after timeout if mouse enters', async function (assert) {
124124
assert.expect(3);
125125

126-
let flashObject = FlashMessage.create({
126+
let flashObject = new FlashMessage({
127127
message: 'hi',
128128
sticky: false,
129129
timeout: timeoutDefault,
@@ -164,7 +164,7 @@ module('Integration | Component | flash message', function (hooks) {
164164

165165
this.set(
166166
'flash',
167-
FlashMessage.create({
167+
new FlashMessage({
168168
message: 'flash message content',
169169
sticky: true,
170170
destroyOnClick: false,
@@ -192,7 +192,7 @@ module('Integration | Component | flash message', function (hooks) {
192192

193193
test('exiting class is applied for sticky messages', async function (assert) {
194194
assert.expect(2);
195-
let flashObject = FlashMessage.create({
195+
let flashObject = new FlashMessage({
196196
message: 'flash message content',
197197
sticky: true,
198198
extendedTimeout: 100,
@@ -213,7 +213,7 @@ module('Integration | Component | flash message', function (hooks) {
213213

214214
test('custom message type class name prefix is applied', async function (assert) {
215215
assert.expect(2);
216-
let flashObject = FlashMessage.create({
216+
let flashObject = new FlashMessage({
217217
message: 'flash message content',
218218
type: 'test',
219219
sticky: true,

test-app/tests/unit/flash/object-test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let flash = null;
1010
module('FlashMessageObject disableTimeout', function (hooks) {
1111
hooks.beforeEach(function () {
1212
disableTimeout();
13-
flash = FlashMessage.create({
13+
flash = new FlashMessage({
1414
type: 'test',
1515
message: 'Cool story brah',
1616
timeout: testTimerDuration,
@@ -32,7 +32,7 @@ module('FlashMessageObject enableTimeout', function (hooks) {
3232
hooks.beforeEach(function () {
3333
disableTimeout();
3434
enableTimeout();
35-
flash = FlashMessage.create({
35+
flash = new FlashMessage({
3636
type: 'test',
3737
message: 'Cool story brah',
3838
timeout: testTimerDuration,
@@ -54,7 +54,7 @@ module('FlashMessageObject enableTimeout', function (hooks) {
5454

5555
module('FlashMessageObject', function (hooks) {
5656
hooks.beforeEach(function () {
57-
flash = FlashMessage.create({
57+
flash = new FlashMessage({
5858
type: 'test',
5959
message: 'Cool story brah',
6060
timeout: testTimerDuration,
@@ -70,17 +70,17 @@ module('FlashMessageObject', function (hooks) {
7070
});
7171

7272
test('it sets a timer after init', function (assert) {
73-
assert.ok(isPresent(flash.get('timerTaskInstance')));
73+
assert.ok(isPresent(flash.timerTaskInstance));
7474
});
7575

7676
test('it destroys the message after the timer has elapsed', function (assert) {
7777
let result;
7878
const done = assert.async();
7979
assert.expect(3);
8080

81-
flash.set('onDidDestroyMessage', () => {
81+
flash.onDidDestroyMessage = () => {
8282
result = 'foo';
83-
});
83+
};
8484

8585
later(() => {
8686
assert.true(flash.isDestroyed, 'it sets `isDestroyed` to true');
@@ -98,7 +98,7 @@ module('FlashMessageObject', function (hooks) {
9898
const done = assert.async();
9999
assert.expect(1);
100100

101-
const stickyFlash = FlashMessage.create({
101+
const stickyFlash = new FlashMessage({
102102
type: 'test',
103103
message: 'Cool story brah',
104104
timeout: testTimerDuration,
@@ -119,30 +119,30 @@ module('FlashMessageObject', function (hooks) {
119119
flash.destroyMessage();
120120
});
121121

122-
assert.true(flash.get('isDestroyed'));
123-
assert.notOk(flash.get('timer'));
122+
assert.true(flash.isDestroyed);
123+
assert.notOk(flash.timer);
124124
});
125125

126126
test('it sets `exiting` to true after the timer has elapsed', function (assert) {
127127
assert.expect(2);
128128
const done = assert.async();
129129

130-
const exitFlash = FlashMessage.create({
130+
const exitFlash = new FlashMessage({
131131
timeout: testTimerDuration,
132132
extendedTimeout: testTimerDuration,
133133
});
134134

135135
later(() => {
136-
assert.true(exitFlash.get('exiting'), 'it sets `exiting` to true');
137-
assert.notOk(exitFlash.get('timer'), 'it cancels the `timer`');
136+
assert.true(exitFlash.exiting, 'it sets `exiting` to true');
137+
assert.notOk(exitFlash.timer, 'it cancels the `timer`');
138138
done();
139139
}, testTimerDuration * 2);
140140
});
141141

142142
test('it calls `onDestroy` when object is destroyed', function (assert) {
143143
assert.expect(1);
144144

145-
const callbackFlash = FlashMessage.create({
145+
const callbackFlash = new FlashMessage({
146146
sticky: true,
147147
onDestroy() {
148148
assert.ok(true, 'onDestroy is called');

test-app/tests/unit/utils/computed-test.js

-37
This file was deleted.

0 commit comments

Comments
 (0)