Skip to content

Commit

Permalink
Feature/hooks (#15) - Added Hooks
Browse files Browse the repository at this point in the history
Added hooks into the caching system for use with other systems
  • Loading branch information
mikevalstar authored Sep 6, 2023
1 parent 77f24d0 commit 71a5df7
Show file tree
Hide file tree
Showing 20 changed files with 1,450 additions and 1,330 deletions.
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120,
"bracketSameLine": true
}
46 changes: 25 additions & 21 deletions clients/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { useState, useEffect } from "react";
import Gorgon, {GorgonPolicyInput} from '@gorgonjs/gorgon';
import { useState, useEffect } from 'react';
import Gorgon, { GorgonPolicyInput } from '@gorgonjs/gorgon';

export type UseGorgonOptions = {
debug?: boolean;
};

const defaultOptions = {
debug: false
debug: false,
} as UseGorgonOptions;

// Wraps the Gorgon get method with a React hook
export const useGorgon = <R>(key: string, asyncFunc: () => Promise<R>, policy?: GorgonPolicyInput, options?:UseGorgonOptions) => {
export const useGorgon = <R>(
key: string,
asyncFunc: () => Promise<R>,
policy?: GorgonPolicyInput,
options?: UseGorgonOptions,
) => {
const [data, setData] = useState<null | R>(null);
const [error, setError] = useState<null | Error>(null);
const [loading, setLoading] = useState(false);
Expand All @@ -24,39 +29,38 @@ export const useGorgon = <R>(key: string, asyncFunc: () => Promise<R>, policy?:

Gorgon.get(key, asyncFunc, policy)
.then(async (returnedData) => {
if(isStillMounted){
if(opts.debug) console.info('Gorgon returned the data from the function', returnedData);
if (isStillMounted) {
if (opts.debug) console.info('Gorgon returned the data from the function', returnedData);
setData(await returnedData);
setError(null);
setLoading(false);
}
}).catch(
(err) => {
if(opts.debug) console.error('Gorgon error', err);
if(isStillMounted){
setError(err);
setData(null);
setLoading(false);
}
})
.catch((err) => {
if (opts.debug) console.error('Gorgon error', err);
if (isStillMounted) {
setError(err);
setData(null);
setLoading(false);
}
);
});

return () => {
isStillMounted = false;
}
};
}, [key, refetchCount]);

const refetch = ({clearKey}:{clearKey?: string} = {}) => {
const refetch = ({ clearKey }: { clearKey?: string } = {}) => {
Gorgon.clear(clearKey || key);
setRefetchCount(refetchCount + 1);
}
};

return {data, error, loading, refetch};
}
return { data, error, loading, refetch };
};

// helper function to clear the cache
export const clearGorgon = (key?: string) => {
Gorgon.clear(key);
}
};

