GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — :sparkles: launch lifecycle · nullstack/nullstack@b52dd7b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • server
    • index.js
  • shared
    • generateTree.js
  • tests/src
    • FullStackLifecycle.njs
    • FullStackLifecycle.test.js
    • PersistentComponent.njs
    • PersistentComponent.test.js
  • types
    • Self.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎server/index.js‎

Copy file name to clipboardExpand all lines: server/index.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Nullstack {
4646
_self = {
4747
prerendered: true,
4848
initiated: false,
49-
hydrated: false
49+
hydrated: false,
50+
terminated: false,
5051
}
5152

5253
constructor(scope) {
Collapse file

‎shared/generateTree.js‎

Copy file name to clipboardExpand all lines: shared/generateTree.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ async function generateBranch(parent, node, depth, scope) {
5151
if (scope.memory) {
5252
memory = scope.memory[key];
5353
if (memory) {
54+
instance._self.prerendered = true;
5455
instance._self.initiated = true;
5556
Object.assign(instance, memory);
5657
delete scope.memory[key];
5758
}
5859
}
5960
let shouldHydrate = false;
61+
const shouldLaunch = !instance._self.prerendered || (instance._self.persistent && instance._self.terminated)
6062
if (instance._self.terminated) {
6163
shouldHydrate = true;
6264
instance._self.terminated = false;
@@ -69,6 +71,7 @@ async function generateBranch(parent, node, depth, scope) {
6971
if (scope.context.environment.server) {
7072
instance.initiate && await instance.initiate();
7173
instance._self.initiated = true;
74+
instance.launch && instance.launch();
7275
} else {
7376
scope.initiationQueue.push(instance);
7477
}
@@ -77,6 +80,7 @@ async function generateBranch(parent, node, depth, scope) {
7780
}
7881
if (scope.hydrationQueue) {
7982
if (shouldHydrate) {
83+
shouldLaunch && instance.launch && instance.launch();
8084
scope.hydrationQueue.push(instance);
8185
} else if (instance._self.initiated == true) {
8286
instance.update && instance.update();
Collapse file

‎tests/src/FullStackLifecycle.njs‎

Copy file name to clipboardExpand all lines: tests/src/FullStackLifecycle.njs
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class FullStackLifecycle extends Nullstack {
44

55
prepared = false;
66
initiated = false;
7+
launched = true;
78
hydrated = false;
89
updated = false;
910

@@ -15,17 +16,21 @@ class FullStackLifecycle extends Nullstack {
1516
this.initiated = true;
1617
}
1718

19+
launch() {
20+
this.launched = true
21+
}
22+
1823
hydrate() {
1924
this.hydrated = true;
2025
}
2126

2227
update() {
23-
if(!this.updated) {
28+
if (!this.updated) {
2429
this.updated = true;
2530
}
2631
}
27-
28-
async terminate({params}) {
32+
33+
async terminate({ params }) {
2934
params.terminated = true;
3035
}
3136

@@ -34,6 +39,7 @@ class FullStackLifecycle extends Nullstack {
3439
<div class="FullStackLifecycle">
3540
<div data-prepared={this.prepared} />
3641
<div data-initiated={this.initiated} />
42+
<div data-launched={this.launched} />
3743
<div data-hydrated={this.hydrated} />
3844
<div data-updated={this.updated} />
3945
<a href="/"> Terminate </a>
Collapse file

‎tests/src/FullStackLifecycle.test.js‎

Copy file name to clipboardExpand all lines: tests/src/FullStackLifecycle.test.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ describe('FullStackLifecycle', () => {
2929
expect(element).toBeTruthy();
3030
});
3131

32+
test('launch should run', async () => {
33+
await page.waitForSelector('[data-launched]');
34+
const element = await page.$('[data-launched]');
35+
expect(element).toBeTruthy();
36+
});
37+
3238
test('update should run', async () => {
3339
await page.waitForSelector('[data-updated]');
3440
const element = await page.$('[data-updated]');
Collapse file

‎tests/src/PersistentComponent.njs‎

Copy file name to clipboardExpand all lines: tests/src/PersistentComponent.njs
+20-2Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import Nullstack from 'nullstack';
33
class PersistentComponent extends Nullstack {
44

55
count = 0
6+
launchCount = 0
67

78
prepare() {
89
this.count = -1;
910
}
1011

11-
initiate() {
12+
async initiate() {
1213
this.count = -1;
1314
}
1415

16+
launch() {
17+
this.launchCount++
18+
}
19+
1520
async hydrate() {
1621
this.count++
1722
}
@@ -20,10 +25,23 @@ class PersistentComponent extends Nullstack {
2025
this.count++
2126
}
2227

28+
self({ self }) {
29+
return self
30+
}
31+
2332
render({ self, instances }) {
2433
const aCount = instances['PersistentComponent/0-0-33/persistent-component/a']?.count
34+
const aTerminated = instances['PersistentComponent/0-0-33/persistent-component/a']?.self?.()?.terminated
2535
return (
26-
<div data-count={this.count} data-key={self.key} data-a-count={aCount}>
36+
<div
37+
data-count={this.count}
38+
data-key={self.key}
39+
data-a-count={aCount}
40+
data-launch-count={this.launchCount}
41+
data-persistent={self.persistent}
42+
data-prerendered={self.prerendered}
43+
data-a-terminated={aTerminated}
44+
>
2745
<a href="/persistent-component/a"> a </a>
2846
<a href="/persistent-component/b"> b </a>
2947
</div>
Collapse file

‎tests/src/PersistentComponent.test.js‎

Copy file name to clipboardExpand all lines: tests/src/PersistentComponent.test.js
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ describe('PersistentComponent instantiated', () => {
2222
expect(element).toBeTruthy();
2323
});
2424

25+
test('persistent components self should have a key persistent', async () => {
26+
await page.waitForSelector('[data-persistent]');
27+
const element = await page.$('[data-persistent]');
28+
expect(element).toBeTruthy();
29+
});
30+
31+
test('persistent components should only launch once when prerendered', async () => {
32+
await page.waitForSelector('[data-launch-count="1"]');
33+
const element = await page.$('[data-launch-count="1"]');
34+
expect(element).toBeTruthy();
35+
});
36+
2537
});
2638

2739
describe('PersistentComponent terminated', () => {
@@ -48,6 +60,12 @@ describe('PersistentComponent terminated', () => {
4860
expect(element).toBeTruthy();
4961
});
5062

63+
test('persistent instanes self should have a terminated key when off dom', async () => {
64+
await page.waitForSelector('[data-a-terminated]');
65+
const element = await page.$('[data-a-terminated]');
66+
expect(element).toBeTruthy();
67+
});
68+
5169
});
5270

5371
describe('PersistentComponent reinstantiated', () => {
@@ -88,6 +106,24 @@ describe('PersistentComponent reinstantiated', () => {
88106
expect(element).toBeTruthy();
89107
});
90108

109+
test('persistent components should launch again when reinstantiated', async () => {
110+
await page.waitForSelector('[data-launch-count="2"]');
111+
const element = await page.$('[data-launch-count="2"]');
112+
expect(element).toBeTruthy();
113+
});
114+
115+
test('persistent components self should have a key persistent when reinstantiated', async () => {
116+
await page.waitForSelector('[data-persistent]');
117+
const element = await page.$('[data-persistent]');
118+
expect(element).toBeTruthy();
119+
});
120+
121+
test('persistent components self should have a key prerendered when prerendered then reinstantiated', async () => {
122+
await page.waitForSelector('[data-prerendered]');
123+
const element = await page.$('[data-prerendered]');
124+
expect(element).toBeTruthy();
125+
});
126+
91127
});
92128

93129
afterAll(async () => {
Collapse file

‎types/Self.d.ts‎

Copy file name to clipboardExpand all lines: types/Self.d.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export type NullstackSelf = {
44

55
hydrated: boolean,
66

7+
terminated: boolean,
8+
79
prerendered: boolean,
810

911
/**

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.