main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | import stampit from 'stampit';
|
---|
2 | import { propEq } from 'ramda';
|
---|
3 | import { isNotUndefined, isString } from 'ramda-adjunct';
|
---|
4 | const ReferenceSet = stampit({
|
---|
5 | props: {
|
---|
6 | rootRef: null,
|
---|
7 | refs: [],
|
---|
8 | circular: false
|
---|
9 | },
|
---|
10 | init({
|
---|
11 | refs = []
|
---|
12 | } = {}) {
|
---|
13 | this.refs = [];
|
---|
14 | refs.forEach(ref => this.add(ref));
|
---|
15 | },
|
---|
16 | methods: {
|
---|
17 | get size() {
|
---|
18 | // @ts-ignore
|
---|
19 | return this.refs.length;
|
---|
20 | },
|
---|
21 | add(reference) {
|
---|
22 | if (!this.has(reference)) {
|
---|
23 | this.refs.push(reference);
|
---|
24 | this.rootRef = this.rootRef === null ? reference : this.rootRef;
|
---|
25 | reference.refSet = this; // eslint-disable-line no-param-reassign
|
---|
26 | }
|
---|
27 | return this;
|
---|
28 | },
|
---|
29 | merge(anotherRefSet) {
|
---|
30 | for (const reference of anotherRefSet.values()) {
|
---|
31 | this.add(reference);
|
---|
32 | }
|
---|
33 | return this;
|
---|
34 | },
|
---|
35 | has(thing) {
|
---|
36 | const uri = isString(thing) ? thing : thing.uri;
|
---|
37 | return isNotUndefined(this.find(propEq(uri, 'uri')));
|
---|
38 | },
|
---|
39 | find(callback) {
|
---|
40 | return this.refs.find(callback);
|
---|
41 | },
|
---|
42 | *values() {
|
---|
43 | yield* this.refs;
|
---|
44 | },
|
---|
45 | clean() {
|
---|
46 | this.refs.forEach(ref => {
|
---|
47 | // eslint-disable-next-line no-param-reassign
|
---|
48 | ref.refSet = null;
|
---|
49 | });
|
---|
50 | this.refs = [];
|
---|
51 | }
|
---|
52 | }
|
---|
53 | });
|
---|
54 | export default ReferenceSet; |
---|
Note:
See
TracBrowser
for help on using the repository browser.