mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat: Scoped Packages (#7911)
* 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
This commit is contained in:
committed by
GitHub
parent
6c7139477e
commit
cc97a16800
77
nativescript-core/trace/Readme.md
Normal file
77
nativescript-core/trace/Readme.md
Normal file
@@ -0,0 +1,77 @@
|
||||
The trace module is used to track code execution and to receive notifications for various events.
|
||||
It is disabled by default and to enable it you will need to call its enable() method.
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
trace.enable();
|
||||
```
|
||||
|
||||
The module writes its output through a collection of TraceWriter objects. By default there is one ConsoleWriter instance added to the module. There is a filtering functionality which is implemented by the `setCategories(string)` method. The method argument is a string containing one or more comma-delimited categories: `"Layout,VisualTreeEvents"`.
|
||||
|
||||
How to specify category(s):
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
// only the Layout messages are traced
|
||||
trace.setCategories(traceCategories.Layout);
|
||||
```
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
// set Layout + VisualTreeEvents categories
|
||||
trace.setCategories(traceCategories.concat(traceCategories.Layout, traceCategories.VisualTreeEvents));
|
||||
```
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
// trace everything
|
||||
trace.setCategories(traceCategories.All);
|
||||
```
|
||||
|
||||
How to trace:
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
traceWrite("tracing message", traceCategories.Layout);
|
||||
```
|
||||
|
||||
The module also supports events notifications through the EventListener interface. You may call the `trace.notifyEvent` method and all registered listeners will receive a notification for the event.
|
||||
|
||||
How to create and register a listener:
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
|
||||
class Listener implements trace.EventListener {
|
||||
public filter: string;
|
||||
public receivedEvents: Array<{ sender: Object; name: string }> = [];
|
||||
|
||||
constructor(filter: string) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
public on(object: Object, name: string) {
|
||||
this.receivedEvents.push({ sender: object, name: name });
|
||||
}
|
||||
|
||||
public reset() {
|
||||
this.receivedEvents = [];
|
||||
}
|
||||
}
|
||||
|
||||
// listen only for the _onAttached event
|
||||
var listener = new Listener("_onAttached");
|
||||
|
||||
// register the listener
|
||||
trace.addEventListener(listener);
|
||||
```
|
||||
|
||||
How to raise events:
|
||||
|
||||
```js
|
||||
import * as trace from "trace";
|
||||
import * as view from "ui/core/view";
|
||||
|
||||
var newView = new view.View();
|
||||
trace.notifyEvent(newView, "_viewCreated");
|
||||
```
|
||||
6
nativescript-core/trace/package.json
Normal file
6
nativescript-core/trace/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "trace",
|
||||
"main": "trace",
|
||||
"types": "trace.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
146
nativescript-core/trace/trace.d.ts
vendored
Normal file
146
nativescript-core/trace/trace.d.ts
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Allows you to trace and print specific information based on categories.
|
||||
* @module "trace"
|
||||
*/ /** */
|
||||
|
||||
/**
|
||||
* Enables the trace module.
|
||||
*/
|
||||
export function enable(): void;
|
||||
|
||||
/**
|
||||
* Disables the trace module.
|
||||
*/
|
||||
export function disable(): void;
|
||||
|
||||
/**
|
||||
* A function that returns whether the tracer is enabled and there is a point in writing messages.
|
||||
* Check this to avoid writing complex string templates.
|
||||
* Send error messages even if tracing is disabled.
|
||||
*/
|
||||
export function isEnabled(): boolean;
|
||||
|
||||
/**
|
||||
* Adds a TraceWriter instance to the trace module.
|
||||
* @param writer The TraceWriter instance to add.
|
||||
*/
|
||||
export function addWriter(writer: TraceWriter);
|
||||
|
||||
/**
|
||||
* Removes a TraceWriter instance from the trace module.
|
||||
* @param writer The TraceWriter instance to remove.
|
||||
*/
|
||||
export function removeWriter(writer: TraceWriter);
|
||||
|
||||
/**
|
||||
* Clears all the writers from the trace module.
|
||||
*/
|
||||
export function clearWriters();
|
||||
|
||||
/**
|
||||
* Sets the categories the module will trace.
|
||||
* @param categories The comma-separated list of categories. If not specified all messages from all categories will be traced.
|
||||
*/
|
||||
export function setCategories(categories: string);
|
||||
|
||||
/**
|
||||
* Adds categories to existing categories the module will trace.
|
||||
* @param categories The comma-separated list of categories. If not specified all messages from all categories will be traced.
|
||||
*/
|
||||
export function addCategories(categories: string);
|
||||
|
||||
/**
|
||||
* Check if category is already set in trace module.
|
||||
* @param category The category to check.
|
||||
*/
|
||||
export function isCategorySet(category: string): boolean;
|
||||
|
||||
/**
|
||||
* Writes a message using the available writers.
|
||||
* @param message The message to be written.
|
||||
* @param category The category of the message.
|
||||
* @param type Optional, the type of the message - info, warning, error.
|
||||
*/
|
||||
export function write(message: any, category: string, type?: number);
|
||||
|
||||
/**
|
||||
* Passes an error to the registered ErrorHandler
|
||||
* @param error The error to be handled.
|
||||
*/
|
||||
export function error(error: string | Error);
|
||||
/**
|
||||
* Notifies all the attached listeners for an event that has occurred in the sender object.
|
||||
* @param object The Object instance that raised the event.
|
||||
* @param name The name of the raised event.
|
||||
* @param data An optional parameter that passes the data associated with the event.
|
||||
*/
|
||||
export function notifyEvent(object: Object, name: string, data?: any);
|
||||
|
||||
export function addEventListener(listener: EventListener);
|
||||
|
||||
export function removeEventListener(listener: EventListener);
|
||||
|
||||
export function getErrorHandler(): ErrorHandler;
|
||||
|
||||
export function setErrorHandler(handler: ErrorHandler);
|
||||
|
||||
/**
|
||||
* An enum that defines all predefined categories.
|
||||
*/
|
||||
export module categories {
|
||||
export const VisualTreeEvents: string;
|
||||
export const Layout: string;
|
||||
export const Style: string;
|
||||
export const ViewHierarchy: string;
|
||||
export const NativeLifecycle: string;
|
||||
export const Debug: string;
|
||||
export const Navigation: string;
|
||||
export const Test: string;
|
||||
export const Binding: string;
|
||||
export const Error: string;
|
||||
export const Animation: string;
|
||||
export const Transition: string;
|
||||
export const Livesync: string;
|
||||
export const ModuleNameResolver: string;
|
||||
|
||||
export const separator: string;
|
||||
export const All: string;
|
||||
|
||||
export function concat(...categories: string[]): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* An enum that defines all predefined message types.
|
||||
*/
|
||||
export module messageType {
|
||||
export const log: number;
|
||||
export const info: number;
|
||||
export const warn: number;
|
||||
export const error: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface used to define a writer used by trace to print (log).
|
||||
*/
|
||||
export interface TraceWriter {
|
||||
write(message: any, category: string, type?: number);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface used to trace information about specific event.
|
||||
*/
|
||||
export interface EventListener {
|
||||
filter: string;
|
||||
on(object: Object, name: string, data?: any);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface used to for handling trace error
|
||||
*/
|
||||
export interface ErrorHandler {
|
||||
handlerError(error: Error);
|
||||
}
|
||||
|
||||
export class DefaultErrorHandler implements ErrorHandler {
|
||||
handlerError(error);
|
||||
}
|
||||
216
nativescript-core/trace/trace.ts
Normal file
216
nativescript-core/trace/trace.ts
Normal file
@@ -0,0 +1,216 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user