本文整理匯總了TypeScript中rxjs.Observer類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observer類的具體用法?TypeScript Observer怎麽用?TypeScript Observer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Observer類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: function
var observe = Observable.create(function(
obs: Observer<inquirer.Question<{ phone: string }>>
) {
obs.next({
type: "input",
name: "first_name",
message: "What's your first name"
});
obs.next({
type: "input",
name: "last_name",
message: "What's your last name",
default: function() {
return "Doe";
}
});
obs.next({
type: "input",
name: "phone",
message: "What's your phone number",
validate: function(value) {
var pass = value.match(
/^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i
);
if (pass) {
return true;
}
return "Please enter a valid phone number";
}
});
obs.complete();
});
示例2:
return Observable.create((observer: Observer<Array<IPeopleEntity>>) => {
let result: Array<IPeopleEntity> = [];
for (let i = 0; i < 10; i++) {
result.push({
Id: i.toString(),
Email: "marvio.bezerra@gmail.com",
Name: "Marvio Bezerra"
});
}
observer.next(result);
observer.complete();
});
示例3: Observable
return new Observable((obs : Observer<U>) => {
let hasErroredOrCompleted = false;
if (isPending) {
hasErroredOrCompleted = true;
obs.complete();
return undefined;
}
isPending = true;
func(...args)
.subscribe(
(i) => { obs.next(i); },
(e) => {
hasErroredOrCompleted = true;
isPending = false;
obs.error(e);
},
() => {
hasErroredOrCompleted = true;
isPending = false;
obs.complete();
}
);
return () => {
// handle unsubscription
if (!hasErroredOrCompleted) {
isPending = false;
}
};
});
示例4:
const onComplete = () => {
if (savedError !== null) {
observer.error(savedError);
} else {
observer.next(savedResult);
observer.complete();
}
};
示例5: return
return ({ err, response, isDisposed }: WritePacket) => {
if (err) {
return observer.error(err);
} else if (isDisposed) {
return observer.complete();
}
observer.next(response);
};
示例6: handleInvalidWorkstation
handleInvalidWorkstation(path: string): void {
if (path.startsWith(WS_MANAGE_PATH)) {
// user is navigating to the WS admin page.
this.observer.complete();
} else {
this.router.navigate([WS_MANAGE_PATH]);
this.observer.error(`Auth session linked to no
workstation or a workstation unknown to this browser`);
}
}
示例7: catch
.then((data) => {
try {
const json = JSON.parse(data);
observer.next(successResponse(url, json, 200));
observer.complete();
} catch (error) {
// Even though the response status was 2xx, this is still an error.
// The parse error contains the text of the body that failed to parse.
const errorResult = { error, text: data };
observer.error(errorResponse(url, errorResult, 200));
}
}, (err: Object) => {
示例8: setTimeout
setTimeout(() => {
observer.next([
{label: 'First', content: 'Content 1'},
{label: 'Second', content: 'Content 2'},
{label: 'Third', content: 'Content 3'},
]);
}, 1000);
示例9:
newRef.set(value, (err: Error) => {
if (err) {
observer.error({
type: `CREATE_FAILED_IN_${PREFIX}`,
payload: {
originalPayload: value,
error: err,
}
});
} else {
observer.next({
type: `CREATED_IN_${PREFIX}`,
payload: Object.assign({}, value, { firebaseKey: newRef.key() })
});
}
});
示例10:
const connectionHandler = (socket: Socket) => {
let os: Observable<IRxSocket>;
os = observableFromStream<Buffer>(socket).map(obsBuf => {
return {socket, buffer: obsBuf};
});
observer.next(os);
};