Skip to content

Commit f52cb8a

Browse files
committed
performance improvements :
* do nothing if CmdNone * do not forceUpdate if updated model is ref equals updated model.
1 parent b03450e commit f52cb8a

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

core/src/TeaCup/Cmd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export abstract class Cmd<Msg> {
6363
/**
6464
* A command that does nothing.
6565
*/
66-
class CmdNone<Msg> extends Cmd<Msg> {
66+
// exported for perf optimisation reasons
67+
export class CmdNone<Msg> extends Cmd<Msg> {
6768
execute(dispatch: Dispatcher<Msg>): void {
6869
// it's a noop !
6970
}

tea-cup/src/TeaCup/Program.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
import { Component, ReactNode } from 'react';
27-
import { Dispatcher, Cmd, Sub, nextUuid } from 'tea-cup-core';
27+
import { Dispatcher, Cmd, Sub, nextUuid, CmdNone } from 'tea-cup-core';
2828
import { DevToolsEvent, DevTools } from './DevTools';
2929

3030
/**
@@ -93,18 +93,25 @@ export class Program<Model, Msg> extends Component<ProgramProps<Model, Msg>, nev
9393

9494
// perform commands in a separate timout, to
9595
// make sure that this dispatch is done
96-
setTimeout(() => {
97-
// console.log("dispatch: processing commands");
98-
// debug("performing command", updated[1]);
99-
updated[1].execute(d);
100-
// debug("<<< done");
101-
}, 0);
96+
const cmd = updated[1];
97+
if (!(cmd instanceof CmdNone)) {
98+
setTimeout(() => {
99+
// console.log("dispatch: processing commands");
100+
// debug("performing command", updated[1]);
101+
updated[1].execute(d);
102+
// debug("<<< done");
103+
}, 0);
104+
}
105+
106+
const needsUpdate = this.currentModel === updated[0];
102107

103108
this.currentModel = updated[0];
104109
this.currentSub = newSub;
105110

106111
// trigger rendering
107-
this.forceUpdate();
112+
if (needsUpdate) {
113+
this.forceUpdate();
114+
}
108115
}
109116
}
110117

0 commit comments

Comments
 (0)