本文整理汇总了TypeScript中streamline-runtime._.cast方法的典型用法代码示例。如果您正苦于以下问题:TypeScript _.cast方法的具体用法?TypeScript _.cast怎么用?TypeScript _.cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类streamline-runtime._
的用法示例。
在下文中一共展示了_.cast方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: proxyConnect
proxyConnect(_: _) {
const options = this._options;
if (options.isHttps) {
// TODO: Don't authenticate with ntlm, nodejs raises "Parse error" in return of connect with 407 -> HPE_INVALID_CONSTANT
return _.cast((callback: (err?: Error, resolved?: HttpClientRequest) => void) => {
const proxyOpt = {
host: options.proxy.host,
port: options.proxy.port,
method: 'CONNECT',
path: options.host + ":" + options.port,
headers: options.proxy.headers
};
var replied = false;
// open proxy socket
options.proxy.module.request(proxyOpt).on('connect', (res: never, socket: net.Socket, head: never) => {
options.socket = socket;
options.agent = false;
//
if (!replied) callback(undefined, new HttpClientRequest(options));
replied = true;
}).on('error', (err: Error) => {
if (!replied) callback(err);
replied = true;
}).end();
return this;
})(_);
} else {//
if (options.proxyAuthenticate) {
options.proxyAuthenticate(_, options);
}
return new HttpClientRequest(options);
}
}
示例2: _drain
_drain(_: _) {
_.cast((callback: (err?: Error) => void) => {
this._onDrain = (err) => {
this._onDrain = nop;
callback(err);
callback = nop;
};
})(_);
};
示例3:
this.writer = generic.writer((_: _, data?: Data) => {
if (this._error) throw new Error(this._error.message);
// node streams don't differentiate between null and undefined. So end in both cases
if (data != null) {
// if data is empty do nothing but it's not to be interpreted as end
if (!data.length) return this.writer;
if (typeof data === "string") data = Buffer.from(data, this._encoding || "utf8");
//
if (!this._emitter.write(data)) this._drain(_);
} else {
_.cast(this._emitter.end).call(this._emitter, _);
}
return this.writer;
});
示例4: dequeue
/// * `reader = group.dequeue()`
/// Dequeues values in the order in which they are delivered by the readers.
/// Returns a stream on which other operations may be chained.
dequeue() {
interface Result {
i: number;
e: any | undefined;
v: T | undefined;
next: () => void;
}
const results: Result[] = [];
var alive = this.readers.length;
var resume: ((err: any, val?: T) => void) | undefined;
this.readers.forEach((stream, i) => {
if (!stream) return;
const next = () => {
if (alive === 0) return;
_.run(_ => stream.read(_), (e, v) => {
if (!e && v === undefined) alive--;
if (e || v !== undefined || alive === 0) {
if (resume) {
var cb = resume;
resume = undefined;
cb(e, v);
next();
} else {
results.push({
i: i,
e: e,
v: v,
next: next,
});
}
}
});
};
next();
});
return new Reader(_.cast(function read(cb) {
if (alive <= 0) return cb(null), void 0;
const res = results.shift();
if (res) {
if (res.next) res.next();
return cb(res.e, res.v);
} else {
resume = cb;
}
}), undefined, this);
}
示例5: close
close(_: _) {
_.cast((callback: (err?: any) => void) => {
if (typeof callback !== 'function') throw new TypeError("bad callback parameter: " + typeof callback);
if (this._closed) return callback();
const close = this._emitter.end || this._emitter.close || this._emitter.destroySoon;
if (typeof close !== "function") return callback();
this._onClose = err => {
this._closed = true;
this._onClose = nop;
callback(err);
callback = nop;
};
if (this._doesNotEmitClose) {
this._emitter.emit("close");
}
close.call(this._emitter);
})(_);
}
示例6: listen
listen(_: _, ...args: any[]) {
return _.cast((callback: (err?: Error, result?: Server<EmitterT>) => void) => {
if (this._closed) throw new Error("cannot listen: server is closed");
var replied = false;
var reply = (err: Error | undefined, result?: Server<EmitterT>) => {
if (!replied) callback(err, result);
replied = true;
}
args.push(() => {
reply(undefined, this);
});
this._autoClosed.push(() => {
reply(new Error("server was closed unexpectedly"));
});
this._emitter.on('error', reply);
this._emitter.listen.apply(this._emitter, args);
})(_);
}
示例7: buffer
/// * `reader = reader.buffer(max)`
/// Returns a stream which is identical to the original one but in which up to `max` entries may have been buffered.
buffer(max: number) {
const parent = this;
const buffered: (T | undefined)[] = [];
var resume: ((err: any, val?: T) => void) | undefined;
var err: any;
var pending = false;
const fill = () => {
if (pending) return;
pending = true;
_.run(_ => parent.read(_), (e, v) => {
pending = false;
if (e) err = err || e;
else buffered.push(v);
if (resume) {
var cb = resume;
resume = undefined;
if (buffered.length > 0) {
v = buffered.shift();
fill();
cb(null, v);
} else {
cb(err)
}
} else if (buffered.length < max) {
if (!err && v !== undefined) fill();
}
});
}
fill();
return new Reader(_.cast(function read(cb) {
if (buffered.length > 0) {
var val = buffered.shift();
fill();
cb(null, val);
} else {
resume = cb;
}
}), undefined, parent);
}
示例8: response
/// * `response = request.response(_)`
/// returns the response.
response(_: _) {
var response = this._response || _.cast(this._responseCb).call(this, _);
return new HttpClientResponse(response, this._options); // options.reader?
}
示例9: bounceReader
export function create<T>(): Uturn<T> {
var state = 'idle', pendingData: T | undefined, error: any;
const id = ++lastId;
var pendingReaderCb: ((_: _, value: (T | undefined)) => void) | null;
function bounceReader(err?: any, val?: T) {
const lcb = pendingReaderCb;
pendingReaderCb = null;
if (lcb) lcb(err, val);
}
var pendingWriterCb: ((_: _, value?: Writer<T>) => void) | null;
function bounceWriter(err?: any, val?: Writer<T>) {
const lcb = pendingWriterCb;
pendingWriterCb = null;
if (lcb) lcb(err, val);
}
var pendingStopCb: ((_: _, value: any) => void) | null;
function bounceStop(err?: any, val?: any) {
const lcb = pendingStopCb;
pendingStopCb = null;
if (lcb) lcb(err, val);
}
const uturn = {
reader: new Reader(_.cast(function read(cb: (err?: any, val?: T) => void) {
nextTick(() => {
tracer && tracer(id, "READ", state, pendingData);
const st = state;
switch (st) {
case 'writing':
state = pendingData === undefined ? 'done' : 'idle';
// acknowledge the write
bounceWriter(null, uturn.writer);
// return the data posted by the write
cb(null, pendingData);
pendingData = undefined;
break;
case 'idle':
// remember it
state = 'reading';
pendingReaderCb = cb;
break;
case 'readStopping':
case 'writeStopping':
state = 'done';
const arg = stopException.unwrap(error);
// acknowledge the stop
bounceStop();
// return undefined or throw
cb(arg && arg !== true ? arg : null);
break;
case 'done':
cb(error);
break;
default:
state = 'done';
cb(error || new Error('invalid state ' + st));
break;
}
});
}), _.cast(function stop(cb: (err?: any, arg?: any) => void, arg?: any) {
nextTick(() => {
error = error || stopException.make(arg);
tracer && tracer(id, "STOP READER", state, arg);
const st = state;
switch (st) {
case 'reading':
state = 'done';
// send undefined or exception to read
bounceReader(arg && arg !== 1 ? arg : null);
// acknowledge the stop
cb();
break;
case 'writing':
state = 'done';
// send to write
bounceWriter(error, uturn.writer);
// acknowledge the stop
cb();
break;
case 'idle':
// remember it
state = 'readStopping';
pendingStopCb = cb;
break;
case 'done':
cb(error);
break;
default:
state = 'done';
cb(error || new Error('invalid state ' + st));
break;
}
});
})),
writer: new Writer(_.cast(function write(this: Writer<T>, cb: (err?: any, val?: Writer<T>) => void, data: T) {
nextTick(() => {
tracer && tracer(id, "WRITE", state, data);
//.........这里部分代码省略.........
示例10: join
/// * `reader = group.join(fn, thisObj)`
/// Combines the values read from the readers to produce a single value.
/// `fn` is called as `fn(_, values)` where `values` is the set of values produced by
/// all the readers that are still active.
/// `fn` returns the value which will be read from the joined stream. `fn` _must_ also reset to `undefined` the `values` entries
/// that it has consumed. The next `read(_)` on the joined stream will fetch these values.
/// Note that the length of the `values` array will decrease every time an input stream is exhausted.
/// Returns a stream on which other operations may be chained.
join(fn: (_: _, values: (T | undefined)[]) => T | undefined, thisObj?: any) {
thisObj = thisObj !== undefined ? thisObj : this;
var last = 0; // index of last value read by default fn
if (!fn) fn = ((_, values) => {
var i = last;
do {
i = (i + 1) % values.length;
var v = values[i];
if (v !== undefined) {
values[i] = undefined;
last = i;
return v;
}
} while (i !== last);
return undefined;
});
const values: T[] = [];
var active = this.readers.length;
var done = false;
var reply: ((err?: any, val?: T) => void) | undefined;
const joinerCb = (err: any, val: T) => {
if (err || val === undefined) {
done = true;
return reply && reply(err, val);
}
// be careful with re-entrancy
const rep = reply;
reply = undefined;
if (rep) rep(null, val);
};
const callbacks = this.readers.map((reader, i) => ((err: any, data: T) => {
if (active === 0) return reply && reply();
if (err) {
done = true;
return reply && reply(err);
}
values[i] = data;
if (data === undefined) {
this.readers[i] = null;
if (--active === 0) return reply && reply();
}
const vals = values.filter((val) => val !== undefined);
if (vals.length === active) {
fn.call(thisObj, joinerCb, values);
}
}));
const refill = () => {
var count = 0;
this.readers.forEach((rd, j) => {
if (rd && values[j] === undefined) {
count++;
_.run(_ => rd.read(_), callbacks[j]);
}
});
if (count === 0) throw new Error("bad joiner: must pick and reset at least one value");
}
return new Reader(_.cast(function read(cb) {
if (done) {
cb();
return;
}
reply = cb;
refill();
}), undefined, this);
}