Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
window.global = window;
window.__debug_react_done_tracker = true;
// window.__debug_react_done_tracker = true;
</script>
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,28 @@ import { ForkLeafDoneTracker } from "react-done-tracker";

In certain situations, it's useful to know when the children of a certain component have changed, e.g. when you want to screenshot those components after a change. On first load, you can wait for a `done` event. But when the children change in a non-async way, there will not be a second `done` event.

Because of that, you can trigger the `change` event if you want a parent component to know that the children have changed.
Previously, this library had a `change` event, but we now recommend to provide your own change emitters:

```tsx
// child
useEffect(() => {
doneTracker.signalChange();
}, [doneTracker, dep]);

// parent
useDoneTrackerSubscription(doneTracker, {
change: () => console.log("children have changed")
});
class ChangeEmitter {
private listeners: (() => void)[] = [];
emit = () => {
this.listeners.forEach((listener) => listener());
}
subscribe = (listener: () => void) => {
this.listeners.push(listener);
return () => {
this.listeners.splice(this.listeners.indexOf(listener), 1);
};
}
fork = (): ChangeEmitter => {
const emitter = new ChangeEmitter();
this.listeners.push(emitter.emit);
return emitter;
}
}
```

The `change` event is not part of the "core" of this library. It was added because it's commonly needed.

## Caveats

### Slow hooks
Expand Down
10 changes: 0 additions & 10 deletions src/base-done-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class BaseDoneTracker
abortListeners: DoneTrackerListener<"abort">[] = [];
errorListeners: DoneTrackerListener<"error">[] = [];
resetListeners: DoneTrackerListener<"reset">[] = [];
changeListeners: DoneTrackerListener<"change">[] = [];

addEventListener: DoneTracker["addEventListener"] = (event, listener) => {
if (event === "done") {
Expand All @@ -22,8 +21,6 @@ export class BaseDoneTracker
this.errorListeners.push(listener as any);
} else if (event === "reset") {
this.resetListeners.push(listener as any);
} else if (event === "change") {
this.changeListeners.push(listener as any);
}
};

Expand All @@ -48,11 +45,6 @@ export class BaseDoneTracker
this.resetListeners.indexOf(listener as any),
1
);
} else if (event === "change") {
this.changeListeners.splice(
this.changeListeners.indexOf(listener as any),
1
);
}
};

