Skip to content

Instantly share code, notes, and snippets.

View nthung2112's full-sized avatar
🏠
Working from home

Hung Nguyen nthung2112

🏠
Working from home
View GitHub Profile
@nthung2112
nthung2112 / zustand-internals.jsx
Created April 22, 2024 14:57 — forked from arkatsy/zustand-internals.jsx
How zustand works internally
import { useSyncExternalStore } from "react";
// For more on the useSyncExternalStore hook, see https://react.dev/reference/react/useSyncExternalStore
// The code is almost identical to the source code of zustand, without types and some features stripped out.
// Check the links to see the references in the source code.
// The links are referencing the v5 of the library. If you plan on reading the source code yourself v5 is the best way to start.
// The current v4 version contains lot of deprecated code and extra stuff that makes it hard to reason about if you're new to this.
// https://github.com/pmndrs/zustand/blob/fe47d3e6c6671dbfb9856fda52cb5a3a855d97a6/src/vanilla.ts#L57-L94
function createStore(createState) {
function countShips(B) {
const result = [0, 0, 0];
const map = B.map(row=>row.split(''));
const n = map.length;
const m = map[0].length;
for (let row = 0; row < n; row++) {
for (let col = 0; col < m; col++) {
if (map[row][col] === '#') {
map[row][col] = '.';
@nthung2112
nthung2112 / index.html
Created July 28, 2023 17:43 — forked from dsheiko/index.html
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {
@nthung2112
nthung2112 / kodi-config.json
Created February 3, 2023 19:08
Kodi Plugin
[
{
"name": "Sport",
"logo": "https://i.imgur.com/LTdCdqy.png",
"version": 1,
"searchable": False,
"sites": [
{
"name": "90p.live",
"logo": "https://i.imgur.com/jyM3inb.png",
@nthung2112
nthung2112 / index.js
Created December 27, 2022 06:57
Log execute Class in Javascript
class Foo {
method1(p1, p2) {
this.p1 = p1;
this.p2 = p2;
}
method2(p3) {
this.p3 = p3;
}
}
@nthung2112
nthung2112 / pubsub.ts
Created September 29, 2022 00:39
Pub sub in browser
// @ts-ignore
import { v4 as uuid, validate as validateUUID } from "uuid";
type Topic = string;
type Message = Record<string, unknown>;
type ID = string;
type OnMessageFn = (message: Message) => void;
export type Subscribe = (topic: Topic, onMessage: OnMessageFn) => ID;
export type Publish = (topic: Topic, message: Record<string, unknown>) => void;
@nthung2112
nthung2112 / settings.json
Created September 12, 2022 02:38
VSCode settings
{
"prettier.printWidth": 100,
"prettier.singleQuote": true,
"emmet.includeLanguages": {
"postcss": "css",
"javascript": "javascriptreact"
},
"emmet.syntaxProfiles": {
"postcss": "css"
},
@nthung2112
nthung2112 / code.json
Created August 6, 2022 07:15
VSCode Sort import
// https://marketplace.visualstudio.com/items?itemName=dozerg.tsimportsorter
{
"tsImportSorter.configuration.groupRules": [
[
"^react-*",
{}
],
"^[@]",
"^pages",
"^components",
@nthung2112
nthung2112 / Router.js
Created May 21, 2022 04:22
React Routing in Plain React
/*
Implements React Routing in Plain React, without reliance
on React-Router or any other libraries.
To use:
In your top-level React Component (e.g. App or Index.js):
- Import Router (e.g.: import Router from './Router')
- Create a const with your routes and their associated component
- Create a const with the component to show on urls not routed (i.e. 404 page)
- Return a Router component as the top-level component of your App
Example:
@nthung2112
nthung2112 / invariant.js
Created March 30, 2022 17:31
Invariant ES6
const invariant = (condition, format, ...args) => {
if (!condition) {
if (!format) {
throw new Error('General error occured.');
}
let i = 0;
throw new Error(format.replace(/%s/g, () => args[i++]));
}