Skip to content
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

chore: upgrade effection to v4 #52

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 6 additions & 6 deletions action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export const ActionContext = createContext(

export function useActions(pattern: ActionPattern): Stream<AnyAction, void> {
return {
*subscribe() {
const actions = yield* ActionContext;
[Symbol.iterator]: function* () {
const actions = yield* ActionContext.expect();
const match = matcher(pattern);
yield* SignalQueueFactory.set(() => createFilterQueue(match) as any);
return yield* actions.subscribe();
return yield* actions;
},
};
}
Expand All @@ -49,7 +49,7 @@ export function emit({
}

export function* put(action: AnyAction | AnyAction[]) {
const signal = yield* ActionContext;
const signal = yield* ActionContext.expect();
return emit({
signal,
action,
Expand Down Expand Up @@ -106,7 +106,7 @@ export function* takeLeading<T>(
}

export function* waitFor(
predicate: Callable<boolean>,
predicate: Callable<Operation<boolean> | Promise<boolean> | boolean>,
) {
const init = yield* call(predicate as any);
if (init) {
Expand All @@ -115,7 +115,7 @@ export function* waitFor(

while (true) {
yield* take("*");
const result = yield* call(() => predicate as any);
const result = yield* call(predicate as any);
if (result) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function compose<Ctx extends BaseCtx = BaseCtx, T = unknown>(
// last called middleware #
let index = -1;

function* dispatch(i: number): Generator<Instruction, void, void> {
function* dispatch(i: number): Generator<Instruction, void, unknown> {
if (i <= index) {
throw new Error("next() called multiple times");
}
Expand Down
36 changes: 36 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type {
Stream,
Subscription,
Task,
} from "https://deno.land/x/effection@3.0.0-beta.3/mod.ts";
} from "https://deno.land/x/effection@4.0.0-alpha.3/mod.ts";
export {
action,
call,
Expand All @@ -35,7 +35,7 @@ export {
suspend,
useAbortSignal,
useScope,
} from "https://deno.land/x/effection@3.0.0-beta.3/mod.ts";
} from "https://deno.land/x/effection@4.0.0-alpha.3/mod.ts";

import React from "https://esm.sh/[email protected]?pin=v135";
export { React };
Expand Down
4 changes: 2 additions & 2 deletions fx/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { call, useAbortSignal } from "../deps.ts";

export function* request(url: string | URL | Request, opts?: RequestInit) {
const signal = yield* useAbortSignal();
const response = yield* call(fetch(url, { signal, ...opts }));
const response = yield* call(() => fetch(url, { signal, ...opts }));
return response;
}

export function* json(response: Response) {
const result = yield* call(response.json());
const result = yield* call(() => response.json());
return result;
}
2 changes: 1 addition & 1 deletion mdw/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function* request<CurCtx extends FetchCtx = FetchCtx>(

const { url, ...req } = ctx.req();
const request = new Request(url, req);
const result = yield* safe(fetch(request));
const result = yield* safe(() => fetch(request));
if (result.ok) {
ctx.response = result.value;
} else {
Expand Down
4 changes: 2 additions & 2 deletions npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ async function init() {
},
],
mappings: {
"https://deno.land/x/effection@3.0.0-beta.3/mod.ts": {
"https://deno.land/x/effection@4.0.0-alpha.3/mod.ts": {
name: "effection",
version: "3.0.0-beta.3",
version: "4.0.0-alpha.3",
},
"https://esm.sh/[email protected]?pin=v135": {
name: "react",
Expand Down
4 changes: 2 additions & 2 deletions query/thunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
}

function* register() {
storeId = yield* IdContext;
storeId = yield* IdContext.expect();
if (storeId && storeMap.has(storeId)) {
console.warn("This thunk instance is already registered.");
return;
}

signal = yield* ActionContext;
signal = yield* ActionContext.expect();
storeMap.set(storeId, signal);

yield* ensure(function* () {
Expand Down
2 changes: 1 addition & 1 deletion store/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function createBatchMdw<S extends AnyState>(
return function* batchMdw(_: UpdaterCtx<S>, next: Next) {
if (!notifying) {
notifying = true;
yield* action<void>(function* (resolve) {
yield* action<void>((resolve) => () => {
queue(() => {
notifying = false;
resolve();
Expand Down
6 changes: 3 additions & 3 deletions store/fx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getIdFromAction, take } from "../action.ts";
export function* updateStore<S extends AnyState>(
updater: StoreUpdater<S> | StoreUpdater<S>[],
): Operation<UpdaterCtx<S>> {
const store = yield* StoreContext;
const store = yield* StoreContext.expect();
// had to cast the store since StoreContext has a generic store type
const st = store as FxStore<S>;
const ctx = yield* st.update(updater);
Expand All @@ -26,7 +26,7 @@ export function* select<S, R, P>(
selectorFn: (s: S, p?: P) => R,
p?: P,
): Operation<R> {
const store = yield* StoreContext;
const store = yield* StoreContext.expect();
return selectorFn(store.getState() as S, p);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export function* waitForLoaders<M extends AnyState>(
actions: (ThunkAction | ActionFnWithPayload)[],
) {
const group = yield* parallel(
actions.map((action) => waitForLoader(loaders, action)),
actions.map((action) => () => waitForLoader(loaders, action)),
);
return yield* group;
}
Expand Down
2 changes: 1 addition & 1 deletion store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function createStore<S extends AnyState>({
}

function* notifyChannelMdw(_: UpdaterCtx<S>, next: Next) {
const chan = yield* StoreUpdateContext;
const chan = yield* StoreUpdateContext.expect();
yield* chan.send();
yield* next();
}
Expand Down
12 changes: 7 additions & 5 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ it(tests, "POST", async () => {

store.dispatch(createUser({ email: mockUser.email }));

await store.run(waitFor(function* (): Operation<boolean> {
const res = yield* select((state: AnyState) => state.users["1"].id);
return res !== "";
}));
await store.run(() =>
waitFor(function* (): Operation<boolean> {
const res = yield* select((state: AnyState) => state.users["1"].id);
return res !== "";
})
);

expect(store.getState().users).toEqual({
"1": { id: "1", name: "test", email: "[email protected]" },
Expand Down Expand Up @@ -297,7 +299,7 @@ it(tests, "with hash key on a large post", async () => {
const action = createUserDefaultKey({ email, largetext });
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const s = store.getState();
const expectedKey = createKey(action.payload.name, {
Expand Down
12 changes: 6 additions & 6 deletions test/batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ it(batch, "should batch notify subscribers based on mdw", async () => {
});
await store.run(function* () {
const group: any = yield* parallel([
schema.update(schema.cache.add({ "1": "one" })),
schema.update(schema.cache.add({ "2": "two" })),
schema.update(schema.cache.add({ "3": "three" })),
schema.update(schema.cache.add({ "4": "four" })),
schema.update(schema.cache.add({ "5": "five" })),
schema.update(schema.cache.add({ "6": "six" })),
() => schema.update(schema.cache.add({ "1": "one" })),
() => schema.update(schema.cache.add({ "2": "two" })),
() => schema.update(schema.cache.add({ "3": "three" })),
() => schema.update(schema.cache.add({ "4": "four" })),
() => schema.update(schema.cache.add({ "5": "five" })),
() => schema.update(schema.cache.add({ "6": "six" })),
]);
yield* group;
// make sure it will still notify subscribers after batched round
Expand Down
26 changes: 14 additions & 12 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ it(
const action = fetchUsers();
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual(mockUser);
Expand Down Expand Up @@ -106,7 +106,7 @@ it(
const action = fetchUsers();
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const data = "this is some text";
expect(actual).toEqual({ ok: true, value: data });
Expand Down Expand Up @@ -142,7 +142,7 @@ it(tests, "error handling", async () => {
const action = fetchUsers();
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual(errMsg);
Expand Down Expand Up @@ -181,7 +181,7 @@ it(tests, "status 204", async () => {
const action = fetchUsers();
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual({});
Expand Down Expand Up @@ -220,7 +220,7 @@ it(tests, "malformed json", async () => {
const action = fetchUsers();
store.dispatch(action);

await store.run(waitForLoader(schema.loaders, action));
await store.run(() => waitForLoader(schema.loaders, action));

const data = {
message: "Unexpected token 'o', \"not json\" is not valid JSON",
Expand Down Expand Up @@ -261,7 +261,7 @@ it(tests, "POST", async () => {
const action = fetchUsers();
store.dispatch(action);

const loader = await store.run(waitForLoader(schema.loaders, action));
const loader = await store.run(() => waitForLoader(schema.loaders, action));
if (!loader.ok) {
throw loader.error;
}
Expand Down Expand Up @@ -324,7 +324,7 @@ it(tests, "POST multiple endpoints with same uri", async () => {
store.dispatch(action2);

const results = await store.run(
waitForLoaders(schema.loaders, [action1, action2]),
() => waitForLoaders(schema.loaders, [action1, action2]),
);
if (!results.ok) {
throw results.error;
Expand Down Expand Up @@ -443,7 +443,7 @@ it(
const action = fetchUsers();
store.dispatch(action);

const loader = await store.run(waitForLoader(schema.loaders, action));
const loader = await store.run(() => waitForLoader(schema.loaders, action));
if (!loader.ok) {
throw loader.error;
}
Expand Down Expand Up @@ -484,7 +484,7 @@ it(
const action = fetchUsers();
store.dispatch(action);

const loader = await store.run(waitForLoader(schema.loaders, action));
const loader = await store.run(() => waitForLoader(schema.loaders, action));
if (!loader.ok) {
throw loader.error;
}
Expand Down Expand Up @@ -515,7 +515,9 @@ it(
store.run(api.bootup);
store.dispatch(fetchUsers());

const loader = await store.run(waitForLoader(schema.loaders, fetchUsers));
const loader = await store.run(() =>
waitForLoader(schema.loaders, fetchUsers)
);
if (!loader.ok) {
throw loader.error;
}
Expand Down Expand Up @@ -545,7 +547,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
const dynamicUser = { id: "2", email: "[email protected]" };
fetchUsers.use(mdw.response(new Response(JSON.stringify(dynamicUser))));
store.dispatch(fetchUsers());
let loader = await store.run(waitForLoader(schema.loaders, fetchUsers));
let loader = await store.run(() => waitForLoader(schema.loaders, fetchUsers));
if (!loader.ok) {
throw loader.error;
}
Expand All @@ -554,7 +556,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
// reset dynamic mdw and try again
api.reset();
store.dispatch(fetchUsers());
loader = await store.run(waitForLoader(schema.loaders, fetchUsers));
loader = await store.run(() => waitForLoader(schema.loaders, fetchUsers));
if (!loader.ok) {
throw loader.error;
}
Expand Down
6 changes: 3 additions & 3 deletions test/mdw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ it(tests, "createApi with own key", async () => {

store.dispatch(createUserCustomKey({ email: newUEmail }));

await store.run(waitForLoader(schema.loaders, createUserCustomKey));
await store.run(() => waitForLoader(schema.loaders, createUserCustomKey));

const expectedKey = theTestKey
? `/users [POST]|${theTestKey}`
Expand Down Expand Up @@ -469,7 +469,7 @@ it(tests, "createApi with custom key but no payload", async () => {
store.run(query.bootup);
store.dispatch(getUsers());

await store.run(waitForLoader(schema.loaders, getUsers));
await store.run(() => waitForLoader(schema.loaders, getUsers));

const expectedKey = theTestKey
? `/users [GET]|${theTestKey}`
Expand Down Expand Up @@ -571,7 +571,7 @@ it(tests, "stub predicate", async () => {
store.run(api.bootup);
store.dispatch(fetchUsers());

await store.run(waitFor(() => actual.ok));
await store.run(() => waitFor(() => actual.ok));

expect(actual).toEqual({
ok: true,
Expand Down
2 changes: 1 addition & 1 deletion test/put.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it(putTests, "should send actions through channel", async () => {
const actual: string[] = [];

function* genFn(arg: string) {
const actions = yield* ActionContext;
const actions = yield* ActionContext.expect();
const task = yield* spawn(function* () {
for (const action of yield* each(actions)) {
actual.push(action.type);
Expand Down
Loading
Loading