Expand All @@ -68,8 +60,6 @@ export class BaseDoneTracker
this.errorListeners.forEach((fn) => fn(arg as any));
} else if (event === "reset") {
this.resetListeners.forEach((fn) => fn(arg as any));
} else if (event === "change") {
this.changeListeners.forEach((fn) => fn(arg as any));
}
}
}
2 changes: 0 additions & 2 deletions src/components/ImperativeTrackDone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function ImperativeTrackDone({
onDone,
onError,
onPending,
onChange,
}: TrackComponentDoneProps<{
children: (doneTracker: NodeDoneTracker) => any;
forceRefreshRef?: MutableRefObject<(() => void) | null>;
Expand All @@ -29,7 +28,6 @@ export function ImperativeTrackDone({
done: onDone,
error: onError,
pending: onPending,
change: onChange,
});

return (
Expand Down
7 changes: 0 additions & 7 deletions src/components/TrackDone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function TrackDoneRoot({
onDone,
onError,
onPending,
onChange,
}: TrackComponentDoneProps<{
children: any;
forceRefreshRef?: MutableRefObject<(() => void) | null>;
Expand All @@ -30,7 +29,6 @@ export function TrackDoneRoot({
done: onDone,
error: onError,
pending: onPending,
change: onChange,
});

return (
Expand All @@ -46,7 +44,6 @@ export function ForkedTrackDone({
onDone,
onError,
onPending,
onChange,
forceRefreshRef,
}: TrackComponentDoneProps<{
name: string;
Expand All @@ -64,7 +61,6 @@ export function ForkedTrackDone({
done: onDone,
error: onError,
pending: onPending,
change: onChange,
});

return (
Expand All @@ -82,7 +78,6 @@ export function TrackDone({
onDone,
onError,
onPending,
onChange,
}: TrackComponentDoneProps<{
children: any;
forceRoot?: boolean;
Expand All @@ -99,7 +94,6 @@ export function TrackDone({
onDone={onDone}
onError={onError}
onPending={onPending}
onChange={onChange}
>
{children}
</ForkedTrackDone>
Expand All @@ -113,7 +107,6 @@ export function TrackDone({
onDone={onDone}
onError={onError}
onPending={onPending}
onChange={onChange}
>
{children}
</TrackDoneRoot>
Expand Down
2 changes: 0 additions & 2 deletions src/done-tracker-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export interface DoneTracker {
readonly erroredAt: number | null;
readonly pendingAt: number;

readonly preventChangePropagation: boolean;

addEventListener<K extends keyof DoneTrackerEventMap>(event: K, listener: DoneTrackerListener<K>): void;

removeEventListener<K extends keyof DoneTrackerEventMap>(event: K, listener: DoneTrackerListener<K>): void;
Expand Down
15 changes: 0 additions & 15 deletions src/leaf-done-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export class LeafDoneTracker extends BaseDoneTracker implements DoneTracker {
private _erroredAt: number | null = null;
private _pendingAt: number = performance.now();

public preventChangePropagation = false;

get id() {
return this._name ? `${this._id}:${this._name}` : this._id;
}
Expand Down Expand Up @@ -127,19 +125,6 @@ export class LeafDoneTracker extends BaseDoneTracker implements DoneTracker {
this.dispatchEvent("error", err, this);
};

signalChange = () => {
if (this.aborted) {
warn("Already aborted, can't signal change", this.id);
return;
}
if (!this.done) {
debug("Not done, not signaling change", this.id);
return;
}
log("🌀 Signaling changed", this.id, "after");
this.dispatchEvent("change");
};

reset = () => {
if (this.aborted) {
warn("Already aborted, can't repend", this.id);
Expand Down
11 changes: 0 additions & 11 deletions src/node-done-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export class NodeDoneTracker extends BaseDoneTracker implements DoneTracker {
private _erroredAt: number | null = null;
private _pendingAt: number = performance.now();

public preventChangePropagation = false;

get id() {
return this._name ? `${this._id}:${this._name}` : this._id;
}
Expand Down Expand Up @@ -131,15 +129,6 @@ export class NodeDoneTracker extends BaseDoneTracker implements DoneTracker {
if (!canReset) return;
this.reset();
});
if (!child.preventChangePropagation) {
child.addEventListener("change", () => {
debug("Child of", this.id, "changed");
if (!this.done) return;
// needs to be available for useDoneTrackerSubscription
this.dispatchEvent("change");
});
}

if (child.done) {
debug("Child was already done when added", child.id);
this.checkAndDispatchState();
Expand Down
1 change: 0 additions & 1 deletion src/stories/AsyncTree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/DelayedContainer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
};
Expand Down
1 change: 0 additions & 1 deletion src/stories/DelayedContainerAndChildren.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/EmptyNode.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

const Tree = () => {
Expand Down
1 change: 0 additions & 1 deletion src/stories/Image.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImmediatelyDone.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImmediatelyDoneWithChildren.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImmediatelyErroring.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const { actions, actionsMockClear } = createSpyableActions({
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
});

export default {
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeAsyncTree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeAsyncTree2.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeAsyncTree3.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeImage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeRecursiveTree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/ImperativeSimpleAsyncTree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/Intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const actions = {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
};

# React Done Tracker
Expand Down
1 change: 0 additions & 1 deletion src/stories/Intro.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const actions = {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
};

const VisualizedDelayedComponent = visualizeDoneWrapper(DelayedComponent);
Expand Down
1 change: 0 additions & 1 deletion src/stories/MixedUsage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/NestedTrackDone.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
1 change: 0 additions & 1 deletion src/stories/PropDelayer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default {
onAbort: action("abort"),
onError: action("error"),
onPending: action("pending"),
onChange: action("change"),
}),
],
} as Meta;
Expand Down
Loading