-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(activities): rewrite spheres model
- Loading branch information
Showing
13 changed files
with
145 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { useUnit } from 'effector-react'; | ||
import { useAtom } from '@reatom/npm-react'; | ||
|
||
import { activitySpheresModel } from '../model'; | ||
import { ActivitySpheres, activitySpheresModel } from '../models'; | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const useActivitySpheres = () => { | ||
return useUnit(activitySpheresModel.query); | ||
export interface UseActivitySpheresResult { | ||
readonly data: ActivitySpheres; | ||
readonly pending: boolean; | ||
} | ||
|
||
export const useActivitySpheres = (): UseActivitySpheresResult => { | ||
const model = activitySpheresModel.create(); | ||
|
||
const [data] = useAtom(model.spheresAtom); | ||
const [pending] = useAtom(model.pendingAtom); | ||
|
||
return { data, pending, }; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './actions'; | ||
export * from './spheres'; | ||
export * as activitiesInRoomModel from './activities-in-room'; | ||
export * as activitySpheresModel from './activity-spheres'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './types'; | ||
export * as activitySpheresModel from './model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { beforeEach, describe, expect, test } from 'vitest'; | ||
|
||
import { create } from './model'; | ||
import { ActivitySpheresModel } from './types'; | ||
|
||
import { TestCtx, spheres, createTestCtx, waitNextTick } from '~/test-utils'; | ||
|
||
describe('src/entities/activitites/models/spheres/model', () => { | ||
let ctx: TestCtx; | ||
let model: ActivitySpheresModel; | ||
|
||
const createModel = () => { | ||
model = create(); | ||
}; | ||
|
||
beforeEach(() => { | ||
ctx = createTestCtx(); | ||
}); | ||
|
||
test('should create signleton model', () => { | ||
createModel(); | ||
|
||
const anotherModel = create(); | ||
|
||
expect(model).toBe(anotherModel); | ||
}); | ||
|
||
test('should stale model only after last susbcriber unsubscribe', () => { | ||
createModel(); | ||
const anotherModel = create(); | ||
|
||
const track = ctx.subscribeTrack(model.spheresAtom); | ||
const anotherTrack = ctx.subscribeTrack(anotherModel.spheresAtom); | ||
|
||
expect(model).toBe(anotherModel); | ||
|
||
anotherTrack.unsubscribe(); | ||
|
||
expect(model).toBe(create()); | ||
|
||
track.unsubscribe(); | ||
|
||
expect(model).not.toBe(create()); | ||
}); | ||
|
||
test('should load all spheres', async () => { | ||
createModel(); | ||
|
||
const track = ctx.subscribeTrack(model.spheresAtom); | ||
|
||
await waitNextTick(); | ||
|
||
expect(track.lastInput()).toStrictEqual(spheres); | ||
|
||
track.unsubscribe(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
atom, | ||
onDisconnect, | ||
reatomResource, | ||
withCache, | ||
withDataAtom | ||
} from '@reatom/framework'; | ||
|
||
import { activitiesApi } from '@/shared/api'; | ||
import { | ||
constructName, | ||
createSingletonFactory, | ||
mapStandardResponse | ||
} from '@/shared/lib'; | ||
|
||
import { ActivitySpheres, ActivitySpheresModel } from './types'; | ||
|
||
const modelName = constructName('activitites', 'spheres'); | ||
|
||
export const create = createSingletonFactory( | ||
(): ActivitySpheresModel => { | ||
const getSpheres = reatomResource(async (ctx) => { | ||
return ctx.schedule(() => activitiesApi.getSpheres()); | ||
}, constructName(modelName, 'getSpheres')).pipe( | ||
withDataAtom([] as ActivitySpheres, mapStandardResponse), | ||
withCache() | ||
); | ||
|
||
const pendingAtom = atom( | ||
(ctx) => !!ctx.spy(getSpheres.pendingAtom), | ||
constructName(modelName, 'pendingAtom') | ||
); | ||
|
||
return { | ||
spheresAtom: getSpheres.dataAtom, | ||
pendingAtom, | ||
}; | ||
}, | ||
{ | ||
key: modelName, | ||
hooks: { | ||
staleOn: (result, stale) => onDisconnect(result.spheresAtom, stale), | ||
}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Atom } from '@reatom/framework'; | ||
import { Number, Record, Static, String } from 'runtypes'; | ||
|
||
export const activitySphereRT = Record({ | ||
id: Number, | ||
name: String, | ||
}).asReadonly(); | ||
|
||
export interface ActivitySphere extends Static<typeof activitySphereRT> {} | ||
export type ActivitySpheres = ActivitySphere[]; | ||
|
||
export interface ActivitySpheresModel { | ||
readonly spheresAtom: Atom<ActivitySpheres>; | ||
readonly pendingAtom: Atom<boolean>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters