web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.5 KiB

  1. [![Build Status](https://travis-ci.org/DirtyHairy/microevent.svg?branch=master)](https://travis-ci.org/DirtyHairy/microevent)
  2. [![npm version](https://badge.fury.io/js/microevent.ts.svg)](https://badge.fury.io/js/microevent.ts)
  3. # What is it?
  4. This package implements an event system with minimal dispatch overhead. Instead
  5. of recording handlers bound to an event in dynamic data structures, this library
  6. binds the handlers to autogenerated code. This enables the VM to inline the
  7. handler invocation and generate code that is just as fast as invoking the handlers
  8. directly.
  9. In a (completely unscientific) benchmark, this library performs bettern than
  10. NodeJS events in terms of event dispatch calls per second by about two orders
  11. of magnitude.
  12. # How to use it?
  13. ## Installation
  14. You can install the library into your project via npm
  15. npm install microevent.ts
  16. The library is written in Typescript and will work in any environment that
  17. supports ES5. No external typings are required for using this library with
  18. Typescript (version >= 2).
  19. ## Importing
  20. ES5 / CommonJS
  21. var Event = require('microevent.ts').Event;
  22. ES6
  23. import {Event} from 'microevent.ts';
  24. Typescript
  25. import {Event, EventInterface} from 'microevent.ts';
  26. The `EventInterface` covers only the client side of an event, that is adding
  27. and removing handlers.
  28. ## API
  29. ### Creating
  30. ES5/ES6
  31. const event = new Event();
  32. Typescript
  33. const event = new Event<PayloadT>();
  34. Create a new event that will dispatch a payload of type `PayloadT`.
  35. ### Dispatching
  36. event.dispatch(payload);
  37. This will call all handlers in the order they were registered, passing `payload`
  38. as first argument.
  39. **IMPORTANT** `dispatch` is a property that refers to dynamically generated code.
  40. **DO NOT KEEP ANY REFERENCES** to `dispatch` as adding and removing handlers
  41. will invalidate them.
  42. ### Registering handlers
  43. event.addHandler(handler, context);
  44. `context` is an optional parameter that will be passed to the handler on
  45. each invocation.
  46. ### Removing handlers
  47. event.removeHandler(handler, context);
  48. Both `handler` and `context` must be identical to the values used when registering
  49. the handler in the first place.
  50. ### Checking for handlers
  51. event.isHandlerAttached(handler, context)
  52. This will check whether a handler was attached in a given context.
  53. event.hasHandlers
  54. `true` if the event has any handlers attached, false otherwise.
  55. # License
  56. Feel free to use this library under the conditions of the MIT license.