Created
February 7, 2012 21:23
-
-
Save swvitaliy/1762060 to your computer and use it in GitHub Desktop.
Producer-consumer pattern implementation (send message on change status).
This file contains hidden or 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
window.statusProducer = function (d, options) { | |
var options = options || {}, | |
availableStatuses = options.statuses || {}, | |
dataKey = options.dataKey || ('statusData' + new Date().getTime()); | |
errorDialog = options.errorDialog || alert; | |
return { | |
addListener: function (status, callee) { | |
if (typeof callee !== 'function') | |
return errorDialog("тип слушателя может быть только \"function\""); | |
availableStatuses[status] = callee; | |
}, | |
addState: function (status) { | |
if (!availableStatuses.hasOwnProperty(status)) | |
availableStatuses[status] = status; | |
}, | |
setStatus: function (newStatus, content) { | |
if (!availableStatuses.hasOwnProperty(newStatus)) | |
return errorDialog('статус "' + newStatus + '" неопределен'); | |
if (newStatus === this.getStatus()) | |
return; | |
var statusValue = availableStatuses[newStatus]; | |
if (typeof statusValue === 'function') { | |
statusValue(d, content, newStatus); | |
} | |
d.data(dataKey, { status: newStatus, content: content }); | |
}, | |
getStatus: function (key) { | |
return (d.data(dataKey) || {})[key || 'status']; | |
}, | |
getData: function () { | |
return d.data(); | |
}, | |
hasStatus: function (test) { | |
return test === this.getStatus(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment