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

Commit 3aff659

Browse files
mgolCaerusKaru
authored andcommitted
feat: rename noReplay to replay; set to true by default (#71)
Fixes #69
1 parent bad8951 commit 3aff659

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ replayer.replayAll();
7575
#### PrebootOptions
7676

7777
* `appRoot` (**required**) - One or more selectors for apps in the page (i.e. so one string or an array of strings).
78-
* `buffer` (default true) - If true, preboot will attempt to buffer client rendering to an extra hidden div. In most
78+
* `buffer` (default `true`) - If true, preboot will attempt to buffer client rendering to an extra hidden div. In most
7979
cases you will want to leave the default (i.e. true) but may turn off if you are debugging an issue.
8080
* `minify` (deprecated) - minification has been removed in v6. Minification should be handled by the end-user
8181
* `eventSelectors` (defaults below) - This is an array of objects which specify what events preboot should be listening for
8282
on the server view and how preboot should replay those events to the client view.
8383
See Event Selector section below for more details but note that in most cases, you can just rely on the defaults
8484
and you don't need to explicitly set anything here.
85-
* `noReplay` (default false) - The only reason why you would want to set this to true is if you want to
86-
manually trigger the replay yourself. This contrasts with the event selector `noReplay`, because this option is global
85+
* `replay` (default `true`) - The only reason why you would want to set this to `false` is if you want to
86+
manually trigger the replay yourself. This contrasts with the event selector `replay`, because this option is global
8787

8888
This comes in handy for situations where you want to hold off
8989
on the replay and buffer switch until AFTER some async events occur (i.e. route loading, http calls, etc.). By
9090
default, replay occurs right after bootstrap is complete. In some apps, there are more events after bootstrap
9191
however where the page continues to change in significant ways. Basically if you are making major changes to
92-
the page after bootstrap then you will see some jank unless you set `noReplay` to `true` and then trigger replay
92+
the page after bootstrap then you will see some jank unless you set `replay` to `false` and then trigger replay
9393
yourself once you know that all async events are complete.
9494

9595
To manually trigger replay, simply inject the EventReplayer like this:
@@ -118,12 +118,12 @@ Each event selector has the following properties:
118118
* `events` - An array of event names to listen for (ex. `['focusin', 'keyup', 'click']`)
119119
* `keyCodes` - Only do something IF event includes a key pressed that matches the given key codes.
120120
Useful for doing something when user hits return in a input box or something similar.
121-
* `preventDefault` - If true, `event.preventDefault()` will be called to prevent any further event propagation.
122-
* `freeze` - If true, the UI will freeze which means displaying a translucent overlay which prevents
121+
* `preventDefault` - If `true`, `event.preventDefault()` will be called to prevent any further event propagation.
122+
* `freeze` - If `true`, the UI will freeze which means displaying a translucent overlay which prevents
123123
any further user action until preboot is complete.
124124
* `action` - This is a function callback for any custom code you want to run when this event occurs
125125
in the server view.
126-
* `noReplay` - If true, the event won't be recorded or replayed. Useful when you utilize one of the other options above.
126+
* `replay` - If `false`, the event won't be recorded or replayed. Useful when you utilize one of the other options above.
127127

128128
Here are some examples of event selectors from the defaults:
129129

@@ -141,7 +141,7 @@ var eventSelectors = [
141141
{ selector: 'form', events: ['submit'], preventDefault: true, freeze: true },
142142

143143
// for tracking focus (no need to replay)
144-
{ selector: 'input,textarea', events: ['focusin', 'focusout', 'mousedown', 'mouseup'], noReplay: true },
144+
{ selector: 'input,textarea', events: ['focusin', 'focusout', 'mousedown', 'mouseup'], replay: false },
145145

146146
// user clicks on a button
147147
{ selector: 'button', events: ['click'], preventDefault: true, freeze: true }
@@ -152,12 +152,12 @@ var eventSelectors = [
152152

153153
Preboot registers its reply code at the `APP_BOOTSTRAP_LISTENER` token which is called by Angular for every component that is bootstrapped. If you don't have the `bootstrap` property defined in your `AppModule`'s `NgModule` but you instead use the `ngDoBootrap` method (which is done e.g. when using ngUpgrade) this code will not run at all.
154154

155-
To make Preboot work correctly in such a case you need to specify `noReplay: true` in the Preboot options and replay the events yourself. That is, import `PrebootModule` like this:
155+
To make Preboot work correctly in such a case you need to specify `replay: false` in the Preboot options and replay the events yourself. That is, import `PrebootModule` like this:
156156

157157
```typescript
158158
PrebootModule.withConfig({
159159
appRoot: 'app-root',
160-
noReplay: true,
160+
replay: false,
161161
})
162162
```
163163

src/lib/api/event.recorder.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function start(prebootData: PrebootData, win?: PrebootWindow) {
8080

8181
const _document = <Document>(theWindow.document || {});
8282
const opts = prebootData.opts || ({} as PrebootOptions);
83-
const eventSelectors = opts.eventSelectors || [];
83+
let eventSelectors = opts.eventSelectors || [];
8484

8585
// create an overlay that can be used later if a freeze event occurs
8686
prebootData.overlay = createOverlay(_document);
@@ -97,6 +97,13 @@ export function start(prebootData: PrebootData, win?: PrebootWindow) {
9797
prebootData.apps.push(appData);
9898
}
9999

100+
eventSelectors = eventSelectors.map(eventSelector => {
101+
if (!eventSelector.hasOwnProperty('replay')) {
102+
eventSelector.replay = true;
103+
}
104+
return eventSelector;
105+
});
106+
100107
// loop through all the eventSelectors and create event handlers
101108
eventSelectors.forEach(eventSelector => handleEvents(prebootData, appData, eventSelector));
102109
});
@@ -287,7 +294,7 @@ export function createListenHandler(
287294

288295
// we will record events for later replay unless explicitly marked as
289296
// doNotReplay
290-
if (!eventSelector.noReplay) {
297+
if (eventSelector.replay) {
291298
appData.events.push({
292299
node,
293300
nodeKey,

src/lib/api/inline.preboot.code.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {PrebootOptions} from '../common/preboot.interfaces';
8+
import {EventSelector, PrebootOptions} from '../common/preboot.interfaces';
99
import {getNodeKeyForPreboot} from '../common/get-node-key';
1010

1111
import {
@@ -36,6 +36,7 @@ const eventRecorder = {
3636
export const defaultOptions = <PrebootOptions>{
3737
buffer: true,
3838
minify: true,
39+
replay: true,
3940

4041
// these are the default events are are listening for an transfering from
4142
// server view to client view
@@ -68,7 +69,7 @@ export const defaultOptions = <PrebootOptions>{
6869
{
6970
selector: 'input,textarea',
7071
events: ['focusin', 'focusout', 'mousedown', 'mouseup'],
71-
noReplay: true
72+
replay: false
7273
},
7374

7475
// user clicks on a button

src/lib/common/preboot.interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface EventSelector {
1313
preventDefault?: boolean; // will prevent default handlers if true
1414
freeze?: boolean; // if true, the UI will freeze when this event occurs
1515
action?: Function; // custom action to take with this event
16-
noReplay?: boolean; // if true, no replay will occur
16+
replay?: boolean; // if false, no replay will occur
1717
}
1818

1919
export interface ServerClientRoot {
@@ -30,7 +30,7 @@ export interface PrebootOptions {
3030
buffer?: boolean; // if true, attempt to buffer client rendering to hidden div
3131
eventSelectors?: EventSelector[]; // when any of these events occur, they are recorded
3232
appRoot: string | string[]; // define selectors for one or more server roots
33-
noReplay?: boolean;
33+
replay?: boolean;
3434
}
3535

3636
// our wrapper around DOM events in preboot

src/lib/module.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function prebootHook(doc: Document,
3535
eventReplayer: EventReplayer) {
3636
// necessary because of angular/angular/issues/14485
3737
const res = () => {
38+
3839
if (isPlatformServer(platformId)) {
3940
const inlineCode = getInlinePrebootCode(prebootOpts);
4041
const script = doc.createElement('script');
@@ -44,14 +45,17 @@ export function prebootHook(doc: Document,
4445
script.textContent = inlineCode;
4546
doc.head.appendChild(script);
4647
}
47-
if (isPlatformBrowser(platformId) && !prebootOpts.noReplay) {
48-
appRef.isStable
49-
.pipe(
50-
filter(stable => stable),
51-
take(1)
52-
).subscribe(() => {
48+
if (isPlatformBrowser(platformId)) {
49+
const replay = prebootOpts.replay != null ? prebootOpts.replay : true;
50+
if (replay) {
51+
appRef.isStable
52+
.pipe(
53+
filter(stable => stable),
54+
take(1)
55+
).subscribe(() => {
5356
eventReplayer.replayAll();
5457
});
58+
}
5559
}
5660
};
5761

0 commit comments

Comments
 (0)