Skip to content

Instantly share code, notes, and snippets.

@pwFoo
pwFoo / sw.js
Created July 25, 2021 13:53 — forked from felquis/sw.js
sw.js
/*
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
const WebSocket = require("ws");
const redis = require("redis");
const subscriber = redis.createClient({
url: "redis://localhost:6379"
});
const publisher = subscriber.duplicate();
const WS_CHANNEL = "ws:messages";
const mockedUsers = [
@pwFoo
pwFoo / Service_Workers_Messaging.md
Created October 22, 2023 12:55 — forked from Jonarod/Service_Workers_Messaging.md
Describes how to send messages between a page's main thread and a service worker thread

Page to ServiceWorker

// in page.html
navigator.serviceWorker.controller.postMessage({'hello':'world'});
// in sw.js
self.addEventListener('message', event => { 
@pwFoo
pwFoo / index.html
Created August 8, 2023 07:10 — forked from Dletta/index.html
Gun in Service Worker
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Concept Chat</title>
<link rel="stylesheet" href="index.css" >
</head>
<body>
@pwFoo
pwFoo / minimal_pubsub.js
Created July 31, 2023 06:33 — forked from sivagao/minimal_pubsub.js
minimal pubsub js
var pubsub = function(l, u, r, i) { // cool! 闭包并且初始化vars
return function(n, f) {
r = l[n] = l[n] || [], i = -1;
if (f && f.call) r.push(f);
else while (r[++i]) r[i].apply(u, arguments);
}
}({});
// subscribe to event
pubsub("eat_cookie", function() {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>capture microphone audio into buffer</title>
</head>
<body>
<audio id="player" controls></audio>
<input
type="text"
id="username"
@pwFoo
pwFoo / custom-element-minimal.js
Created October 21, 2022 18:20 — forked from drodsou/custom-element-minimal.js
minimal custom element - web component, webcomponent
/* non-shadow dom minimal custom element */
customElements.define("counter-ce", class extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<button>
${this.innerHTML} <span></span>
</button>
`;
this.update = ()=>{
@pwFoo
pwFoo / proxyftw.js
Created February 28, 2022 19:44 — forked from stelf/proxyftw.js
using ES6 proxies and async/await to dynamically, yet almost transparently connect to some remote data provider
let providerHandler = {
get: async(target, name) => {
console.log('load someting from remote...')
return new Promise( (res, rej) => {
setTimeout(() => res(42), 4200)
})
},
set: function (obj, prop, value) {
return new Promise((res, rej) => {
@pwFoo
pwFoo / jkq.js
Created February 27, 2022 19:00 — forked from jakerella/jkq.js
An ultra-light, jQuery-like micro-library for selecting DOM elements and manipulating them.
(function() {
'use strict';
/**
* Core method, similar to jQuery (only simpler)
*
* @param {String|HTMLElement} s The CSS selector to search for or HTML element to wrap with functionality
* @param {HTMLElement} root OPTIONAL An HTML element to start the element query from
* @return {Array} The collection of elements, wrapped with functionality (see API methods)
*/
@pwFoo
pwFoo / mo_vs.proxy.js
Created February 26, 2022 11:23 — forked from ebidel/mo_vs.proxy.js
MutationObserver vs. Proxy to detect .textContent changes
<!--
This demo shows two ways to detect changes to a DOM node `.textContent`, one
using a `MutationObserver` and the other using an ES2015 `Proxy`.
From testing, a `Proxy` appears to be 6-8x faster than using a MO in Chrome 50.
**Update**: removing the `Proxy` altogether speeds up the MO to be inline with the Proxy.
This has something to do with how the browser queues/prioritizes Proxies over MO.