本文整理汇总了TypeScript中mobx-state-tree.flow函数的典型用法代码示例。如果您正苦于以下问题:TypeScript flow函数的具体用法?TypeScript flow怎么用?TypeScript flow使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了flow函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: flow
.actions(self => ({
loadTeams: flow(function* load() {
const backendSrv = getEnv(self).backendSrv;
const rsp = yield backendSrv.get('/api/teams/search/', { perpage: 50, page: 1 });
self.map.clear();
for (let team of rsp.teams) {
self.map.set(team.id.toString(), Team.create(team));
}
}),
setSearchQuery(query: string) {
self.search = query;
},
loadById: flow(function* load(id: string) {
if (self.map.has(id)) {
return;
}
const backendSrv = getEnv(self).backendSrv;
const team = yield backendSrv.get(`/api/teams/${id}`);
self.map.set(id, Team.create(team));
}),
}));
示例2: addMiddleware
.actions(self => {
addMiddleware(self, atomic)
return {
inc(x) {
self.z += x
return self.z
},
throwingFn(x) {
self.z += x
throw "Oops"
},
incProcess: flow(function*(x) {
yield delay(2)
self.z += x
yield delay(2)
self.z += x
return self.z
}),
throwingProcess: flow(function*(x) {
yield delay(2)
self.z += x
yield delay(2)
self.z += x
throw "Oops"
})
}
})
示例3: return
.actions(self => ({
addPartialMovie: (obj: any) => {
return (self as any).addMovie(obj, true);
},
addMovie: (obj: any, isPartial: boolean) => {
if (self.movies.has(obj.id)) {
const movie = self.movies.get(obj.id);
return movie!.update(obj, isPartial);
}
return self.movies.put(
Movie.create(mapMovie(obj, isPartial)),
);
},
loadMovieById: flow(function* loadMovieById(movieId: string) {
const start = new Date();
const end = addDays(start, 5);
const result = yield client.query({
query: fetchMovieByIdQuery,
fetchPolicy: 'network-only',
variables: {
movieId,
start: start.toISOString().substr(0, 10),
end: end.toISOString().substr(0, 10),
},
});
(self as any).addMovie(result.data.Movie, false);
}),
loadNewMovies: flow(function* loadNewMovies() {
const query = (fetchPolicy: FetchPolicy = 'network-only') => client.query({
fetchPolicy,
query: fetchNewMoviesQuery,
variables: {
from: new Date().toISOString().substr(0, 10),
},
});
let result = yield query();
const isNetworkError = result.error && result.error.message.includes('Network error');
if (isNetworkError) {
result = yield query('cache-only');
}
self.isOffline = isNetworkError;
self.isCache = isNetworkError && result.data;
if (!result.data) {
return;
}
result.data.allMovies.map(Movies.addPartialMovie);
self.comingSoon.clear();
self.comingSoon.push(
...result.data.allMovies.map((movie: { id: string }) => movie.id),
);
}),
}))
示例4: flow
.actions(self => ({
loadWeek: flow(function* loadWeek() {
self.loading = true;
const start = new Date();
const end = addDays(start, 5);
const query = (fetchPolicy: FetchPolicy = 'network-only') => new Promise((resolve) => {
client.query({
fetchPolicy,
query: fetchMoviesForWeekQuery,
variables: {
start: start.toISOString().substr(0, 10),
end: end.toISOString().substr(0, 10),
},
})
.then(resolve);
if (fetchPolicy === 'network-only') {
setTimeout(() => resolve({ error: { message: 'Network error' } }), 5000);
}
});
let result = yield query();
const isNetworkError = result.error && result.error.message.includes('Network error');
if (isNetworkError) {
result = yield query('cache-only');
}
self.isOffline = isNetworkError;
self.isCache = isNetworkError && result.data;
if (!result.data) {
return;
}
result.data.allMovies.map(Movies.addPartialMovie);
result.data.allMovies.forEach((movie: IMovieFromDb) => {
movie.showtimes.forEach((showtime) => {
const dateKey = addHours(showtime.playingAt, -4).toISOString().substr(0, 10);
if (!self.dates.has(dateKey)) {
self.dates.put({
date: dateKey,
movies: {},
});
}
const dateOf = self.dates.get(dateKey);
if (dateOf) {
dateOf.movies.put({ id: movie.id, movie: movie.id });
}
});
});
self.loaded = true;
self.loading = false;
}),
}))
示例5: update
.actions(self => ({
update() {
return flow(function* () {
self.updateMetadata = yield CodePush.getUpdateMetadata();
})();
},
}))
示例6: flow
.actions(self => ({
loadRules: flow(function* load(filters) {
const backendSrv = getEnv(self).backendSrv;
self.stateFilter = filters.state; // store state filter used in api query
const apiRules = yield backendSrv.get('/api/alerts', filters);
self.rules.clear();
for (let rule of apiRules) {
setStateFields(rule, rule.state);
if (rule.state !== 'paused') {
if (rule.executionError) {
rule.info = 'Execution Error: ' + rule.executionError;
}
if (rule.evalData && rule.evalData.noData) {
rule.info = 'Query returned no data';
}
}
self.rules.push(AlertRule.create(rule));
}
}),
setSearchQuery(query: string) {
self.search = query;
},
}));
示例7: flow
.actions(self => ({
setTitle: flow(function* setTitle(newTitle) {
self.helper() // should not be logged
yield self.helper2() // should be logged
self.title = newTitle
return
})
}))
示例8: flow
.actions(self => ({
load: flow(function* load(uid: string) {
// clear folder state
if (self.folder && self.folder.uid !== uid) {
self.folder = null;
}
const backendSrv = getEnv(self).backendSrv;
const res = yield backendSrv.getFolderByUid(uid);
self.folder = Folder.create({
id: res.id,
uid: res.uid,
title: res.title,
url: res.url,
canSave: res.canSave,
hasChanged: false,
version: res.version,
});
return res;
}),
setTitle: function(originalTitle: string, title: string) {
self.folder.title = title;
self.folder.hasChanged = originalTitle.toLowerCase() !== title.trim().toLowerCase() && title.trim().length > 0;
},
saveFolder: flow(function* saveFolder(options: any) {
const backendSrv = getEnv(self).backendSrv;
self.folder.title = self.folder.title.trim();
const res = yield backendSrv.updateFolder(self.folder, options);
self.folder.url = res.url;
self.folder.version = res.version;
return `${self.folder.url}/settings`;
}),
deleteFolder: flow(function* deleteFolder() {
const backendSrv = getEnv(self).backendSrv;
return backendSrv.deleteFolder(self.folder.uid);
}),
}));
示例9: flow
.actions(self => {
const fetchItems = flow(function*() {
const items: Array<typeof Item.Type> = yield api.getItems()
items.forEach(item => self._cache.set(item.id.toString(), item))
})
return {
fetchItems
}
})
示例10: flow
.actions((self) => {
const addCinema = (obj: any) => {
const cinema = { ...obj };
if (self.cinemas.has(cinema.id)) {
return;
}
return self.cinemas.put(
Cinema.create(cinema),
);
};
const loadAllCinemas = flow(function* () {
const result = yield client.query({
query: fetchAllCinemas,
fetchPolicy: 'network-only',
});
result.data.allCinemas.map(addCinema);
});
const loadCinemaById = flow(function* (cinemaId: string) {
const result = yield client.query({
query: fetchCinemaByIdQuery,
fetchPolicy: 'network-only',
variables: {
cinemaId,
},
});
addCinema(result.data.Cinema);
});
return {
loadAllCinemas,
loadCinemaById,
addCinema,
};
})