Skip to content

Commit

Permalink
will i break da rulez?
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzypants1989 committed Oct 16, 2023
1 parent 7ab2e61 commit 358584c
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 243 deletions.
321 changes: 135 additions & 186 deletions addMethods.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function addProxy(type, string, fixed = false) {
)
}

return addMethods(type, string, element, fixed)
return addMethods(type, string, element[1] ? element : element[0], fixed)
}

export function createQueue() {
Expand Down
4 changes: 3 additions & 1 deletion errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export let defaultErrorHandler = (error, context) => {
console.error(context, "\n", error)
typeof context === "string"
? console.error(`${context}\n`, error)
: console.error(context, error)
}

export function setErrorHandler(handler) {
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ declare module "jessquery" {
* });
* @example
* $('#news-item').fromJSON('/api/news-item', (element, json) => {
* { title, summary } = json;
* const { title, summary } = json;
*
* element.html(`<h1>${title}</h1>
* <p>${summary}</p>`);
Expand Down
24 changes: 11 additions & 13 deletions promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,34 @@ export function promisify(
) {
return (...args) => {
return new Promise((resolve, reject) => {
let hasSettled = false
let settled = false

const guardedResolve = (value) => {
if (!hasSettled) {
hasSettled = true
const Resolve = (value) => {
if (!settled) {
settled = true
resolve(value)
}
}

const guardedReject = (reason) => {
if (!hasSettled) {
hasSettled = true
const Reject = (reason) => {
if (!settled) {
settled = true
reject(reason)
}
}

setTimeout(() => {
if (!hasSettled) {
hasSettled = true
if (!settled) {
settled = true
resolve()
defaultErrorHandler(
new Error(
`Promisify Timeout (second argument): ${timeout}ms exceeded.`
),
new Error(`Timeout: ${timeout}ms exceeded.`),
meta
)
}
}, timeout)

fn(guardedResolve, guardedReject, ...args)
fn(Resolve, Reject, ...args)
})
}
}
73 changes: 73 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import http from "http"

// const server = http.createServer((req, res) => {
// // Allow requests from any origin
// res.setHeader("Access-Control-Allow-Origin", "*")

// // Set other CORS headers as needed
// res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
// res.setHeader("Access-Control-Allow-Headers", "Content-Type")

// // Respond with the data
// res.writeHead(200, { "Content-Type": "application/octet-stream" })

// res.write("Hello World\n")

// // Simulate streaming by sending chunks of data every 1 second
// const intervalId = setInterval(() => {
// res.write(`${new Date().toLocaleTimeString()}\n`)
// }, 1000)

// // End the stream and clear the interval after 5 seconds
// setTimeout(() => {
// clearInterval(intervalId)
// res.end()
// }, 5000)

// // Clear the interval if the client disconnects before 5 seconds
// res.on("close", () => clearInterval(intervalId))
// })

// server.listen(8080, () => {
// console.log("Server is running on port 8080")
// })

const server = http.createServer((req, res) => {
// Allow requests from any origin
res.setHeader("Access-Control-Allow-Origin", "*")

// Set other CORS headers as needed
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
res.setHeader("Access-Control-Allow-Headers", "Content-Type")

// Check if the client requested SSE
if (req.headers.accept && req.headers.accept === "text/event-stream") {
// Set headers for SSE
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
})

// Send an event every second
const intervalId = setInterval(() => {
res.write(
`data: The server time is: ${new Date().toLocaleTimeString()}\n\n`
)
}, 1000)

// Close the connection when the client disconnects
req.on("close", () => {
clearInterval(intervalId)
res.end()
})
} else {
// Handle other requests (e.g., API requests, static files, etc.)
res.writeHead(404)
res.end()
}
})

server.listen(8080, () => {
console.log("Server is running on port 8080")
})
57 changes: 57 additions & 0 deletions tests/body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test send Function</title>
</head>
<body>
<form id="myForm">
<input id="butt" type="text" name="title" value="My Title" />
<textarea name="body">This is the body of my post.</textarea>
<button id="submitButton">Submit</button>
</form>

<script type="module">
import { $, $$ } from "../index.js"

// submitButton.addEventListener("click", async () => {
// const url = "https://jsonplaceholder.typicode.com/posts" // JSONPlaceholder API
// const options = {
// method: "POST",
// json: true,
// oncomplete: () => {
// // Callback function to handle completion
// console.log("Request completed.")
// },
// onerror: (error) => {
// // Callback function to handle errors
// console.error("Request error:", error)
// },
// }

// try {
// const response = await send(url, formElement, options)
// console.log("Response:", response)
// } catch (error) {
// console.error("Error:", error)
// }
// })

$("#submitButton").on("click", async (event) => {
$("#submitButton").send({
event,
url: "https://jsonplaceholder.typicode.com/posts",
json: true,
oncomplete: (r) => {
console.log("Response:", r)
$("#submitButton").text(JSON.stringify(r))
},
onerror: (error) => {
console.error("Request error:", error)
},
})
})
</script>
</body>
</html>
6 changes: 3 additions & 3 deletions tests/createElement.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<script type="module">
import { $, $$, setErrorHandler } from "../index.js"

setErrorHandler((err) => {
alert(err)
})
// setErrorHandler((err) => {
// alert(err)
// })
$().moveTo("#testContainer", { position: "before" })
</script>
</body>
Expand Down
44 changes: 44 additions & 0 deletions tests/demo copy 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="container">
<h1>jQuery is for old people.</h1>
<div class="card">
<button id="counter" type="button"></button>
</div>
<h2 class="truth">This will disappear.</h2>
<h3 class="truth">I love you.</h3>
</div>
<input type="text" />
<div class="container" id="parentTwo">
<input id="myFavorite" type="text" />
<h4 class="truth">Why are you still reading this?</h4>
</div>
<script type="module">
import { $, $$ } from "../index.js"
let count = 0
const counter = $("#counter").text(count)
counter
.text(count)
.on("click", () => {
count++
counter.text(count)
})
.css("border", "2px solid black")
.css({
padding: "10px",
borderRadius: "5px",
cursor: "pointer",
})
.parent()
.css("border", "2px solid red")
.parent()
.css("border", "2px solid blue")
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions tests/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,26 @@ <h4 class="truth">Why are you still reading this?</h4>

const counter = $("#counter")

counter.set("hidden")

counter.text(count).on("click", () => {
count++
counter.text(count)
}).style.border = "2px solid black"

const truth = $$(".truth")

truth.data({
truth: true,
lie: false,
})

truth.data("other", "other")

const container = $(".container")

const card = $(".card")

const randomColor = () => {
const r = Math.floor(Math.random() * 256)
const g = Math.floor(Math.random() * 256)
Expand Down
7 changes: 6 additions & 1 deletion tests/fromHTML.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@

const body = $("body")

body.fromHTML("http://localhost:5500/tests/moveTo2.html", {
// body.fromHTML("http://localhost:5500/tests/moveTo2.html", {
// onComplete: () => body.attach("<h1>It worked!</h1>"),
// })

body.fromStream("http://localhost:8080", {
onComplete: () => body.attach("<h1>It worked!</h1>"),
sse: true,
})
</script>
</body>
Expand Down
5 changes: 3 additions & 2 deletions tests/fromJSON.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
</head>
<body>
<div id="testDiv"></div>
<div id="testDiv"></div>

<script type="module">
import { $, $$ } from "../index.js"

$("#testDiv").fromJSON(
$$("#testDiv").fromJSON(
"https://jsonplaceholder.typicode.com/todos/1",
(element, data) => {
element.text(`Title: ${data.title}`).addClass("updated")
element.html(`<h1>Title: ${data.title}</h1>`).addClass("updated")
},
{
fallback: "I'M A CUSTOM FALLBACK",
Expand Down
Loading

0 comments on commit 358584c

Please sign in to comment.