Skip to content

Commit

Permalink
Fix eslint, sherif, and knip
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins committed Feb 17, 2024
1 parent aa447aa commit 87dd588
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 188 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const config = {
],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/method-signature-style': 'error',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
Expand Down
9 changes: 9 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
"packages/angular-store": {
"ignoreDependencies": ["@angular/compiler-cli"]
},
"packages/store": {
"ignore": ["src/tests/derived.bench.ts"],
"ignoreDependencies": [
"@angular/core",
"@preact/signals",
"solid-js",
"vue"
]
},
"packages/vue-store": {
"ignoreDependencies": ["vue2", "vue2.7"]
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@
"react-dom": "^18.2.0",
"rimraf": "^5.0.5",
"sherif": "^0.7.0",
"solid-js": "^1.7.8",
"solid-js": "^1.8.14",
"typescript": "^5.2.2",
"typescript49": "npm:typescript@4.9",
"typescript50": "npm:typescript@5.0",
"typescript51": "npm:typescript@5.1",
"vite": "^5.1.0",
"vitest": "^1.2.2",
"vue": "^3.3.4"
"vue": "^3.4.15"
}
}
2 changes: 1 addition & 1 deletion packages/solid-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@tanstack/store": "workspace:*"
},
"devDependencies": {
"solid-js": "^1.7.8",
"solid-js": "^1.8.14",
"vite-plugin-solid": "^2.8.0"
}
}
22 changes: 11 additions & 11 deletions packages/store/src/derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ export type Deps = Array<Derived<any> | Store<any>>

export class Derived<TState> {
_store!: Store<TState>
rootStores: Set<Store<unknown>> = new Set()
rootStores = new Set<Store<unknown>>()
deps: Deps

// Functions representing the subscriptions. Call a function to cleanup
_subscriptions: Array<() => void> = []

// What store called the current update, if any
_whatStoreIsCurrentlyInUse: Store<unknown> | null = null;
_whatStoreIsCurrentlyInUse: Store<unknown> | null = null

constructor(deps: Deps, fn: () => TState, options?: DerivedOptions<TState>) {
this.deps = deps
this._store = new Store(fn(), {
onSubscribe: options?.onSubscribe?.bind(this) as never,
onUpdate: options?.onUpdate
onUpdate: options?.onUpdate,
})
/**
* This is here to solve the pyramid dependency problem where:
Expand All @@ -40,8 +40,8 @@ export class Derived<TState> {
*
* This is a record of stores, because derived stores are not able to write values to, but stores are
*/
const storeToDerived: Map<Store<unknown>, Set<Derived<unknown>>> = new Map()
const derivedToStore: Map<Derived<unknown>, Set<Store<unknown>>> = new Map()
const storeToDerived = new Map<Store<unknown>, Set<Derived<unknown>>>()
const derivedToStore = new Map<Derived<unknown>, Set<Store<unknown>>>()

const updateStoreToDerived = (
store: Store<unknown>,
Expand All @@ -67,12 +67,12 @@ export class Derived<TState> {
let __depsThatHaveWrittenThisTick: Deps = []

deps.forEach((dep) => {
const isDepAStore = dep instanceof Store;
const isDepAStore = dep instanceof Store
let relatedLinkedDerivedVals: null | Set<Derived<unknown>> = null

const unsub = dep.subscribe(() => {
const store = isDepAStore ? dep : dep._whatStoreIsCurrentlyInUse;
this._whatStoreIsCurrentlyInUse = store;
const store = isDepAStore ? dep : dep._whatStoreIsCurrentlyInUse
this._whatStoreIsCurrentlyInUse = store
if (store) {
relatedLinkedDerivedVals = storeToDerived.get(store) ?? null
}
Expand All @@ -86,7 +86,7 @@ export class Derived<TState> {
this._store.setState(fn)
// Cleanup the deps that have written this tick
__depsThatHaveWrittenThisTick = []
this._whatStoreIsCurrentlyInUse = null;
this._whatStoreIsCurrentlyInUse = null
return
}
})
Expand All @@ -96,7 +96,7 @@ export class Derived<TState> {
}

get state() {
return this._store.state;
return this._store.state
}

cleanup = () => {
Expand All @@ -108,6 +108,6 @@ export class Derived<TState> {
}

subscribe = (listener: Listener) => {
return this._store.subscribe(listener);
return this._store.subscribe(listener)
}
}
9 changes: 5 additions & 4 deletions packages/store/src/effect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Deps, Derived} from "./derived";
import { Derived } from './derived'
import type { Deps } from './derived'

export class Effect {
_derived: Derived<void>;
_derived: Derived<void>

constructor(items: Deps, effectFn: () => void) {
this._derived = new Derived(items, () => {}, {
onUpdate() {
effectFn();
effectFn()
},
});
})
}

cleanup() {
Expand Down
12 changes: 6 additions & 6 deletions packages/store/src/tests/derived.bench.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* istanbul ignore file -- @preserve */
import { Store } from '../store'
import { Derived } from '../derived'
import { describe, bench } from 'vitest'
import { computed as vueComputed, ref, watch } from 'vue'
import { bench, describe } from 'vitest'
import { ref, computed as vueComputed, watch } from 'vue'
import { createEffect, createMemo, createSignal } from 'solid-js'
import {
signal as preactSignal,
computed as preactComputed,
effect as preactEffect,
signal as preactSignal,
} from '@preact/signals'
import {
signal as angularSignal,
computed as angularComputed,
signal as angularSignal,
} from '@angular/core'
import { createWatch } from '@angular/core/primitives/signals'
import { Store } from '../store'
import { Derived } from '../derived'

function noop(val: any) {
val
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/tests/derived.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, expect, vi } from 'vitest'
import { Store } from '../store'
import { Derived } from '../derived'
import { afterEach, expect, vi } from 'vitest'

function viFnSubscribe(subscribable: Store<any> | Derived<any>) {
const fn = vi.fn()
Expand Down
12 changes: 6 additions & 6 deletions packages/store/src/tests/effect.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, vi } from 'vitest'
import { Store } from '../store'
import { Derived } from '../derived'
import { expect, vi } from 'vitest'
import {Effect} from "../effect";
import { Effect } from '../effect'

describe('Effect', () => {
test('Side effect free', () => {
Expand All @@ -19,8 +19,8 @@ describe('Effect', () => {
return halfCount.state + doubleCount.state
})

const fn = vi.fn();
new Effect([sumDoubleHalfCount], () => fn(sumDoubleHalfCount.state));
const fn = vi.fn()
new Effect([sumDoubleHalfCount], () => fn(sumDoubleHalfCount.state))

count.setState(() => 20)

Expand Down Expand Up @@ -50,8 +50,8 @@ describe('Effect', () => {
const f = new Derived([c], () => c.state)
const g = new Derived([d, e, f], () => d.state + e.state + f.state)

const fn = vi.fn();
new Effect([g], () => fn(g.state));
const fn = vi.fn()
new Effect([g], () => fn(g.state))

a.setState(() => 2)

Expand Down
2 changes: 1 addition & 1 deletion packages/vue-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"devDependencies": {
"@vue/composition-api": "^1.7.2",
"vue": "^3.3.4",
"vue": "^3.4.15",
"vue2": "npm:vue@2.6",
"vue2.7": "npm:vue@2.7",
"@vitejs/plugin-vue": "^5.0.2"
Expand Down
Loading

0 comments on commit 87dd588

Please sign in to comment.