electricessence / TypeScript.NET

A JavaScript-Friendly .NET Based TypeScript Library (Moved)
https://github.com/electricessence/TypeScript.NET-Core
Other
251 stars 36 forks source link

Sample on how to use the Event classes #41

Closed mhsimkin closed 7 years ago

mhsimkin commented 7 years ago

Can you please provide a sample on how to use the event classes? There are no tests in the repo that can provide a reference point.

Thanks

marc

electricessence commented 7 years ago

For reference: https://github.com/electricessence/TypeScript.NET/tree/master/source/System/Events

electricessence commented 7 years ago

Ok so the basis for the event classes is similar to how the DOM has events as well as how the original Flash and Dart event dispatchers work.

Except for EventSimple, which doesn't require a string registry of events. It simply IS and event. And much like an Observer style object, can 'dispatch' a signal.

electricessence commented 7 years ago

Example 1:

var es = new EventSimple();
ex.add((x,y,z)=>{/* some stuff */});
ex.add((x,y,z)=>{/* some stuff */});
/* time goes by */
ex.dispatch(x,y,z);
/* All done? */
ex.dispose();
electricessence commented 7 years ago

Example 2:

const EVENT_A = "eventNameA", EVENT_B = "eventNameB";
var ed = new EventDispatcherBase();
ed.addEventListener(EVENT_A,()=>{/* do something */});
ed.addEventListener(EVENT_B,()=>{/* do something */});
/* time goes by */
ed.dispatch(EVENT_A);
ed.dispatch(EVENT_B);
/* All done? */
ed.dispose();
electricessence commented 7 years ago

The obvious alternatives are Observable classes where you 'subscribe' and 'unsubscribe': https://github.com/electricessence/TypeScript.NET/tree/master/source/System/Observable

electricessence commented 7 years ago

And another alternative: https://github.com/electricessence/TypeScript.NET/blob/master/source/System/Collections/Array/Dispatch.ts

electricessence commented 7 years ago

@mhsimkin: Lemme know if that answered your question.

mhsimkin commented 7 years ago

Thank you. This is very helpful.