-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
1 parent
34943d3
commit 3ea4aa2
Showing
94 changed files
with
3,114 additions
and
525 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
/.shards/ | ||
.vscode | ||
node_modules | ||
coverage |
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
{ | ||
"license": "MIT", | ||
"private": true, | ||
"dependencies": { | ||
"@preact/signals": "^1.2.2", | ||
"@testing-library/preact": "^3.2.3", | ||
"@vitest/coverage-v8": "^1.1.1", | ||
"esbuild": "^0.19.8", | ||
"event-propagation-path": "^1.0.5", | ||
"fast-equals": "^5.0.1", | ||
"indent-string": "^5.0.0", | ||
"jsdom": "^23.0.1", | ||
"preact": "^10.19.2", | ||
"prettier": "^3.1.0", | ||
"route-parser": "mint-lang/mint-route-parser" | ||
"route-parser": "mint-lang/mint-route-parser", | ||
"uuid-random": "^1.3.2", | ||
"vitest": "^1.1.1" | ||
} | ||
} |
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
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,45 +1,45 @@ | ||
import { useEffect, useMemo } from "preact/hooks"; | ||
import { untracked } from "@preact/signals"; | ||
import { useEffect } from "preact/hooks"; | ||
import { compare } from "./equality"; | ||
import uuid from "uuid-random"; | ||
|
||
// This creates a function which is used for subscribing to a provider. We use | ||
// `untracked` to not to subscribe to any outside signals. | ||
export const createProvider = (subscriptions, update) => { | ||
// This is the subscription function. | ||
return (subscription) => { | ||
untracked(() => { | ||
subscriptions.value = [...subscriptions.value, subscription]; | ||
update(); | ||
}); | ||
return (owner, object) => { | ||
const unsubscribe = () => { | ||
if (subscriptions.has(owner)) { | ||
subscriptions.delete(owner); | ||
untracked(update); | ||
} | ||
} | ||
|
||
// Cleanup function. | ||
return () => { | ||
untracked(() => { | ||
subscriptions.value = subscriptions.value.filter( | ||
(item) => item != subscription, | ||
); | ||
update(); | ||
}); | ||
}; | ||
}; | ||
}; | ||
// This will only run when the component unmounts. | ||
useEffect(() => { | ||
return unsubscribe | ||
}, []); | ||
|
||
// A hook for using providers. On mount and every change we subscribe to the | ||
// given providers (if the condition is missing or true). The cleanup runs | ||
// before the resubscription so there won't be any invalid subscriptions. | ||
export const useProviders = (providers) => { | ||
useEffect(() => { | ||
const cleanups = providers.map((provider) => { | ||
if (provider.length === 1 || provider[1]) { | ||
return provider[0](); | ||
} | ||
}); | ||
// This runs on every update so we don't return a cleanup function. | ||
useEffect(() => { | ||
// If the object is null that means we need to unsubscribe. | ||
if (object === null) { | ||
unsubscribe(); | ||
} else { | ||
const current = subscriptions.get(owner); | ||
|
||
return () => { | ||
cleanups.forEach((item) => { | ||
if (item) { | ||
item(); | ||
if (!compare(current, object)) { | ||
subscriptions.set(owner, object); | ||
untracked(update); | ||
} | ||
}); | ||
}; | ||
}); | ||
} | ||
}) | ||
}; | ||
}; | ||
|
||
// Returns the subscriptions as an array. | ||
export const subscriptions = (items) => Array.from(items.values()); | ||
|
||
// Returns a unique ID for a component which doesn't change. | ||
export const useId = () => useMemo(uuid, []); | ||
|
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
Oops, something went wrong.