Skip to content

Commit

Permalink
refactor: add some PR #153 suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
josex2r committed Dec 23, 2021
1 parent 31d319b commit 4ebec8f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
/tests/
/yarn-error.log
/yarn.lock
/.releaserc
/jsconfig.json
/greenkeeper.json
.gitkeep

# ember-try
Expand Down
4 changes: 3 additions & 1 deletion addon/components/modal-wrapper/index.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div role="dialog" data-id={{this.dataId}} data-modal-show={{this.dataModalShow}} {{did-insert this.onDidInsert}}>
<@model.componentName @model={{@model}} @changeVisibility={{this.changeVisibility}} @element={{this.element}} />
{{#let (ensure-safe-component (component this.componentName)) as |Modal|}}
<Modal @model={{@model}} @changeVisibility={{this.changeVisibility}} @element={{this.element}} />
{{/let}}
</div>
4 changes: 4 additions & 0 deletions addon/components/modal-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class ModalWrapperComponent extends Component {
return String(this.visible);
}

get componentName() {
return this.args.model.fullname;
}

@action
onDidInsert(element) {
this.element = element;
Expand Down
15 changes: 10 additions & 5 deletions addon/components/modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ export default class ModalComponent extends Component {
constructor() {
super(...arguments);

// [Service closes modal] Prevent creating an uncaught promise.
this.model.promise.catch(() => {
this._attachToServiceClose();

next(this, '_open');
}

async _attachToServiceClose() {
try {
await this.model.promise;
} catch {
if (!this._fullfillmentFn) {
this._close();
}
});

next(this, '_open');
}
}

@isNotDestroyed
Expand Down
12 changes: 1 addition & 11 deletions addon/models/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { defer } from 'rsvp';
import { isBlank } from '@ember/utils';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import { tracked } from '@glimmer/tracking';
import { ensureSafeComponent } from '@embroider/util';
import { getOwner } from '@ember/application';

export default class ModalModel extends EmberObject.extend(PromiseProxyMixin) {
@tracked name;
Expand All @@ -17,7 +15,7 @@ export default class ModalModel extends EmberObject.extend(PromiseProxyMixin) {
@tracked isFulfilled = false;
@tracked isRejected = false;

@tracked _deferred = defer();
_deferred = defer();

get promise() {
return this._deferred.promise;
Expand All @@ -33,14 +31,6 @@ export default class ModalModel extends EmberObject.extend(PromiseProxyMixin) {
return `modal-${dasherize(name)}`;
}

get componentName() {
const componentFactory = getOwner(this).factoryFor(
`component:${this.fullname}`
);

return ensureSafeComponent(componentFactory.class, this);
}

resolve() {
this.isPending = false;
this.isSettled = true;
Expand Down
16 changes: 11 additions & 5 deletions addon/services/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ export default class ModalService extends Service.extend(Evented) {

this.trigger('open', model);

model.promise
.catch(() => {})
.finally(() => {
this.trigger('close', model);
});
this._attachToModalClose(model);

return model.promise;
}

async _attachToModalClose(model) {
try {
await model.promise;
} catch {
// Nope...
} finally {
this.trigger('close', model);
}
}

_closeByModel(model) {
// Remove from DOM
this.content.removeObject(model);
Expand Down

0 comments on commit 4ebec8f

Please sign in to comment.