Skip to content

Commit

Permalink
Support ember-modifier v4 (#888)
Browse files Browse the repository at this point in the history
This adds support for ember-modifier v4. ember-modifier v3 is supported in parallel. This eases upgrades for applications, which have an indirect dependency on ember-modifier through several packages like this one.

As ember-modifier v3 is still supported going forward, this is not a breaking change.
  • Loading branch information
jelhan authored Jan 22, 2023
1 parent 7509019 commit 303c95c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
3 changes: 2 additions & 1 deletion ember-file-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@glimmer/component": "^1.0.4",
"@glimmer/tracking": "^1.0.4",
"ember-auto-import": "^2.0.0",
"ember-modifier": "^3.2.7",
"ember-modifier": "^3.2.7 || ^4.0.0",
"tracked-built-ins": "^3.0.0"
},
"devDependencies": {
Expand All @@ -73,6 +73,7 @@
"@types/ember__destroyable": "^4.0.0",
"@types/ember__engine": "^4.0.0",
"@types/ember__object": "^4.0.0",
"@types/ember__owner": "^4.0.0",
"@types/ember__runloop": "^4.0.0",
"@types/ember__service": "^4.0.0",
"@types/ember__string": "^3.16.3",
Expand Down
10 changes: 0 additions & 10 deletions ember-file-upload/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ export enum FileSource {
Blob = 'blob',
}

export interface SelectFileModifierSignature {
Args: {
Named: {
filter?: (file: File, files: File[], index: number) => boolean;
onFilesSelected?: (files: UploadFile[]) => void;
};
};
Element: HTMLInputElement;
}

export interface FileDropzoneArgs {
queue?: Queue;

Expand Down
24 changes: 15 additions & 9 deletions ember-file-upload/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { modifier } from 'ember-modifier';
import { TrackedSet } from 'tracked-built-ins';
import { UploadFile } from './upload-file';
import type FileQueueService from './services/file-queue';
import {
FileSource,
FileState,
QueueListener,
QueueName,
SelectFileModifierSignature,
} from './interfaces';
import { FileSource, FileState, QueueListener, QueueName } from './interfaces';

/**
* The Queue is a collection of files that
Expand Down Expand Up @@ -181,8 +175,18 @@ export class Queue {
}
}

selectFile = modifier<SelectFileModifierSignature>(
(element, _positional, { filter, onFilesSelected }) => {
selectFile = modifier(
(
element: HTMLInputElement,
_positional: [],
{
filter,
onFilesSelected,
}: {
filter?: (file: File, files: File[], index: number) => boolean;
onFilesSelected?: (files: UploadFile[]) => void;
}
) => {
const changeHandler = (event: Event) => {
const { files: fileList } = event.target as HTMLInputElement;
if (!fileList) {
Expand Down Expand Up @@ -226,6 +230,8 @@ export class Queue {
element.removeEventListener('change', changeHandler);
};
},
// @ts-expect-error ember-modifier@^3 requires an options hash as second argument
// used to opt-in to lazy argument handling, which is the default for ember-modifier@^4
{ eager: false }
);
}
3 changes: 2 additions & 1 deletion ember-file-upload/src/system/drag-listener-modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Modifier, { ArgsFor, NamedArgs } from 'ember-modifier';
import { DragListenerModifierSignature } from '../interfaces';
import DragListener from './drag-listener';
import { registerDestructor } from '@ember/destroyable';
import type Owner from '@ember/owner';

function cleanup(instance: DragListenerModifier) {
if (!instance.listener) return;
Expand All @@ -12,7 +13,7 @@ function cleanup(instance: DragListenerModifier) {
export default class DragListenerModifier extends Modifier<DragListenerModifierSignature> {
listener?: DragListener;

constructor(owner: unknown, args: ArgsFor<DragListenerModifierSignature>) {
constructor(owner: Owner, args: ArgsFor<DragListenerModifierSignature>) {
super(owner, args);
registerDestructor(this, cleanup);
}
Expand Down
27 changes: 16 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 303c95c

Please sign in to comment.