HardPro challengeJavaScriptTypeScript

Subscription Manager

ReactPatternsPub/Sub

Implement createSubscription() returning { subscribe, notify, count }.
subscribe(fn) returns an unsubscribe() function.

solve(events) runs the events and returns the log.
Events: 'sub' (subscribes), 'unsub:N' (unsubscribes Nth sub), 'notify:msg' (notifies all).

Sample tests

Test #1One sub one notify
Input: [["sub","notify:hi"]]
Output: ["s0:hi"]
Test #2Two subs notified
Input: [["sub","sub","notify:x"]]
Output: ["s0:x","s1:x"]
Test #3Unsubbed before notify
Input: [["sub","unsub:0","notify:y"]]
Output: []
Test #4Only s1 remains
Input: [["sub","sub","unsub:0","notify:z"]]
Output: ["s1:z"]
Test #5No subscribers
Input: [["notify:nobody"]]
Output: []