-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf-singleton.html
More file actions
196 lines (177 loc) · 5.8 KB
/
Copy pathf-singleton.html
File metadata and controls
196 lines (177 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<link rel="import" href="../finterface-key-value-store/finterface-key-value-store.html">
<!--
-->
<script>
window.FirmFirm = window.FirmFirm || {};
FirmFirm.FSingleton = (function() {
'use strict'
let instances = [];
let state = {};
/**
* `<f-singleton>` is a Polymer element that helps implementing singleton pattern declaratively.
*
* Common use case is to provide shared state in your elements (e.g. for configuration).
*
* ## Private data
*
* Every instance of `<f-singleton>` points to the same `data` object. **Do not**
* use Polymer's two-way binding to set properties of `data` directly.
* Instead, if absolutely needed, call methods on `<f-singleton>` element like this:
*
* this.$$('f-singleton').set('data.prop', 'val')
*
* Supported methods are:
* - `set(path, value)`
* - `push(path, item)`
* - `notifyPath(path, value, fromAbove)`
*
* However, in most cases you will be good with just `key` and `value` properties.
*
* Example:
*
* <f-singleton key="myKey" value="something"></f-singleton>
* <f-singleton key="myKey" value="{{myValue}}"></f-singleton>
* <p>[[myValue]]</p>
*
* `key` is a property name to bind `value` to,
* e.g. `key="myKey"` would bind `value` to `data.myKey`.
*
* You can also use deep keys, like `key="myObj.myProp"`.
*
* @polymerElement
* @mixes Polymer.MutableData
* @mixes FInterface.KeyValueStore
* @demo demo/index.html
*/
class FSingleton extends Polymer.MutableData(FInterface.KeyValueStore(Polymer.Element)) {
static get is() { return 'f-singleton'; }
static get properties() {
return {
/**
* Shared data object
*/
data: {
type: Object,
readOnly: true,
notify: true,
value: () => state,
}
};
}
static get observers() {
return [
'_dataChanged(data.*)',
'_keyChanged(key)',
'_valueChanged(value)'
];
}
constructor() {
super();
var override = (originalFn, notifyAll) => (...args) => {
const result = originalFn.apply(this, args);
if (args[0].split(".")[0] === "data") {
notifyAll(args, result);
}
return result;
};
this._originalNotifyPath = this.notifyPath;
var notifyPathAll = (args) => this._invokeInstances(this._originalNotifyPath, args);
this.set = override(this.set, notifyPathAll);
this.notifyPath = override(this.notifyPath, notifyPathAll);
this._originalNotifySplices = this.notifySplices;
var notifySplicesAll = (args) => this._invokeInstances(this._originalNotifySplices, args);
this.notifySplices = override(this.notifySplices, notifySplicesAll);
const overrideSplices = (originalFn, getSplice) => override(
originalFn,
([path, ...args], result) => {
const object = this.get(path);
notifySplicesAll([
path,
[Object.assign({type: 'splice', object}, getSplice(args, object, result))],
]);
}
);
this.splice = overrideSplices(this.splice, ([index, deleteCount, ...items], array, removed) => ({
removed, index, addedCount: items.length,
}));
this.push = overrideSplices(this.push, ([...pushed], array) => ({
removed: [],
addedCount: pushed.length,
index: array.length - pushed.length
}));
this.pop = overrideSplices(this.pop, (_, array, result) => ({
removed: [result],
addedCount: 0,
index: array.length,
}));
this.shift = overrideSplices(this.shift, (_, array, result) => ({
removed: [result],
addedCount: 0,
index: 0,
}));
this.unshift = overrideSplices(this.unshift, ([...items], array, result) => ({
removed: [],
addedCount: items.length,
index: 0,
}));
}
connectedCallback() {
super.connectedCallback();
instances.push(this);
}
disconnectedCallback() {
super.disconnectedCallback();
instances.splice(instances.indexOf(this), 1);
}
_invokeInstances(fn, args) {
for (var i = 0; i < instances.length; i++) {
let instance = instances[i];
if (instance !== this) {
fn.apply(instance, args);
}
}
}
_keyChanged(key) {
if (!key) return;
const newValue = key.split('.').reduce(
(data, key) => data ? data[key] : null, this.data);
if (typeof newValue !== "undefined" && this.value !== newValue) {
this.value = newValue;
}
}
_valueChanged(value) {
if (!this.key || typeof value === "undefined") return;
const parts = this.key.split('.');
const currentValue = parts.reduce((d, k) => d && d[k], this.data);
if (currentValue !== value) {
// Create path in data object if doesnt exist
parts.pop();
parts.reduce(function(data, key) {
if (!data[key]) data[key] = {};
return data[key];
}, this.data);
this.set('data.' + this.key, value);
}
}
_dataChanged(changeRecord) {
if (!this.key) return;
var path = 'data.' + this.key;
if (changeRecord.path === path) {
// Exact match, dirty check
if (this.value !== changeRecord.value) {
this.value = changeRecord.value;
}
} else if (changeRecord.path.startsWith(path)) {
// Part of value changed
var valuePath = changeRecord.path.replace(path, 'value');
this._originalNotifyPath(valuePath, changeRecord.value);
} else if (path.startsWith(changeRecord.path)) {
// The whole value changed - have to recalculate
this._keyChanged(this.key);
}
}
}
customElements.define(FSingleton.is, FSingleton);
return FSingleton;
})();
</script>