-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Convert Context/Provider/Subscription to Typescript #1742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
211cdff
5e6c80d
286a131
5d71c44
1e49d3c
7865ed2
2966e30
088504b
420891d
e4eacc9
6a617e2
f1a4dbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,16 @@ import { getBatch } from './batch' | |
// well as nesting subscriptions of descendant components, so that we can ensure the | ||
// ancestor components re-render before descendants | ||
|
||
const nullListeners = { notify() {} } | ||
type Listener = { | ||
callback: () => void; | ||
next: Listener | null; | ||
prev: Listener | null; | ||
}; | ||
|
||
function createListenerCollection() { | ||
const batch = getBatch() | ||
let first = null | ||
let last = null | ||
let first: Listener | null = null | ||
let last: Listener | null = null | ||
|
||
return { | ||
clear() { | ||
|
@@ -37,7 +41,7 @@ function createListenerCollection() { | |
return listeners | ||
}, | ||
|
||
subscribe(callback) { | ||
subscribe(callback: () => void) { | ||
let isSubscribed = true | ||
|
||
let listener = (last = { | ||
|
@@ -57,6 +61,7 @@ function createListenerCollection() { | |
isSubscribed = false | ||
|
||
if (listener.next) { | ||
//@ts-expect-error -- listener.next is always null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because However when it gets to this line, it causes an error for potentially accessing this I'm not entirely sure what's going on here -- either Typescript is wrong that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sorted this by explicitly marking the variable as type |
||
listener.next.prev = listener.prev | ||
} else { | ||
last = listener.prev | ||
|
@@ -71,29 +76,35 @@ function createListenerCollection() { | |
} | ||
} | ||
|
||
type ListenerCollection = ReturnType<typeof createListenerCollection>; | ||
|
||
export default class Subscription { | ||
constructor(store, parentSub) { | ||
private store: any; | ||
private parentSub?: Subscription; | ||
private unsubscribe?: () => void; | ||
private listeners?: ListenerCollection; | ||
public onStateChange?: () => void; | ||
|
||
constructor(store: any, parentSub?: Subscription) { | ||
this.store = store | ||
this.parentSub = parentSub | ||
this.unsubscribe = null | ||
this.listeners = nullListeners | ||
this.unsubscribe = undefined | ||
this.listeners = undefined | ||
|
||
this.handleChangeWrapper = this.handleChangeWrapper.bind(this) | ||
} | ||
|
||
addNestedSub(listener) { | ||
addNestedSub(listener: () => void) { | ||
this.trySubscribe() | ||
return this.listeners.subscribe(listener) | ||
return this.listeners?.subscribe(listener) | ||
} | ||
|
||
notifyNestedSubs() { | ||
this.listeners.notify() | ||
this.listeners?.notify() | ||
} | ||
|
||
handleChangeWrapper() { | ||
if (this.onStateChange) { | ||
this.onStateChange() | ||
} | ||
this.onStateChange?.() | ||
} | ||
|
||
isSubscribed() { | ||
|
@@ -113,9 +124,9 @@ export default class Subscription { | |
tryUnsubscribe() { | ||
if (this.unsubscribe) { | ||
this.unsubscribe() | ||
this.unsubscribe = null | ||
this.listeners.clear() | ||
this.listeners = nullListeners | ||
this.unsubscribe = undefined | ||
this.listeners?.clear() | ||
this.listeners = undefined; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.