Skip to content

Commit

Permalink
useRxInitialValues
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Aug 6, 2024
1 parent 5559b6b commit d35dc44
Show file tree
Hide file tree
Showing 9 changed files with 1,262 additions and 1,619 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-eagles-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

fix queueMicrotask usage in browsers
5 changes: 5 additions & 0 deletions .changeset/gorgeous-nails-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx-react": patch
---

add useRxInitialValues hook
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
"lint-fix": "pnpm lint --fix"
},
"devDependencies": {
"@babel/cli": "^7.24.5",
"@babel/core": "^7.24.5",
"@babel/plugin-transform-export-namespace-from": "^7.24.1",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/cli": "^7.24.8",
"@babel/core": "^7.25.2",
"@babel/plugin-transform-export-namespace-from": "^7.24.7",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@effect/build-utils": "^0.7.6",
"@changesets/cli": "^2.27.7",
"@effect/build-utils": "^0.7.7",
"@effect/docgen": "^0.4.3",
"@effect/eslint-plugin": "^0.1.2",
"@effect/eslint-plugin": "^0.2.0",
"@effect/language-service": "^0.1.0",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@vitest/coverage-v8": "^1.5.3",
"@typescript-eslint/eslint-plugin": "^8.0.1",
"@typescript-eslint/parser": "^8.0.1",
"@vitest/coverage-v8": "^2.0.5",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"eslint": "^8.57.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-codegen": "^0.28.0",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-sort-destructure-keys": "^2.0.0",
"fast-check": "^3.18.0",
"glob": "^10.3.12",
"madge": "^7.0.0",
"prettier": "^3.2.5",
"tsx": "^4.8.2",
"typescript": "^5.4.5",
"vitest": "^1.5.3"
"fast-check": "^3.20.0",
"glob": "^11.0.0",
"madge": "^8.0.0",
"prettier": "^3.3.3",
"tsx": "^4.16.5",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
}
}
4 changes: 2 additions & 2 deletions packages/rx-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"@types/react": "^18.3.1",
"@types/react": "^18.3.3",
"@types/scheduler": "^0.23.0",
"effect": "^3.1.1",
"effect": "^3.6.1",
"react": "^18.3.1",
"scheduler": "^0.23"
},
Expand Down
24 changes: 24 additions & 0 deletions packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ function useStore<A>(registry: Registry.Registry, rx: Rx.Rx<A>): A {
return React.useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot)
}

const initialValuesSet = globalValue(
"@effect-rx/rx-react/initialValuesSet",
() => new WeakMap<Registry.Registry, WeakSet<Rx.Rx<any>>>()
)

/**
* @since 1.0.0
* @category hooks
*/
export const useRxInitialValues = (initialValues: Iterable<readonly [Rx.Rx<any>, any]>): void => {
const registry = React.useContext(RegistryContext)
let set = initialValuesSet.get(registry)
if (set === undefined) {
set = new WeakSet()
initialValuesSet.set(registry, set)
}
for (const [rx, value] of initialValues) {
if (!set.has(rx)) {
set.add(rx)
;(registry as any).ensureNode(rx).setValue(value)
}
}
}

/**
* @since 1.0.0
* @category hooks
Expand Down
4 changes: 2 additions & 2 deletions packages/rx-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"effect": "^3.1.1",
"vue": "^3.4.26"
"effect": "^3.6.1",
"vue": "^3.4.35"
},
"peerDependencies": {
"effect": "^3.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/rx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"effect": "^3.1.1"
"effect": "^3.6.1"
},
"peerDependencies": {
"effect": "^3.0.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/rx/src/internal/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RegistryImpl implements Registry.Registry {

constructor(
initialValues?: Iterable<readonly [Rx.Rx<any>, any]>,
readonly scheduleTask = queueMicrotask,
readonly scheduleTask = (cb: () => void): void => queueMicrotask(cb),
timeoutResolution?: number,
readonly defaultIdleTTL?: number
) {
Expand Down
Loading

0 comments on commit d35dc44

Please sign in to comment.