baremetal.js 1.0.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/state.js CHANGED
@@ -1,3 +1,10 @@
1
+ /**
2
+ * baremetal.js v1.2.1
3
+ * A lightweight, dependency-free Vanilla JavaScript SPA engine prioritizing extreme performance, native browser features, and explicit lifecycle management.
4
+ * (c) 2026 dkydivyansh
5
+ * Released under the GPL-3.0 License
6
+ */
7
+
1
8
  class StateManager {
2
9
  constructor() {
3
10
  this.state = {};
@@ -5,27 +12,32 @@ class StateManager {
5
12
  this.eventBus = {};
6
13
  }
7
14
 
8
- // --- Reactive State ---
15
+ initPersistence() {
16
+ try {
17
+ const saved = sessionStorage.getItem('baremetal_state');
18
+ if (saved) {
19
+ const parsed = JSON.parse(saved);
20
+ for (const [key, value] of Object.entries(parsed)) {
21
+ this.state[key] = value;
22
+ }
23
+ }
24
+ } catch (e) {
25
+ console.warn('Failed to hydrate state', e);
26
+ }
27
+ }
9
28
 
10
- /**
11
- * Initialize a state property with a default value
12
- */
13
29
  init(key, defaultValue) {
14
30
  if (this.state[key] === undefined) {
15
31
  this.state[key] = defaultValue;
16
32
  }
17
33
  }
18
34
 
19
- /**
20
- * Subscribe to changes on a specific state key
21
- */
22
35
  subscribe(key, callback) {
23
36
  if (!this.listeners[key]) {
24
37
  this.listeners[key] = [];
25
38
  }
26
39
  this.listeners[key].push(callback);
27
-
28
- // Call immediately with current value if exists
40
+
29
41
  if (this.state[key] !== undefined) {
30
42
  callback(this.state[key]);
31
43
  }
@@ -33,33 +45,28 @@ class StateManager {
33
45
  return () => this.unsubscribe(key, callback);
34
46
  }
35
47
 
36
- /**
37
- * Unsubscribe from a state key
38
- */
39
48
  unsubscribe(key, callback) {
40
49
  if (!this.listeners[key]) return;
41
50
  this.listeners[key] = this.listeners[key].filter(cb => cb !== callback);
42
51
  }
43
52
 
44
- /**
45
- * Update a state value and trigger listeners
46
- */
47
53
  update(key, value) {
48
54
  this.state[key] = value;
49
55
  if (this.listeners[key]) {
50
56
  this.listeners[key].forEach(callback => callback(value));
51
57
  }
58
+
59
+ if (window.__baremetal_persist_state) {
60
+ try {
61
+ sessionStorage.setItem('baremetal_state', JSON.stringify(this.state));
62
+ } catch(e) {}
63
+ }
52
64
  }
53
65
 
54
- /**
55
- * Get current state value
56
- */
57
66
  get(key) {
58
67
  return this.state[key];
59
68
  }
60
69
 
61
- // --- Event Bus (Pub/Sub) for one-off actions ---
62
-
63
70
  on(event, callback) {
64
71
  if (!this.eventBus[event]) {
65
72
  this.eventBus[event] = [];
package/src/transition.js CHANGED
@@ -1,7 +1,13 @@
1
+ /**
2
+ * baremetal.js v1.2.1
3
+ * A lightweight, dependency-free Vanilla JavaScript SPA engine prioritizing extreme performance, native browser features, and explicit lifecycle management.
4
+ * (c) 2026 dkydivyansh
5
+ * Released under the GPL-3.0 License
6
+ */
7
+
1
8
  export function mount({ state }) {
2
9
  console.log('[Transition Module] Mounted');
3
10
 
4
- // Create Protected Root
5
11
  let root = document.getElementById('baremetal-transition-root');
6
12
  if (!root) {
7
13
  root = document.createElement('div');
@@ -9,20 +15,18 @@ export function mount({ state }) {
9
15
  document.body.appendChild(root);
10
16
  }
11
17
 
12
- // Create Progress Bar
13
18
  const progressBar = document.createElement('div');
14
19
  progressBar.style.position = 'fixed';
15
20
  progressBar.style.top = '0';
16
21
  progressBar.style.left = '0';
17
22
  progressBar.style.height = '3px';
18
- progressBar.style.backgroundColor = '#3498db'; // YouTube blue-ish
23
+ progressBar.style.backgroundColor = '#3498db';
19
24
  progressBar.style.width = '0%';
20
25
  progressBar.style.zIndex = '999999';
21
26
  progressBar.style.transition = 'width 0.2s ease-out, opacity 0.3s ease';
22
27
  progressBar.style.opacity = '0';
23
28
  progressBar.style.pointerEvents = 'none';
24
29
 
25
- // Create Fade Overlay
26
30
  const overlay = document.createElement('div');
27
31
  overlay.style.position = 'fixed';
28
32
  overlay.style.top = '0';
@@ -38,15 +42,14 @@ export function mount({ state }) {
38
42
  root.appendChild(overlay);
39
43
  root.appendChild(progressBar);
40
44
 
41
- // Subscriptions
42
45
  const unsubs = [];
43
46
 
44
47
  unsubs.push(state.on('ROUTE_START', () => {
45
- progressBar.style.backgroundColor = '#3498db'; // Reset to blue
48
+ progressBar.style.backgroundColor = '#3498db';
46
49
  progressBar.style.opacity = '1';
47
50
  progressBar.style.width = '5%';
48
-
49
- overlay.style.pointerEvents = 'all'; // block clicks during load
51
+
52
+ overlay.style.pointerEvents = 'all';
50
53
  overlay.style.opacity = '1';
51
54
  }));
52
55
 
@@ -58,13 +61,12 @@ export function mount({ state }) {
58
61
 
59
62
  unsubs.push(state.on('ROUTE_END', () => {
60
63
  progressBar.style.width = '100%';
61
-
64
+
62
65
  setTimeout(() => {
63
66
  progressBar.style.opacity = '0';
64
67
  overlay.style.opacity = '0';
65
68
  overlay.style.pointerEvents = 'none';
66
-
67
- // Reset after fade out
69
+
68
70
  setTimeout(() => {
69
71
  progressBar.style.width = '0%';
70
72
  }, 300);
@@ -72,7 +74,7 @@ export function mount({ state }) {
72
74
  }));
73
75
 
74
76
  unsubs.push(state.on('ROUTE_ERROR', () => {
75
- progressBar.style.backgroundColor = '#e74c3c'; // red
77
+ progressBar.style.backgroundColor = '#e74c3c';
76
78
  setTimeout(() => {
77
79
  progressBar.style.opacity = '0';
78
80
  overlay.style.opacity = '0';
@@ -0,0 +1,65 @@
1
+ /**
2
+ * baremetal.js v1.2.1
3
+ * A lightweight, dependency-free Vanilla JavaScript SPA engine prioritizing extreme performance, native browser features, and explicit lifecycle management.
4
+ * (c) 2026 dkydivyansh
5
+ * Released under the GPL-3.0 License
6
+ */
7
+
8
+ export class Virtualizer {
9
+ constructor(containerId, items, renderRow, itemHeight, visibleCount = 20) {
10
+ this.container = document.getElementById(containerId);
11
+ if (!this.container) return;
12
+
13
+ this.items = items;
14
+ this.renderRow = renderRow;
15
+ this.itemHeight = itemHeight;
16
+ this.visibleCount = visibleCount;
17
+
18
+ this.totalHeight = this.items.length * this.itemHeight;
19
+ this.container.style.overflowY = 'auto';
20
+ this.container.style.position = 'relative';
21
+
22
+ this.innerWrapper = document.createElement('div');
23
+ this.innerWrapper.style.height = `${this.totalHeight}px`;
24
+ this.innerWrapper.style.position = 'relative';
25
+ this.container.appendChild(this.innerWrapper);
26
+
27
+ this.startIndex = 0;
28
+ this.onScroll = this.onScroll.bind(this);
29
+ this.container.addEventListener('scroll', this.onScroll);
30
+ this.render();
31
+ }
32
+
33
+ onScroll() {
34
+ const scrollTop = this.container.scrollTop;
35
+ const startIndex = Math.max(0, Math.floor(scrollTop / this.itemHeight) - 2);
36
+ if (startIndex !== this.startIndex) {
37
+ this.startIndex = startIndex;
38
+ this.render();
39
+ }
40
+ }
41
+
42
+ render() {
43
+ this.innerWrapper.innerHTML = '';
44
+ const endIndex = Math.min(this.items.length - 1, this.startIndex + this.visibleCount + 4);
45
+
46
+ for (let i = this.startIndex; i <= endIndex; i++) {
47
+ const node = this.renderRow(this.items[i], i);
48
+ node.style.position = 'absolute';
49
+ node.style.top = `${i * this.itemHeight}px`;
50
+ node.style.width = '100%';
51
+ this.innerWrapper.appendChild(node);
52
+ }
53
+ }
54
+
55
+ destroy() {
56
+ if (this.container) {
57
+ this.container.removeEventListener('scroll', this.onScroll);
58
+ this.container.innerHTML = '';
59
+ }
60
+ }
61
+ }
62
+
63
+ export const virtualize = (containerId, items, renderRow, itemHeight, visibleCount) => {
64
+ return new Virtualizer(containerId, items, renderRow, itemHeight, visibleCount);
65
+ };
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
@@ -1,34 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
-
4
- name: Node.js Package
5
-
6
- on:
7
- workflow_dispatch:
8
- release:
9
- types: [created]
10
-
11
- jobs:
12
- build:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - uses: actions/checkout@v4
16
- - uses: actions/setup-node@v4
17
- with:
18
- node-version: 20
19
- - run: npm install
20
- - run: npm test
21
-
22
- publish-npm:
23
- needs: build
24
- runs-on: ubuntu-latest
25
- steps:
26
- - uses: actions/checkout@v4
27
- - uses: actions/setup-node@v4
28
- with:
29
- node-version: 20
30
- registry-url: https://registry.npmjs.org/
31
- - run: npm install
32
- - run: npm publish
33
- env:
34
- NODE_AUTH_TOKEN: ${{secrets.npm_token}}
@@ -1,122 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, religion, or sexual identity
10
- and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the
26
- overall community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or
31
- advances of any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email
35
- address, without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official e-mail address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- coc@dkydivyansh.com.
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series
86
- of actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or
93
- permanent ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within
113
- the community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.1, available at
119
- [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
-
121
- [homepage]: https://www.contributor-covenant.org
122
- [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
package/CONTRIBUTING.md DELETED
@@ -1,53 +0,0 @@
1
- # Contributing to BareMetal.js
2
-
3
- First off, thank you for considering contributing to BareMetal.js! It's people like you that make BareMetal.js such a great tool.
4
-
5
- ## How to Contribute
6
-
7
- ### Reporting Bugs
8
-
9
- If you find a bug in the source code, you can help us by submitting an issue to our GitHub Repository. Even better, you can submit a Pull Request with a fix.
10
-
11
- When reporting an issue, please include:
12
- - A quick summary of the issue
13
- - Steps to reproduce
14
- - Expected behavior vs actual behavior
15
- - Browser and OS version
16
-
17
- ### Suggesting Enhancements
18
-
19
- If you have an idea to improve BareMetal.js, we would love to hear it! Please open an issue to discuss your idea before implementing it to ensure it aligns with the project's goals.
20
-
21
- ### Pull Requests
22
-
23
- 1. Fork the repo and create your branch from `main`.
24
- 2. If you've added code that should be tested, add tests.
25
- 3. If you've changed APIs, update the documentation.
26
- 4. Ensure the test suite passes.
27
- 5. Issue that pull request!
28
-
29
- ## Code Style
30
-
31
- - We use ES modules (no build step for the core engine).
32
- - Keep dependencies to absolute zero.
33
- - Maintain a clean, minimalist approach. Performance is a key feature.
34
-
35
- ## Development Setup
36
-
37
- To run the project locally:
38
-
39
- 1. Clone your fork:
40
- ```bash
41
- git clone https://github.com/dkydivyansh/BareMetal.js.git
42
- ```
43
- 2. Navigate to the project directory:
44
- ```bash
45
- cd BareMetal.js
46
- ```
47
- 3. Start a local server. For example:
48
- ```bash
49
- npx serve .
50
- ```
51
- 4. Open `http://localhost:3000/demo/page1.html` in your browser.
52
-
53
- Thanks for contributing!
@@ -1,9 +0,0 @@
1
- export function mount({ state }) {
2
-
3
-
4
- return {
5
- destroy: () => {
6
-
7
- }
8
- };
9
- }
@@ -1,23 +0,0 @@
1
- export function mount() {
2
-
3
- let timer;
4
- let count = 0;
5
-
6
- const container = document.getElementById('page1-widget');
7
- if (container) {
8
- const display = document.createElement('p');
9
- display.innerText = `Active for 0 seconds`;
10
- container.appendChild(display);
11
-
12
- timer = setInterval(() => {
13
- count++;
14
- display.innerText = `Active for ${count} seconds`;
15
- }, 1000);
16
- }
17
-
18
- return {
19
- destroy: () => {
20
- clearInterval(timer);
21
- }
22
- };
23
- }
@@ -1,15 +0,0 @@
1
- export function mount({ state }) {
2
- const container = document.getElementById('page2-widget');
3
- if (container) {
4
- const btn = document.createElement('button');
5
- btn.innerText = "Click Me";
6
- btn.onclick = () => alert("Hello from Page 2 specific module!");
7
- container.appendChild(btn);
8
- }
9
-
10
- return {
11
- destroy: () => {
12
- // Cleanup if needed
13
- }
14
- };
15
- }
@@ -1,56 +0,0 @@
1
- export function mount({ state }) {
2
-
3
-
4
- // Initialize state if not exists
5
- state.init('transCount', 0);
6
-
7
- // Re-attach UI to state
8
- const btn = document.getElementById('btn-translate');
9
- const display = document.getElementById('trans-count');
10
-
11
- // We store the handler on the module level so we can remove it on destroy if needed
12
- // But wait, if this module is kept alive across page transitions where the DOM body is swapped,
13
- // the old DOM elements are removed and replaced by new identical ones from the fetch!
14
- // So a keep-alive module must re-bind its DOM elements whenever the DOM changes.
15
-
16
- // To handle the SPA HTML-swap paradigm:
17
- // When a module is kept alive, its `mount` is NOT called again.
18
- // Instead, the framework should either preserve the exact DOM elements during the swap,
19
- // OR the module needs an `update()` lifecycle method.
20
-
21
- // For this simple demo, let's assume the router replaces the whole body.
22
- // Thus, we need a way to re-bind. We can listen to a global event 'PAGE_CHANGED'.
23
-
24
- const bindUI = () => {
25
- const btn = document.getElementById('btn-translate');
26
- const display = document.getElementById('trans-count');
27
-
28
- if (btn) {
29
- // Avoid duplicate listeners
30
- btn.onclick = () => {
31
- const current = state.get('transCount');
32
- state.update('transCount', current + 1);
33
- };
34
- }
35
-
36
- // Subscribe to state to update UI
37
- // We use a unique listener to update whatever the current DOM element is
38
- state.subscribe('transCount', (val) => {
39
- const currentDisplay = document.getElementById('trans-count');
40
- if (currentDisplay) currentDisplay.innerText = val;
41
- });
42
- };
43
-
44
- bindUI();
45
-
46
- // Listen for route changes to re-bind UI since DOM body is swapped
47
- state.on('DOM_SWAPPED', bindUI);
48
-
49
- // Return destroy method
50
- return {
51
- destroy: () => {
52
-
53
- state.off('DOM_SWAPPED', bindUI);
54
- }
55
- };
56
- }
@@ -1,49 +0,0 @@
1
- export function mount({ state }) {
2
-
3
-
4
- // Initialize state (true = expanded, false = collapsed)
5
- state.init('sidebarExpanded', true);
6
-
7
- const bindUI = () => {
8
- const sidebar = document.getElementById('sidebar');
9
- const toggleBtn = document.getElementById('sidebar-toggle');
10
- const content = document.getElementById('main-content');
11
-
12
- if (!sidebar || !toggleBtn || !content) return;
13
-
14
- // Remove old listeners just in case
15
- toggleBtn.onclick = null;
16
-
17
- toggleBtn.onclick = () => {
18
- const isExpanded = state.get('sidebarExpanded');
19
- state.update('sidebarExpanded', !isExpanded);
20
- };
21
-
22
- // Make sure we update the DOM when the state changes
23
- state.subscribe('sidebarExpanded', (expanded) => {
24
- const el = document.getElementById('sidebar');
25
- const mainEl = document.getElementById('main-content');
26
- if (el && mainEl) {
27
- if (expanded) {
28
- el.style.width = '250px';
29
- mainEl.style.marginLeft = '250px';
30
- } else {
31
- el.style.width = '60px';
32
- mainEl.style.marginLeft = '60px';
33
- }
34
- }
35
- });
36
- };
37
-
38
- bindUI();
39
-
40
- // Rebind UI when DOM is swapped by Router
41
- state.on('DOM_SWAPPED', bindUI);
42
-
43
- return {
44
- destroy: () => {
45
-
46
- state.off('DOM_SWAPPED', bindUI);
47
- }
48
- };
49
- }
package/demo/main.js DELETED
@@ -1,18 +0,0 @@
1
- import { BareMetal } from '../src/index.js';
2
-
3
- // Initialize the BareMetal engine with our desired config
4
- BareMetal.init({
5
- debug: false,
6
- keepAliveSameModules: true,
7
- autoWrap: false,
8
- hoverPrefetch: false,
9
- showErrorNotification: false,
10
- transition: {
11
- enabled: true,
12
- simulatedDelay: 500 // Adds a half-second artificial delay to see the progress bar in local dev
13
- }
14
- });
15
-
16
- // For demonstration purposes, we will expose state to the window
17
- // so we can see the reactive updates in action via console if desired.
18
- window.appState = BareMetal.state;