本文整理汇总了TypeScript中@riim/map-set-polyfill.Map类的典型用法代码示例。如果您正苦于以下问题:TypeScript Map类的具体用法?TypeScript Map怎么用?TypeScript Map使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Map类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: set
set(typeName: string, type: EventEmitter): this {
let types = this._types.get(typeName);
if (!types) {
types = new Map<any, EventEmitter>();
this._types.set(typeName, types);
}
types.set((type as any).id, type);
return this;
}
示例2: isObjectOrArray
push<T = any>(data: any): T {
if (Array.isArray(data)) {
let list = [] as Array<any>;
for (let i = 0, l = data.length; i < l; i++) {
let value = data[i];
list[i] = isObjectOrArray(value) ? this.push(value) : value;
}
return list as any;
}
let typeName = data.__type;
if (typeName) {
let typeConstructor = this._typeConstructors.get(typeName);
if (!typeConstructor) {
throw new TypeError(`Type "${typeName}" is not defined`);
}
let types = this._types.get(typeName);
if (!types) {
types = new Map<any, EventEmitter>();
this._types.set(typeName, types);
}
let id = data.id;
let type = types.get(id);
if (!type) {
type = new typeConstructor();
types.set(id, type);
}
for (let name in data) {
if (name != '__type') {
let value = data[name];
if (value && typeof value == 'object') {
if (Object.getPrototypeOf(value) === ObjectProto) {
value = this.push(value);
if (Object.getPrototypeOf(value) === ObjectProto) {
value = new ObservableMap(value);
}
} else if (Array.isArray(value)) {
value = new ObservableList(this.push(value) as Array<any>);
}
}
(type as any)[name] = value;
}
}
return type as any;
}
let dataCopy = {} as { [name: string]: any };
for (let name in data) {
let value = data[name];
dataCopy[name] = isObjectOrArray(value) ? this.push(value) : value;
}
return dataCopy as any;
}
示例3: if
_on(type: string, listener: TListener, context: any) {
let index = type.indexOf(':');
if (index != -1) {
let propName = type.slice(index + 1);
currentlySubscribing = true;
(this[propName + 'Cell'] || (this[propName], this[propName + 'Cell'])).on(
type.slice(0, index),
listener,
context
);
currentlySubscribing = false;
} else {
let events = this._events.get(type);
let evt = { listener, context };
if (!events) {
this._events.set(type, evt);
} else if (Array.isArray(events)) {
events.push(evt);
} else {
this._events.set(type, [events, evt]);
}
}
}
示例4: TypeError
emit(
evt:
| {
target?: EventEmitter;
type: string;
bubbles?: boolean;
defaultPrevented?: boolean;
propagationStopped?: boolean;
data?: {
[name: string]: any;
};
}
| string,
data?: { [name: string]: any }
): IEvent {
if (typeof evt == 'string') {
evt = {
target: this,
type: evt
};
} else if (!evt.target) {
evt.target = this;
} else if (evt.target != this) {
throw new TypeError('Event cannot be emitted on this object');
}
if (data) {
evt.data = data;
}
if (transactionLevel) {
let events = transactionEvents.get(this);
if (!events) {
events = Object.create(null) as { [type: string]: IEvent };
transactionEvents.set(this, events);
}
(evt.data || (evt.data = {})).prev = events[evt.type] || null;
events[evt.type] = evt as IEvent;
} else {
this.handleEvent(evt as IEvent);
}
return evt as IEvent;
}
示例5:
getAll<T extends EventEmitter = EventEmitter>(typeName: string): Array<T> {
let types = [] as Array<T>;
((this._types.get(typeName) || []) as Array<T>).forEach(type => {
types.push(type);
});
return types;
}
示例6:
getEvents(type?: string) {
let events: any;
if (type) {
events = this._events.get(type);
if (!events) {
return [];
}
return Array.isArray(events) ? events : [events];
}
events = Object.create(null);
this._events.forEach((typeEvents, type) => {
events[type] = Array.isArray(typeEvents) ? typeEvents : [typeEvents];
});
return events;
}