Skip to content

Commit

Permalink
added idle decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Jun 15, 2021
1 parent 082bdc6 commit 68c9110
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
28 changes: 28 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@
* [hovering over the text editor triggers hover over source button instantly #138](https://github.com/jodit/jodit-react/issues/138)
* Allow insert in image dialog - relative path

#### :rocket: New Feature

* Added `idle` decorator - allow wrap class method in `requestIdleCallback`

```ts
@component
class UIInput extends UIElement {
@idle()
onSomeEvent() {
console.log('Some data');
}
}

const elm = new UIInput(jodit);
elm.onSomeEvent();
for (let i = 0; i < 5; i += 1) {
console.log(i);
}
```

Output
```
1
2
3
4
Some data
```

## 3.6.18

Expand Down
40 changes: 40 additions & 0 deletions src/core/decorators/idle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2021 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
import type {
DecoratorHandler,
IDictionary,
IViewBased,
IViewComponent
} from '../../types';
import { Component, STATUSES } from '../component';
import { error, isFunction, isViewObject } from '../helpers';

/**
* Wrap function in requestIdleCallback wrapper*
*/
export function idle<V = IViewComponent | IViewBased>(): DecoratorHandler {
return <T extends Component & IDictionary>(
target: IDictionary,
propertyKey: string
): void => {
if (!isFunction(target[propertyKey])) {
throw error('Handler must be a Function');
}

target.hookStatus(STATUSES.ready, (component: V) => {
const view = isViewObject(component)
? component
: (component as unknown as IViewComponent).jodit;

const originalMethod = (component as any)[propertyKey];

(component as any)[propertyKey] = (...args: unknown[]) =>
view.async.requestIdleCallback(
originalMethod.bind(component, ...args)
);
});
};
}
1 change: 1 addition & 0 deletions src/core/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export * from './cache';
export * from './component';
export * from './debounce';
export * from './idle';
export * from './hook';
export * from './nonenumerable';
export * from './persistent';
Expand Down
5 changes: 4 additions & 1 deletion src/modules/toolbar/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ export class ToolbarButton<T extends IViewBased = IViewBased>
button: this
});

if (result !== false) {
// For memorise exec
if (result !== false && result !== true) {
this.j?.e?.fire('synchro');

if (this.parentElement) {
Expand All @@ -425,7 +426,9 @@ export class ToolbarButton<T extends IViewBased = IViewBased>
* @event afterExec
*/
this.j?.e?.fire('closeAllPopups afterExec');
}

if (result !== false) {
return;
}
}
Expand Down

0 comments on commit 68c9110

Please sign in to comment.