Skip to content

State dependencies #195

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions packages/core/src/__tests__/new-gen/deps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createSource } from "../../AsyncState";
import {expect} from "@jest/globals";

describe("instance deps", () => {
// test to shape the API of dependencies and how it should work
// dependencies are state instances that we will replay when the state changes
// the API would to allow deps gathering from
// - source creation via createSource
// - subscriptions via useAsync and useData

// The challenges:
// - some deps may be present from source and hooks, we should run them once
// - some deps may not be present at the moment of declaration, until invocation

// Open questions:
// - should we replay only, or support run too run via args (types!!)
// - should we always run or only when there is an active subscription

// Needed features:
// - run or not based on state and status
// - integrate with all existing features seamlessly
// - declare deps by: key or source
// - provide array of deps
it.skip("should automatically replay when state changes", () => {
let main = createSource<number>("main-1", null);
let spy = jest.fn().mockImplementation(({args}) => args[0]);
let dep1 = createSource<number, [number]>("dep-1", spy);
dep1.run(15);

expect(dep1.getState().data).toBe(15);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0].args).toEqual([15]);
spy.mockClear();

main.setData(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0].args).toEqual([15]);
});
it.skip("not run when condition is falsy", () => {

});
});