https://github.com/andy9775/shared.js
A factory for a passive mediator design pattern in <25 lines of Javascript
https://github.com/andy9775/shared.js
Last synced: about 2 months ago
JSON representation
A factory for a passive mediator design pattern in <25 lines of Javascript
- Host: GitHub
- URL: https://github.com/andy9775/shared.js
- Owner: andy9775
- License: mit
- Created: 2015-11-14T18:24:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-14T04:44:04.000Z (over 9 years ago)
- Last Synced: 2025-03-23T01:02:20.765Z (3 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# shared.js [](https://travis-ci.org/andy9775/shared.js)
A factory for a passive mediator design pattern in <20 lines of Javascript## Use case:
The motivation of this project was to easily share state amongst React Native components allowing for lose coupling between them. This allows a component to update the state and the others to see the update, and is a light weight alternative to using an event emitter since it does not use callbacks. Rather it is up to the Observers to check the state changes.
### Installation
Use `npm install shared.js --save`
#### Example
A use case could include a button that needs to check the state of multiple on/off switches before proceeding. Rather than coupling the switches to the button, a shared object can be used by the switches to change state,and the button to perform a check when it is pressed.```
var Share = require('shared.js');
var id = 'buttonTest';
Share.getShare(id).canProceed = function() {for (var internal in this) {
if (internal.indexOf('toggle') !== -1) {
if (!this[internal]) {
return false;
}
}
}
return true;
};Share.getShare(id).switch = function() {
Share.getShare(id)[this.name] ?
Share.getShare(id)[this.name] = false : Share.getShare(id)[this.name] = true;
};
var toggle1 = {
name: 'toggle1',
share: Share.getShare(id, 'toggle1', false),
toggle: Share.getShare(id).switch
};var toggle2 = {
name: 'toggle2',
share: Share.getShare(id, 'toggle2', false),
toggle: Share.getShare(id).switch
};var button = {
share: Share.getShare(id),
press: function() {
return this.share.canProceed();
}
};console.log(button.press());// false
toggle1.toggle();
console.log(button.press());// false
toggle2.toggle();
console.log(button.press());// true// lets add a new toggle
var toggle3 = {
name: 'toggle3',
share: Share.getShare(id, 'toggle3', false),
toggle: Share.getShare(id).switch
};
console.log(button.press()); // false
toggle3.toggle();
console.log(button.press());// true
```### Functions
- **`getShare`** _(id, property, initialValue)_ - retrieve a shared state object as identified by the id argument. All arguments are optional and if none are passed the function returns a default system-wide share object. The same system-wide object is returned when `getShare` is called without arguments.---
**MIT Licenced**