mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
217 lines
5.3 KiB
TypeScript
217 lines
5.3 KiB
TypeScript
import { EventListener, TraceWriter, ErrorHandler } from ".";
|
|
|
|
let enabled = false;
|
|
let _categories = {};
|
|
let _writers: Array<TraceWriter> = [];
|
|
let _eventListeners: Array<EventListener> = [];
|
|
let _errorHandler: ErrorHandler;
|
|
|
|
export function enable() {
|
|
enabled = true;
|
|
}
|
|
|
|
export function disable() {
|
|
enabled = false;
|
|
}
|
|
|
|
export function isEnabled() {
|
|
return enabled;
|
|
}
|
|
|
|
export function isCategorySet(category: string): boolean {
|
|
return category in _categories;
|
|
}
|
|
|
|
export function addWriter(writer: TraceWriter) {
|
|
_writers.push(writer);
|
|
}
|
|
|
|
export function removeWriter(writer: TraceWriter) {
|
|
let index = _writers.indexOf(writer);
|
|
if (index >= 0) {
|
|
_writers.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
export function clearWriters() {
|
|
if (_writers.length > 0) {
|
|
_writers.splice(0, _writers.length);
|
|
}
|
|
}
|
|
|
|
export function setCategories(categories: string) {
|
|
_categories = {};
|
|
addCategories(categories);
|
|
}
|
|
|
|
export function addCategories(categories: string) {
|
|
let split = categories.split(",");
|
|
for (let i = 0; i < split.length; i++) {
|
|
_categories[split[i].trim()] = true;
|
|
}
|
|
}
|
|
|
|
export function write(message: any, category: string, type?: number) {
|
|
// print error no matter what
|
|
let i;
|
|
if (type === messageType.error) {
|
|
for (i = 0; i < _writers.length; i++) {
|
|
_writers[i].write(message, category, type);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (!enabled) {
|
|
return;
|
|
}
|
|
|
|
if (!(category in _categories)) {
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < _writers.length; i++) {
|
|
_writers[i].write(message, category, type);
|
|
}
|
|
}
|
|
|
|
export function notifyEvent(object: Object, name: string, data?: any) {
|
|
if (!enabled) {
|
|
return;
|
|
}
|
|
|
|
let i,
|
|
listener: EventListener,
|
|
filters: Array<string>;
|
|
for (i = 0; i < _eventListeners.length; i++) {
|
|
listener = _eventListeners[i];
|
|
if (listener.filter) {
|
|
filters = listener.filter.split(",");
|
|
filters.forEach((value: string) => {
|
|
if (value.trim() === name) {
|
|
listener.on(object, name, data);
|
|
}
|
|
});
|
|
} else {
|
|
listener.on(object, name, data);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function addEventListener(listener: EventListener) {
|
|
_eventListeners.push(listener);
|
|
}
|
|
|
|
export function removeEventListener(listener: EventListener) {
|
|
const index = _eventListeners.indexOf(listener);
|
|
if (index >= 0) {
|
|
_eventListeners.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
export module messageType {
|
|
export const log = 0;
|
|
export const info = 1;
|
|
export const warn = 2;
|
|
export const error = 3;
|
|
}
|
|
|
|
export module categories {
|
|
export const VisualTreeEvents = "VisualTreeEvents";
|
|
export const Layout = "Layout";
|
|
export const Style = "Style";
|
|
export const ViewHierarchy = "ViewHierarchy";
|
|
export const NativeLifecycle = "NativeLifecycle";
|
|
export const Debug = "Debug";
|
|
export const Navigation = "Navigation";
|
|
export const Test = "Test";
|
|
export const Binding = "Binding";
|
|
export const BindingError = "BindingError";
|
|
export const Error = "Error";
|
|
export const Animation = "Animation";
|
|
export const Transition = "Transition";
|
|
export const Livesync = "Livesync";
|
|
export const ModuleNameResolver = "ModuleNameResolver";
|
|
|
|
export const separator = ",";
|
|
export const All = [
|
|
VisualTreeEvents, Layout, Style,
|
|
ViewHierarchy, NativeLifecycle,
|
|
Debug, Navigation, Test, Binding,
|
|
Error, Animation, Transition, Livesync,
|
|
ModuleNameResolver]
|
|
.join(separator);
|
|
|
|
export function concat(): string {
|
|
let result: string;
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
if (!result) {
|
|
result = arguments[i];
|
|
continue;
|
|
}
|
|
|
|
result = result.concat(separator, arguments[i]);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
class ConsoleWriter implements TraceWriter {
|
|
public write(message: any, category: string, type?: number) {
|
|
if (!console) {
|
|
return;
|
|
}
|
|
|
|
let msgType;
|
|
if (type === undefined) {
|
|
msgType = messageType.log;
|
|
} else {
|
|
msgType = type;
|
|
}
|
|
|
|
switch (msgType) {
|
|
case messageType.log:
|
|
console.log(category + ": " + message);
|
|
break;
|
|
case messageType.info:
|
|
console.info(category + ": " + message);
|
|
break;
|
|
case messageType.warn:
|
|
console.warn(category + ": " + message);
|
|
break;
|
|
case messageType.error:
|
|
console.error(category + ": " + message);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// register a ConsoleWriter by default
|
|
addWriter(new ConsoleWriter());
|
|
|
|
export class DefaultErrorHandler implements ErrorHandler {
|
|
handlerError(error) {
|
|
throw error;
|
|
}
|
|
}
|
|
setErrorHandler(new DefaultErrorHandler());
|
|
|
|
export function getErrorHandler(): ErrorHandler {
|
|
return _errorHandler;
|
|
}
|
|
|
|
export function setErrorHandler(handler: ErrorHandler) {
|
|
_errorHandler = handler;
|
|
}
|
|
export function error(error: string | Error) {
|
|
if (!_errorHandler) {
|
|
return;
|
|
}
|
|
|
|
if (typeof error === "string") {
|
|
error = new Error(error);
|
|
}
|
|
|
|
_errorHandler.handlerError(error);
|
|
}
|