-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a better jQuery module. Still half done though. Added new documentation about the browser code.
- Loading branch information
Showing
4 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,49 @@ | ||
//removeIf(node) | ||
if (window.jQuery || window.$) { | ||
const $ = window.jQuery || window.$; | ||
const lookups = new WeakMap(); | ||
const pubsubs = new Map(); | ||
const names = new WeakMap(); | ||
|
||
$.pubsub = function(name, ...params) { | ||
if (!pubsubs.has(name)) pubsubs.set(name, new PubSub(...params)); | ||
return pubsubs.get(name); | ||
function weakMapAddSet(weakmap, ref, value) { | ||
if (!weakmap.has(ref)) weakmap.set(ref, new Set()); | ||
if (value !== undefined) weakmap.get(ref).add(value); | ||
return weakmap.get(ref); | ||
} | ||
|
||
$.fn.pubsub = function(name, ...params) { | ||
if (name) { | ||
if (!pubsubs.has(name)) pubsubs.set(name, new PubSub(...params)); | ||
weakMapAddSet(names, pubsubs.get(name), name); | ||
this.each((n, item)=>weakMapAddSet(lookups, item, pubsubs.get(name))); | ||
} | ||
|
||
function getPubSubs(items) { | ||
const pubsubs = new Set(); | ||
items.each((n, item)=>{ | ||
weakMapAddSet(lookups, item).forEach(pubsub=>{ | ||
if (!name || names.get(pubsub).has(name)) pubsubs.add(pubsub) | ||
}); | ||
}); | ||
return pubsubs; | ||
} | ||
|
||
this.publish = (...params)=>{ | ||
getPubSubs(this).forEach(pubsub=>pubsub.publish(...params)); | ||
return this; | ||
}; | ||
|
||
this.broadcast = (...params)=>{ | ||
getPubSubs(this).forEach(pubsub=>pubsub.broadcast(...params)); | ||
return this; | ||
}; | ||
|
||
this.subscribe = (...params)=>{ | ||
getPubSubs(this).forEach(pubsub=>pubsub.subscribe(...params)); | ||
return this; | ||
}; | ||
|
||
return this; | ||
}; | ||
} | ||
//endRemoveIf(node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters