Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript and refactoring #3

Draft
wants to merge 15 commits into
base: chore/v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(ts): get right with test app
  • Loading branch information
gilest committed Oct 15, 2023
commit a33ee8e42bdeb8dd152b95633b3ddbf63180e4f8
8 changes: 5 additions & 3 deletions ember-cli-flash/declarations/flash/object.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Evented from '@ember/object/evented';
import EmberObject from '@ember/object';
import FlashMessagesService from '../services/flash-messages.ts';
import type { EmberRunTimer } from "@ember/runloop/types";
import type { EmberRunTimer } from '@ember/runloop/types';
declare const FlashObject_base: Readonly<typeof EmberObject> & (new (properties?: object | undefined) => Evented & EmberObject) & (new (...args: any[]) => Evented & EmberObject);
export default class FlashObject extends FlashObject_base {
flashService: FlashMessagesService;
exitTimer: null;
exiting: boolean;
exitTimer: null;
isExitable: boolean;
initializedTime: number;
exitTaskInstance?: EmberRunTimer;
Expand All @@ -22,13 +22,15 @@ export default class FlashObject extends FlashObject_base {
preventDuplicates: boolean;
onDestroy?: () => void;
_guid: string;
constructor();
init(): void;
willDestroy(): void;
destroyMessage(): void;
exitMessage(): void;
preventExit(): void;
allowExit(): void;
timerTask(): void;
exitTimerTask(): void;
_setInitializedTime(): number;
_getElapsedTime(): number;
_cancelTimer(taskName?: 'timerTaskInstance' | 'exitTaskInstance'): void;
_checkIfShouldExit(): void;
Expand Down
55 changes: 37 additions & 18 deletions ember-cli-flash/src/flash/object.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import Evented from '@ember/object/evented';
import EmberObject from '@ember/object';
import { cancel, later } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
import { guidFor } from '../utils/computed.ts';
import FlashMessagesService from '../services/flash-messages.ts';
import { registerDestructor } from '@ember/destroyable';
import type { EmberRunTimer } from "@ember/runloop/types";

function destructor(instance: FlashObject) {
instance.onDestroy?.();

instance._cancelTimer();
instance._cancelTimer('exitTaskInstance');
}
import type { EmberRunTimer } from '@ember/runloop/types';

// Note:
// To avoid https://github.com/adopted-ember-addons/ember-cli-flash/issues/341 from happening, this class can't simply be called Object
export default class FlashObject extends EmberObject.extend(Evented) {
declare flashService: FlashMessagesService;

@tracked exiting = false;

exitTimer = null;
exiting = false;
isExitable = true;
initializedTime: number = new Date().getTime();

Expand All @@ -30,24 +24,38 @@ export default class FlashObject extends EmberObject.extend(Evented) {
declare message: string;
declare type: string;
declare timeout?: number;
declare priority?: number
declare priority?: number;
declare sticky: boolean;
declare showProgress: boolean;
declare destroyOnClick: boolean;
declare preventDuplicates: boolean;
declare onDestroy?: () => void;

@(guidFor('message').readOnly()) declare _guid: string;
@guidFor('message').readOnly() declare _guid: string;

constructor() {
super();
init() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
super.init(...arguments);

if (this.sticky) {
return;
}
this.timerTask();
this._setInitializedTime();
}

willDestroy() {
if (this.onDestroy) {
this.onDestroy();
}

registerDestructor(this, destructor);
this._cancelTimer();
this._cancelTimer('exitTaskInstance');

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
super.willDestroy(...arguments);
}

destroyMessage() {
Expand Down Expand Up @@ -91,7 +99,7 @@ export default class FlashObject extends EmberObject.extend(Evented) {
if (this.isDestroyed) {
return;
}
this.exiting, true;
this.exiting = true;
if (this.extendedTimeout) {
let exitTaskInstance = later(() => {
this._teardown();
Expand All @@ -102,13 +110,22 @@ export default class FlashObject extends EmberObject.extend(Evented) {
}
}

_setInitializedTime() {
let currentTime = new Date().getTime();
this.initializedTime = currentTime;

return this.initializedTime;
}

_getElapsedTime() {
let currentTime = new Date().getTime();

return currentTime - this.initializedTime;
}

_cancelTimer(taskName: 'timerTaskInstance' | 'exitTaskInstance' = 'timerTaskInstance') {
_cancelTimer(
taskName: 'timerTaskInstance' | 'exitTaskInstance' = 'timerTaskInstance',
) {
if (this[taskName]) {
cancel(this[taskName]);
}
Expand All @@ -123,7 +140,9 @@ export default class FlashObject extends EmberObject.extend(Evented) {

_teardown() {
if (this.flashService?.queue) {
this.flashService.queue = this.flashService.queue.filter((flash) => flash !== this);
this.flashService.queue = this.flashService.queue.filter(
(flash) => flash !== this,
);
}
this.destroy();
this.trigger('didDestroyMessage');
Expand Down
18 changes: 9 additions & 9 deletions test-app/tests/unit/services/flash-messages-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module('FlashMessagesService', function (hooks) {

hooks.afterEach(function () {
run(() => {
this.service.queue.clear();
this.service.queue = [];
this.service.destroy();
});

Expand Down Expand Up @@ -233,7 +233,7 @@ module('FlashMessagesService', function (hooks) {
customProperty: 'ohai',
});

assert.strictEqual(this.service.queue.firstObject.customProperty, 'ohai');
assert.strictEqual(this.service.queue[0].customProperty, 'ohai');
});

test('when preventDuplicates is `true`, setting a message is required', function (assert) {
Expand Down Expand Up @@ -268,7 +268,7 @@ module('FlashMessagesService', function (hooks) {
set(this, 'service.defaultPreventDuplicates', false);
const expectedResult = emberArray(['foo', 'foo', 'bar']);
expectedResult.forEach((message) => this.service.success(message));
const result = this.service.queue.mapBy('message');
const result = this.service.queue.map((flash) => flash.message);

assert.deepEqual(
result,
Expand All @@ -287,7 +287,7 @@ module('FlashMessagesService', function (hooks) {
const messages = emberArray(['foo', 'foo', 'bar']);
const expectedResult = messages.uniq();
messages.forEach((message) => this.service.success(message));
const result = this.service.queue.mapBy('message');
const result = this.service.queue.map((flash) => flash.message);

assert.deepEqual(
result,
Expand All @@ -308,7 +308,7 @@ module('FlashMessagesService', function (hooks) {
messages.forEach((message) =>
this.service.success(message, { preventDuplicates: true })
);
const result = this.service.queue.mapBy('message');
const result = this.service.queue.map((flash) => flash.message);

assert.deepEqual(
result,
Expand All @@ -329,7 +329,7 @@ module('FlashMessagesService', function (hooks) {
messages.forEach((message) =>
this.service.success(message, { preventDuplicates: false })
);
const result = this.service.queue.mapBy('message');
const result = this.service.queue.map((flash) => flash.message);

assert.deepEqual(
result,
Expand All @@ -352,7 +352,7 @@ module('FlashMessagesService', function (hooks) {
this.service.success('foo');
this.service.success('baz', { preventDuplicates: true });

const result = this.service.queue.mapBy('message');
const result = this.service.queue.map((flash) => flash.message);

assert.deepEqual(
result,
Expand All @@ -374,12 +374,12 @@ module('FlashMessagesService', function (hooks) {
.meow('bar');

assert.strictEqual(
this.service.queue.firstObject.message,
this.service.queue.at(0).message,
'foo',
'should support chaining'
);
assert.strictEqual(
this.service.queue.lastObject.message,
this.service.queue.at(-1).message,
'bar',
'should support chaining'
);
Expand Down
37 changes: 0 additions & 37 deletions test-app/tests/unit/utils/get-with-default-test.js

This file was deleted.

25 changes: 0 additions & 25 deletions test-app/tests/unit/utils/object-compact-test.js

This file was deleted.

25 changes: 0 additions & 25 deletions test-app/tests/unit/utils/object-only-test.js

This file was deleted.