Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 92d5152

Browse files
committed
added bucket option
1 parent 3e2d14f commit 92d5152

File tree

7 files changed

+61
-10
lines changed

7 files changed

+61
-10
lines changed

benchmark/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ The `Benchmark Test Suites` are supposed to showcase where AgileTs is roughly lo
44
I know a counter doesn't really show real world app performance,
55
but it is better than nothing.
66

7+
### What do the results from benchmark js mean?
8+
https://stackoverflow.com/questions/28524653/what-do-the-results-from-benchmark-js-mean
9+
710
## Counter Benchmark
811

912
```ts

benchmark/benchmarks/react/1000fields/bench/agilets/nestedState.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Agile, Logger, State } from '@agile-ts/core';
44
import { useAgile } from '@agile-ts/react';
55

66
export default function (target: HTMLElement, fieldsCount: number) {
7-
const AgileApp = new Agile({ logConfig: { level: Logger.level.ERROR } });
7+
const AgileApp = new Agile({
8+
logConfig: { level: Logger.level.ERROR },
9+
});
810

911
const FIELDS = AgileApp.createState(
1012
Array.from(Array(fieldsCount).keys()).map((i) =>

benchmark/benchmarks/react/counter/bench/agilets.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import ReactDom from 'react-dom';
33
import { Agile, Logger } from '@agile-ts/core';
44
import { useAgile } from '@agile-ts/react';
55

6-
const AgileApp = new Agile({ logConfig: { level: Logger.level.ERROR } });
6+
const AgileApp = new Agile({
7+
logConfig: { level: Logger.level.ERROR },
8+
});
79
const COUNT = AgileApp.createState(0);
810

911
const App = () => {

packages/core/src/agile.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ export class Agile {
8989
logConfig: {},
9090
bindGlobal: false,
9191
autoIntegrate: true,
92+
bucket: true,
9293
});
9394
this.config = {
9495
waitForMount: config.waitForMount as any,
96+
bucket: config.bucket as any,
9597
};
9698
this.key = config.key;
9799
this.integrations = new Integrations(this, {
@@ -368,6 +370,15 @@ export interface CreateAgileConfigInterface
368370
* @default undefined
369371
*/
370372
key?: AgileKey;
373+
/**
374+
* Whether to put render events into "The bucket" of the browser,
375+
* where all events are first put in wait for the UI thread
376+
* to be done with whatever it's doing.
377+
*
378+
* [Learn more](https://stackoverflow.com/questions/9083594/call-settimeout-without-delay)
379+
* @default true
380+
*/
381+
bucket?: boolean;
371382
}
372383

373384
export interface AgileConfigInterface {
@@ -377,4 +388,13 @@ export interface AgileConfigInterface {
377388
* @default true
378389
*/
379390
waitForMount: boolean;
391+
/**
392+
* Whether to put render events into "The bucket" of the browser,
393+
* where all events are first put in wait for the UI thread
394+
* to be done with whatever it's doing.
395+
*
396+
* [Learn more](https://stackoverflow.com/questions/9083594/call-settimeout-without-delay)
397+
* @default true
398+
*/
399+
bucket: boolean;
380400
}

packages/core/src/runtime/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ export class Runtime {
123123
} else {
124124
this.isPerformingJobs = false;
125125
if (this.jobsToRerender.length > 0) {
126-
// https://stackoverflow.com/questions/9083594/call-settimeout-without-delay
127-
setTimeout(() => {
128-
this.updateSubscribers();
129-
});
126+
if (this.agileInstance().config.bucket) {
127+
// https://stackoverflow.com/questions/9083594/call-settimeout-without-delay
128+
setTimeout(() => {
129+
this.updateSubscribers();
130+
});
131+
} else this.updateSubscribers();
130132
}
131133
}
132134
}

packages/core/tests/unit/agile.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe('Agile Tests', () => {
8888

8989
expect(agile.config).toStrictEqual({
9090
waitForMount: true,
91+
bucket: true,
9192
});
9293
expect(agile.key).toBeUndefined();
9394
expect(IntegrationsMock).toHaveBeenCalledWith(agile, {
@@ -111,6 +112,7 @@ describe('Agile Tests', () => {
111112
it('should instantiate Agile (specific config)', () => {
112113
const agile = new Agile({
113114
waitForMount: false,
115+
bucket: false,
114116
localStorage: false,
115117
logConfig: {
116118
level: Logger.level.DEBUG,
@@ -125,6 +127,7 @@ describe('Agile Tests', () => {
125127

126128
expect(agile.config).toStrictEqual({
127129
waitForMount: false,
130+
bucket: false,
128131
});
129132
expect(agile.key).toBe('jeff');
130133
expect(IntegrationsMock).toHaveBeenCalledWith(agile, {

packages/core/tests/unit/runtime/runtime.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../../../src';
1010
import * as Utils from '@agile-ts/utils';
1111
import { LogMock } from '../../helper/logMock';
12+
import waitForExpect from 'wait-for-expect';
1213

1314
describe('Runtime Tests', () => {
1415
let dummyAgile: Agile;
@@ -111,8 +112,10 @@ describe('Runtime Tests', () => {
111112

112113
it(
113114
"should perform specified Job and all remaining Jobs in the 'jobQueue' " +
114-
"and call 'updateSubscribers' if at least one performed Job needs to rerender",
115+
"and call 'updateSubscribers' in a setTimeout " +
116+
'if at least one performed Job needs to rerender (config.bucket = true)',
115117
async () => {
118+
runtime.agileInstance().config.bucket = true;
116119
runtime.jobQueue.push(dummyJob2);
117120
runtime.jobQueue.push(dummyJob3);
118121

@@ -129,8 +132,23 @@ describe('Runtime Tests', () => {
129132
expect(runtime.jobQueue).toStrictEqual([]);
130133
expect(runtime.jobsToRerender).toStrictEqual([dummyJob1, dummyJob2]);
131134

132-
// Sleep 5ms because updateSubscribers is called in a timeout
133-
await new Promise((resolve) => setTimeout(resolve, 5));
135+
// Because 'updateSubscribers' is called in a timeout
136+
await waitForExpect(() => {
137+
expect(runtime.updateSubscribers).toHaveBeenCalledTimes(1);
138+
});
139+
}
140+
);
141+
142+
it(
143+
"should perform specified Job and all remaining Jobs in the 'jobQueue' " +
144+
"and call 'updateSubscribers' " +
145+
'if at least one performed Job needs to rerender (config.bucket = false)',
146+
async () => {
147+
runtime.agileInstance().config.bucket = false;
148+
runtime.jobQueue.push(dummyJob2);
149+
runtime.jobQueue.push(dummyJob3);
150+
151+
runtime.perform(dummyJob1);
134152

135153
expect(runtime.updateSubscribers).toHaveBeenCalledTimes(1);
136154
}
@@ -151,7 +169,8 @@ describe('Runtime Tests', () => {
151169

152170
it(
153171
"should perform specified Job and all remaining Jobs in the 'jobQueue' " +
154-
"and shouldn't call 'updateSubscribes' if no performed Job needs to rerender",
172+
"and shouldn't call 'updateSubscribes' " +
173+
'if no performed Job needs to rerender',
155174
async () => {
156175
dummyJob1.rerender = false;
157176
runtime.jobQueue.push(dummyJob3);

0 commit comments

Comments
 (0)