Closed Bug 616770 Opened 14 years ago Closed 14 years ago

Convert simple-storage events to style described in bug 593921

Categories

(Add-on SDK Graveyard :: General, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: adw, Assigned: adw)

References

Details

(Whiteboard: [cherry-pick-1.0b1])

Attachments

(2 files, 2 obsolete files)

Attached patch patch (obsolete) — Splinter Review
See bug 593921 comment 8.
Attachment #495331 - Flags: review?(myk)
Comment on attachment 495331 [details] [diff] [review]
patch

Note that the `this` object in OverQuota handlers is currently the `manager` object, whereas it should be the `exports` object, since that is the object to which one adds the handler, and to avoid exposing the `manager` object to API consumers.

Offhand, however, I don't see a way to fix that.  EventEmitter._emit just uses this._public.  Perhaps it can be made to accept a thisObject parameter?
Attachment #495331 - Flags: review?(myk) → review+
Attached patch patch 2 (obsolete) — Splinter Review
Yes.  It's my fault, I should have caught it in review.

Modifying _emit to take a thisArg, although it would be easy to do, would punch a hole in the traits pattern.  In the long run I think that might cause confusion and encourage trading good class design for easy hacks.  The real problem is that exports can't be an EventEmitter because it can't be set to a new object.

So I made this even worse hack, but it's limited to simple-storage, and it works.  Maybe I'm being too rigid about _emit and a thisArg.  I'm willing to bend!
Attachment #495331 - Attachment is obsolete: true
Attachment #495344 - Flags: review?(myk)
The selection module faces this same problem...  Modifying _emit to take a thisArg is the better way to go I guess, but another reason that'll suck is because it's variadic; all its callers will have to be modified too.
Or, there could be another function _emitOnObject that takes a thisArg and delegates to _emit -- or the other way around I guess.  _emitOnObject would only be used where needed.
Attached patch patch 3Splinter Review
See comments 3 and 4.
Attachment #495344 - Attachment is obsolete: true
Attachment #495399 - Flags: review?(myk)
Attachment #495344 - Flags: review?(myk)
I wonder if there should be a version of _emit specifically for high-level APIs that always passes a single object to listeners that automatically includes emitter: targetObj?
This adds _emitEventObject like comment 6 says.  For high-level APIs that don't need to fuss with the `this` issue, they can just do:

  _emitEventObject("FooEvent");
  _emitEventObject("BarEvent", { data: someMessage });

Those that do need to fuss can pass an extra arg to set `this`.  Either way the single object that listeners receive automatically has `emitter` set.

While I was there an _emitMessage seemed like it'd be useful, so I made that too.  It automatically sets `data` to the message and works similarly to _emitEventObject otherwise.

Comes with tests.
Attachment #495419 - Flags: review?(myk)
Comment on attachment 495419 [details] [diff] [review]
alternate _emitEventObject and _emitMessage patch

> So I made this even worse hack, but it's limited to simple-storage, and it
> works.  Maybe I'm being too rigid about _emit and a thisArg.  I'm willing to
> bend!

Hacks are ok as long as they're safe enough and understood to be temporary!

Note bug 577782, which should make it possible for an exports object to be an event emitter.

This patch seems like a good approach to me and looks/works great. r=myk!
Attachment #495419 - Flags: review?(myk) → review+
Attachment #495399 - Flags: review?(myk)
Whiteboard: [cherry-pick-needed]
Target Milestone: 0.10 → 1.0b1
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: