-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<style> | ||
.block { | ||
margin: 25px; | ||
border-style: dotted; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="block"> | ||
Fragment 3 | ||
|
||
<div class="counter">Counter <span class="value">0</span></div> | ||
<button id="inc">+</button> | ||
<button id="dec">-</button> | ||
|
||
|
||
</div> | ||
|
||
<script> | ||
const { state } = parent; | ||
|
||
|
||
const COUNT_CHANGE = "countChange"; | ||
|
||
state.on(COUNT_CHANGE, ({ count }) => { | ||
document.querySelector("span.value").textContent = count; | ||
}); | ||
|
||
document.getElementById("inc").addEventListener("click", function () { | ||
state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count + 1 })); | ||
}); | ||
|
||
document.getElementById("dec").addEventListener("click", function () { | ||
state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count - 1 })); | ||
}); | ||
|
||
</script> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<style> | ||
.block { | ||
margin: 25px; | ||
border-style: dotted; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="block"> | ||
Fragment 4 | ||
|
||
<div class="counter">Counter <span class="value">0</span></div> | ||
<button id="inc">+</button> | ||
<button id="dec">-</button> | ||
|
||
|
||
</div> | ||
|
||
|
||
<script> | ||
const { state } = parent; | ||
|
||
state.init({ count: 0 }) | ||
|
||
const COUNT_CHANGE = "countChange"; | ||
|
||
state.on(COUNT_CHANGE, ({ count }) => { | ||
document.querySelector("span.value").textContent = count; | ||
}); | ||
|
||
document.getElementById("inc").addEventListener("click", function () { | ||
state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count + 1 })); | ||
}); | ||
|
||
document.getElementById("dec").addEventListener("click", function () { | ||
state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count - 1 })); | ||
}); | ||
|
||
</script> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import WebFragment from "./fragment.js" | ||
import State from "./state.js" | ||
|
||
|
||
if (!window.state) { | ||
window.state = new State; | ||
} | ||
|
||
customElements.define('web-merge', WebFragment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const deepFreeze = (o) => { | ||
Object.freeze(o); | ||
|
||
Object.keys(o).forEach(key => { | ||
if ( | ||
o.hasOwnProperty(key) && | ||
o[key] !== null && | ||
(typeof o[key] === "object" || typeof o[key] === "function") && | ||
!Object.isFrozen(o[key]) | ||
) { | ||
deepFreeze(o[key]); | ||
} | ||
}); | ||
|
||
return o; | ||
} | ||
|
||
|
||
export default class Stater { | ||
|
||
constructor(initialStore = {}) { | ||
this.store = initialStore; | ||
this.events = {} | ||
} | ||
|
||
init(initialStore) { | ||
this.store = { ...this.store, ...initialStore }; | ||
} | ||
|
||
dispatch(eventName, payload) { | ||
|
||
if (typeof payload == "function") payload = payload(this.store); | ||
|
||
if (Object.prototype.toString.call(payload) !== "[object Object]") { | ||
console.error("Payload should be an object"); | ||
return false; | ||
} | ||
|
||
if (!this.events.hasOwnProperty(eventName)) { | ||
console.error(`Event "${eventName}" does not exists`); | ||
return false; | ||
} | ||
|
||
this.store = { ...this.store, ...payload }; | ||
|
||
this.events[eventName].forEach(({ dep, cb }) => { | ||
if (dep.length == 0) cb(deepFreeze(this.store)); | ||
else { | ||
const t = {}; | ||
dep.forEach(k => { | ||
if (this.store.hasOwnProperty(k)) t[k] = store[k]; | ||
}); | ||
|
||
cb(deepFreeze(t)); | ||
} | ||
}); | ||
} | ||
|
||
on(eventName, cb, dep = []) { | ||
if (typeof cb !== "function") { | ||
console.error("on() method expects 2nd argument as a callback function"); | ||
return false; | ||
} | ||
|
||
if (Object.prototype.toString.call(dep) !== "[object Array]") { | ||
console.error("on() method expects 3nd argument as an array"); | ||
return false; | ||
} | ||
|
||
if (!this.events.hasOwnProperty(eventName)) this.events[eventName] = []; | ||
|
||
this.events[eventName].push({ dep, cb }); | ||
|
||
return true; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.