export { Gorgon };
4 changes: 2 additions & 2 deletions clients/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gorgonjs/react",
"version": "1.4.0",
"version": "1.5.0",
"description": "A simple React caching library for async functions",
"homepage": "https://gorgonjs.dev",
"main": "dist/index.umd.js",
Expand Down Expand Up @@ -52,6 +52,6 @@
"react": "^18.2.0"
},
"dependencies": {
"@gorgonjs/gorgon": "^1.4.0"
"@gorgonjs/gorgon": "^1.5.0"
}
}
8 changes: 4 additions & 4 deletions gorgonjs.dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"dev": "astro dev",
"start": "astro dev",
"build": "astro build; npx -y pagefind --source dist",
"build:preview" : "astro build; npx -y pagefind --source dist --serve",
"build:preview": "astro build; npx -y pagefind --source dist --serve",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/prism": "1.0.2",
"astro": "^1.6.7",
"@astrojs/prism": "3.0.0",
"astro": "^3.0.7",
"prismjs": "^1.29.0"
},
"devDependencies": {
"@astrojs/sitemap": "^1.0.0",
"@astrojs/sitemap": "^3.0.0",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
"eslint-plugin-astro": "^0.21.0",
Expand Down
1 change: 1 addition & 0 deletions gorgonjs.dev/src/components/DocsNav.astro
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const { selected } = Astro.props;
<ul>
<li class={selected === 'custom-storage' ? 'selected' : ''}><a href="/docs/custom-storage">Custom Storage Providers</a></li>
<li class={selected === 'retry' ? 'selected' : ''}><a href="/docs/retry">Retry Thresholds</a></li>
<li class={selected === 'hooks' ? 'selected' : ''}><a href="/docs/hooks">Hooks</a></li>
</ul>
</li>
</ul>
30 changes: 30 additions & 0 deletions gorgonjs.dev/src/pages/docs/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: ../../layouts/Docs.astro
title: Hooks
selectedNav: hooks
---

# Hooks

If you would like to hook into the caching process you can do so by adding hooks to tge Gorgon instance.

```javascript
Gorgon.addHook('clear', (input, output) => {
console.log('Clearing cache', input);
});
```

You can use the following hooks:

- `settings`
- `addProvider`
- `put`
- `clear`
- `clearAll`
- `overwrite`
- `get`
- `valueError`

## Notes

You can add multiple hooks to a single event. They will be executed in the order they were added.
2 changes: 1 addition & 1 deletion gorgonjs.dev/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ console.log(todo3);`,

<Layout title="Welcome to Gorgon.js" selectedNav={'content1'}>
<main>
<h1>Gorgon.js <em>Version 1.4.0</em></h1>
<h1>Gorgon.js <em>Version 1.5.0</em></h1>
<h2>Faster Frontends, Faster servers, Less database calls. <a href="/docs/simplified">Simple</a>.</h2>
<p>A 3.83kb (1.3kb gzipped) caching library for your application.</p>

Expand Down
90 changes: 5 additions & 85 deletions library/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,71 +1,29 @@
{
"rules": {
"accessor-pairs": 0,
"array-bracket-spacing": 0,
"arrow-body-style": 0,
"arrow-parens": ["error", "as-needed"],
"arrow-spacing": 0,
"block-scoped-var": 0,
"block-spacing": 0,
"brace-style": [
2,
"1tbs",
{
"allowSingleLine": true
}
],
"callback-return": 0,
"camelcase": 2,
"comma-dangle": [
1,
"always-multiline"
],
"comma-spacing": 0,
"comma-style": 0,
"complexity": 0,
"computed-property-spacing": 0,
"consistent-return": 0,
"consistent-this": 0,
"constructor-super": 0,
"curly": [
2,
"all"
],
"curly": [2, "all"],
"default-case": 2,
"dot-location": [
2,
"property"
],
"dot-location": [2, "property"],
"dot-notation": 0,
"eol-last": 0,
"eqeqeq": 2,
"func-names": 0,
"func-style": 0,
"generator-star-spacing": 0,
"global-require": 0,
"guard-for-in": 2,
"handle-callback-err": 0,
"id-length": 0,
"id-match": 0,
"indent": 0,
"indent-legacy": [
2,
2,
{"SwitchCase": 1}
],
"indent-legacy": [2, 2, { "SwitchCase": 1 }],
"init-declarations": 0,
"key-spacing": [
2,
{
"beforeColon": false,
"afterColon": true
}
],
"linebreak-style": [
2,
"unix"
],
"lines-around-comment": 0,
"max-depth": 0,
"max-len": [
2,
Expand All @@ -87,7 +45,6 @@
}
],
"new-parens": 0,
"newline-after-var": 2,
"no-alert": 0,
"no-array-constructor": 2,
"no-arrow-condition": 0,
Expand Down Expand Up @@ -127,13 +84,9 @@
"no-implicit-coercion": 0,
"no-implied-eval": 0,
"no-inline-comments": 0,
"no-inner-declarations": [
2,
"functions"
],
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 0,
"no-invalid-this": 0,
"no-irregular-whitespace": 0,
"no-iterator": 0,
"no-label-var": 0,
"no-labels": 0,
Expand All @@ -142,7 +95,6 @@
"no-loop-func": 2,
"no-magic-numbers": 0,
"no-mixed-requires": 0,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 0,
"no-multi-str": 2,
"no-multiple-empty-lines": 0,
Expand Down Expand Up @@ -174,13 +126,11 @@
"no-sequences": 0,
"no-shadow": 0,
"no-shadow-restricted-names": 0,
"no-spaced-func": 2,
"no-sparse-arrays": 0,
"no-sync": 0,
"no-ternary": 0,
"no-this-before-super": 0,
"no-throw-literal": 0,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 0,
"no-undefined": 0,
Expand All @@ -197,17 +147,9 @@
"no-void": 0,
"no-warning-comments": 0,
"no-with": 2,
"object-curly-spacing": [
2,
"never"
],
"object-shorthand": 0,
"one-var": 0,
"operator-assignment": 0,
"operator-linebreak": [
2,
"after"
],
"padded-blocks": 0,
"prefer-arrow-callback": 0,
"prefer-const": 1,
Expand All @@ -225,30 +167,8 @@
"radix": 0,
"require-jsdoc": 0,
"require-yield": 0,
"semi-spacing": 0,
"sort-vars": 0,
"space-after-keywords": 0,
"space-before-blocks": [
2,
"always"
],
"space-before-function-paren": [
2,
"never"
],
"space-before-keywords": 0,
"space-in-parens": [
2,
"never"
],
"space-infix-ops": 2,
"space-return-throw-case": 0,
"space-unary-ops": 0,
"spaced-comment": 0,
"strict": [
2,
"global"
],
"strict": [2, "global"],
"use-isnan": 0,
"valid-jsdoc": 2,
"valid-typeof": 0,
Expand Down
4 changes: 2 additions & 2 deletions library/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Gorgon
![coverage](https://img.shields.io/badge/coverage-97%25-brightgreen)
![size](https://img.shields.io/badge/size-4.03KB-brightgreen)
![version](https://img.shields.io/badge/version-1.4.0-blue)
![size](https://img.shields.io/badge/size-5.67KB-brightgreen)
![version](https://img.shields.io/badge/version-1.5.0-blue)
![license](https://img.shields.io/badge/license-MIT-blue)

A typescript async based caching library for node or the browser.
Expand Down
Loading

0 comments on commit 71a5df7

Please sign in to comment.